I'm trying to get my Macs main audio output, and tap into it, and then use speech to text on it.
The part I'm currently stuck on is getting my Macs main sound output. For example, if I were to go onto Youtube and play a video, it outputs the sound to my laptop speakers. I'm trying to tap into that output.
I'm using
AVAudioEngine.mainMixerNode
However, I dont get any output. I've also tried AVAudioEngine.outputNode
Here's what I've tried:
engine = AVAudioEngine()
let _ = engine.mainMixerNode
engine.prepare()
do {
try engine.start()
} catch {
print("Start Error")
return
}
do {
let settings = engine.mainMixerNode.outputFormat(forBus: 0).settings
print("FileType: \(settings[AVAudioFileTypeKey])") // Returns nil
self.file = try! AVAudioFile(forWriting: url, settings: settings)
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: nil) { // I've tried with a format and without
(buffer: AVAudioPCMBuffer?, time: AVAudioTime!) -> Void in
do {
// Buffer must be empty because the CAF audio length is correct, but there's no sound. It's flat.
try self.file.write(from: buffer!)
} catch _{
print("Problem Writing Buffer")
}
}
}
The above code has Privacy - Microphone Usage Description set. (Even though I'm not looking to tap the mic)
The first problem here is that I'm not sure what file type I should use for my output. I've tried CAF, but opening it in GarageBand produces no sound (although there is length)
I've tried renaming the file to m4a and wav, but Apple's Music just doesn't want to play it.
What am I doing wrong? Any help is greatly appreciated.