Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtAudioFileRead in iOS falling with error code -50

I use ExtAudioFileRead Function to load audio file to memory. But I found there is alway an error with code -50. That means I pass the wrong parameters to the function. But I have no idea which one is the wrong parameter.

The Audio File's data format is alac, sampleRate 44100k, has 2 channels.

My code is shown below:

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);
like image 344
jeff kit Avatar asked Aug 19 '13 11:08

jeff kit


1 Answers

Good question.

This error happened to me when I ExtAudioFileRead a MONO file, using a STEREO client data format in your call to ExtAudioFileSetProperty.

I don't think ExtAudioFileRead automatically upconverts mono files to stereo files, if there is a mismatch there I think it fails with this -50 error.

Either make the mono file stereo, or set inFormat.mChannelsPerFrame=1 for the mono files.

Remember, if you don't upconvert, you must account for the mono files in your audiorenderfunction by writing L/R channels from the single mono channel of data.

like image 72
bobobobo Avatar answered Oct 31 '22 11:10

bobobobo