Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MKAudioOutput.m « src - github.com/mumble-voip/mumblekit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d47dcbc987e24a2c57dca5c95fb51681bcbf094 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// 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 "MKUtils.h"
#import "MKAudioOutput.h"
#import "MKAudioOutputSpeech.h"
#import "MKAudioOutputUser.h"
#import "MKAudioOutputSidetone.h"
#import "MKAudioDevice.h"

#import <AudioUnit/AudioUnit.h>
#import <AudioUnit/AUComponent.h>
#import <AudioToolbox/AudioToolbox.h>

@interface MKAudioOutput () {
    MKAudioDevice        *_device;
    MKAudioSettings       _settings;
    AudioUnit             _audioUnit;
    int                   _sampleSize;
    int                   _frameSize;
    int                   _mixerFrequency;
    int                   _numChannels;
    float                *_speakerVolume;
    NSLock               *_outputLock;
    NSMutableDictionary  *_outputs;
    
    NSLock               *_mixerInfoLock;
    NSDictionary         *_mixerInfo;

    double                _cngAmpliScaler;
    double                _cngLastSample;
    long                  _cngRegister1;
    long                  _cngRegister2;
    BOOL                  _cngEnabled;
}
@end

@implementation MKAudioOutput

- (id) initWithDevice:(MKAudioDevice *)device andSettings:(MKAudioSettings *)settings {
    if ((self = [super init])) {
        memcpy(&_settings, settings, sizeof(MKAudioSettings));
        _device = [device retain];
        _sampleSize = 0;
        _frameSize = SAMPLE_RATE / 100;
        _mixerFrequency = 0;
        _outputLock = [[NSLock alloc] init];
        _outputs = [[NSMutableDictionary alloc] init];
        
        _mixerFrequency = [_device inputSampleRate];
        _numChannels = [_device numberOfOutputChannels];
        _sampleSize = _numChannels * sizeof(short);
        
        _cngRegister1 = 0x67452301;
        _cngRegister2 = 0xefcdab89;
        _cngEnabled = settings->enableComfortNoise;
        _cngAmpliScaler = 2.0f / 0xffffffff;
        _cngAmpliScaler *= 0.00150;
        _cngAmpliScaler *= settings->comfortNoiseLevel;
        _cngLastSample = 0.0;
            
       if (_speakerVolume) {
            free(_speakerVolume);
        }
        _speakerVolume = malloc(sizeof(float)*_numChannels);
        
        int i;
        for (i = 0; i < _numChannels; ++i) {
            _speakerVolume[i] = 1.0f;
        }
        
        [_device setupOutput:^BOOL(short *frames, unsigned int nsamp) {
            return [self mixFrames:frames amount:nsamp];
        }];
        
        _mixerInfo = [[NSDictionary dictionaryWithObjectsAndKeys:
                [NSDate date], @"last-update",
                [NSArray array], @"sources",
                [NSArray array], @"removed",
            nil] retain];
        _mixerInfoLock = [[NSLock alloc] init];
    }
    return self;
}

- (void) dealloc {
    [_mixerInfoLock release];
    [_mixerInfo release];
    [_device setupOutput:NULL];
    [_device release];
    [_outputLock release];
    [_outputs release];
    [super dealloc];
}

- (NSDictionary *) audioOutputDebugDescription:(id)ou {
    if ([ou isKindOfClass:[MKAudioOutputSpeech class]]) {
        MKAudioOutputSpeech *ous = (MKAudioOutputSpeech *)ou;
        
        NSString *msgType = nil;
        switch ([ous messageType]) {
            case UDPVoiceCELTAlphaMessage:
                msgType = @"celt-alpha";
                break;
            case UDPVoiceCELTBetaMessage:
                msgType = @"celt-beta";
                break;
            case UDPVoiceSpeexMessage:
                msgType = @"speex";
                break;
            case UDPVoiceOpusMessage:
                msgType = @"opus";
                break;
            default:
                msgType = @"unknown";
                break;
        }
        
        return [NSDictionary dictionaryWithObjectsAndKeys:
                @"user", @"kind",
                [NSString stringWithFormat:@"session %lu codec %@", (unsigned long) [ous userSession], msgType], @"identifier",
            nil];
    } else if ([ou isKindOfClass:[MKAudioOutputSidetone class]]) {
        return [NSDictionary dictionaryWithObjectsAndKeys:
                    @"sidetone", @"kind",
                    @"sidetone", @"identifier",
            nil];
    } else {
        return [NSDictionary dictionaryWithObjectsAndKeys:
                @"unknown", @"kind",
                @"unknown", @"identifier",
            nil];
    }
}

- (BOOL) mixFrames:(void *)frames amount:(unsigned int)nsamp {
    unsigned int i, s;
    BOOL retVal = NO;

    NSMutableArray *mix = [[NSMutableArray alloc] init];
    NSMutableArray *del = [[NSMutableArray alloc] init];
    unsigned int nchan = _numChannels;

    [_outputLock lock];
    for (NSNumber *sessionKey in _outputs) {
        MKAudioOutputUser *ou = [_outputs objectForKey:sessionKey];
        if (! [ou needSamples:nsamp]) {
            [del addObject:ou];
        } else {
            [mix addObject:ou];
        }
    }
    
    if (_settings.enableSideTone) {
        MKAudioOutputSidetone *sidetone = [[MKAudio sharedAudio] sidetoneOutput];
        if ([sidetone needSamples:nsamp]) {
            [mix addObject:[[MKAudio sharedAudio] sidetoneOutput]];
        }
    }
    
    if (_settings.audioMixerDebug) {
        NSMutableDictionary *mixerInfo = [[[NSMutableDictionary alloc] init] autorelease];
        NSMutableArray *sources = [[[NSMutableArray alloc] init] autorelease];
        NSMutableArray *removed = [[[NSMutableArray alloc] init] autorelease];

        for (id ou in mix) {
            [sources addObject:[self audioOutputDebugDescription:ou]];
        }
    
        for (id ou in del) {
            [sources addObject:[self audioOutputDebugDescription:ou]];
        }

    
        [mixerInfo setObject:[NSDate date] forKey:@"last-update"];
        [mixerInfo setObject:sources forKey:@"sources"];
        [mixerInfo setObject:removed forKey:@"removed"];
    
        [_mixerInfoLock lock];
        [_mixerInfo release];
        _mixerInfo = [mixerInfo retain];
        [_mixerInfoLock unlock];
    }
    
    float *mixBuffer = alloca(sizeof(float)*_numChannels*nsamp);
    memset(mixBuffer, 0, sizeof(float)*_numChannels*nsamp);

    if ([mix count] > 0) {
        for (MKAudioOutputUser *ou in mix) {
            const float * restrict userBuffer = [ou buffer];
            for (s = 0; s < nchan; ++s) {
                const float str = _speakerVolume[s];
                float * restrict o = (float *)mixBuffer + s;
                for (i = 0; i < nsamp; ++i) {
                    o[i*nchan] += userBuffer[i] * str;
                }
            }
        }

        short *outputBuffer = (short *)frames;
        for (i = 0; i < nsamp * _numChannels; ++i) {
            if (mixBuffer[i] >= 1.0f) {
                outputBuffer[i] = 32767;
            } else if (mixBuffer[i] < -1.0f) {
                outputBuffer[i] = -32768;
            } else {
                outputBuffer[i] = mixBuffer[i] * 32768.0f;
            }
        }
    } else {
        memset((short *)frames, 0, nsamp * _numChannels * sizeof(short));
    }
    [_outputLock unlock];

    for (MKAudioOutputUser *ou in del) {
        [self removeBuffer:ou];
    }

    retVal = [mix count] > 0;

    [mix release];
    [del release];

    if(!retVal && _cngEnabled) {
        short *outputBuffer = (short *)frames;
        
        for (i = 0; i < nsamp * _numChannels; ++i) {
            float    runningvalue;
            
            _cngRegister1 ^= _cngRegister2;
            runningvalue = (float)_cngRegister2 * _cngAmpliScaler;
            runningvalue += _cngLastSample; //one pole smoother
            runningvalue *= 0.5;            //one pole smoother
            _cngLastSample = runningvalue;
            _cngRegister2 += _cngRegister1;
            
            if (runningvalue >= 1.0f) {
                outputBuffer[i] = 32767;
            } else if (runningvalue < -1.0f) {
                outputBuffer[i] = -32768;
            } else {
                outputBuffer[i] = runningvalue * 32768.0f;
            }
        }
        retVal = YES;
    }

    return retVal;
}

- (void) removeBuffer:(MKAudioOutputUser *)u {
    if ([u respondsToSelector:@selector(userSession)]) {
        [_outputLock lock];
        [_outputs removeObjectForKey:[NSNumber numberWithUnsignedInteger:[(id)u userSession]]];
        [_outputLock unlock];
    }
}

- (void) addFrameToBufferWithSession:(NSUInteger)session data:(NSData *)data sequence:(NSUInteger)seq type:(MKUDPMessageType)msgType {
    if (_numChannels == 0)
        return;

    [_outputLock lock];
    MKAudioOutputSpeech *outputUser = [_outputs objectForKey:[NSNumber numberWithUnsignedInteger:session]];
    [outputUser retain];
    [_outputLock unlock];

    if (outputUser == nil || [outputUser messageType] != msgType) {
        if (outputUser != nil) {
            [self removeBuffer:outputUser];
            [outputUser release];
        }
        outputUser = [[MKAudioOutputSpeech alloc] initWithSession:session sampleRate:_mixerFrequency messageType:msgType];
        [_outputLock lock];
        [_outputs setObject:outputUser forKey:[NSNumber numberWithUnsignedInteger:session]];
        [_outputLock unlock];
    }

    [outputUser addFrame:data forSequence:seq];
    [outputUser release];
}

- (NSDictionary *) copyMixerInfo {
    NSDictionary *mixerInfoCopy = nil;
    [_mixerInfoLock lock];
    mixerInfoCopy = [_mixerInfo copy];
    [_mixerInfoLock unlock];
    return mixerInfoCopy;
}

@end