Cabbage Logo
Back to Cabbage Site

Limits of Csound in Unity

I am curious about the efficiency and limits of Csound. I have a fractal that generates multiple voices/pitches and have no problem running my project when the synth uses a simple oscillator voice. If I make a synth that uses a sawtooth and filters it with a moog-type filter I can’t get more than a few hundred voices before the audio begins to break up and eventually gives out. If I allow the project to generate the full number (1 voice per fractal, up to 1554) the whole system crashes.
I guess the simple answer is “Don’t DO that!” but is there anything I can look at that can give me a better idea of what I can expect in terms of the overhead to run different synthesis models? Thanks!

In your case you are creating quite a lot of voices. Opcodes like vco2 are not that efficient., as you’ve noticed. You can always add your own harmonics to the function table the oscillator reads from. Using GEN10 should give you some nice results. You can can choose the function table based on the frequency of the note in order to avoid aliasing. It would be far more efficient that a vco2. You could also avoid the use of a filter this way too by simply limiting the number of harmonics in each tone.

I’m using a Gen10. When the voices begin to add up Unity bombs out. I can generate the first 2 levels of fractals, and then things blow up. I’ll see what I can do…

You sure it’s not just amplitude levels going through the roof? My levels saturated the audio output quite quickly in my tests.

I can check, I thought I was setting levels at 1/1554. Would high levels cause unity to crash?

Sorry, I thought my first message hadn’t got through so I wrote another message!

Amplitude levels shouldn’t cause a crash. Are you filtering your GEN10 tables with a moogladder? Try taking the moogladder out and see if it helps. Does it actually kill Unity or does it just causes it to go unresponsive? Perhaps you can monitor your system resources too and see if they are being pushed to the limit.

1550 instances of an instrument does seem like you may be pushing Csound to the absolute limit. I’m not sure which approach you took in the end, but are you turning off each instance when it’s done? Or is it going off on its own?

I just replaced your csd example with a GEN10 with a saw wave and put it through a moog filter. Attach CsoundUnity to the sphere object. I keep the voices going forever and try to alter pitch, frequency, amp, and resonance. I can set the depth of the fractal, when it’s 2 it runs but sound becomes distorted, but not loud. At a depth of 3 Unity crashes completely. Do you want to see the files?

What happens if you remove the filter from the instrument? Can you share your Csound code?

sure.
Basic.csd (1.3 KB)

Did you try removing the filter? You could probably achieve the same sound without using a filter at all by modifying the number of harmonics that are being played back. Take out the filter first and see if it makes a difference.

Just did, crashes Unity at 3 levels.

But when you use a simple sine wave oscillator it works fine?

I believe it did when I used your example - I’ll have to set that up again…

It is able to play 1554 voices without crashes, but the audio stutters quite badly. Maybe the number of channel updates from so many objects is an additional problem? I’ll turn on system monitoring and check the overhead.

ok, framerate in Unity is at about 60 fps, DSP usage about 40%, audio level at -35 db, lots of static and stuttering from Csound. CPU usage about 180%. No crash in Unity.

I think you could be right. Can you take the chnget opcodes out and see how it works??

Its stays cleaner longer, but still eventually breaks up. Curiously, my DSP usage seems to have gone up to 60%, framerate dropped to around 40.

When you use a single sine wave instead of the sawtooth how is it?

I’ve been using a sine wave. when you say single, do you mean 1 frequency? each fractal obj has it’s starting pitch.

Ah Ok. So it runs to about 1554 voices. I’d say you may have come to some kind of limit here. It most likely depends on the speed of your PC. Check this thread
So you may need to rethink how you are going about this. If you need to produce so many harmonics/voices, you may be better of using something other than plain old additive synthesis. Or maybe you could rethink the mapping completely. Could you get away with using only every second voice? Do you need one for every element on screen?