Audio Units Issues With Input Callback
Audio Units Issues With Input Callback
- Subject: Audio Units Issues With Input Callback
- From: Colin Cornaby <email@hidden>
- Date: Sat, 12 Feb 2011 18:47:24 -0800
Hi everyone,
I have some code that captures pcm audio packets from the microphone, and as a debugging measure I can write the packets in order to a file. When I first wrote the code, I had my callback attached as an output renderer, and it worked absolutely perfectly. The issue was that the only way this callback worked was to enable output, and I didn't want to output the audio to a device, only capture it.
With that in mind, I set my callback instead using kAudioOutputUnitProperty_SetInputCallback. I preallocated a single buffer in a buffer list, and now I render into the buffer list in my callback. The problem is that my captured audio now sounds garbagy with the nearly exact same callback.
The following should be 16000 hz mono 16 bit pcm. It should sound like a busy coffee shop...
http://homepage.mac.com/gomac/test.pcm
Looking at the contents of the file, it looks like I'm getting duplicate data in the file.
Is there anything different about input callbacks that I should know?
The following is my setup code:
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
AudioComponentInstanceNew(comp, inRemoteIOUnit);
UInt32 one = 1;
UInt32 zero = 0;
AudioUnitSetProperty(*inRemoteIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one));
AudioUnitSetProperty(*inRemoteIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &zero, sizeof(zero));
outFormat->mChannelsPerFrame=1;
outFormat->mSampleRate = 16000;
outFormat->mBitsPerChannel = 16;
outFormat->mFormatID = kAudioFormatLinearPCM;
outFormat->mFormatFlags = kAudioFormatFlagsCanonical;
outFormat->mBytesPerPacket = outFormat->mBytesPerFrame = (outFormat->mBitsPerChannel / 8) * outFormat->mChannelsPerFrame;
outFormat->mFramesPerPacket = 1;
outFormat->mReserved = 0;
OSStatus result = AudioUnitSetProperty(*inRemoteIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, outFormat, sizeof(*outFormat));
result = AudioUnitSetProperty(*inRemoteIOUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 1, &inRenderProc, sizeof(inRenderProc));
result = AudioUnitSetProperty(*inRemoteIOUnit, kAudioUnitProperty_ShouldAllocateBuffer, kAudioUnitScope_Input, 1,&one, sizeof(one));
UInt32 propsize, bufferSizeFrames;
//calculate number of buffers from channels
propsize = offsetof(AudioBufferList, mBuffers[0]) + (sizeof(AudioBuffer) * outFormat->mChannelsPerFrame);
//malloc buffer lists
mInputBuffer = (AudioBufferList *)malloc(propsize);
mInputBuffer->mNumberBuffers = outFormat->mChannelsPerFrame;
//pre-malloc buffers for AudioBufferLists
for(UInt32 i =0; i< mInputBuffer->mNumberBuffers ; i++) {
mInputBuffer->mBuffers[i].mNumberChannels = 1;
mInputBuffer->mBuffers[i].mDataByteSize = (2400) * outFormat->mBytesPerPacket;
mInputBuffer->mBuffers[i].mData = malloc(mInputBuffer->mBuffers[i].mDataByteSize);
}
result = AudioUnitInitialize(*inRemoteIOUnit);
Here is my input callback function:
static OSStatus audioInputHandler(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
DMAudioCaptureSession *captureSession = ((DMAudioCaptureSession *)inRefCon);
AudioUnitRender(captureSession->rioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, captureSession->mInputBuffer);
//do some things here...
[pool release];
return 0;
}
Thanks,
Colin
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden