0

像示例项目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");
4

1 回答 1

0

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

  • 您的采样时间无效,您必须为再现的每个循环增加它。

  • 您的 AudioBuffer 和 AudioBufferList 设置不正确,混音器的输出数据格式通常将通道拆分为单独AudioBuffer的 s 而不是交错。无论哪种方式,您都没有分配足够的空间,您在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);
    }
于 2013-03-02T01:28:53.517 回答