Adjust the length of an AudioUnit Buffer

后端 未结 1 1459
春和景丽
春和景丽 2020-12-30 18:14

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://

相关标签:
1条回答
  • 2020-12-30 18:35

    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.

    0 讨论(0)
提交回复
热议问题