0

我的应用程序对来自麦克风输入的音频进行大量数据处理。为了获得“演示模式”,我想基于本地 .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。所以我有一些 dispatch_syncprocessaudiorealtimeUpdate. ExtAudioFileRead 似乎真的不喜欢这并且它冻结了。谢谢你的帮助。
4

1 回答 1

0

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

于 2015-04-02T19:29:24.230 回答