Adjust the length of an AudioUnit Buffer

不问归期 提交于 2019-11-30 07:55:45

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. 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?

EDIT

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!