iOS中的ExtAudioFileRead下载错误代码-50

时间:2013-08-19 11:53:10

标签: ios core-audio extaudiofile

我使用ExtAudioFileRead函数将音频文件加载到内存中。但我发现代码-50总是出错。这意味着我将错误的参数传递给函数。但我不知道哪一个是错误的参数。

音频文件的数据格式为alac,sampleRate 44100k,有2个频道。

我的代码如下所示:

ExtAudioFileRef recordFile;
OSStatus error = noErr;
error = ExtAudioFileOpenURL((CFURLRef)file, &recordFile);
checkError(error, "open file");

SInt64 frameCount;
UInt32 size = sizeof(frameCount);
error = ExtAudioFileGetProperty(recordFile, kExtAudioFileProperty_FileLengthFrames, &size, &frameCount);
checkError(error, "get frameTotlal");

soundStruct *sound = &_sound;
sound->frameCount = frameCount;
sound->isStereo = true;
sound->audioDataLeft = (SInt16 *)calloc(frameCount, sizeof(SInt16));
sound->audioDataRight = (SInt16 *)calloc(frameCount, sizeof(SInt16));

AudioStreamBasicDescription desc;
UInt32 descSize = sizeof(desc);
error = ExtAudioFileGetProperty(recordFile, kExtAudioFileProperty_FileDataFormat, &descSize, &desc);


[self printASBD:desc];

UInt32 channels = desc.mChannelsPerFrame;

error = ExtAudioFileSetProperty(recordFile, kExtAudioFileProperty_ClientDataFormat, sizeof(inFormat), &inFormat);

AudioBufferList *bufferList;
bufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (channels - 1));

AudioBuffer emptyBuff = {0};
size_t arrayIndex;
for (arrayIndex = 0; arrayIndex < channels; arrayIndex ++) {
    bufferList->mBuffers[arrayIndex] = emptyBuff;
}

bufferList->mBuffers[0].mData = sound->audioDataLeft;
bufferList->mBuffers[0].mNumberChannels = 1;
bufferList->mBuffers[0].mDataByteSize = frameCount * sizeof(SInt16);
if (channels == 2) {
    bufferList->mBuffers[1].mData = sound->audioDataRight;
    bufferList->mBuffers[1].mNumberChannels = 1;
    bufferList->mBuffers[1].mDataByteSize = frameCount * sizeof(SInt16);
    bufferList->mNumberBuffers = 2;
}

UInt32 count = (UInt32)frameCount;

error = ExtAudioFileRead(recordFile, &count, bufferList);
checkError(error, "reading");  // Get a -50 error

free(bufferList);
ExtAudioFileDispose(recordFile);

1 个答案:

答案 0 :(得分:2)

好问题。

当我ExtAudioFileRead个MONO文件时,在ExtAudioFileSetProperty的通话中使用STEREO客户端数据格式时,我发生了此错误。

我认为ExtAudioFileRead不会自动将单声道文件上转换为立体声文件,如果存在不匹配,我认为它会因为-50错误而失败。

为单声道文件制作单声道文件立体声,设置inFormat.mChannelsPerFrame=1

请记住,如果您不进行上转换,则必须通过从单个单声道数据通道写入L / R声道来解释您的audiorenderfunction中的单声道文件。