-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCAPlayThrough.cpp
883 lines (713 loc) · 28.3 KB
/
CAPlayThrough.cpp
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
/*
File: CAPlayThrough.cpp
Abstract: CAPlayThough Classes.
Version: 1.2.2
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2013 Apple Inc. All Rights Reserved.
*/
#include "CAPlayThrough.h"
#pragma mark -- CAPlayThrough
// we define the class here so that is is not accessible from any object aside from CAPlayThroughManager
class CAPlayThrough
{
public:
CAPlayThrough(AudioDeviceID input, AudioDeviceID output);
~CAPlayThrough();
OSStatus Init(AudioDeviceID input, AudioDeviceID output);
void Cleanup();
OSStatus Start();
OSStatus Stop();
Boolean IsRunning();
OSStatus SetInputDeviceAsCurrent(AudioDeviceID in);
OSStatus SetOutputDeviceAsCurrent(AudioDeviceID out);
AudioDeviceID GetInputDeviceID() { return mInputDevice.mID; }
AudioDeviceID GetOutputDeviceID() { return mOutputDevice.mID; }
private:
OSStatus SetupGraph(AudioDeviceID out);
OSStatus MakeGraph();
OSStatus SetupAUHAL(AudioDeviceID in);
OSStatus EnableIO();
OSStatus CallbackSetup();
OSStatus SetupBuffers();
void ComputeThruOffset();
static OSStatus InputProc(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData);
static OSStatus OutputProc(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData);
AudioUnit mInputUnit;
AudioBufferList *mInputBuffer;
AudioDevice mInputDevice, mOutputDevice;
CARingBuffer *mBuffer;
//AudioUnits and Graph
AUGraph mGraph;
AUNode mVarispeedNode;
AudioUnit mVarispeedUnit;
AUNode mOutputNode;
AudioUnit mOutputUnit;
//Buffer sample info
Float64 mFirstInputTime;
Float64 mFirstOutputTime;
Float64 mInToOutSampleOffset;
};
#pragma mark ---Public Methods---
#pragma mark ---CAPlayThrough Methods---
CAPlayThrough::CAPlayThrough(AudioDeviceID input, AudioDeviceID output):
mBuffer(NULL),
mFirstInputTime(-1),
mFirstOutputTime(-1),
mInToOutSampleOffset(0)
{
OSStatus err = noErr;
err = Init(input,output);
if(err) {
fprintf(stderr,"CAPlayThrough ERROR: Cannot Init CAPlayThrough");
exit(1);
}
}
CAPlayThrough::~CAPlayThrough()
{
Cleanup();
}
OSStatus CAPlayThrough::Init(AudioDeviceID input, AudioDeviceID output)
{
OSStatus err = noErr;
//Note: You can interface to input and output devices with "output" audio units.
//Please keep in mind that you are only allowed to have one output audio unit per graph (AUGraph).
//As you will see, this sample code splits up the two output units. The "output" unit that will
//be used for device input will not be contained in a AUGraph, while the "output" unit that will
//interface the default output device will be in a graph.
//Setup AUHAL for an input device
err = SetupAUHAL(input);
checkErr(err);
//Setup Graph containing Varispeed Unit & Default Output Unit
err = SetupGraph(output);
checkErr(err);
err = SetupBuffers();
checkErr(err);
// the varispeed unit should only be conected after the input and output formats have been set
err = AUGraphConnectNodeInput(mGraph, mVarispeedNode, 0, mOutputNode, 0);
checkErr(err);
err = AUGraphInitialize(mGraph);
checkErr(err);
//Add latency between the two devices
ComputeThruOffset();
return err;
}
void CAPlayThrough::Cleanup()
{
//clean up
Stop();
delete mBuffer;
mBuffer = 0;
if(mInputBuffer){
for(UInt32 i = 0; i<mInputBuffer->mNumberBuffers; i++)
free(mInputBuffer->mBuffers[i].mData);
free(mInputBuffer);
mInputBuffer = 0;
}
AudioUnitUninitialize(mInputUnit);
AUGraphClose(mGraph);
DisposeAUGraph(mGraph);
AudioComponentInstanceDispose(mInputUnit);
}
#pragma mark --- Operation---
OSStatus CAPlayThrough::Start()
{
OSStatus err = noErr;
if(!IsRunning()){
//Start pulling for audio data
err = AudioOutputUnitStart(mInputUnit);
checkErr(err);
err = AUGraphStart(mGraph);
checkErr(err);
//reset sample times
mFirstInputTime = -1;
mFirstOutputTime = -1;
}
return err;
}
OSStatus CAPlayThrough::Stop()
{
OSStatus err = noErr;
if(IsRunning()){
//Stop the AUHAL
err = AudioOutputUnitStop(mInputUnit);
checkErr(err);
err = AUGraphStop(mGraph);
checkErr(err);
mFirstInputTime = -1;
mFirstOutputTime = -1;
}
return err;
}
Boolean CAPlayThrough::IsRunning()
{
OSStatus err = noErr;
UInt32 auhalRunning = 0, size = 0;
Boolean graphRunning = false;
size = sizeof(auhalRunning);
if(mInputUnit)
{
err = AudioUnitGetProperty(mInputUnit,
kAudioOutputUnitProperty_IsRunning,
kAudioUnitScope_Global,
0, // input element
&auhalRunning,
&size);
checkErr(err);
}
if(mGraph) {
err = AUGraphIsRunning(mGraph,&graphRunning);
checkErr(err);
}
return (auhalRunning || graphRunning);
}
OSStatus CAPlayThrough::SetOutputDeviceAsCurrent(AudioDeviceID out)
{
UInt32 size = sizeof(AudioDeviceID);;
OSStatus err = noErr;
// UInt32 propsize = sizeof(Float32);
//AudioObjectPropertyScope theScope = mIsInput ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput;
AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
if(out == kAudioDeviceUnknown) //Retrieve the default output device
{
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &size, &out);
checkErr(err);
}
mOutputDevice.Init(out, false);
//Set the Current Device to the Default Output Unit.
err = AudioUnitSetProperty(mOutputUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&mOutputDevice.mID,
sizeof(mOutputDevice.mID));
return err;
}
OSStatus CAPlayThrough::SetInputDeviceAsCurrent(AudioDeviceID in)
{
UInt32 size = sizeof(AudioDeviceID);
OSStatus err = noErr;
AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
if(in == kAudioDeviceUnknown) //get the default input device if device is unknown
{
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &size, &in);
checkErr(err);
}
mInputDevice.Init(in, true);
//Set the Current Device to the AUHAL.
//this should be done only after IO has been enabled on the AUHAL.
err = AudioUnitSetProperty(mInputUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&mInputDevice.mID,
sizeof(mInputDevice.mID));
checkErr(err);
return err;
}
#pragma mark -
#pragma mark --Private methods---
OSStatus CAPlayThrough::SetupGraph(AudioDeviceID out)
{
OSStatus err = noErr;
AURenderCallbackStruct output;
//Make a New Graph
err = NewAUGraph(&mGraph);
checkErr(err);
//Open the Graph, AudioUnits are opened but not initialized
err = AUGraphOpen(mGraph);
checkErr(err);
err = MakeGraph();
checkErr(err);
err = SetOutputDeviceAsCurrent(out);
checkErr(err);
//Tell the output unit not to reset timestamps
//Otherwise sample rate changes will cause sync los
UInt32 startAtZero = 0;
err = AudioUnitSetProperty(mOutputUnit,
kAudioOutputUnitProperty_StartTimestampsAtZero,
kAudioUnitScope_Global,
0,
&startAtZero,
sizeof(startAtZero));
checkErr(err);
output.inputProc = OutputProc;
output.inputProcRefCon = this;
err = AudioUnitSetProperty(mVarispeedUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&output,
sizeof(output));
checkErr(err);
return err;
}
OSStatus CAPlayThrough::MakeGraph()
{
OSStatus err = noErr;
AudioComponentDescription varispeedDesc,outDesc;
//Q:Why do we need a varispeed unit?
//A:If the input device and the output device are running at different sample rates
//we will need to move the data coming to the graph slower/faster to avoid a pitch change.
varispeedDesc.componentType = kAudioUnitType_FormatConverter;
varispeedDesc.componentSubType = kAudioUnitSubType_Varispeed;
varispeedDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
varispeedDesc.componentFlags = 0;
varispeedDesc.componentFlagsMask = 0;
outDesc.componentType = kAudioUnitType_Output;
outDesc.componentSubType = kAudioUnitSubType_DefaultOutput;
outDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
outDesc.componentFlags = 0;
outDesc.componentFlagsMask = 0;
//////////////////////////
///MAKE NODES
//This creates a node in the graph that is an AudioUnit, using
//the supplied ComponentDescription to find and open that unit
err = AUGraphAddNode(mGraph, &varispeedDesc, &mVarispeedNode);
checkErr(err);
err = AUGraphAddNode(mGraph, &outDesc, &mOutputNode);
checkErr(err);
//Get Audio Units from AUGraph node
err = AUGraphNodeInfo(mGraph, mVarispeedNode, NULL, &mVarispeedUnit);
checkErr(err);
err = AUGraphNodeInfo(mGraph, mOutputNode, NULL, &mOutputUnit);
checkErr(err);
// don't connect nodes until the varispeed unit has input and output formats set
return err;
}
OSStatus CAPlayThrough::SetupAUHAL(AudioDeviceID in)
{
OSStatus err = noErr;
AudioComponent comp;
AudioComponentDescription desc;
//There are several different types of Audio Units.
//Some audio units serve as Outputs, Mixers, or DSP
//units. See AUComponent.h for listing
desc.componentType = kAudioUnitType_Output;
//Every Component has a subType, which will give a clearer picture
//of what this components function will be.
desc.componentSubType = kAudioUnitSubType_HALOutput;
//all Audio Units in AUComponent.h must use
//"kAudioUnitManufacturer_Apple" as the Manufacturer
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
//Finds a component that meets the desc spec's
comp = AudioComponentFindNext(NULL, &desc);
if (comp == NULL) exit (-1);
//gains access to the services provided by the component
err = AudioComponentInstanceNew(comp, &mInputUnit);
checkErr(err);
//AUHAL needs to be initialized before anything is done to it
err = AudioUnitInitialize(mInputUnit);
checkErr(err);
err = EnableIO();
checkErr(err);
err= SetInputDeviceAsCurrent(in);
checkErr(err);
err = CallbackSetup();
checkErr(err);
//Don't setup buffers until you know what the
//input and output device audio streams look like.
err = AudioUnitInitialize(mInputUnit);
return err;
}
OSStatus CAPlayThrough::EnableIO()
{
OSStatus err = noErr;
UInt32 enableIO;
///////////////
//ENABLE IO (INPUT)
//You must enable the Audio Unit (AUHAL) for input and disable output
//BEFORE setting the AUHAL's current device.
//Enable input on the AUHAL
enableIO = 1;
err = AudioUnitSetProperty(mInputUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
1, // input element
&enableIO,
sizeof(enableIO));
checkErr(err);
//disable Output on the AUHAL
enableIO = 0;
err = AudioUnitSetProperty(mInputUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0, //output element
&enableIO,
sizeof(enableIO));
return err;
}
OSStatus CAPlayThrough::CallbackSetup()
{
OSStatus err = noErr;
AURenderCallbackStruct input;
input.inputProc = InputProc;
input.inputProcRefCon = this;
//Setup the input callback.
err = AudioUnitSetProperty(mInputUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global,
0,
&input,
sizeof(input));
checkErr(err);
return err;
}
//Allocate Audio Buffer List(s) to hold the data from input.
OSStatus CAPlayThrough::SetupBuffers()
{
OSStatus err = noErr;
UInt32 bufferSizeFrames,bufferSizeBytes,propsize;
CAStreamBasicDescription asbd,asbd_dev1_in,asbd_dev2_out;
Float64 rate=0;
//Get the size of the IO buffer(s)
UInt32 propertySize = sizeof(bufferSizeFrames);
err = AudioUnitGetProperty(mInputUnit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &bufferSizeFrames, &propertySize);
checkErr(err);
bufferSizeBytes = bufferSizeFrames * sizeof(Float32);
//Get the Stream Format (Output client side)
propertySize = sizeof(asbd_dev1_in);
err = AudioUnitGetProperty(mInputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &asbd_dev1_in, &propertySize);
checkErr(err);
//printf("=====Input DEVICE stream format\n" );
//asbd_dev1_in.Print();
//Get the Stream Format (client side)
propertySize = sizeof(asbd);
err = AudioUnitGetProperty(mInputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &asbd, &propertySize);
checkErr(err);
//printf("=====current Input (Client) stream format\n");
//asbd.Print();
//Get the Stream Format (Output client side)
propertySize = sizeof(asbd_dev2_out);
err = AudioUnitGetProperty(mOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &asbd_dev2_out, &propertySize);
checkErr(err);
//printf("=====Output (Device) stream format\n");
//asbd_dev2_out.Print();
//////////////////////////////////////
//Set the format of all the AUs to the input/output devices channel count
//For a simple case, you want to set this to the lower of count of the channels
//in the input device vs output device
//////////////////////////////////////
asbd.mChannelsPerFrame =((asbd_dev1_in.mChannelsPerFrame < asbd_dev2_out.mChannelsPerFrame) ?asbd_dev1_in.mChannelsPerFrame :asbd_dev2_out.mChannelsPerFrame) ;
//printf("Info: Input Device channel count=%ld\t Input Device channel count=%ld\n",asbd_dev1_in.mChannelsPerFrame,asbd_dev2_out.mChannelsPerFrame);
//printf("Info: CAPlayThrough will use %ld channels\n",asbd.mChannelsPerFrame);
// We must get the sample rate of the input device and set it to the stream format of AUHAL
propertySize = sizeof(Float64);
AudioObjectPropertyAddress theAddress = { kAudioDevicePropertyNominalSampleRate,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
err = AudioObjectGetPropertyData(mInputDevice.mID, &theAddress, 0, NULL, &propertySize, &rate);
checkErr(err);
asbd.mSampleRate =rate;
propertySize = sizeof(asbd);
//Set the new formats to the AUs...
err = AudioUnitSetProperty(mInputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &asbd, propertySize);
checkErr(err);
err = AudioUnitSetProperty(mVarispeedUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &asbd, propertySize);
checkErr(err);
//Set the correct sample rate for the output device, but keep the channel count the same
propertySize = sizeof(Float64);
err = AudioObjectGetPropertyData(mOutputDevice.mID, &theAddress, 0, NULL, &propertySize, &rate);
checkErr(err);
asbd.mSampleRate =rate;
propertySize = sizeof(asbd);
//Set the new audio stream formats for the rest of the AUs...
err = AudioUnitSetProperty(mVarispeedUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &asbd, propertySize);
checkErr(err);
err = AudioUnitSetProperty(mOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &asbd, propertySize);
checkErr(err);
//calculate number of buffers from channels
propsize = offsetof(AudioBufferList, mBuffers[0]) + (sizeof(AudioBuffer) *asbd.mChannelsPerFrame);
//malloc buffer lists
mInputBuffer = (AudioBufferList *)malloc(propsize);
mInputBuffer->mNumberBuffers = asbd.mChannelsPerFrame;
//pre-malloc buffers for AudioBufferLists
for(UInt32 i =0; i< mInputBuffer->mNumberBuffers ; i++) {
mInputBuffer->mBuffers[i].mNumberChannels = 1;
mInputBuffer->mBuffers[i].mDataByteSize = bufferSizeBytes;
mInputBuffer->mBuffers[i].mData = malloc(bufferSizeBytes);
}
//Alloc ring buffer that will hold data between the two audio devices
mBuffer = new CARingBuffer();
mBuffer->Allocate(asbd.mChannelsPerFrame, asbd.mBytesPerFrame, bufferSizeFrames * 20);
return err;
}
void CAPlayThrough::ComputeThruOffset()
{
//The initial latency will at least be the saftey offset's of the devices + the buffer sizes
mInToOutSampleOffset = SInt32(mInputDevice.mSafetyOffset + mInputDevice.mBufferSizeFrames +
mOutputDevice.mSafetyOffset + mOutputDevice.mBufferSizeFrames);
}
#pragma mark -
#pragma mark -- IO Procs --
OSStatus CAPlayThrough::InputProc(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
OSStatus err = noErr;
CAPlayThrough *This = (CAPlayThrough *)inRefCon;
if (This->mFirstInputTime < 0.)
This->mFirstInputTime = inTimeStamp->mSampleTime;
//Get the new audio data
err = AudioUnitRender(This->mInputUnit,
ioActionFlags,
inTimeStamp,
inBusNumber,
inNumberFrames, //# of frames requested
This->mInputBuffer);// Audio Buffer List to hold data
checkErr(err);
if(!err) {
err = This->mBuffer->Store(This->mInputBuffer, Float64(inNumberFrames), SInt64(inTimeStamp->mSampleTime));
}
return err;
}
inline void MakeBufferSilent (AudioBufferList * ioData)
{
for(UInt32 i=0; i<ioData->mNumberBuffers;i++)
memset(ioData->mBuffers[i].mData, 0, ioData->mBuffers[i].mDataByteSize);
}
OSStatus CAPlayThrough::OutputProc(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *TimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
OSStatus err = noErr;
CAPlayThrough *This = (CAPlayThrough *)inRefCon;
Float64 rate = 0.0;
AudioTimeStamp inTS, outTS;
if (This->mFirstInputTime < 0.) {
// input hasn't run yet -> silence
MakeBufferSilent (ioData);
return noErr;
}
//use the varispeed playback rate to offset small discrepancies in sample rate
//first find the rate scalars of the input and output devices
err = AudioDeviceGetCurrentTime(This->mInputDevice.mID, &inTS);
// this callback may still be called a few times after the device has been stopped
if (err)
{
MakeBufferSilent (ioData);
return noErr;
}
err = AudioDeviceGetCurrentTime(This->mOutputDevice.mID, &outTS);
checkErr(err);
rate = inTS.mRateScalar / outTS.mRateScalar;
err = AudioUnitSetParameter(This->mVarispeedUnit,kVarispeedParam_PlaybackRate,kAudioUnitScope_Global,0, rate,0);
checkErr(err);
//get Delta between the devices and add it to the offset
if (This->mFirstOutputTime < 0.) {
This->mFirstOutputTime = TimeStamp->mSampleTime;
Float64 delta = (This->mFirstInputTime - This->mFirstOutputTime);
This->ComputeThruOffset();
//changed: 3865519 11/10/04
if (delta < 0.0)
This->mInToOutSampleOffset -= delta;
else
This->mInToOutSampleOffset = -delta + This->mInToOutSampleOffset;
MakeBufferSilent (ioData);
return noErr;
}
//copy the data from the buffers
err = This->mBuffer->Fetch(ioData, inNumberFrames, SInt64(TimeStamp->mSampleTime - This->mInToOutSampleOffset));
if(err != kCARingBufferError_OK)
{
MakeBufferSilent (ioData);
SInt64 bufferStartTime, bufferEndTime;
This->mBuffer->GetTimeBounds(bufferStartTime, bufferEndTime);
This->mInToOutSampleOffset = TimeStamp->mSampleTime - bufferStartTime;
}
return noErr;
}
#pragma mark -- Listeners --
// this sample now uses AudioObjectAddPropertyListenerBlock instead of AudioObjectAddPropertyListener
/*OSStatus CAPlayThroughHost::StreamListener(AudioObjectID inObjectID,
UInt32 inNumberAddresses,
const AudioObjectPropertyAddress inAddresses[],
void* inClientData)
{
CAPlayThroughHost *This = (CAPlayThroughHost *)inClientData;
This->ResetPlayThrough();
return noErr;
}*/
#pragma mark -
#pragma mark -- CAPlayThroughHost Methods --
CAPlayThroughHost::CAPlayThroughHost(AudioDeviceID input, AudioDeviceID output):
mPlayThrough(NULL)
{
CreatePlayThrough(input, output);
}
CAPlayThroughHost::~CAPlayThroughHost()
{
DeletePlayThrough();
}
void CAPlayThroughHost::CreatePlayThrough(AudioDeviceID input, AudioDeviceID output)
{
mPlayThrough = new CAPlayThrough(input, output);
StreamListenerQueue = dispatch_queue_create("com.CAPlayThough.StreamListenerQueue", DISPATCH_QUEUE_SERIAL);
//if (StreamListenerQueue) dispatch_set_context(StreamListenerQueue, this);
AddDeviceListeners(input);
}
void CAPlayThroughHost::DeletePlayThrough()
{
if(mPlayThrough)
{
mPlayThrough->Stop();
RemoveDeviceListeners(mPlayThrough->GetInputDeviceID());
dispatch_release(StreamListenerQueue);
StreamListenerQueue = NULL;
delete mPlayThrough;
mPlayThrough = NULL;
}
}
void CAPlayThroughHost::ResetPlayThrough ()
{
AudioDeviceID input = mPlayThrough->GetInputDeviceID();
AudioDeviceID output = mPlayThrough->GetOutputDeviceID();
DeletePlayThrough();
CreatePlayThrough(input, output);
mPlayThrough->Start();
}
bool CAPlayThroughHost::PlayThroughExists()
{
return (mPlayThrough != NULL) ? true : false;
}
OSStatus CAPlayThroughHost::Start()
{
if (mPlayThrough) return mPlayThrough->Start();
return noErr;
}
OSStatus CAPlayThroughHost::Stop()
{
if (mPlayThrough) return mPlayThrough->Stop();
return noErr;
}
Boolean CAPlayThroughHost::IsRunning()
{
if (mPlayThrough) return mPlayThrough->IsRunning();
return noErr;
}
void CAPlayThroughHost::AddDeviceListeners(AudioDeviceID input)
{
// creating the block here allows us access to the this pointer so we can call Reset when required
AudioObjectPropertyListenerBlock listenerBlock = ^(UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[]) {
ResetPlayThrough();
};
// need to retain the listener block so that we can remove it later
StreamListenerBlock = Block_copy(listenerBlock);
AudioObjectPropertyAddress theAddress = { kAudioDevicePropertyStreams,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster };
// StreamListenerBlock is called whenever the sample rate changes (as well as other format characteristics of the device)
UInt32 propSize;
OSStatus err = AudioObjectGetPropertyDataSize(input, &theAddress, 0, NULL, &propSize);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyDataSize\n", (long)err);
if(!err) {
AudioStreamID *streams = (AudioStreamID*)malloc(propSize);
err = AudioObjectGetPropertyData(input, &theAddress, 0, NULL, &propSize, streams);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyData\n", (long)err);
if(!err) {
UInt32 numStreams = propSize / sizeof(AudioStreamID);
for(UInt32 i=0; i < numStreams; i++) {
UInt32 isInput;
propSize = sizeof(UInt32);
theAddress.mSelector = kAudioStreamPropertyDirection;
theAddress.mScope = kAudioObjectPropertyScopeGlobal;
err = AudioObjectGetPropertyData(streams[i], &theAddress, 0, NULL, &propSize, &isInput);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyData\n", (long)err);
if(!err && isInput) {
theAddress.mSelector = kAudioStreamPropertyPhysicalFormat;
err = AudioObjectAddPropertyListenerBlock(streams[i], &theAddress, StreamListenerQueue, StreamListenerBlock);
//err = AudioObjectAddPropertyListener(streams[i], &theAddress, StreamListener, this);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectAddPropertyListenerBlock\n", (long)err);
}
}
}
if (NULL != streams) free(streams);
}
}
void CAPlayThroughHost::RemoveDeviceListeners(AudioDeviceID input)
{
AudioObjectPropertyAddress theAddress = { kAudioDevicePropertyStreams,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster };
UInt32 propSize;
OSStatus err = AudioObjectGetPropertyDataSize(input, &theAddress, 0, NULL, &propSize);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyDataSize\n", (long)err);
if(!err) {
AudioStreamID *streams = (AudioStreamID*)malloc(propSize);
err = AudioObjectGetPropertyData(input, &theAddress, 0, NULL, &propSize, streams);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyData\n", (long)err);
if(!err) {
UInt32 numStreams = propSize / sizeof(AudioStreamID);
for(UInt32 i=0; i < numStreams; i++) {
UInt32 isInput;
propSize = sizeof(UInt32);
theAddress.mSelector = kAudioStreamPropertyDirection;
theAddress.mScope = kAudioObjectPropertyScopeGlobal;
err = AudioObjectGetPropertyData(streams[i], &theAddress, 0, NULL, &propSize, &isInput);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectGetPropertyData\n", (long)err);
if(!err && isInput) {
theAddress.mSelector = kAudioStreamPropertyPhysicalFormat;
err = AudioObjectRemovePropertyListenerBlock(streams[i], &theAddress, StreamListenerQueue, StreamListenerBlock);
//err = AudioObjectRemovePropertyListener(streams[i], &theAddress, StreamListener, this);
if (err) fprintf(stderr, "Error %ld returned from AudioObjectRemovePropertyListenerBlock\n", (long)err);
Block_release(StreamListenerBlock);
}
}
}
if (NULL != streams) free(streams);
}
}