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/
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);
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.
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:
- 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.
- 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.
来源:https://stackoverflow.com/questions/4863330/adjust-the-length-of-an-audiounit-buffer