Cabbage Logo
Back to Cabbage Site

Csound crashes Unity completely (resolved)

In my test I started a new instance of the Csound instrument using the sendScoeEvent() method. I passed frequency to p4. If you don’t need to have further control over the frequency this will do fine. If you do you should create a unique channel name for each instance.

I won’t be near my PC till Monday. If you don’t get anywhere I can help then.

I need to keep control over the synth voice so I can affect it dynamically. Ideally it seems cleanest to call csound, have it spawn an instrument and pass back a handle so it can be dynamically controlled by Uniy. Because of the number of voices I want to generate it seems unwieldy to have to name channels in advance. I don’t know enough about csound to know whether there is something like a GenInst or SynthGen function to spawn a new instrument on the fly - is there a way to do this?

You just need to send a score event to start a new instance of an instrument. But you will need to know your channel names. The normal think to do is to number each channel according to its index. Like “freq1”, “freq1”, etc. It’s not a big deal. Quite simple. I can send something on tomorrow.

Are you saying I should string names together programmatically? I’d love for see a sample of the setup and instrument. I thought channels were parsed based on instruments. I don’t know how frequency maps to a voice if one instrument is sounding hundreds of voices, and, alternatively I don’t know how to create hundreds of instruments programmatically. Any help is most welcome!

Ok, here you go. I created a very simple project. Each Sphere script object(tagged as Sphere), when instantiated, starts a new instance of a very simple Csound instrument using sendScoreEvent(). When I trigger the score event I pass the instance number as p5, and an initial random frequency as p5.

int index = GameObject.FindGameObjectsWithTag ("Sphere").Length; = "Sphere"+index;
channelName = "freq"+index;	
    csoundUnity.sendScoreEvent("i\"SPHERE\" 0 10 " + Random.Range(100, 1000) + " " + index);

My Csound instrument creates a unique channel name based on this info:

instr SPHERE
	aEnv linen .1, .01, p3, 0.01		;amp env, low amp to avoid distortion..
	iIndex = p5				;index is passed as p5		
	SChannel sprintf "freq%d", iIndex	;construct unique channel name 

	kFreqScale chnget SChannel		;get freq info from Unity
	a1 oscili aEnv, p4*kFreqScale		;set oscillator, 
	outs a1, a1				;output signal

In my Sphere’s update method I transform the frequency over time to drop from its initial pitch to close to 0, based on its current localScale, which as you see when you play the scene reduces in size over time.

csoundUnity.setChannel(channelName, transform.localScale.x/2f);

Not very interesting musically, but it shows how you can use a simple integer ID to communicate between each Csound instance, and each GameObject instance. Btw, you scene looks really cool. I won’t give away the details, but I look forward to seeing how it turns out! Here’s the project. I didn’t package Csound in order to keep the size down. You’ll need to add that yourself. You could probably just add this package to your existing one.

DynamicObjects.unitypackage (33.9 KB)

I recommend putting in these checks every time you call a CsoundUnity object:


You can remove them when it’s time to build the final game as they will slow things down somewhat. But in development I find they are really great for stopping Unity from crashing. Let me know how you get on.

Thanks Rory, I’ll give it a try! I guess the key thing is that when you start a score event you are tying the sound to the inputs, so all will be good for continuing sounds.
I’ll keep you posted when I get some more things happening in my scene.

Everything works, now just have to figure out how I want to map object rotations, among other things…

That’s great. When you get a little further along maybe you’d post a screencast of it working. I’m sure others here would be interested to see what kind of things you’re getting up to with it.

Will do. Am thinking of trying to implement a filter “landscape” in csound.

I once created a mountain range out of FFT data. There’s no end to the fun of Unity and Csound!

Cool! I’m thinking of a filter space that sound objects will traverse through, so depending on their position in the scene will make timbal shifts. Happy to say that 1554 voices doesn’t seem to slow things down.

Wow, don’t hold back!