41 #ifndef __CAAudioUnitOutputCapturer_h__
42 #define __CAAudioUnitOutputCapturer_h__
44 #include <AudioToolbox/ExtendedAudioFile.h>
64 class CAAudioUnitOutputCapturer {
68 CAAudioUnitOutputCapturer(AudioUnit au, CFURLRef outputFileURL, AudioFileTypeID fileType,
const AudioStreamBasicDescription &format, UInt32 busNumber = 0) :
70 mClientFormatSet(false),
73 mBusNumber (busNumber)
75 CFShow(outputFileURL);
76 OSStatus err = ExtAudioFileCreateWithURL(outputFileURL, fileType, &format, NULL, kAudioFileFlags_EraseFile, &mExtAudioFile);
83 if (!mClientFormatSet) {
84 AudioStreamBasicDescription clientFormat;
85 UInt32 size =
sizeof(clientFormat);
86 AudioUnitGetProperty(mAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, mBusNumber, &clientFormat, &size);
87 ExtAudioFileSetProperty(mExtAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat);
88 mClientFormatSet =
true;
90 ExtAudioFileWriteAsync(mExtAudioFile, 0, NULL);
91 AudioUnitAddRenderNotify(mAudioUnit, RenderCallback,
this);
97 AudioUnitRemoveRenderNotify(mAudioUnit, RenderCallback,
this);
102 ExtAudioFileDispose(mExtAudioFile);
103 mExtAudioFile = NULL;
107 ~CAAudioUnitOutputCapturer() {
112 static OSStatus RenderCallback(
void * inRefCon,
113 AudioUnitRenderActionFlags * ioActionFlags,
114 const AudioTimeStamp * inTimeStamp,
116 UInt32 inNumberFrames,
117 AudioBufferList * ioData)
119 if (*ioActionFlags & kAudioUnitRenderAction_PostRender) {
120 CAAudioUnitOutputCapturer *This = (CAAudioUnitOutputCapturer *)inRefCon;
121 static int TEMP_kAudioUnitRenderAction_PostRenderError = (1 << 8);
122 if (This->mBusNumber == inBusNumber && !(*ioActionFlags & TEMP_kAudioUnitRenderAction_PostRenderError)) {
123 OSStatus result = ExtAudioFileWriteAsync(This->mExtAudioFile, inNumberFrames, ioData);
124 if (result) DebugMessageN1(
"ERROR WRITING FRAMES: %d\n", result);
131 bool mClientFormatSet;
132 AudioUnit mAudioUnit;
133 ExtAudioFileRef mExtAudioFile;
137 #endif // __CAAudioUnitOutputCapturer_h__