blob: 597e1e9b189c6d8a5c915d44f680c8e81b1a43af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// Copyright 2005-2012 The MumbleKit Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#import <MumbleKit/MKAudio.h>
#import "MKAudioOutputUser.h"
#import "MKAudioOutputUserPrivate.h"
#import "MKAudioOutputSidetone.h"
@interface MKAudioOutputSidetone () {
NSMutableArray *_frames;
NSUInteger _offset;
NSUInteger _filled;
float _volume;
MKAudioSettings _settings;
}
@end
@implementation MKAudioOutputSidetone
- (id) initWithSettings:(MKAudioSettings *)settings {
if ((self = [super init])) {
memcpy(&_settings, settings, sizeof(MKAudioSettings));
_frames = [[NSMutableArray alloc] init];
_filled = 0;
_offset = 0;
_volume = _settings.sidetoneVolume;
}
return self;
}
- (void) dealloc {
[_frames release];
[super dealloc];
}
- (void) addFrame:(NSData *)data {
@synchronized(_frames) {
[_frames addObject:data];
}
}
- (BOOL) needSamples:(NSUInteger)nsamples {
[self resizeBuffer:nsamples];
while (_filled < nsamples) {
NSData *frameData = nil;
@synchronized(_frames) {
if ([_frames count] > 0) {
frameData = [_frames objectAtIndex:0];
}
}
if (frameData != nil) {
NSUInteger frameSize = [frameData length]/2;
short *input = (short *) [frameData bytes];
float *output = _buffer+_filled;
NSUInteger maxFrames = frameSize;
if (_filled+frameSize > nsamples) {
maxFrames = nsamples - _filled;
}
if (_offset > 0) {
for (NSUInteger i = _offset; i < maxFrames; i++) {
output[0] = (input[i] / 32767.0f) * _volume;
_filled++;
output++;
}
_offset = 0;
} else {
for (NSUInteger i = 0; i < maxFrames; i++) {
output[i] = (input[i] / 32767.0f) * _volume;
}
_filled += maxFrames;
}
if (maxFrames < frameSize) {
_offset = maxFrames;
} else {
@synchronized(_frames) {
[_frames removeObjectAtIndex:0];
}
}
} else {
return NO;
}
}
for (NSUInteger i = 0; i < nsamples; i++) {
if (_buffer[i] > 1.0f)
_buffer[i] = 1.0f;
else if (_buffer[i] < -1.0f)
_buffer[i] = -1.0f;
}
_filled = 0;
return YES;
}
@end
|