使用ExtAudioFileRead播放.caf音频文件不同步

时间:2015-03-22 19:26:34

标签: ios audio core-audio remoteio extaudiofileread

我的应用程序对来自麦克风输入的音频进行大量数据处理。 为了获得“演示模式”,我想基于本地.caf音频文件做同样的事情。 我设法得到了音频文件。 现在我尝试使用ExtAudioFileRead读取.caf文件,然后进行数据处理。

void readFile()
{
OSStatus err = noErr;

// Audio file
NSURL *path = [[NSBundle mainBundle] URLForResource:@"output" withExtension:@"caf"];
ExtAudioFileOpenURL((__bridge CFURLRef)path, &audio->audiofile);
assert(audio->audiofile);

// File's format.
AudioStreamBasicDescription fileFormat;
UInt32 size = sizeof(fileFormat);
err = ExtAudioFileGetProperty(audio->audiofile, kExtAudioFileProperty_FileDataFormat, &size, &fileFormat);

// tell the ExtAudioFile API what format we want samples back in
//bzero(&audio->clientFormat, sizeof(audio->clientFormat));
audio->clientFormat.mSampleRate         = SampleRate;
audio->clientFormat.mFormatID           = kAudioFormatLinearPCM;
audio->clientFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audio->clientFormat.mFramesPerPacket    = 1;
audio->clientFormat.mChannelsPerFrame   = 1;
audio->clientFormat.mBitsPerChannel     = 16;//sizeof(AudioSampleType) * 8;
audio->clientFormat.mBytesPerPacket     = 2 * audio->clientFormat.mChannelsPerFrame;
audio->clientFormat.mBytesPerFrame      = 2 * audio->clientFormat.mChannelsPerFrame;
err = ExtAudioFileSetProperty(audio->audiofile, kExtAudioFileProperty_ClientDataFormat, sizeof(audio->clientFormat), &audio->clientFormat);

// find out how many frames we need to read
SInt64 numFrames = 0;
size = sizeof(numFrames);
err = ExtAudioFileGetProperty(audio->audiofile, kExtAudioFileProperty_FileLengthFrames, &size, &numFrames);

// create the buffers for reading in data
AudioBufferList *bufferList = malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (audio->clientFormat.mChannelsPerFrame - 1));
bufferList->mNumberBuffers = audio->clientFormat.mChannelsPerFrame;
for (int ii=0; ii < bufferList->mNumberBuffers; ++ii)
{
    bufferList->mBuffers[ii].mDataByteSize = sizeof(float) * (int)numFrames;
    bufferList->mBuffers[ii].mNumberChannels = 1;
    bufferList->mBuffers[ii].mData = malloc(bufferList->mBuffers[ii].mDataByteSize);
}

UInt32 maxReadFrames = 1024;
UInt32 rFrames = (UInt32)numFrames;
while(rFrames > 0)
{
    UInt32 framesToRead = (maxReadFrames > rFrames) ? rFrames : maxReadFrames;
    err = ExtAudioFileRead(audio->audiofile, &framesToRead, bufferList);
    [audio processAudio:bufferList];
    if (rFrames % SampleRate == 0)
        [audio realtimeUpdate:nil];
    rFrames = rFrames - maxReadFrames;
}

// Close the file
ExtAudioFileDispose(audio->audiofile);

// destroy the buffers
for (int ii=0; ii < bufferList->mNumberBuffers; ++ii)
{
    free(bufferList->mBuffers[ii].mData);
}
free(bufferList);
bufferList = NULL;
}

显然有一些我不理解的东西,或者我对ExtAudioFileRead做错了,因为这段代码根本不起作用。我有两个主要问题:

  1. 文件即时播放。我的意思是44'100个样本显然不等于1秒。我的3分钟音频文件处理在几秒钟内完成......
  2. 在处理过程中,我需要更新UI。所以我在processaudiorealtimeUpdate中有一些dispatch_sync。这似乎真的不被ExtAudioFileRead所理解,它冻结了。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您编写的代码只是从文件中读取样本然后调用processAudio。这将尽快完成。 processAudio完成后,将读取下一批样本并再次调用processAudio。您不应该假设从音频文件(低级别和非阻塞操作系统调用)中读取音频读取所需的时间相同。 如果您想根据采样率处理文件中的音频,您应该使用AUFilePlayer音频单元。这可以以正确的速度播放文件,您可以使用回调来处理实际音频时间的样本,而不是“尽可能快”。