AVAudioEngine: CoreAudio Error -10875 When trying to play-through the audio from a USB device

60 Views Asked by At

I'm new to this - so I hope it makes sense. I've cobbled together all that I can from the internet. Basically I'm trying to play-through the audio from an Elgato HD60 X (if that matters) on macOS Sonoma.
Here's where my code is right now:

import Foundation
import AVFoundation

private var audioEngine = AVAudioEngine()
var inputNode = audioEngine.inputNode
var outputNode = audioEngine.outputNode

class UVCAudio {
    
    init() {
        startAudio()
    }
    
    func startAudio() {
        
        //_ = AudioEnumerable()
        //print(AudioDevice(deviceID: 72).name)

        var inputDeviceID: AudioDeviceID = 72
        let sizeOfAudioDevId = UInt32(MemoryLayout<AudioDeviceID>.size)
        
        var audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 48000, channels: 2, interleaved: false)
        
        let error = AudioUnitSetProperty(audioEngine.inputNode.audioUnit!,
                                         kAudioOutputUnitProperty_CurrentDevice,
                                         kAudioUnitScope_Global,
                                         0,
                                         &inputDeviceID,
                                         sizeOfAudioDevId)

        if error > 0 {
            print("Error setting input device: \(error)")
        }
            
        let format = inputNode.inputFormat(forBus: 0)
      
        if format.sampleRate != 0 {
            audioEngine.connect(inputNode, to: audioEngine.mainMixerNode, format: nil)
            audioEngine.connect(audioEngine.mainMixerNode, to: audioEngine.outputNode, format: nil)
            do {
                try audioEngine.start()
            } catch {
                print("Error: \(error.localizedDescription)")
            }
        } else {
            print("Error audio type mismatch")
        }
    }
}

The error pops on audioEngine.start(), there is no error as far as I can tell on the AudioUnitSetProperty function.

I have tried commenting the AudioUnitSetProperty line and changing the input in Settings, and that works. I've also tried leaving the line in but setting the AudioDeviceID to one of my microphones (either USB or built-in) this also works regardless of which input is specified in Settings.

Capability is granted for microphone access as well.

Is AudioEngine limited to microphones specifically?
Or is it possible, but I'm initializing it wrong?

EDIT: Here's the output:

AVAEInternal.h:88    required condition is false: [AVAudioEngineGraph.mm:1357:Initialize: (IsFormatSampleRateAndChannelCountValid(outputHWFormat))]
Error: The operation couldn’t be completed. (com.apple.coreaudio.avfaudio error -10875.)
0

There are 0 best solutions below