1313const int kBufferLength = 384000 ;
1414const int kNumberOfChannels = 1 ;
1515const int kDefaultSampleRate = 48000 ;
16- const int kSampleCount = 1920 ;
16+ const int kSampleCount_incoming_audio = 1920 ;
17+ const int kSampleCount_outgoing_audio = (1920 / 2 );
1718const int kBitsPerByte = 8 ;
1819const int kFramesPerPacket = 1 ;
1920// if you make this too small, the output queue will silently not play,
2021// but you will still get fill callbacks; it's really weird
21- const int kFramesPerOutputBuffer = kSampleCount / 4 ;
22+ const int kFramesPerOutputBuffer_incoming_audio = kSampleCount_incoming_audio / 4 ;
23+ const int kFramesPerOutputBuffer_outgoing_audio = kSampleCount_outgoing_audio / 4 ;
2224const int kBytesPerSample = sizeof (SInt16);
2325const int kNumberOfAudioQueueBuffers = 8 ;
2426
@@ -157,11 +159,13 @@ - (instancetype)initWithDeviceID:(NSString *)devID isOutput:(BOOL)output error:(
157159
158160- (instancetype )initWithInputDeviceID : (NSString *)devID error : (NSError **)error
159161{
162+ // TOXAUDIO: -outgoing-audio-
160163 return [self initWithDeviceID: devID isOutput: NO error: error];
161164}
162165
163166- (instancetype )initWithOutputDeviceID : (NSString *)devID error : (NSError **)error
164167{
168+ // TOXAUDIO: -incoming-audio-
165169 return [self initWithDeviceID: devID isOutput: YES error: error];
166170}
167171
@@ -182,9 +186,11 @@ - (OSStatus)createAudioQueue
182186{
183187 OSStatus err;
184188 if (self.isOutput ) {
189+ // TOXAUDIO: -incoming-audio-
185190 err = _AudioQueueNewOutput (&_streamFmt, (void *)&FillOutputBuffer, (__bridge void *)self, NULL , kCFRunLoopCommonModes , 0 , &_audioQueue);
186191 }
187192 else {
193+ // TOXAUDIO: -outgoing-audio-
188194 err = _AudioQueueNewInput (&_streamFmt, (void *)&InputAvailable, (__bridge void *)self, NULL , kCFRunLoopCommonModes , 0 , &_audioQueue);
189195 }
190196
@@ -215,9 +221,16 @@ - (BOOL)begin:(NSError **)error
215221 }
216222
217223 for (int i = 0 ; i < kNumberOfAudioQueueBuffers ; ++i) {
218- _AudioQueueAllocateBuffer (self.audioQueue , kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer , &(_AQBuffers[i]));
224+ if (self.isOutput ) {
225+ // TOXAUDIO: -incoming-audio-
226+ _AudioQueueAllocateBuffer (self.audioQueue , kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer_incoming_audio , &(_AQBuffers[i]));
227+ } else {
228+ // TOXAUDIO: -outgoing-audio-
229+ _AudioQueueAllocateBuffer (self.audioQueue , kBytesPerSample * kNumberOfChannels * kFramesPerOutputBuffer_outgoing_audio , &(_AQBuffers[i]));
230+ }
219231 _AudioQueueEnqueueBuffer (self.audioQueue , _AQBuffers[i], 0 , NULL );
220232 if (self.isOutput ) {
233+ // TOXAUDIO: -outgoing-audio-
221234 // For some reason we have to fill it with zero or the callback never gets called.
222235 FillOutputBuffer (self, self.audioQueue , _AQBuffers[i]);
223236 }
@@ -354,11 +367,25 @@ static void InputAvailable(OCTAudioQueue *__unsafe_unretained context,
354367
355368 int32_t availableBytesToConsume;
356369 void *tail = TPCircularBufferTail (&context->_buffer , &availableBytesToConsume);
357- int32_t minimalBytesToConsume = kSampleCount * kNumberOfChannels * sizeof (SInt16);
370+
371+ // TOXAUDIO: -outgoing-audio-
372+ int32_t minimalBytesToConsume = kSampleCount_outgoing_audio * kNumberOfChannels * sizeof (SInt16);
373+
374+ if (context.isOutput ) {
375+ // TOXAUDIO: -incoming-audio-
376+ minimalBytesToConsume = kSampleCount_incoming_audio * kNumberOfChannels * sizeof (SInt16);
377+ }
378+
358379 int32_t cyclesToConsume = availableBytesToConsume / minimalBytesToConsume;
359380
360381 for (int32_t i = 0 ; i < cyclesToConsume; i++) {
361- context.sendDataBlock (tail, kSampleCount , context.streamFmt .mSampleRate , kNumberOfChannels );
382+ if (context.isOutput ) {
383+ // TOXAUDIO: -incoming-audio-
384+ context.sendDataBlock (tail, kSampleCount_incoming_audio , context.streamFmt .mSampleRate , kNumberOfChannels );
385+ } else {
386+ // TOXAUDIO: -outgoing-audio-
387+ context.sendDataBlock (tail, kSampleCount_outgoing_audio , context.streamFmt .mSampleRate , kNumberOfChannels );
388+ }
362389 TPCircularBufferConsume (&context->_buffer , minimalBytesToConsume);
363390 tail = TPCircularBufferTail (&context->_buffer , &availableBytesToConsume);
364391 }
0 commit comments