0

Like the sample project MixerHost,I want to write the mixed output stream to a file instead of to the speakers.So I used kAudioUnitSubType_GenericOutput and call:

CheckError(AudioUnitInitialize(mixerUnit), "AudioUnitInitialize mixerUnit");
CheckError(AudioUnitInitialize(ioUnit), "AudioUnitInitialize iOUnit");
CheckError(AudioOutputUnitStart(ioUnit), "AudioOutputUnitStart iOUnit");

AudioUnitRenderActionFlags ioActionFlags = 0;
AudioTimeStamp inTimeStamp;
memset(&inTimeStamp, 0, sizeof(AudioTimeStamp));
inTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
UInt32 inOutputBusNumber = 0;
UInt32 inNumberFrames = 100;
AudioBuffer buffer;
buffer.mNumberChannels = 2;
buffer.mDataByteSize = inNumberFrames*2;
buffer.mData = malloc(inNumberFrames*2);
AudioBufferList ioData;
ioData.mNumberBuffers = 1;
ioData.mBuffers[0] = buffer;
CheckError(AudioUnitRender(ioUnit,
                           &ioActionFlags,
                           &inTimeStamp,
                           inOutputBusNumber,
                           inNumberFrames,
                           &ioData),
           "AudioUnitRender");

Error: AudioUnitRender (-50),Please tell me the correct way to call AudioUnitRender()

iluvcapra is right,I changed and it works:

AudioUnitRenderActionFlags flags = 0;
AudioTimeStamp inTimeStamp;
memset(&inTimeStamp, 0, sizeof(AudioTimeStamp));
inTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
UInt32 busNumber = 0;
UInt32 numberFrames = 100;

int channelCount = 2;
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList)+sizeof(AudioBuffer)*(channelCount-1));
bufferList->mNumberBuffers = channelCount;
for (int i=0; i<channelCount; i++) {
    AudioBuffer buffer = {0};
    buffer.mNumberChannels = 1;
    buffer.mDataByteSize = numberFrames*sizeof(AudioUnitSampleType);
    buffer.mData = calloc(numberFrames, sizeof(AudioUnitSampleType));

    bufferList->mBuffers[i] = buffer;

}
CheckError(AudioUnitRender(ioUnit,
                           &flags,
                           &inTimeStamp,
                           busNumber,
                           numberFrames,
                           bufferList),
           "AudioUnitRender ioUnit");

1 Answers1

0

Two things, though without seeing more I can't say if this is all that's wrong:

  • Your sample time isn't valid, you have to increment this for every loop of the rendition.

  • Your AudioBuffer and AudioBufferList aren't being set up properly, the mixer's output data format generally splits channels into separate AudioBuffers instead of interleaving. Either way you're not allocating enough space, you're mallocing when you should be callocing, and you're allocating in sample-sized objects, not byte-sized ones.

Your AudioBufferList allocation should look more like this (after MixerHost's):

AudioBufferList *ioData = (AudioBufferList *) malloc (
        sizeof (AudioBufferList) + sizeof (AudioBuffer) * (channelCount - 1)
    );


    ioData->mNumberBuffers = channelCount;

    int i;
    for (i = 0; i < channelCount; i++) {
        ioData->mBuffers[i] = {0};
        ioData->mBuffers[i].mNumberChannels  = 1;
        ioData->mBuffers[i].mDataByteSize = BUFFER_SIZE_IN_SAMPLES * 
             sizeof (AudioUnitSampleType);
        ioData->mBuffers[i].mData = calloc(BUFFER_SIZE_IN_SAMPLES, 
             sizeof(AudioUnitSampleType);
    }
iluvcapra
  • 9,436
  • 2
  • 30
  • 32
  • Thank you very much! could you tell me where is the proper place to call AudioUnitRender repeatedly,should i call it in a new thread? – user2122490 Mar 03 '13 at 15:26
  • You can call it here or from a new thread, just never call Render from two different threads. You might also consider just wrapping this up in an AUGraph and letting it do all the backgrounding for you; you can get the output of an AUGraph from *it's* render callback. – iluvcapra Mar 04 '13 at 17:11
  • I'm considering use AUGraph, did you mean use AUGraphAddRenderNotify to get the output of an AUGraph? and where to first call Render, because nothing happens after i called AUGraphStart. – user2122490 Mar 06 '13 at 10:04