ExtAudioFileRead在iOS 5中给出了不同的结果

时间:2011-11-10 10:26:43

标签: objective-c ios

我正在尝试从MP3文件中提取线性PCM数据。 在iOS 5之前,我可以使用 AudioToolbox 框架,特别是 ExtAudioFileRead 功能成功完成。 但是,iOS 5中的 ExtAudioFileRead 函数与iOS 4中的结果完全不同。

首先,它无法读取源MP3文件中的所有数据包。 例如,它只读取1637个数据包,而源MP3文件总共有2212个数据包。

其次,从该功能获得的PCM值与iOS 4中获得的PCM值完全不同。

我无法弄清楚我做错了什么:( 相同的框架,相同的功能和相同的代码......但结果完全不同? 我怀疑它是iOS 5的一个错误所以我已经向Apple报告了这个问题。 但苹果公司已经有两周时间没有回复我的错误报告了!

这是导致问题的代码。 执行代码后,我希望在 pcmBuffer 中有正确的PCM数据。 在iOS 4中,代码给出了我期望的结果。 但是在iOS 5中,结果完全不同而且错误。

拜托,有人帮助我!

OSStatus status;
ExtAudioFileRef fileRef;

CFURLRef fileURL = (CFURLRef)[NSURL fileURLWithPath:filePath];
status = ExtAudioFileOpenURL((CFURLRef)fileURL, &fileRef);  

AudioStreamBasicDescription dataFormat; 
dataFormat.mSampleRate = SAMPLE_RATE;
dataFormat.mFormatID = kAudioFormatLinearPCM;
dataFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
dataFormat.mFramesPerPacket = 1;
dataFormat.mChannelsPerFrame = 1;
dataFormat.mBitsPerChannel = 16;
dataFormat.mBytesPerPacket = 2;
dataFormat.mBytesPerFrame = 2;

UInt32 propDataSize;

AudioStreamBasicDescription originalDataFormat;
propDataSize = (UInt32)sizeof(originalDataFormat);
status = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_FileDataFormat, &propDataSize, &originalDataFormat);

SInt64 numPackets;
propDataSize = sizeof(numPackets);
status = ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_FileLengthFrames, &propDataSize, &numPackets);

propDataSize = (UInt32)sizeof(dataFormat);
status = ExtAudioFileSetProperty(fileRef, kExtAudioFileProperty_ClientDataFormat, propDataSize, &dataFormat);

numPackets = (SInt64)numPackets / (SInt64)(originalDataFormat.mSampleRate / SAMPLE_RATE);

size_t bufferSize = (size_t)(numPackets * sizeof(SInt16));
SInt16 *pcmBuffer = (SInt16 *)malloc(bufferSize);

AudioBufferList bufList;
bufList.mNumberBuffers = 1;
bufList.mBuffers[0].mNumberChannels = 1; 
bufList.mBuffers[0].mDataByteSize = bufferSize;
bufList.mBuffers[0].mData = pcmBuffer;

ExtAudioFileSeek(fileRef, 0);
UInt32 totalFramesRead = 0;
do {
    UInt32 framesRead = numPackets - totalFramesRead;
    bufList.mBuffers[0].mData = pcmBuffer + (totalFramesRead * (sizeof(SInt16)));
    ExtAudioFileRead(fileRef, &framesRead, &bufList);
    totalFramesRead += framesRead;
    if(framesRead == 0) {
        break;
    }
    NSLog(@"read %lu frames\n", framesRead);
} while (totalFramesRead < numPackets);

int totalPackets = totalFramesRead;
status = ExtAudioFileDispose(fileRef);

NSLog(@"numPackets : %lld, totalPackets : %d", numPackets, totalPackets);

1 个答案:

答案 0 :(得分:1)

哎哟。我注意到,如果歌曲的原始采样率不同,则数字不同。回到原点1.