I am trying to capture the input data stream to the output unit in an audio graph so I can write it to a file. I have registered an input callback function for the output unit (default output) after creating the graph like so:
AudioComponent comp = AudioComponentFindNext (NULL, &cd);
if (comp == NULL) {
printf ("can't get output unit");
exit (-1);
}
CheckError (AudioComponentInstanceNew(comp, &player->outputUnit),
"Couldn't open component for outputUnit"); // outputUnit is of type AudioUnit
// register render callback
AURenderCallbackStruct input;
input.inputProc = MyRenderProc;
input.inputProcRefCon = player;
CheckError(AudioUnitSetProperty(player->outputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input)),
"AudioUnitSetProperty failed");
// initialize unit
CheckError (AudioUnitInitialize(player->outputUnit),"Couldn't initialize output unit");
The callback function gets called but when I try to read the input data stream from ioData->mBuffers[0].mData, all I get is zeros. Here is the callback function:
OSStatus MyRenderProc(void *inRefCon, // PLAYER CODE
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData) {
int frame = 0;
Float32 leftFloat = 0;
Float32 rightFloat = 0;
NSNumber *leftNumber;
NSNumber *rightNumber;
for (frame = 0; frame < inNumberFrames; ++frame)
{
Float32 *data = (Float32*)ioData->mBuffers[0].mData;
leftFloat = (data)[frame];
leftNumber = [NSNumber numberWithDouble:leftFloat];
(data)[frame] = 0;
// copy to right channel too
data = (Float32*)ioData->mBuffers[1].mData;
rightFloat = (data)[frame];
rightNumber = [NSNumber numberWithDouble:(data)[frame]];
(data)[frame] = 0;
[leftData addObject:leftNumber];
[rightData addObject:rightNumber];
}
return noErr;
}
Furthermore, if I don't zero out the data I hear noise during playback, which tells me I am misinterpreting the function of the mBuffers. What am I doing wrong here?
A render callback on an output Audio Unit isn't for capturing the output data. It's to provide output data (sample mBuffers) for the output unit to play.
If capturing input data from an
AUGraph
is the task, the critical part of code (more or less) boils down to this simplest one-channel demo example:In the
setupAUGraph()
this callback can be set up in the following way:Please note that the render callback "taps" the connection between the output of instrument node and input of output node, capturing what comes from upstream. The callback just copies
ioData
into some other buffer which can be saved. AFAIK, this is the simplest way of accessingioData
that I know it works, without breaking the API.Please also note there are very efficient plain-C methods for testing if this works for a specific implementation - no need for Objective-C methods inside the callback. Fiddling with some
NSArray
s, adding objects etc, inside a plain-C real-time callback introduces risk of priority issues which may later become difficult to debug. CoreAudio API is written in plain-C for a reason. At the heart of Obj-C runtime there's much of what can't take place on the real time thread without risking glitches (locks, memory management, etc). So, it would be safer to keep off Obj-C on a real time thread.Hope this can help.