Cabbage Logo
Back to Cabbage Site

Csound and Unity on Android


I haven’t yet tested the Unity-Csound integration but from what I see it seems that it is Desktop only and won’t work on Android, am I right?



This is correct, but it should be possible to get it working on Android as Csound runs fine there. It’s something I need to look into shortly myself. If you get anywhere with it let me know. I’ll do the same.

Great, thanks! I got Csound to build and package properly for Android and Unreal Engine but for some reason no audio is produced, even though Csound seems to be running fine, so I want to give Unity a try.

If you wish to use UE4 to output the audio you’ll need to access Csound’s audio IO buffers and write their contents to the UE4 output buffers. It’s not much work, you should also run Csound with -n (in the CsOptions) to specify that you don’t want to use Csound’s own audio IO modules. You don’t, as these will likely cause a conflict with the UE4 audio stuff.

Yes, I already got that working on a Windows PC and indeed it was easy, particularly with the new audio engine in UE4. I just haven’t managed to get to work in Android.

Ah, I should have realised that! I’m just looking at the Android stuff now in Unity. I hope I don’t have to bother with an Java code…

p.s. it’s taking an age to setup Android for Windows. I usually do my Android dev stuff on Linux, but the latest version of Unity on Linux is a 6gb download. I thought choosing to install Android on Windows would be quicker, I may have been wrong. Yawn…

I’m not having much luck with this here. Unity just doesn’t like the file. I’ve read that I should be placing this file into the Assets/Plugins/Android folder, but when I do I get a crash on startup. I’m using the last release of CsoundAndroid that I could find. Perhaps there is an API mismatch or something? I’ll keep digging around. Has to be some way of doing this.

This is what I’ve got so far. Just import it to an empty project to get going. Even though it doesn’t work, you might be able to spot something amiss.

I got a little further today. I run Android Studio in the background while I’m working with Unity, this gives me access to the logcat messages so I can see what’s going on. As far as I can tell the Csound library is now being found. The csd files weren’t, so I added a simple method to generate a csd file in the apk that Csound can read. Tests show the file is being written Ok. But just after this the app crashes. Logcat shows:

11-08 21:42:48.093 23844-23844/? W/UnityMain: type=1400 audit(0.0:5805): avc: denied { read } for uid=10158 name="/" dev="rootfs" ino=2 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:rootfs:s0 tclass=dir permissive=0
11-08 21:42:48.236 1467-1691/? W/InputDispatcher: channel '295ca9b com.test.rory/com.unity3d.player.UnityPlayerActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
11-08 21:42:48.236 1467-1691/? E/InputDispatcher: channel '295ca9b com.test.rory/com.unity3d.player.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

I’m going to take a break now. Maybe someone else will have more luck. I’ve exported the entire scene to here I’ll take another look at it tomorrow.

p.s. it might just have something to do with the player settings, I haven’t messed around with them much…

@virtualHC, do you happen to have working CsoundAndroid libs? If so I would love to try them in Unity. As it stands I get a crash just after my Csound object has been instantiated. It’s strange because my Csound objects appears to be valid. But I still get a crash. If I take out the call which loads Csound I get no crash, so I can only surmise that my Csound library is the problem.

Hi Rory,

Sorry I missed this message. I’m using the libs available in the Csound website. I remember experiencing something similar long time ago when I first tried to get Csound running in Unity and Android but I didn’t look further.

Thanks for the reply. I’m afraid I may have to leave this until someone with more Android experience can take up the reigns. I believe we are close. It’s just a matter of getting Csound loaded correctly.

Do you have any of the work done available? Maybe I could take a look and try to fix it. I’ve done some Android development before.

This is the rough package I have.

The most import bits of the above discussion are probably these:

If we could get it running without a crash, it would be something. Even if it’s not actually working. I’m just keen to see if Csound can be loaded from a Unity activity.

Thanks Rory, I get a 404 error with that Dropbox link.

Sorry, can you try this one:

Got it! Thanks!

Let me know if you get anywhere with it. Or if you need me to try anything for you on my end. It would be great to get this going.

I think it might be best to write an Android plugin that will loads Csound so CsoundUnity doesn’t have to. You seems to be able to load Csound fine in UE4 on Android, hopefully I can do the same.

Hi Rory!

For some reason UE4 seems to be better setup for loading plugins that use external shared libraries. I didn’t have to do much other than figure out how their build system works in that respect. I’ve never done plugin development with Unity but I’m sure there’s a way of just telling Unity to load the libraries at start in Android. I’ll try to take a look at it this week. I also hope to be able to figure why even though Csound is loading without errors in UE/Android, there’s still no audio output.

Sorry I can’t help more with that. Even if you were making a few error in your code, you should at least be hearing some kind of output. Steven Yi suggested I look at creating an AAR package for Csound. That would certainly help on the Unity side of things. Not sure if it be much use on the UE4 side, where you seem to have no issues in actually loading the Csound library.