Swift AVAudioEngine: Changing the Audio Input Device for MacOS
Asked Answered
A

1

2

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:

AVAEInternal.h:82:_AVAE_CheckAndReturnErr: required condition is false: [AVAudioEngineGraph.mm:1295:Initialize: (IsFormatSampleRateAndChannelCountValid(outputHWFormat))]

Current (simplified) code:

var engine = AVAudioEngine()

var inputDeviceID: AudioDeviceID = 41 // another audio input device
let sizeOfAudioDevId = UInt32(MemoryLayout<AudioDeviceID>.size)
let error = AudioUnitSetProperty(engine.inputNode.audioUnit!, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &inputDeviceID, sizeOfAudioDevId)
if error > 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)")
}

The audioDeviceId (41) is correct, because using a random number gives different errors.

What am I doing wrong?

Asteria answered 15/10, 2018 at 14:12 Comment(2)
One year has passed but I have the same issue. Did you solve it?Epizoon
@StefanoBider to be honest, I forgot. We probably found a different solution. I'm no longer working on that project so I can't check. Sorry 'bout that.Asteria
M
0

I can't test your code properly due to hardware issues. Which line is throwing the error?

What exactly are you trying to achieve? It's been a while since I've looked at AVAudioEngine, but I don't think it's necessary to connect the built-in engine nodes. At least not the output.

From the docs for mainMixerNode:

When the property is first accessed the audio engine constructs a singleton main mixer and connects it to the outputNode on demand.

I'd also try checking the format of the offending nodes with node.outputFormat(forBus: 0), and maybe explicitly setting the format when making connections like in this answer.

Martin answered 18/10, 2018 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.