I'm trying to tap the currently selected output audio device on macOS, so I basically have a pass through listener that can monitor the audio stream currently being output without affecting it.
I want to copy this data to a ring buffer in real time so I can operate on it separately.
The combination of Apple docs and (outdated?) SO answers are confusing as to whether I need to write a hacky kernel extension, can utilise CoreAudio for this, or need to interface with the HAL?
I would like to work in Swift if possible.
Many thanks
(ps. I had been looking at this and this)
Here’s how. First, click the Apple menu in the upper-left corner of the screen and select “System Preferences.” In System Preferences, click “Sound.” In the “Sound” preferences window, click the “Output” button. In Output preferences, you will see a list of connected and recognized audio output devices.
Show a volume control in the menu bar: Select the “Show volume in menu bar” checkbox so you can turn the volume up or down from anywhere. To set a separate volume for alert sounds, click Sound Effects. Depending on your Mac and the devices you use, you may be able to set other volume options using the Audio MIDI Setup app.
If the troublesome device is a USB audio interface, check the manufacturer’s website for the latest Mac drivers. You might need to install those to have the device recognized on your Mac. You could also try using a different USB cable to connect the device to your Mac. USB cables sometimes become faulty.
system preferences> Sound > Output “ No Output Devices Found “ help ! MacBook Pro, OS X El Capitan (10.11.3) OS X: What is Safe Boot, Safe Mode? - If you do not have a backup use disk utility to restore the internal disk to an external disk so you can try to recover data. This is the Mac Pro desktop forum.
I don't know about kernel extensions - their use of special "call us" signing certificates or the necessity of turning off SIP discourages casual exploration.
However you can use a combination of CoreAudio and HAL AudioServer plugins to do what you want, and you don't even need to write the plugin yourself, there are several open source versions to choose from.
CoreAudio doesn't give you a way to record from (or "tap") output devices - you can only record from input devices, so the way to get around this is to create a virtual "pass through" device (AudioServerPlugin), not associated with any hardware, that copies output through to input and then set this pass through device as default output and record from its input. I've done this using open source AudioServer Plugins like BackgroundMusic and BlackHole [TODO: add more].
To tap/record from the resulting device you can simply add an AudioDeviceIOProc
callback to it or set the device as the kAudioOutputUnitProperty_CurrentDevice
of an kAudioUnitSubType_HALOutput
AudioUnit
There are two problems with the above virtual pass through device approach:
If 1. is a problem, then a simple is to create a Multi-Output device containing the pass through device and a real output device (see screenshot) & set this as the default output device. Volume controls stop working, but you can still change the real output device's volume in Audio MIDI Setup.app
.
For 2. you can add a listener to the default output device and update the multi-output device above when it changes.
You can do most of the above in swift, although for ringbuffer-stowing from the buffer delivery callbacks you'll have to use C or some other language that can respect the realtime audio rules (no locks, no memory allocation, etc). You could maybe try AVAudioEngine
to do the tap, but IIRC changing input device is a vale of tears.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With