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?