3

I have been working with CoreAudio last couple of days and I'm able to access all the AudioDevices, their properties and receive notifications when something changes. However, I'm now struggling to "intercept" the audio (ideally before it is played) from any application in the mac. I wasn't sure it was possible but this app actually does it. So I'm in search of some guidance. Thank you in advance.

Gama
  • 352
  • 2
  • 16

1 Answers1

6

Unfortunately macOS provides no facility to do this.

The only way to achieve this is to patch lower level frameworks and/or drivers like Audio Hijack does. I will be a messy process.

Depending on the use case however it might suffice to have an audio loopback virtual audio device so application A can send audio into it while application B can receive the audio from it. This can be done by creating a Core Audio AudioServerPlugin which is a user space audio driver mechanism. For an example please have a look at:

https://github.com/ExistentialAudio/BlackHole
https://github.com/kyleneideck/BackgroundMusic

In regards to the limitations, you can read more about it here:
https://stackoverflow.com/a/18595698/2576876

From there you can go in a lot of directions by following the links provided.

Ruurd Adema
  • 920
  • 7
  • 17
  • I was afraid this would be the case lol. Thank you for the insight and the resources! – Gama Mar 05 '21 at 22:04
  • one more question or could you elaborate a bit more on "application A can send audio into it while application be can receive audio from it"? Like the Virtual Audio Device would send the audio to the speakers (or output device) and then my application can also receive that same audio? – Gama Mar 06 '21 at 15:33
  • No, that is not going to work. From an AudioServerPlugin you can’t use any client API’s from Core Audio because that will produce all kinds of problems as mentioned in the Core Audio headers. So this prevents an AudioServerPlugin (which normally represents an audio itself) from accessing any other device in the system. A user application however can use multiple devices at once, so it could use any (external) audio device and a virtual loopback device. Depending on you specific use case this might suffice or not. – Ruurd Adema Mar 06 '21 at 15:42
  • Please also note that it is possible to create aggregate devices programmatically and invisible from the user. In some cases this might provide a solution. – Ruurd Adema Mar 06 '21 at 15:43
  • thank you again for taking the time to elaborate a bit more. I'm new to this low level stuff so I really appreciate it (you probably already figured that lol). My goal is to be able get the audio data that is being played through any device so that eventually I can stream it to somewhere else eventually. – Gama Mar 06 '21 at 16:00
  • also do you happen to have anything on creating an aggregate device programmatically? – Gama Mar 06 '21 at 19:59
  • Yep, see this question here for an example: https://stackoverflow.com/q/60445512/2576876 and my answer here: https://stackoverflow.com/a/60301281/2576876 (especially the paragraph about Aggregate devices you might find interesting) – Ruurd Adema Mar 06 '21 at 20:47
  • Well there's clearly _some_ facility to do what the user is looking for, since apps like Audio Hijack, Loopback, etc do this quite gracefully. What do you think they do? – vercellop Oct 01 '21 at 21:24
  • I think they reverse engineered and patched system frameworks and install that on the users machine. It is quite amazing what they’ve done! However, they couldn’t achieve this functionality with officially provided api’s as there are none to do this. But I’d like to be proven wrong because this is has been a limitation of macOS for years and it would be amazing to have this functionality! So if you find something, please let us know! – Ruurd Adema Oct 02 '21 at 05:41