Search code examples
swiftmacosavaudioengine

AVAudioEngine input on Mac, disrupting low priority sounds


I am analysing sounds by tapping the mic on the Mac and printing the identified sound out.

All is working well, but it disrupts other (what I assume) are low priority sounds e.g. interface sounds like dragging an item off the dock, sending a message in outlook, speaking something in Shortcuts or terminal. Other sounds like music.app playing, Siri speaking are not disrupted. The disruption sounds like the last part of the sound being repeated two extra times, very noticeable. This is the code (direct from here: https://developer.apple.com/documentation/soundanalysis/classifying_sounds_in_an_audio_stream):

import Cocoa
import AVFAudio

func startAudioEngine() {
    // Create a new audio engine.
    audioEngine = AVAudioEngine()


    // Get the native audio format of the engine's input bus.
    inputBus = AVAudioNodeBus(0)
    inputFormat = audioEngine.inputNode.inputFormat(forBus: inputBus)
    
    do {
        // Start the stream of audio data.
        try audioEngine.start()
    } catch {
        print("Unable to start AVAudioEngine: \(error.localizedDescription)")
    }
}

I have tried increasing the buffer, changing the qos to utility (in the hope the sound analysis would become less important than the system sound),running on a non-main thread.

The problem is with 100% AVAudioEngine, any assistance would be appreciated.


Solution

  • I have spoken to Apple developer team about this, they have replicated and it's been submitted as a bug with AVAudioEngine.