Cabbage Logo
Back to Cabbage Site

Returning to CsoundUnity and updating old project

Ah Debug :man_facepalming: Thanks that’s it!

@metaphysician I just tried this instrument and I can see a DSP load of 20%, but it’s runs fine. That a full 16x16 sequencer. I ee no real increase in DSP load if enable each hit for every beat or if I play things more sporadically. But perhaps I’m not setting it up correctly?

[edit] I just add 16 more rows. Now my DSP load is 36%. But it’s not a show stopper.

well, for me that’s too much DSP because there will be several layers of 16x16 instruments. if each one gets 20 percent then that gets me to 4 layers as that is 70-80% and i start getting dropouts.

it just seems like Csound is really efficient at running instruments, because it hardly budged when i removed the other instruments from the script, but it seems to use up much more DSP with the remainder, which is two oscilis and the array.

would an ftable read by a phasor be less processor intensive in comparison?

it does also look like 16 note polyphony is a demanding ask. so i can easily reduce one layer to one row using a note value at that position (like an analog sequencer) if i want the track to be monophonic. i would like some layers to have say 4 voice polyphony though, so i need info on how you assign voices for limited polyphony.

It’s pretty crazy. With 64 rows Cabbage shows about 4%, yet the DSP Unity load in Unity is 60/70%. Do you even need Unity’s audio system? Could you simply bypass it and output directly from Csound?

Yeah we need Unity Audio as we don’t have direct access to soundcards.
Not sure where the overhead comes from, all happens in ProcessBlock

Csound can speak directly to the sound card, i’ve done this before when I wanted low latency audio input, but it’s not really a solution. Let me dig a little further.

I can’t see any red flags there. But I did just try 256 instances, which equates to 16 16x16 grids. It spikes at the start but then settles at 70/80%.

Ok but how do we handle different platforms? Not sure how it would work (also the AudioSources could be spatialized)

I guess we need to reduce ksmps as basically we are calling PerformKsmps every sample if using AudioSetting.outputSampleRate for both sr and kr.
Try overriding the sample rate using the inspector toggle, you can then choose kr as you wish

Edit:
Like this:
image

Ok, now it makes sense. I didn’t realise ksmps=1 is the default.

I just changed to ksmps of 64 and I’m able to run 4096 instances of the sequencer with pretty good performance. :+1:

2 Likes

Oh, I’m not suggested we actually do this :rofl: But it is possible. I did this for an installation where I needed very low latency audio IO. It worked well, but I didn’t use an AudioSource. I just performed Csound using a thread rather than the OnAudioFilterRead() approach.

1 Like

@rorywalsh let me try on my end. how do i set the same thing? i know i check override sampling rate. should i put in 4410 or 64 for control rate? or is that done in the script itself?

looking at my script it’s set to ksmps = 32, but do i have to check the override option for that to work? i guess i’ll keep trying different things.

i don’t have that option that @giovannibedetti showed on my end actually - i think my version of CsoundUnity must be older.

well, it looks like updating Csound Unity to 3.5.1 led to some compiling errors:

Library/PackageCache/com.csound.csoundunity@e26c21e302/Runtime/CsoundUnity.cs(1830,26): error CS0103: The name ‘UnityWebRequestMultimedia’ does not exist in the current context

Library/PackageCache/com.csound.csoundunity@e26c21e302/Runtime/CsoundUnity.cs(1851,24): error CS0103: The name ‘DownloadHandlerAudioClip’ does not exist in the current context

i’ll see what happens if i downgrade to a slightly earlier version.

looks like still the same errors as before in 3.5.0 as well as 3.5.1. i’m on Unity 2022.3.14 BTW

Update - it’s an issue with disabled Built-In packages - i needed to enable Web Request Audio in the Package Manager


okay - so with that fixed and running 3.5.0 and setting control rate to 480 from 48000 sample rate putting ksmps at 100 i have now lowered DSP to about 12% for one layer with that script - not bad, and if i were to optimize some layers to be monophonic that would save even more CPU.

but this is still not squaring with @rorywalsh running 4096 instances - even if that’s 4096 rows that would be 256 layers of 16x16 which is way beyond what i can muster currently - i think i could get maybe 6 full 16 voice polyphonic tracks before the overhead becomes too much.

so something is still not adding up. switching to best performance from good latency doesn’t have any effect. are you running 4096 instances of CsoundUnity with that script?

extra question - what’s the significance of the CsoundUnityChild script?


so a bit more testing. it seems that DSP does NOT increase per instance. i started gradually adding layers back in and DSP did not increase past 22%. in the older 3.4.3 version it definitely did but 3.5.0 seems better. and it actually went DOWN when i added back in the other synths from the original script - now at 19%. i need to do a build and see how the audio sounds but this is quite promising!

Thanks for reporting this issue, the WebRequest is for the loading of audio files on mobile platforms

The Unity voice limit should be considered. Some of the AudioSources are culled by the engine, depending on the playform and cpu usage

allright a bit more testing, this time with all the instruments, and performance seems to be good! i think i got 6 layers going and it barely around 25%. so this is a very good sign. this is setting control rate to 480 which puts ksamps at 100.

however i think something is wrong with the sfinstr opcode because i’m getting no output from it. i have not set any environment variables but i also didn’t do this for 3.4.3 in a similar project and that version plays the sound fonts just fine. i’ve uploaded the original script before but for convenience here it is again. instr 4-7 are sf2 based instruments - 4 and 5 are melodic and 6 and 7 are drums and percussion:

Sequencer16-sf2player.csd (9.8 KB)

not sure what the major differences are between 3.4.3 and 3.5.0 but i’ve messed around with ksamps at 1, and at 50 and there’s no difference - still no audio. all the rest of the instruments play fine.


and it actually was the environment folder settings. it’s weird that it worked fine in the other one without them but that project using 3.4.3 was switched back to Standalone while this one is Android, so maybe thats the difference? i don’t know.

allright @giovannibedetti - incremental progress but we are definitely getting there! i have one show stopper that crashes the build on Quest after startup and when i start the playback of the sequences.

pretty sure it’s due to the sf2 files either not being found or not loaded correctly. i have loaded them via Android File Transfer at what i believe is the Persistent Data path on the Quest 3, that being /Android/data/com.Metaphysical.constellationNew/files, but it’s still crashing, unfortunately.

the synth based instruments are doing fine - they aren’t affected when i disable all of the sound font-based layers’ CsoundUnity scripts and AudioSources. perhaps the CSD script needs to load them at a specific path or there’s a config file somewhere perhaps?

anyway i am able to play all instruments on 8 Layers with DSP around 20-25% in the Unity Editor which is fantastic. 8 layers is enough to make a decent demo, so once we get over this hump of SF2 files not loading properly i will finally be at a good enough state to start working on controlling the layers. the ability to set ksamps to much larger values by reducing control rate has done the trick.

1 Like

This is great news!
Adding the possibility to change the ksmps is very recent (3.5), but very helpful!

Have a look at the Initializer here: https://github.com/rorywalsh/CsoundUnity/blob/master/Samples~/EnvironmentVars/SFDIR/Initializer_Alternative.cs

There is an improved version of this script here:

Be sure you have copied the sf files into the persistentDataPath, you can use one of the above scripts for that.
The trick is start with the CsoundUnity objects that will use the sf files not active, so that they won’t start Csound and won’t try to read the sf files. If not found you could have a crash.
Or maybe start in a different scene, that just copies the sf files if not there already.
If you copied them by hand using Android File Transfer then it should be good already, so just be sure to set the Environment Vars for the Android platform to the PersistentDataPath.
Then it should work. If it doesn’t, enable Csound Log output and report here what it says

okay - thanks for that detailed information! so, i used Android File Transfer to copy the SF2 files to what i believe is the persistent data path. it definitely went into the files folder under my bundle name.

but let’s just start with that issue. if i copy those files to the place its supposed to be, instead of using a script then maybe the system is NOT aware of it?

next issue - regarding the loading of SF2 files, there are 8 layers loading the exact same sequencer script with two SF2 files utilized.

at the moment there are 4 instances of the sfinstr opcode that will load and be using SF2 files actively - in other words they are triggered by events from the sequencer. the other four are not playing from the SF2 files.

given this info i don’t believe i have any inactive SF2 files. all layers use it from program start although it seems it doesn’t crash until i enable a layer using sfinstr on the sequencer to play.

i have set the Android environment variables on the tracks actively using the SF2 instruments in Unity.


oh also the Editor normally doesn’t crash - but any builds that are running, if i trigger the playback of all layers it’ll crash. if i disable those last four layers using the SF2 files, no crash.

Post a screenshot of the Env settings.
You can try toggling the ‘Runtime Paths’ in the Env settings to see how the path will look like at runtime on each platform.
Also some logs before the crash would be useful to understand.
You could use the Android Logcat package to gather logs from the headset

Are you load multiple sequencer csd file across different CsoundUnity objects? I am not sure how well these will sync up?