Cabbage Logo
Back to Cabbage Site

Csound and Unity on Android

Yes also Hector uses this script for Android, he modified it to create a list of files (using a json) to be copied later, we surely need something like this. Having the files in persistentDataPath is one way to have them accessed. Another way (I didn’t try) would be using AssetBundles or the new Addressables, but then we should force the user to use them, and write code to save/load them.

Yeah, I had a feeling I put it in there for a good reason! Do you have the file that Hector prepared? Can you post it? I just want to take a looksie…

Here it is (I removed the licence, comments, and your old code, it was commented and not used):

using System;
using System.IO;

#if UNITY_EDITOR

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

class CsoundBuildPreprocessor : IPreprocessBuildWithReport
{
    public int callbackOrder { get { return 0; } }

    public void OnPreprocessBuild(BuildReport report)
    {
        Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);

        if (report.summary.platform == BuildTarget.Android)
        {
            string csoundAssets = Application.streamingAssetsPath + "/CsoundFiles";
            if (Directory.Exists(csoundAssets))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(csoundAssets);
                CsoundFilesInfo filesObj = new CsoundFilesInfo();
                
                FileInfo[] filesInfo = dirInfo.GetFiles();

                string[] fileNames = new string[filesInfo.Length];

                int i = 0;
                foreach (var item in filesInfo)
                {
                    fileNames[i] = item.Name;
                    i++;
                }
                filesObj.fileNames = fileNames;

                if (fileNames.Length > 0)
                {
                    string jsonObj = JsonUtility.ToJson(filesObj);
                    Debug.Log("Serialized file list: " + jsonObj);
                    using (FileStream fs = new FileStream(csoundAssets + "/csoundFiles.json", FileMode.Create))
                    {
                        using (StreamWriter writer = new StreamWriter(fs))
                        {
                            writer.Write(jsonObj);
                        }
                    }
                }
            }
        }
    }
}

#endif

Addressables look like a dream come true (from Addressable Assets development):

Right, so we still need to handle non-android builds, which would be my old code?

So we may no longer need to copy audio files to the StreamingAssets folder, or write them to a file stream on Android?

Btw, do you want me to add Hector’s code to the prebuild script in the source repo, or have you already modified it? I must add @virtualHC as a contributor too…

Yes the code is in the unitypackage I attached here: Csound and Unity on Android, but with his code I have no sound on editor on macOS (so a bit difficult to develop things), didn’t try to build on that platform.

So we may no longer need to copy audio files to the StreamingAssets folder, or write them to a file stream on Android?

So it seems!!

Hey sorry to resurrect this, I’m getting an instant crash when I open a test app i made, csound just plays a sine wave on button press, i’ve got the logcat output here but not sure what the important bits are, this looked of note though (hope its readable):

Any ideas?

I think we need to ping @giovannibedetti on this one. He was the last one to look into this. I just can’t recall how far he got…

Unfortunately I wasn’t able to make it work.
We have to include the newest Csound for Android and hook it up.
@Syl_Morrison if you need a working package for Android we have the old one made by Hector, but it’s based on the “master” version of CsoundUnity and if I remember correctly the library for android is v6.08.
I hope I’ll find some time to get back to this, but atm I cannot promise anything, as I am booked until mid december :sweat_smile:
Also @rorywalsh the master branch has a merge issue! Steven is having a look at the repo, which is GREAT NEWS!! He can surely help us with the Android stuff!

1 Like

He surely can, if he has the time :crossed_fingers:

If I could grab a copy of Hector’s version that’d be amazing, mainly want to proof of concept it, we’ve probably got quite a ways to go before we get the whole project onto android but I wanted to do some stress testing with soundfonts on it to see what I’m gonna need to change between platforms!
I might also see if I can get the changes in Hector’s package up to date with the version of CsoundUnity I have, is the develop branch the one everyone’s working off?

Just sent you the package. Yes the develop branch is the most updated one.
If you can understand what Hector did to make it work that would be awesome!

1 Like

Just don’t forget to post back if you figure it out!

for sure, you absolutely know that if I get it going im not gonna be able to contain my excitement and spam screenshots everywhere until someone tells me to shut up anyway hahahah

1 Like

So I forked the repo, I’ve basically just been trying to merge the functionality between the two versions, I have the latest editor features in but its very much a work in Progress, am still getting that wrong sr issue but this works on my Android, and I added some of the latest stuff that was missing from Hectors package (exposing ClearSpin() etc etc)
Will keep you posted

2 Likes

Great!!! Can’t wait to try it! Well done!! :partying_face:

I actually realized I did this backwards, think it makes sense to work off the dev branch and just try replace the UNITY_ANDROID regions with Hector’s code, will push if it works lol

Not all the code should be needed, since the newest CsoundUnity doesn’t try to load the CSD, but stores the CSD content in a string when you set the CSD in the inspector.
EDIT: for example this is useless:

EDIT 2: Also I removed the check for Csound because it shouldn’t be needed, since the Csound libraries for all the platforms should already be in the package!
Or if you don’t have Csound installed you see the popup message every time you recompile.

1 Like

Yeah I started working off the dev branch and bringing in the Android changes from Hector’s version, decidedly less fruitful hahahah

I’m curious to understand what you did to make it work, because I did the same but for me it wasn’t working!
so WELL DONE!
Can’t wait to try your repo on my Quest!!
Feel free to do a pull req on the develop branch when you’re ready.
I think we should aim to make a new release of CsoundUnity!
:partying_face: