iOS: How to get audio data generated in Unity

Hi,

we are working on an audio app in Unity which targets, among other platforms, iOS devices (and macOS later). For iOS, we are using the “Unity as a library”-approach, so we have a native app part which is written in Swift.

Everything works great, except for one feature we would really like to implement but can’t find out how.
We want to send the audio data generated with our app to another app (“inter-app audio”). To do so, we of course need access to that data (and then feed it into an AVAudioUnit in our native iOS app). But we can not find any information about where to grab that data. It is not clear at all to us how Unity sends the audio to the hardware/low level audio system.

Here is a little diagram which shows our use case:


So we have our native iOS app with all the Unity stuff as a framework. Unity sends the audio data directly to the system (down arrow). But we want to feed it to another iOS audio processing app (“Host Audio App”) which pulls data from an AVAudioUnit in our native app. The little dashed arrow basically is our problem area: how to get the audio data to the AVAudioUnit, ideally instead sending it to the system.

Any help or advice is appreciated!

Marc-André

PS. Because we are basically stuck right now, we are thinking about a very raw solution using a shared buffer (NativeArray) which will be written to on the Unity side and then read on the iOS side. But this seems kind of brittle, especially for timing reasons… So this is kind of a last resort :wink:

Hi, have you looked at the audio renderer?

If you still want the system driven in real time, you could use this in C#

or this for native code:

In both cases, if you don’t want Unity to emit any sound, you can clear the return buffer and having transmitted its data.

Hi, thank you for your reply! I tested the approach of sending data from OnAudioFilterRead.
Unfortunately there seems to be a fundamental conflict with this when using this data in an instance of iOS’s AVAudioEngine, because Unity blocks the audio output.
Next I will try to send the data data manually to the host app (without AVAudioEngine) using older ObjC system calls. If that does not work, I will look at the Native Audio SDK and might be back with more questions :slight_smile: