title: Swift AVAudioEngine: Changing the Audio Input Device for MacOS
body: <p>I'm trying to change the input device used to listen to incoming audio.
  I've tried a number of solutions, but most end up with the following error when preparing and starting the audio-engine:</p>
  
  <pre><code>AVAEInternal.h:82:_AVAE_CheckAndReturnErr: required condition is false: [AVAudioEngineGraph.mm:1295:Initialize: (IsFormatSampleRateAndChannelCountValid(outputHWFormat))]
  </code></pre>
  
  <p>Current (simplified) code:</p>
  
  <pre><code>var engine = AVAudioEngine()
  
  var inputDeviceID: AudioDeviceID = 41 // another audio input device
  let sizeOfAudioDevId = UInt32(MemoryLayout&lt;AudioDeviceID&gt;.size)
  let error = AudioUnitSetProperty(engine.inputNode.audioUnit!, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &amp;inputDeviceID, sizeOfAudioDevId)
  if error &gt; 0
  {
      print(error)
  }
  
  let inputNode = engine.inputNode
  engine.connect(inputNode, to: engine.mainMixerNode, format: nil)
  engine.connect(engine.mainMixerNode, to: engine.outputNode, format: nil)
  
  engine.prepare()
  do
  {
      try engine.start()
  }
  catch
  {
      print("Failed to start the audio input engine: \(error)")
  }
  </code></pre>
  
  <p>The audioDeviceId (41) is correct, because using a random number gives different errors.</p>
  
  <p>What am I doing wrong?</p>