downsampling in AVAudioEngine graph

Hi,


I'm trying to configure AVAudioEngine for downsampling, by inserting an AVAudioUnit with the following description:

let resamplerDescr = AudioComponentDescription(componentType: kAudioUnitType_FormatConverter,
                                                   componentSubType: kAudioUnitSubType_AUConverter,
                                               componentManufacturer: kAudioUnitManufacturer_Apple,
                                               componentFlags: 0, componentFlagsMask: 0)


to the graph:

    let engine = AVAudioEngine()
    let inputNode = engine.inputNode
    inputNode.isVoiceProcessingAGCEnabled = false
    inputNode.isVoiceProcessingBypassed = true
    engine.attach(auNode)
    let inputFormat = inputNode.outputFormat(forBus: 0)
    let adaptFormat = AVAudioFormat(standardFormatWithSampleRate: _sampleRateHz, channels: 2)!
    engine.attach(resampler)
    engine.connect(inputNode, to: resampler,
     format: inputNode.outputFormat(forBus: 0))
    engine.connect(resampler, to: auNode, format: adaptFormat)


auNode does some custom processing assuming the custom sample rate. Rest of graph construction was skipped.

The problem is that as soon as resampler is inserted into the graph the app crashes on engine start crash like this:

[avae]            AVAEInternal.h:76    required condition is false: [AVAudioEngineGraph.mm:798:InitializeActiveNodesI
nInputChain: (false == isInputConnToConverter)]
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: false == isInputConnToConverter'


What I'm doing wrong here? What is the recommended way to downsample in AVAudioEngine graph?

From what I have observed, downsampling or upsampling is automatic, if you use a mixer.

Specifically what I observed was that one end of the mixer is attached to the input node, which was running at 44.1KHz. The other end of the mixer was connected to an AVAudioPlayerNode running at 48KHz. I could hear the network transmitted audio coming out of the speaker.

This confused me at first until I looked at the specific documentation for AVAudioMixer: "The mixer accepts input at any sample rate and efficiently combines sample rate conversions. It also accepts any channel count and correctly upmixes or downmixes to the output channel count."

downsampling in AVAudioEngine graph
 
 
Q