gpt4 book ai didi

ios - 有关用于 iOS 音频文件操作的 ExtFileReader C API 的信息

转载 作者:行者123 更新时间:2023-11-29 11:04:52 24 4
gpt4 key购买 nike

所以,我需要反转一些音频 *.caf 文件,

我看到了 the way to do it应该是:

You cannot just reverse the byte data. I have achieved the same effect using CoreAudio and AudioUnits. Use ExtFileReader C API to read the file into lPCM buffers and then you can reverse the buffers as needed.

但是我找不到任何关于使用

的文档

ExtFileReader C API

所以如果我有一个 *.caf 文件,我如何将它读入线性 PCM,我已经检查了 Core Audio overview但找不到如何完成这个?

我怎样才能将我的 caf 文件读取到线性 PCM?

谢谢!

最佳答案

ExtendedAudioFile 位于 AudioToolbox 框架中。将文件读入您​​喜欢的任何格式都非常简单。这是一个读入 32 位浮点非交错线性 PCM 的快速(编译但未测试)示例:

    #import <AudioToolbox/AudioToolbox.h>

...

    ExtAudioFileRef audioFile = NULL;
CFURLRef url = NULL;
OSStatus err = ExtAudioFileOpenURL(url, &audioFile);

AudioStreamBasicDescription asbd;
UInt32 dataSize = sizeof(asbd);
// get the audio file's format
err = ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileDataFormat, &dataSize, &asbd);

// now set the client format to what we want on read (LPCM, 32-bit floating point)
AudioStreamBasicDescription clientFormat = asbd;
clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagIsPacked;
clientFormat.mBitsPerChannel = 32;
clientFormat.mBytesPerPacket = 4;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerFrame = 4;

err = ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);

// okay, now the ext audio file is set up to convert samples to LPCM on read
// get the total number of samples
SInt64 numFrames = 0;
dataSize = sizeof(numFrames);
err = ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileLengthFrames, &dataSize, &numFrames);

// prepare an audio buffer list to hold the data when we read it from the file

UInt32 maxReadFrames = 4096; // how many samples will we read in at a time?
AudioBufferList *bufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer) * (asbd.mChannelsPerFrame - 1));
bufferList->mNumberBuffers = asbd.mChannelsPerFrame;
for (int ii = 0; ii < bufferList->mNumberBuffers; ++ii) {
bufferList->mBuffers[ii].mDataByteSize = maxReadFrames * sizeof(float);
bufferList->mBuffers[ii].mData = malloc(bufferList->mBuffers[ii].mDataByteSize);
bzero(bufferList->mBuffers[ii].mData, bufferList->mBuffers[ii].mDataByteSize);
bufferList->mBuffers[ii].mNumberChannels = 1;
}

while(numFrames > 0) {
UInt32 framesToRead = (maxReadFrames > numFrames) ? numFrames : maxReadFrames;
err = ExtAudioFileRead(audioFile, &framesToRead, bufferList);
// okay, your LPCM audio data is in `bufferList` -- do whatever processing you'd like!
}

// clean up
for (int ii = 0; ii < bufferList->mNumberBuffers; ++ii) {
free(bufferList->mBuffers[ii].mData);
}
free(bufferList);
ExtAudioFileDispose(audioFile);

关于ios - 有关用于 iOS 音频文件操作的 ExtFileReader C API 的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com