/
AEMixerBuffer.m
1377 lines (1136 loc) · 61.6 KB
/
AEMixerBuffer.m
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// AEMixerBuffer.m
// The Amazing Audio Engine
//
// Created by Michael Tyson on 12/04/2012.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "AEMixerBuffer.h"
#import "TPCircularBuffer.h"
#import "TPCircularBuffer+AudioBufferList.h"
#import "AEFloatConverter.h"
#import "AEUtilities.h"
#import <libkern/OSAtomic.h>
#import <Accelerate/Accelerate.h>
#import <pthread.h>
#ifdef DEBUG
#define dprintf(THIS, n, __FORMAT__, ...) {if ( THIS->_debugLevel >= (n) ) { printf("<AEMixerBuffer %p>: "__FORMAT__ "\n", THIS, ##__VA_ARGS__); }}
#else
#define dprintf(THIS, n, __FORMAT__, ...)
#endif
typedef struct {
AEMixerBufferSource source;
AEMixerBufferSourcePeekCallback peekCallback;
AEMixerBufferSourceRenderCallback renderCallback;
void *callbackUserinfo;
TPCircularBuffer buffer;
uint64_t lastAudioTimestamp;
BOOL synced;
UInt32 consumedFramesInCurrentTimeSlice;
AudioStreamBasicDescription audioDescription;
void *floatConverter;
float volume;
float pan;
BOOL started;
AudioBufferList *skipFadeBuffer;
BOOL unregistering;
} source_t;
typedef void(*AEMixerBufferAction)(AEMixerBuffer *buffer, void *userInfo);
typedef struct {
AEMixerBufferAction action;
void *userInfo;
} action_t;
#define kMaxSources 30
static const NSTimeInterval kResyncTimestampThreshold = 0.002;
static const NSTimeInterval kSourceTimestampIdleThreshold = 1.0;
static const UInt32 kConversionBufferLength = 16384;
static const UInt32 kScratchBufferBytesPerChannel = 16384;
static const UInt32 kSourceBufferFrames = 8192;
static const int kActionBufferSize = 2048;
static const NSTimeInterval kActionMainThreadPollDuration = 0.2;
static const int kMinimumFrameCount = 64;
static const UInt32 kMaxMicrofadeDuration = 512;
@interface AEMixerBuffer () {
AudioStreamBasicDescription _clientFormat;
AudioStreamBasicDescription _mixerOutputFormat;
source_t _table[kMaxSources];
AudioTimeStamp _currentSliceTimestamp;
UInt32 _sampleTime;
UInt32 _currentSliceFrameCount;
AUGraph _graph;
AUNode _mixerNode;
AudioUnit _mixerUnit;
pthread_mutex_t _graphMutex;
AudioConverterRef _audioConverter;
TPCircularBuffer _audioConverterBuffer;
BOOL _audioConverterHasBuffer;
uint8_t *_scratchBuffer;
BOOL _graphReady;
BOOL _automaticSingleSourceDequeueing;
TPCircularBuffer _mainThreadActionBuffer;
NSTimer *_mainThreadActionPollTimer;
float **_microfadeBuffer;
int _configuredChannels;
}
static UInt32 _AEMixerBufferPeek(AEMixerBuffer *THIS, AudioTimeStamp *outNextTimestamp, BOOL respectInfiniteSourceFlag);
static inline source_t *sourceWithID(AEMixerBuffer *THIS, AEMixerBufferSource sourceID, int* index);
static inline void unregisterSources(AEMixerBuffer *THIS);
static void prepareNewSource(AEMixerBuffer *THIS, AEMixerBufferSource sourceID);
static void prepareSkipFadeBufferForSource(AEMixerBuffer *THIS, source_t* source);
- (void)refreshMixingGraph;
@property (nonatomic, strong) AEFloatConverter *floatConverter;
@end
@interface AEMixerBufferPollProxy : NSObject {
__weak AEMixerBuffer *_mixerBuffer;
}
- (id)initWithMixerBuffer:(AEMixerBuffer*)mixerBuffer;
@end
@implementation AEMixerBuffer
@synthesize sourceIdleThreshold = _sourceIdleThreshold;
@synthesize assumeInfiniteSources = _assumeInfiniteSources;
@synthesize floatConverter = _floatConverter;
@synthesize debugLevel = _debugLevel;
- (id)initWithClientFormat:(AudioStreamBasicDescription)clientFormat {
if ( !(self = [super init]) ) return nil;
self.clientFormat = clientFormat;
_sourceIdleThreshold = kSourceTimestampIdleThreshold;
TPCircularBufferInit(&_mainThreadActionBuffer, kActionBufferSize);
_mainThreadActionPollTimer = [NSTimer scheduledTimerWithTimeInterval:kActionMainThreadPollDuration
target:[[AEMixerBufferPollProxy alloc] initWithMixerBuffer:self]
selector:@selector(pollActionBuffer)
userInfo:nil
repeats:YES];
pthread_mutex_init(&_graphMutex, NULL);
return self;
}
- (void)dealloc {
pthread_mutex_destroy(&_graphMutex);
[_mainThreadActionPollTimer invalidate];
TPCircularBufferCleanup(&_mainThreadActionBuffer);
if ( _graph ) {
AECheckOSStatus(AUGraphClose(_graph), "AUGraphClose");
AECheckOSStatus(DisposeAUGraph(_graph), "AUGraphClose");
}
if ( _audioConverter ) {
AECheckOSStatus(AudioConverterDispose(_audioConverter), "AudioConverterDispose");
_audioConverter = NULL;
TPCircularBufferCleanup(&_audioConverterBuffer);
}
for ( int i=0; i<kMaxSources; i++ ) {
if ( _table[i].source ) {
if ( !_table[i].renderCallback ) {
TPCircularBufferCleanup(&_table[i].buffer);
}
if ( _table[i].skipFadeBuffer ) {
for ( int j=0; j<_table[i].skipFadeBuffer->mNumberBuffers; j++ ) {
free(_table[i].skipFadeBuffer->mBuffers[j].mData);
}
free(_table[i].skipFadeBuffer);
}
if ( _table[i].floatConverter ) {
CFBridgingRelease(_table[i].floatConverter);
}
}
}
free(_scratchBuffer);
for ( int i=0; i<_configuredChannels * 2; i++ ) {
free(_microfadeBuffer[i]);
}
free(_microfadeBuffer);
}
-(void)setClientFormat:(AudioStreamBasicDescription)clientFormat {
if ( memcmp(&_clientFormat, &clientFormat, sizeof(AudioStreamBasicDescription)) == 0 ) return;
_clientFormat = clientFormat;
[self respondToChannelCountChange];
self.floatConverter = [[AEFloatConverter alloc] initWithSourceFormat:_clientFormat];
for ( int i=0; i<kMaxSources; i++ ) {
source_t *source = &_table[i];
if ( source->source && !source->audioDescription.mSampleRate ) {
if ( source->skipFadeBuffer ) {
for ( int j=0; j<source->skipFadeBuffer->mNumberBuffers; j++ ) {
free(source->skipFadeBuffer->mBuffers[j].mData);
}
free(source->skipFadeBuffer);
}
prepareSkipFadeBufferForSource(self, source);
if ( !source->renderCallback ) {
TPCircularBufferClear(&source->buffer);
}
}
}
if ( _audioConverter ) {
AECheckOSStatus(AudioConverterDispose(_audioConverter), "AudioConverterDispose");
_audioConverter = NULL;
_audioConverterHasBuffer = NO;
TPCircularBufferCleanup(&_audioConverterBuffer);
}
if ( _mixerUnit ) {
// Try to set mixer's output stream format to our client format
OSStatus result = AudioUnitSetProperty(_mixerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &_clientFormat, sizeof(_clientFormat));
if ( result == kAudioUnitErr_PropertyNotWritable ) {
pthread_mutex_lock(&_graphMutex);
// Need to recreate the mixer. Dispose it here, it'll be recreated when we refresh below
AECheckOSStatus(AUGraphClose(_graph), "AUGraphClose");
AECheckOSStatus(DisposeAUGraph(_graph), "AUGraphClose");
_graph = NULL;
_graphReady = NO;
pthread_mutex_unlock(&_graphMutex);
} else if ( result == kAudioUnitErr_FormatNotSupported ) {
// The mixer only supports a subset of formats. If it doesn't support this one, then we'll convert manually
// Get the existing format, and apply just the sample rate
UInt32 size = sizeof(_mixerOutputFormat);
AECheckOSStatus(AudioUnitGetProperty(_mixerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &_mixerOutputFormat, &size),
"AudioUnitGetProperty(kAudioUnitProperty_StreamFormat)");
_mixerOutputFormat.mSampleRate = _clientFormat.mSampleRate;
AECheckOSStatus(AudioUnitSetProperty(_mixerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &_mixerOutputFormat, sizeof(_mixerOutputFormat)),
"AudioUnitSetProperty(kAudioUnitProperty_StreamFormat");
// Create the audio converter
AECheckOSStatus(AudioConverterNew(&_mixerOutputFormat, &_clientFormat, &_audioConverter), "AudioConverterNew");
TPCircularBufferInit(&_audioConverterBuffer, kConversionBufferLength);
} else {
AECheckOSStatus(result, "AudioUnitSetProperty(kAudioUnitProperty_StreamFormat)");
}
}
[self refreshMixingGraph];
}
void AEMixerBufferEnqueue(__unsafe_unretained AEMixerBuffer *THIS, AEMixerBufferSource sourceID, AudioBufferList *audio, UInt32 lengthInFrames, const AudioTimeStamp *timestamp) {
dprintf(THIS, 1, "Enqueue %u frames at time %0.5lfs for source %p", (unsigned int)lengthInFrames, timestamp ? AESecondsFromHostTicks(timestamp->mHostTime) : 0, sourceID);
source_t *source = sourceWithID(THIS, sourceID, NULL);
if ( !source ) {
if ( pthread_main_np() != 0 ) {
dprintf(THIS, 3, "Preparing new source %p\n", sourceID);
prepareNewSource(THIS, sourceID);
source = sourceWithID(THIS, sourceID, NULL);
} else {
dprintf(THIS, 3, "Enqueueing prepare for new source %p", sourceID);
action_t action = {.action = prepareNewSource, .userInfo = sourceID};
TPCircularBufferProduceBytes(&THIS->_mainThreadActionBuffer, &action, sizeof(action));
return;
}
}
if ( !audio ) return;
assert(!source->renderCallback);
AudioStreamBasicDescription audioDescription = source->audioDescription.mSampleRate ? source->audioDescription : THIS->_clientFormat;
if ( !TPCircularBufferCopyAudioBufferList(&source->buffer, audio, timestamp, lengthInFrames, &audioDescription) ) {
dprintf(THIS, 0, "Out of buffer space");
}
}
- (void)setRenderCallback:(AEMixerBufferSourceRenderCallback)renderCallback peekCallback:(AEMixerBufferSourcePeekCallback)peekCallback userInfo:(void *)userInfo forSource:(AEMixerBufferSource)sourceID {
source_t *source = sourceWithID(self, sourceID, NULL);
if ( !source ) {
source = sourceWithID(self, NULL, NULL);
if ( !source ) return;
memset(source, 0, sizeof(source_t));
source->source = sourceID;
source->volume = 1.0;
source->pan = 0.0;
source->lastAudioTimestamp = AECurrentTimeInHostTicks();
prepareSkipFadeBufferForSource(self, source);
[self refreshMixingGraph];
} else {
TPCircularBufferCleanup(&source->buffer);
}
source->renderCallback = renderCallback;
source->peekCallback = peekCallback;
source->callbackUserinfo = userInfo;
}
struct fillComplexBufferInputProc_t { AudioBufferList *bufferList; UInt32 frames; };
static OSStatus fillComplexBufferInputProc(AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData) {
struct fillComplexBufferInputProc_t *arg = inUserData;
for ( int i=0; i<ioData->mNumberBuffers; i++ ) {
ioData->mBuffers[i].mData = arg->bufferList->mBuffers[i].mData;
ioData->mBuffers[i].mDataByteSize = arg->bufferList->mBuffers[i].mDataByteSize;
}
*ioNumberDataPackets = arg->frames;
return noErr;
}
void AEMixerBufferDequeue(__unsafe_unretained AEMixerBuffer *THIS, AudioBufferList *bufferList, UInt32 *ioLengthInFrames, AudioTimeStamp *outTimestamp) {
dprintf(THIS, 1, "Dequeue %u frames", (unsigned int)*ioLengthInFrames);
unregisterSources(THIS);
if ( !THIS->_graphReady ) {
*ioLengthInFrames = 0;
return;
}
if ( pthread_mutex_trylock(&THIS->_graphMutex) != 0 ) {
*ioLengthInFrames = 0;
return;
}
// If buffer list is provided with NULL mData pointers, use our own scratch buffer
if ( bufferList && !bufferList->mBuffers[0].mData ) {
*ioLengthInFrames = MIN(*ioLengthInFrames, kScratchBufferBytesPerChannel / (THIS->_clientFormat.mBitsPerChannel/8));
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mDataByteSize = kScratchBufferBytesPerChannel * bufferList->mBuffers[i].mNumberChannels;
bufferList->mBuffers[i].mData = THIS->_scratchBuffer + i * bufferList->mBuffers[i].mDataByteSize;
}
}
// Reset time slice
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
// Determine how many frames are available globally
UInt32 sliceFrameCount = _AEMixerBufferPeek(THIS, &THIS->_currentSliceTimestamp, YES);
THIS->_currentSliceFrameCount = sliceFrameCount;
if ( bufferList ) {
*ioLengthInFrames = MIN(*ioLengthInFrames, bufferList->mBuffers[0].mDataByteSize / THIS->_clientFormat.mBytesPerFrame);
}
*ioLengthInFrames = MIN(*ioLengthInFrames, sliceFrameCount);
if ( !bufferList ) {
// Just consume frames
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) {
AEMixerBufferDequeueSingleSource(THIS, THIS->_table[i].source, NULL, ioLengthInFrames, outTimestamp);
}
}
// Reset time slice info
THIS->_currentSliceFrameCount = 0;
memset(&THIS->_currentSliceTimestamp, 0, sizeof(AudioTimeStamp));
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
pthread_mutex_unlock(&THIS->_graphMutex);
return;
}
int numberOfSources = 0;
AEMixerBufferSource firstSource = NULL;
source_t *firstSourceEntry = NULL;
for ( int i=0; i<kMaxSources && numberOfSources < 2; i++ ) {
if ( THIS->_table[i].source ) {
if ( !firstSource ) {
firstSource = THIS->_table[i].source;
firstSourceEntry = &THIS->_table[i];
}
numberOfSources++;
}
}
if ( outTimestamp ) {
*outTimestamp = THIS->_currentSliceTimestamp;
}
if ( numberOfSources == 1 && (!firstSourceEntry->audioDescription.mSampleRate || memcmp(&firstSourceEntry->audioDescription, &THIS->_clientFormat, sizeof(AudioStreamBasicDescription)) == 0) ) {
// Just one source, with the same audio format - pull straight from it
AEMixerBufferDequeueSingleSource(THIS, firstSource, bufferList, ioLengthInFrames, NULL);
// Reset time slice info
THIS->_currentSliceFrameCount = 0;
memset(&THIS->_currentSliceTimestamp, 0, sizeof(AudioTimeStamp));
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
pthread_mutex_unlock(&THIS->_graphMutex);
return;
}
// We'll advance the buffer list pointers as we add audio - save the original buffer list to restore later
AEAudioBufferListCopyOnStack(savedBufferList, bufferList, 0);
THIS->_automaticSingleSourceDequeueing = YES;
int framesToGo = MIN(*ioLengthInFrames, bufferList->mBuffers[0].mDataByteSize / THIS->_clientFormat.mBytesPerFrame);
// Process in small blocks so we don't overwhelm the mixer/converter buffers
int blockSize = framesToGo;
while ( blockSize > 512 ) blockSize /= 2;
while ( framesToGo > 0 ) {
UInt32 frames = MIN(framesToGo, blockSize);
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mDataByteSize = frames * THIS->_clientFormat.mBytesPerFrame;
}
AudioBufferList *intermediateBufferList = bufferList;
if ( THIS->_audioConverter ) {
// Initialise output buffer (to receive audio in mixer format)
intermediateBufferList = TPCircularBufferPrepareEmptyAudioBufferListWithAudioFormat(&THIS->_audioConverterBuffer, &THIS->_mixerOutputFormat, frames, NULL);
assert(intermediateBufferList != NULL);
for ( int i=0; i<intermediateBufferList->mNumberBuffers; i++ ) {
intermediateBufferList->mBuffers[i].mNumberChannels = THIS->_mixerOutputFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved ? 1 : THIS->_mixerOutputFormat.mChannelsPerFrame;
}
}
// Perform render
AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp renderTimestamp;
memset(&renderTimestamp, 0, sizeof(AudioTimeStamp));
renderTimestamp.mSampleTime = THIS->_sampleTime;
renderTimestamp.mFlags = kAudioTimeStampSampleTimeValid;
OSStatus result = AudioUnitRender(THIS->_mixerUnit, &flags, &renderTimestamp, 0, frames, intermediateBufferList);
if ( !AECheckOSStatus(result, "AudioUnitRender") ) {
break;
}
THIS->_currentSliceTimestamp.mSampleTime += frames;
THIS->_currentSliceTimestamp.mHostTime += AEHostTicksFromSeconds((double)frames/THIS->_clientFormat.mSampleRate);
THIS->_sampleTime += frames;
THIS->_currentSliceFrameCount -= frames;
if ( THIS->_audioConverter ) {
// Convert output into client format
OSStatus result = AudioConverterFillComplexBuffer(THIS->_audioConverter,
fillComplexBufferInputProc,
&(struct fillComplexBufferInputProc_t) { .bufferList = intermediateBufferList, .frames = frames },
&frames,
bufferList,
NULL);
if ( !AECheckOSStatus(result, "AudioConverterConvertComplexBuffer") ) {
break;
}
}
// Advance buffers
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mData = (uint8_t*)bufferList->mBuffers[i].mData + (frames * THIS->_clientFormat.mBytesPerFrame);
}
if ( frames == 0 ) break;
framesToGo -= frames;
}
THIS->_automaticSingleSourceDequeueing = NO;
*ioLengthInFrames -= framesToGo;
// Reset time slice info
THIS->_currentSliceFrameCount = 0;
memset(&THIS->_currentSliceTimestamp, 0, sizeof(AudioTimeStamp));
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
// Restore buffers
memcpy(bufferList, savedBufferList, AEAudioBufferListGetStructSize(bufferList));
pthread_mutex_unlock(&THIS->_graphMutex);
}
void AEMixerBufferDequeueSingleSource(__unsafe_unretained AEMixerBuffer *THIS, AEMixerBufferSource sourceID, AudioBufferList *bufferList, UInt32 *ioLengthInFrames, AudioTimeStamp *outTimestamp) {
source_t *source = sourceWithID(THIS, sourceID, NULL);
dprintf(THIS, 1, "Dequeue %u frames from source %p", (unsigned int)*ioLengthInFrames, sourceID);
AudioTimeStamp sliceTimestamp = THIS->_currentSliceTimestamp;
UInt32 sliceFrameCount = THIS->_currentSliceFrameCount;
if ( sliceTimestamp.mFlags == 0 || sliceFrameCount == 0 ) {
// Determine how many frames are available globally
sliceFrameCount = _AEMixerBufferPeek(THIS, &sliceTimestamp, YES);
THIS->_currentSliceTimestamp = sliceTimestamp;
THIS->_currentSliceFrameCount = sliceFrameCount;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
}
AudioStreamBasicDescription audioDescription = source && source->audioDescription.mSampleRate ? source->audioDescription : THIS->_clientFormat;
if ( outTimestamp ) {
*outTimestamp = sliceTimestamp;
if ( source ) {
outTimestamp->mSampleTime += source->consumedFramesInCurrentTimeSlice;
if ( outTimestamp->mFlags & kAudioTimeStampHostTimeValid ) {
outTimestamp->mHostTime += AEHostTicksFromSeconds((double)source->consumedFramesInCurrentTimeSlice / audioDescription.mSampleRate);
}
}
}
*ioLengthInFrames = MIN(*ioLengthInFrames, sliceFrameCount - (source ? source->consumedFramesInCurrentTimeSlice : 0));
// If buffer list is provided with NULL mData pointers, use our own scratch buffer
if ( bufferList && !bufferList->mBuffers[0].mData ) {
*ioLengthInFrames = MIN(*ioLengthInFrames, kScratchBufferBytesPerChannel / (audioDescription.mBitsPerChannel/8));
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mDataByteSize = kScratchBufferBytesPerChannel * bufferList->mBuffers[i].mNumberChannels;
bufferList->mBuffers[i].mData = THIS->_scratchBuffer + i*bufferList->mBuffers[i].mDataByteSize;
}
}
if ( bufferList ) {
// Silence buffer list in advance
for ( int i=0; i<bufferList->mNumberBuffers; i++ ) {
memset(bufferList->mBuffers[i].mData, 0, bufferList->mBuffers[i].mDataByteSize);
}
}
if ( !source ) {
return;
}
AudioTimeStamp sourceTimestamp;
memset(&sourceTimestamp, 0, sizeof(sourceTimestamp));
UInt32 sourceFrameCount = 0;
if ( sliceFrameCount > 0 ) {
// Now determine the frame count and timestamp on the current source
if ( source->peekCallback ) {
sourceFrameCount = source->peekCallback(source->source, &sourceTimestamp, source->callbackUserinfo);
if ( sourceFrameCount == AEMixerBufferSourceInactive ) {
dprintf(THIS, 3, "Source %p is inactive", source->source);
} else {
dprintf(THIS, 3, "Source %p: %u frames @ %0.5lfs", source->source, (unsigned int)sourceFrameCount, AESecondsFromHostTicks(sourceTimestamp.mHostTime));
}
if ( sourceFrameCount != AEMixerBufferSourceInactive && THIS->_assumeInfiniteSources ) sourceFrameCount = UINT32_MAX;
if ( sourceFrameCount == AEMixerBufferSourceInactive ) sourceFrameCount = 0;
} else {
sourceFrameCount = TPCircularBufferPeek(&source->buffer, &sourceTimestamp, &audioDescription);
dprintf(THIS, 3, "Source %p: %u frames @ %0.5lfs", source->source, (unsigned int)sourceFrameCount, AESecondsFromHostTicks(sourceTimestamp.mHostTime));
}
}
if ( sourceFrameCount > 0 ) {
int totalRequiredSkipFrames = 0;
int skipFrames = 0;
if ( sourceTimestamp.mFlags & kAudioTimeStampHostTimeValid
&& sliceTimestamp.mFlags & kAudioTimeStampHostTimeValid
&& sourceTimestamp.mHostTime < sliceTimestamp.mHostTime - AEHostTicksFromSeconds((!source->synced ? 0.001 : kResyncTimestampThreshold)) ) {
// This source is behind. We'll skip some frames.
NSTimeInterval discrepancy = AESecondsFromHostTicks(sliceTimestamp.mHostTime - sourceTimestamp.mHostTime);
totalRequiredSkipFrames = discrepancy * audioDescription.mSampleRate;
skipFrames = MIN(totalRequiredSkipFrames, sourceFrameCount > *ioLengthInFrames ? sourceFrameCount - *ioLengthInFrames : 0);
dprintf(THIS, 3, "Need to skip %d frames, as source is %0.4lfs behind (will skip %d)", totalRequiredSkipFrames, discrepancy, skipFrames);
} else {
source->synced = YES;
source->started = YES;
}
if ( skipFrames > 0 || source->skipFadeBuffer->mBuffers[0].mDataByteSize > 0 ) {
UInt32 microfadeFrames = 0;
if ( skipFrames > 0 && source->synced ) {
#ifdef DEBUG
dprintf(THIS, 1, "Mixer buffer %p skipping %d frames of source %p due to %0.4lfs discrepancy (%0.4lf source, %0.4lf stream)\n",
THIS,
totalRequiredSkipFrames,
source->source,
AESecondsFromHostTicks(sliceTimestamp.mHostTime - sourceTimestamp.mHostTime),
AESecondsFromHostTicks(sourceTimestamp.mHostTime),
AESecondsFromHostTicks(sliceTimestamp.mHostTime));
#endif
source->synced = NO;
}
if ( source->skipFadeBuffer->mBuffers[0].mDataByteSize > 0 ) {
// We have some frames in the skip buffer, ready to crossfade
microfadeFrames = MIN(*ioLengthInFrames, source->skipFadeBuffer->mBuffers[0].mDataByteSize / audioDescription.mBytesPerFrame);
} else {
// Take the first of the frames we're going to skip
microfadeFrames = MIN(skipFrames, MIN(*ioLengthInFrames, kMaxMicrofadeDuration));
for ( int i=0; i<source->skipFadeBuffer->mNumberBuffers; i++ ) {
source->skipFadeBuffer->mBuffers[i].mDataByteSize = audioDescription.mBytesPerFrame * microfadeFrames;
}
dprintf(THIS, 3, "Taking %u frames for microfade", (unsigned int)microfadeFrames);
if ( source->renderCallback ) {
source->renderCallback(source->source, microfadeFrames, source->skipFadeBuffer, &sourceTimestamp, source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&source->buffer, µfadeFrames, source->skipFadeBuffer, NULL, &audioDescription);
}
sourceTimestamp.mSampleTime += microfadeFrames;
sourceTimestamp.mHostTime += AEHostTicksFromSeconds(((double)microfadeFrames / (double)source->audioDescription.mSampleRate));
skipFrames -= microfadeFrames;
}
// Convert the audio to float
if ( !AEFloatConverterToFloat(source->floatConverter ? (__bridge AEFloatConverter*)source->floatConverter : THIS->_floatConverter,
source->skipFadeBuffer,
THIS->_microfadeBuffer,
microfadeFrames) ) {
return;
}
// Apply fade out
float start = 1.0;
float step = -1.0 / (float)microfadeFrames;
if ( audioDescription.mChannelsPerFrame == 2 ) {
vDSP_vrampmul2(THIS->_microfadeBuffer[0], THIS->_microfadeBuffer[1], 1, &start, &step, THIS->_microfadeBuffer[0], THIS->_microfadeBuffer[1], 1, microfadeFrames);
} else {
for ( int i=0; i<audioDescription.mChannelsPerFrame; i++ ) {
start = 1.0;
vDSP_vrampmul(THIS->_microfadeBuffer[i], 1, &start, &step, THIS->_microfadeBuffer[i], 1, microfadeFrames);
}
}
if ( skipFrames > 0 ) {
// Throw away the rest
dprintf(THIS, 3, "Discarding %d frames", skipFrames);
UInt32 discardFrames = skipFrames;
if ( source->renderCallback ) {
source->renderCallback(source->source, discardFrames, NULL, &sourceTimestamp, source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&source->buffer, &discardFrames, NULL, NULL, &audioDescription);
}
sourceTimestamp.mSampleTime += discardFrames;
sourceTimestamp.mHostTime += AEHostTicksFromSeconds((double)discardFrames / (double)source->audioDescription.mSampleRate);
}
for ( int i=0; i<source->skipFadeBuffer->mNumberBuffers; i++ ) {
source->skipFadeBuffer->mBuffers[i].mDataByteSize = 0;
}
// Take the fresh audio
UInt32 freshFrames = *ioLengthInFrames;
dprintf(THIS, 3, "Dequeuing %u fresh frames", (unsigned int)freshFrames);
if ( source->renderCallback ) {
source->renderCallback(source->source, freshFrames, bufferList, &sourceTimestamp, source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&source->buffer, &freshFrames, bufferList, NULL, &audioDescription);
}
sourceTimestamp.mSampleTime += freshFrames;
sourceTimestamp.mHostTime += AEHostTicksFromSeconds((double)freshFrames / (double)source->audioDescription.mSampleRate);
microfadeFrames = MIN(microfadeFrames, freshFrames);
if ( bufferList ) {
// Convert the audio to float
if ( !AEFloatConverterToFloat(source->floatConverter ? (__bridge AEFloatConverter*)source->floatConverter : THIS->_floatConverter,
bufferList,
THIS->_microfadeBuffer + audioDescription.mChannelsPerFrame,
microfadeFrames) ) {
return;
}
// Apply fade in
start = 0.0;
step = 1.0 / (float)microfadeFrames;
if ( audioDescription.mChannelsPerFrame == 2 ) {
vDSP_vrampmul2(THIS->_microfadeBuffer[2+0], THIS->_microfadeBuffer[2+1], 1, &start, &step, THIS->_microfadeBuffer[2+0], THIS->_microfadeBuffer[2+1], 1, microfadeFrames);
} else {
for ( int i=0; i<audioDescription.mChannelsPerFrame; i++ ) {
start = 1.0;
vDSP_vrampmul(THIS->_microfadeBuffer[audioDescription.mChannelsPerFrame + i], 1, &start, &step, THIS->_microfadeBuffer[audioDescription.mChannelsPerFrame + i], 1, microfadeFrames);
}
}
// Add buffers together
for ( int i=0; i<audioDescription.mChannelsPerFrame; i++ ) {
vDSP_vadd(THIS->_microfadeBuffer[i], 1, THIS->_microfadeBuffer[audioDescription.mChannelsPerFrame + i], 1, THIS->_microfadeBuffer[i], 1, microfadeFrames);
}
// Store in output
if ( !AEFloatConverterFromFloat(source->floatConverter ? (__bridge AEFloatConverter*)source->floatConverter : THIS->_floatConverter,
THIS->_microfadeBuffer,
bufferList,
microfadeFrames) ) {
return;
}
}
if ( skipFrames > 0 && skipFrames == totalRequiredSkipFrames ) {
// Now synced
if ( source->started ) {
dprintf(THIS, 3, "Source %p synced", source->source);
}
source->synced = YES;
source->started = YES;
}
} else {
// Consume audio
dprintf(THIS, 3, "Consuming %u frames", (unsigned int)*ioLengthInFrames);
if ( source->renderCallback ) {
source->renderCallback(source->source, *ioLengthInFrames, bufferList, &sourceTimestamp, source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&source->buffer, ioLengthInFrames, bufferList, NULL, &audioDescription);
}
}
}
if ( !THIS->_automaticSingleSourceDequeueing ) {
// If we're pulling the sources individually...
// Increment the consumed frame count for this source for the current time slice
source->consumedFramesInCurrentTimeSlice += *ioLengthInFrames;
// Determine the globally consumed frames
int sourceCount = 0;
UInt32 minConsumedFrameCount = UINT32_MAX;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) {
sourceCount++;
minConsumedFrameCount = MIN(minConsumedFrameCount, THIS->_table[i].consumedFramesInCurrentTimeSlice);
}
}
if ( minConsumedFrameCount > 0 ) {
dprintf(THIS, 3, "Increasing timeline by %u frames", (unsigned int)minConsumedFrameCount);
// Increment time slice info
THIS->_sampleTime += minConsumedFrameCount;
THIS->_currentSliceFrameCount -= minConsumedFrameCount;
THIS->_currentSliceTimestamp.mSampleTime += minConsumedFrameCount;
THIS->_currentSliceTimestamp.mHostTime += AEHostTicksFromSeconds((double)minConsumedFrameCount/THIS->_clientFormat.mSampleRate);
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
}
}
}
UInt32 AEMixerBufferPeek(__unsafe_unretained AEMixerBuffer *THIS, AudioTimeStamp *outNextTimestamp) {
unregisterSources(THIS);
return _AEMixerBufferPeek(THIS, outNextTimestamp, NO);
}
static UInt32 _AEMixerBufferPeek(__unsafe_unretained AEMixerBuffer *THIS, AudioTimeStamp *outNextTimestamp, BOOL respectInfiniteSourceFlag) {
dprintf(THIS, 3, "Peeking");
// Make sure we have at least one source
BOOL hasSources = NO;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) {
hasSources = YES;
break;
}
}
if ( !hasSources ) {
dprintf(THIS, 3, "No sources");
if ( outNextTimestamp ) memset(outNextTimestamp, 0, sizeof(AudioTimeStamp));
return 0;
}
// Clear time slice info
THIS->_currentSliceFrameCount = 0;
memset(&THIS->_currentSliceTimestamp, 0, sizeof(AudioTimeStamp));
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
// Determine lowest buffer fill count, excluding drained sources that we aren't receiving from (for those, we'll return silence),
// and address sources that are behind the timeline
uint64_t now = AECurrentTimeInHostTicks();
AudioTimeStamp earliestEndTimestamp = { .mHostTime = UINT64_MAX };
AudioTimeStamp latestStartTimestamp = { .mHostTime = 0 };
source_t *earliestEndSource = NULL;
UInt32 minFrameCount = UINT32_MAX;
BOOL hasActiveSources = NO;
struct {
source_t *source;
uint64_t endHostTime;
UInt32 frameCount;
AudioTimeStamp timestamp; } peekEntries[kMaxSources];
memset(&peekEntries, 0, sizeof(peekEntries));
int peekEntriesCount = 0;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) {
source_t *source = &THIS->_table[i];
AudioTimeStamp timestamp;
memset(×tamp, 0, sizeof(timestamp));
UInt32 frameCount = 0;
AudioStreamBasicDescription audioDescription = source->audioDescription.mSampleRate ? source->audioDescription : THIS->_clientFormat;
if ( source->peekCallback ) {
frameCount = source->peekCallback(source->source, ×tamp, source->callbackUserinfo);
if ( frameCount != AEMixerBufferSourceInactive && respectInfiniteSourceFlag && THIS->_assumeInfiniteSources ) frameCount = UINT32_MAX;
} else {
frameCount = TPCircularBufferPeek(&source->buffer, ×tamp, &audioDescription);
}
if ( frameCount == AEMixerBufferSourceInactive ) {
dprintf(THIS, 3, "Source %p is inactive", source->source);
} else {
dprintf(THIS, 3, "Source %p: %u frames @ %0.5lfs", source->source, (unsigned int)frameCount, AESecondsFromHostTicks(timestamp.mHostTime));
}
if ( (frameCount == 0 && AESecondsFromHostTicks(now - source->lastAudioTimestamp) > THIS->_sourceIdleThreshold)
|| frameCount == AEMixerBufferSourceInactive ) {
// Not receiving audio - ignore this empty source
dprintf(THIS, 3, "Skipping empty and idle source %p", source->source);
continue;
}
if ( frameCount < minFrameCount ) minFrameCount = frameCount;
source->lastAudioTimestamp = now;
hasActiveSources = YES;
if ( !(timestamp.mFlags & kAudioTimeStampHostTimeValid) ) {
continue;
}
AudioTimeStamp endTimestamp = timestamp;
endTimestamp.mHostTime = frameCount == UINT32_MAX ? UINT64_MAX : (UInt64)(endTimestamp.mHostTime + AEHostTicksFromSeconds(((double)frameCount / audioDescription.mSampleRate)));
endTimestamp.mSampleTime = frameCount == UINT32_MAX ? UINT32_MAX : (endTimestamp.mSampleTime + frameCount);
peekEntries[peekEntriesCount].source = source;
peekEntries[peekEntriesCount].endHostTime = endTimestamp.mHostTime;
peekEntries[peekEntriesCount].frameCount = frameCount;
peekEntries[peekEntriesCount].timestamp = timestamp;
peekEntriesCount++;
if ( timestamp.mHostTime > latestStartTimestamp.mHostTime ) {
latestStartTimestamp = timestamp;
}
if ( endTimestamp.mHostTime < earliestEndTimestamp.mHostTime ) {
earliestEndTimestamp = endTimestamp;
earliestEndSource = source;
}
}
}
if ( !hasActiveSources || minFrameCount == 0 ) {
// No audio available
dprintf(THIS, 3, "No audio available");
if ( outNextTimestamp ) memset(outNextTimestamp, 0, sizeof(AudioTimeStamp));
return 0;
}
unsigned long long latestStartFrames = latestStartTimestamp.mFlags & kAudioTimeStampHostTimeValid
? round(AESecondsFromHostTicks(latestStartTimestamp.mHostTime) * THIS->_clientFormat.mSampleRate)
: 0;
unsigned long long earliestEndFrames = earliestEndTimestamp.mFlags & kAudioTimeStampHostTimeValid
? round(AESecondsFromHostTicks(earliestEndTimestamp.mHostTime) * THIS->_clientFormat.mSampleRate)
: minFrameCount;
if ( earliestEndSource && latestStartFrames >= earliestEndFrames ) {
// One or more of the sources is behind - skip all frames of these sources
for ( int i=0; i<peekEntriesCount; i++ ) {
unsigned long long sourceEndFrames = round(AESecondsFromHostTicks(peekEntries[i].endHostTime) * THIS->_clientFormat.mSampleRate);
if ( latestStartFrames >= sourceEndFrames ) {
#ifdef DEBUG
dprintf(THIS, 1, "Mixer buffer %p skipping %u frames of source %p (ends %0.4lfs/%d frames before latest source starts)",
THIS,
(unsigned int)peekEntries[i].frameCount,
peekEntries[i].source->source,
AESecondsFromHostTicks(latestStartTimestamp.mHostTime-peekEntries[i].endHostTime),
(int)(latestStartFrames-sourceEndFrames));
#endif
UInt32 skipFrames = peekEntries[i].frameCount;
AudioStreamBasicDescription sourceASBD = peekEntries[i].source->audioDescription.mSampleRate ? peekEntries[i].source->audioDescription : THIS->_clientFormat;
if ( peekEntries[i].source->skipFadeBuffer->mBuffers[0].mDataByteSize == 0 ) {
// Take the first of the frames we're going to skip, to crossfade later
UInt32 microfadeFrames = MIN(peekEntries[i].frameCount, kMaxMicrofadeDuration);
skipFrames -= microfadeFrames;
for ( int j=0; j<peekEntries[i].source->skipFadeBuffer->mNumberBuffers; j++ ) {
peekEntries[i].source->skipFadeBuffer->mBuffers[j].mDataByteSize = sourceASBD.mBytesPerFrame * microfadeFrames;
}
if ( peekEntries[i].source->renderCallback ) {
peekEntries[i].source->renderCallback(peekEntries[i].source->source, microfadeFrames, peekEntries[i].source->skipFadeBuffer, &peekEntries[i].timestamp, peekEntries[i].source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&peekEntries[i].source->buffer, µfadeFrames, peekEntries[i].source->skipFadeBuffer, NULL, &sourceASBD);
}
peekEntries[i].timestamp.mSampleTime += microfadeFrames;
peekEntries[i].timestamp.mHostTime += AEHostTicksFromSeconds((double)microfadeFrames / (double)peekEntries[i].source->audioDescription.mSampleRate);
}
if ( skipFrames > 0 ) {
if ( peekEntries[i].source->renderCallback ) {
peekEntries[i].source->renderCallback(peekEntries[i].source->source, skipFrames, NULL, &peekEntries[i].timestamp, peekEntries[i].source->callbackUserinfo);
} else {
TPCircularBufferDequeueBufferListFrames(&peekEntries[i].source->buffer, &skipFrames, NULL, NULL, &sourceASBD);
}
}
}
}
if ( outNextTimestamp ) memset(outNextTimestamp, 0, sizeof(AudioTimeStamp));
return 0;
}
UInt32 frameCount = (UInt32)(earliestEndFrames - latestStartFrames);
int frameDiscrepancyThreshold = kResyncTimestampThreshold * THIS->_clientFormat.mSampleRate; // Account for small time discrepancies
if ( frameCount > (minFrameCount >= frameDiscrepancyThreshold ? minFrameCount - frameDiscrepancyThreshold : minFrameCount) ) {
frameCount = minFrameCount;
}
dprintf(THIS, 3, "%u frames available @ %0.5lfs", (unsigned int)frameCount, AESecondsFromHostTicks(latestStartTimestamp.mHostTime));
if ( frameCount < kMinimumFrameCount ) {
dprintf(THIS, 3, "Less than minimum frame count");
if ( outNextTimestamp ) memset(outNextTimestamp, 0, sizeof(AudioTimeStamp));
return 0;
}
if ( outNextTimestamp ) {
*outNextTimestamp = latestStartTimestamp;
outNextTimestamp->mSampleTime = THIS->_sampleTime;
outNextTimestamp->mFlags |= kAudioTimeStampSampleTimeValid;
}
return frameCount;
}
void AEMixerBufferEndTimeInterval(__unsafe_unretained AEMixerBuffer *THIS) {
if ( THIS->_currentSliceFrameCount == 0 ) return;
dprintf(THIS, 3, "End of time interval marked");
// Determine the minimum consumed frames across those sources that have had frames consumed
UInt32 minConsumedFrameCount = UINT32_MAX;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source && THIS->_table[i].consumedFramesInCurrentTimeSlice != 0 ) {
minConsumedFrameCount = MIN(minConsumedFrameCount, THIS->_table[i].consumedFramesInCurrentTimeSlice);
}
}
// Discard audio of sources that haven't had frames consumed
if ( minConsumedFrameCount > 0 ) {
THIS->_automaticSingleSourceDequeueing = YES;
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source && THIS->_table[i].consumedFramesInCurrentTimeSlice == 0 ) {
UInt32 frames = minConsumedFrameCount;
dprintf(THIS, 3, "Discarding %u frames from source %p", (unsigned int)frames, THIS->_table[i].source);
AEMixerBufferDequeueSingleSource(THIS, THIS->_table[i].source, NULL, &frames, NULL);
}
}
THIS->_automaticSingleSourceDequeueing = NO;
// Increment sample time
THIS->_sampleTime += minConsumedFrameCount;
}
// Clear time slice info
THIS->_currentSliceFrameCount = 0;
memset(&THIS->_currentSliceTimestamp, 0, sizeof(AudioTimeStamp));
for ( int i=0; i<kMaxSources; i++ ) {
if ( THIS->_table[i].source ) THIS->_table[i].consumedFramesInCurrentTimeSlice = 0;
}
}
void AEMixerBufferMarkSourceIdle(__unsafe_unretained AEMixerBuffer *THIS, AEMixerBufferSource sourceID) {
source_t *source = sourceWithID(THIS, sourceID, NULL);
if ( source ) {
dprintf(THIS, 3, "Marking source %p idle", sourceID);
source->lastAudioTimestamp = 0;
source->synced = NO;
}
}