iOS:如何将音频文件读入浮动缓冲区 [英] iOS: How to read an audio file into a float buffer

查看:519
本文介绍了iOS:如何将音频文件读入浮动缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常短的音频文件,例如10秒钟(比如说).PCM格式

I have a really short audio file, say a 10th of a second in (say) .PCM format

我想使用RemoteIO重复循环文件产生连续的音乐基调。那么如何将其读入浮点数组呢?

I want to use RemoteIO to loop through the file repeatedly to produce a continuous musical tone. So how do I read this into an array of floats?

编辑:虽然我可以挖出文件格式,将文件解压缩到NSData并手动处理,我猜测有一种更明智的通用方法......(例如应对不同的格式)

while I could probably dig out the file format, extract the file into an NSData and process it manually, I'm guessing there is a more sensible generic approach... ( that eg copes with different formats )

推荐答案

你可以使用ExtAudioFile以多种客户端格式从任何支持的数据格式读取数据。下面是一个将文件读取为16位整数的示例:

You can use ExtAudioFile to read data from any supported data format in numerous client formats. Here is an example to read a file as 16-bit integers:

CFURLRef url = /* ... */;
ExtAudioFileRef eaf;
OSStatus err = ExtAudioFileOpenURL((CFURLRef)url, &eaf);
if(noErr != err)
  /* handle error */

AudioStreamBasicDescription format;
format.mSampleRate = 44100;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFormatFlagIsPacked;
format.mBitsPerChannel = 16;
format.mChannelsPerFrame = 2;
format.mBytesPerFrame = format.mChannelsPerFrame * 2;
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mFramesPerPacket * format.mBytesPerFrame;

err = ExtAudioFileSetProperty(eaf, kExtAudioFileProperty_ClientDataFormat, sizeof(format), &format);

/* Read the file contents using ExtAudioFileRead */

如果你想要Float32数据,您可以设置格式,如下所示:

If you wanted Float32 data, you would set up format like this:

format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
format.mBitsPerChannel = 32;

这篇关于iOS:如何将音频文件读入浮动缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆