iOS: How to resample audio(PCM data) using Audio Unit at runtime?

后端 未结 1 2017
死守一世寂寞
死守一世寂寞 2020-12-30 15:58

How can i resample audio(PCM data) using Audio Unit at runtime/live ?

I have an Audio Unit setup as follows.

- (void) setUpAudioUnit {
    OSStatus          


        
1条回答
  •  执念已碎
    2020-12-30 16:27

    A converter audio unit will handle your sample rate conversions. I find the best way to deal with this is to adapt your chain to what the hardware is doing natively. This means that you should get the system AudioStreamBasicDescription (sysASBD) then put converter units between the system and the parts of your chain that need something different. So for audio play through with 8K sampleRate you would do this: ReomoteIO(mic) -> converter -> your8Kprocessing -> converter -> RemoteIO(out).

    Here is the description for the converter.

    AudioComponentDescription convDesc;
    convDesc.componentType = kAudioUnitType_FormatConverter;
    convDesc.componentSubType = kAudioUnitSubType_AUConverter;
    convDesc.componentFlags = 0;
    convDesc.componentFlagsMask = 0;
    convDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
    

    Here is how you get the system ASBDin and ASBDout

    UInt32 sizeASBD = sizeof(AudioStreamBasicDescription);
    AudioStreamBasicDescription ioASBDin;
    AudioStreamBasicDescription ioASBDout;
    AudioUnitGetProperty(remoteIO, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 1, &ioASBDin, &sizeASBD);
    AudioUnitGetProperty(remoteIO, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &ioASBDout, &sizeASBD);
    

    All you have to do to use a converter is set it's input ASBD and output ASBD to the desired formats and it does all the work. The make your connections and you have your 8K play through.

    AudioStreamBasicDescription asbd8K;
    
    AudioComponentInstance converter44To8;
    AudioUnitSetProperty(converter44To8,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,& ioASBDin,sizeof(AudioStreamBasicDescription));
    AudioUnitSetProperty(converter44To8,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output,0,&asbd8K,sizeof(AudioStreamBasicDescription));
    
    
    AudioComponentInstance converter8To44;
    AudioUnitSetProperty(converter8To44,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,0,&asbd8K,sizeof(AudioStreamBasicDescription));
    AudioUnitSetProperty(converter8To44,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Output,0,& ioASBDout,sizeof(AudioStreamBasicDescription));
    

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