在渲染回调方法之外调用AudioUnitRender

时间:2013-03-01 06:59:56

标签: ios audiounit

与示例项目MixerHost一样,我想将混合输出流写入文件而不是扬声器。所以我使用了kAudioUnitSubType_GenericOutput并调用:

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");

错误:AudioUnitRender(-50),请告诉我调用AudioUnitRender的正确方法()

iluvcapra是对的,我改变了它的确有效:

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 个答案:

答案 0 :(得分:0)

有两件事,虽然没有看到更多,但我不能说这是不是错了:

  • 您的采样时间无效,您必须为每个演绎循环增加此值。

  • 您的AudioBuffer和AudioBufferList未正确设置,调音台的输出数据格式通常会将频道拆分为单独的AudioBuffer而不是交错。无论哪种方式,你都没有分配足够的空间,你malloc时应该calloc,而你要分配的是样本大小的对象,而不是字节大小的对象。

您的AudioBufferList分配看起来应该更像(在MixerHost之后):

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);
    }