We're using AVAudioEngine and trying to keep things simple in our app, and since we don't need stereo sound, we limit audio processing to a single channel. That works fine since the microphone or engine.inputNode is single channel, and audio files we provide with the app are all single channel.
Unfortunately, the user can select to import audio into the app that is 2 channel. That causes the app to crash with a 'required condition is false: _outputFormat.channelCount == buffer.format.channelCount'
I'm curious if there is an easy fix for this, or whether we have to detect this change in channels, and disconnect nodes and reconnect with the new format. I was hoping that AVAudioEngine could detect the incoming format of an AVAudioPCMBuffer, and handle it appropriately, without much help from me.
If I have to disconnect and reconnect with the new format, how extensively do I need to do this? I have a PCM bufffer going through an AudioPlayerNode to a AVAudioUnitVarispeed and that goes to the mixerNode (a single simple case, and I have more complex ones where multiple AudioUnits are chained together). Do I need to have all the nodes be reset to the new format? Only certain ones?
Doing the following after the graph has already been set up and after the new 2-channel file is loaded, and before scheduling the audioFilePlayer:
[engine connect:audioFilePlayer to:_varispeed format:audioBuffer.format];
fails with a
kAudioUnitErr_FormatNotSupported = -10868
If I don't have that line in there, and load the 2-channel file after the graph has already beeen loaded for 1-channel operation, gives me:
required condition is false: _outputFormat.channelCount == buffer.format.channelCount
If I have to tear down the graph and redo it in order to make a 2 channel sound to work, would it be better if I did a conversion of the audio, on-the-fly?
Thank you in advance for any tips and pointers.
mz