调整AudioUnit Buffer的长度 [英] Adjust the length of an AudioUnit Buffer

查看:431
本文介绍了调整AudioUnit Buffer的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题涉及AudioUnits。为了设计iPhone的语音交换应用程序(使用Objective-C xCode),我使用来自这个网站的RemoteIO audioUnit示例:

my Problem concerns AudioUnits. In order to design a voicechanging App for iPhone (with Objective-C xCode) i use RemoteIO audioUnit sample from this website:

http://atastypixel.com/blog/using-remoteio-audio-unit/

audioUnit缓冲区的长度设置为256个样本。对于我的项目,我需要更多(约22050)。
引用的页面说明audioUnit缓冲区的长度可以像这样调整:

The audioUnit buffers are set to a length of 256 samples. For my project i need alot more (about 22050). The quoted page says that the length of the audioUnit buffers can be adjusted like this:

float aBufferLength = 0.005; // In seconds
AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, 
                        sizeof(aBufferLength), &aBufferLength);

现在我的具体问题:上面的代码不适合上面提到的audioUnit,因为没有使用AudioSession ,从未初始化,因此产生错误。
除了kAudioSessionProperty_PreferredHardwareIOBufferDuration之外,还有其他可能的调整缓冲区持续时间吗?
在这种情况下,文档并不是很有用...
提前致谢Lukas。

Now my specific question: The code above is not suitable for the mentioned audioUnit, because AudioSession is not used, never initialized and therefore produces an error. Is there any other other possibilty to adjust the buffer-duration except for "kAudioSessionProperty_PreferredHardwareIOBufferDuration"? The documentation is not quite useful in this case... Thanks in advance, Lukas.

推荐答案

使用 RemoteIO 音频单元定义和初始化 AudioSession 没有问题,这就是设置方法所需的缓冲长度。我有一些代码正是这样做的,但是我需要几个小时才能回到家里并发布它。您可以查看Apple的 AurioTouch代码示例,或者等到我以后发布。

There is no problem in defining and initializing an AudioSession with the RemoteIO Audio-Unit, and that's the way to set the desired buffer length. I have some code doing exactly this, but it will take me a few hours till I get back home and can post it. You can look at Apple's AurioTouch code-sample, or wait till I post it later.

无论如何要记住两件事:

Anyway keep in mind 2 things:


  1. 缓冲区长度只会改变在设备上,所以如果你改变它并且在模拟器上没有看到任何差别也不要感到惊讶。

  2. 你无法获得任何你想要的缓冲区长度 - 这就是为什么要调用该属性 PreferredHardwareIOBufferDuration 。缓冲区大小始终是2的幂。

  1. The buffer length will change only on the device, so don't be surprised if you change it and see no difference on the simulator.
  2. You cannot get any buffer length you wish - that's why the property is called PreferredHardwareIOBufferDuration. The buffer size is always a power of 2.

考虑到这一点,您是否考虑分配自己的缓冲区并累积它直到您是否有所需数量的样本?

With that in mind, did you consider allocating your own buffer and accumulate it till you have the desired number of samples?

编辑

初始化代码音频会话(应该在音频单元初始化之前):

The code for initializing the audio session (should go before the audio unit is initialized):

OSStatus result;
result = AudioSessionInitialize(NULL, NULL, NULL, NULL);

UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);

// set preferred buffer size
Float32 preferredBufferSize = .04; // in seconds
result = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferSize), &preferredBufferSize);

// get actuall buffer size
Float32 audioBufferSize;
UInt32 size = sizeof (audioBufferSize);
result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareIOBufferDuration, &size, &audioBufferSize);

result = AudioSessionSetActive(true);

您可以/应该在每次检查后检查结果打电话以寻找可能的错误。
您可以阅读 AudioSessionInitialize 的文档以获取更多信息,但为所有4个参数传递 NULL 仍然有效。例如,如果需要建立中断监听器回调,则应该更改它。

You can/should inspect result after each call in order to look for possible errors. You can read the documentation for AudioSessionInitialize for more information, but passing NULL for all 4 arguments still work. You should change it if, for example, you need to establish an interruption-listener callback.

这篇关于调整AudioUnit Buffer的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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