Cabbage Logo
Back to Cabbage Site

Questions and suggestions about writing audio API for a game engine

Hello Rory,

In terms of how you programmed the CsoundUnity API, I’m kinda curious, how can we get access to the AudioClip of and Audio Source component and manipulate the audio buffers/signal from it, adding our external C++ DSP codes before it outputs back to the output component of the AudioSource? Can you share us some knowledge or tutorial about it? Do we have to get access to the original Unity engine C++ source code (Not script) to do that?

I think this would be really for everyone:) Thank you.

Cheers!

I’m away at the moment with nothing to write in but my small mobile phone :frowning: I’ll get back to you with some answers early next week, if I don’t, feel free to remind me again. I fear a large deluge of mails waiting for me one I get back from my short break away!

Hi @CsoundNoob, sorry for the delay, I’m only getting back to this now!

If you wish to use Unity’s AudioSource/AudioClip then you can use the OnAudioFilterRead method. It will give you access to the AudioClip samples. If you wish to process those samples using external code, you will need to create a library that Unity can call when it launches. You can do this using DllImport. Check out how I load the Csound library here.

Your DSP library will need to have some kind of processing loop. Each time Unity enters the OnAudioFilterRead() method, it should call your libraries processing method. I do the same thing with Csound here.

There is another approach that you may take. And that’s to use the Native Unity Plugin SDK. It doesn’t give you direct access to Unity’s AudioSource components, but it means you can do more in C/C++ and less in C#.

Hope this helps. I’m happy to answer any further questions if you have them. Note I’m no expert in Unity audio. Others may well be able to provide better advice!

It’s so helpful, thanks!