简体   繁体   中英

Stereo mic passthrough with AVAudioEngine?

I'm trying to create a graph with AVAudioEngine that takes input from the device microphone, and supports stereo output.

Right now the graph looks like this:

[audioEngine.inputNode] --> [Mic input mixer node] --> [Summing mixer node] --> [audioEngine.mainMixerNode]

As the output of audioEngine.inputNode is 1 ch, 48000 Hz, Float32 , I wanted to run through a mixer node after the mic so I can a) send the signal to multiple audio units and b) convert to a stereo signal for more interesting routing, panning etc…

However, the audio passthrough is only working if I set the entire graph to match the input format of 1 ch, 48000 Hz, Float32 , like this:

audioEngine.connect(microphoneNode!, to: microphoneInputMixer, format: inputSessionFormat!)
audioEngine.connect(microphoneInputMixer, to: summingMixer, format: inputSessionFormat!)
audioEngine.connect(summingMixer, to: mainMixerNode!, format: inputSessionFormat!)

I have two format variables, the mono inputSessionFormat and stereo outputSettingFormat , which is 2 ch, 44100 Hz, Float32, non-inter .

As I understood from the docs AVAudioMixerNode should handle format conversion, but in this case it's refusing to play audio unless all connections are set to the same (mono) format. I've also tested this code with AVAudioConnectionPoint as well, to no avail.

Only AVAudioEngine's inputNode and outputNodes have the fixed formats. Use nil when connecting to/from these. Mixers will implicitly convert between their input and output formats. Connect to microphoneInputMixer using nil , and from microphoneInputMixer using the desired format. The mainMixerNode will take any input format and its output format cannot be set.

let desiredFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2)
audioEngine.connect(microphoneNode!, to: microphoneInputMixer, format:nil)
audioEngine.connect(microphoneInputMixer, to: summingMixer, format: desiredFormat)
audioEngine.connect(summingMixer, to: mainMixerNode!, format: desiredFormat)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM