Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get macOS Output Device Audio Buffers in Realtime

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)

like image 455
Darius Avatar asked Dec 14 '20 12:12

Darius


People also ask

How do I enable audio output on my MacBook Pro?

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.

How do I Turn Up the volume on my Mac?

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.

Why is my USB audio interface not working on my Mac?

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.

How to fix “no output devices found” on Mac Pro?

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.


1 Answers

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:

  1. you can't your hear output anymore, because it's being consumed by the pass through device
  2. changing default output device will switch away from your device and the tap will fall silent.

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.

Audio MIDI Setup app showing multi-output device set as default output, containing pass thru device (Background Music) and normal output device (Mac-mini speakers), Mac-mini speakers set as master device

like image 73
Rhythmic Fistman Avatar answered Oct 12 '22 12:10

Rhythmic Fistman