Cabbage Logo
Back to Cabbage Site

Csound crashes Unity completely (resolved)

Just got back to developing my Unity project after some traveling and other things. Have a brand new project, tried to add Csound and I keep getting complete Unity crashes after the first setChannel() write. Csound plays alright if I don’t do any setChannel writes. Any advice for debugging this?
Thanks!

Can you send me the simple .csd file you’re using? Or better still, can you create a test package for me to try?

Ok, I sent it to your email at cabbageaudio.com. Thanks!

Actually, I never set that up! Can you send it via private message? That usually works ok…

How di I do that? To where?

Just click my name and go to send message…

I tried to message it, it tells me 3 MB is the maximum.

Sounds large for a simple scene? Can you wetransfer it?

That’s what I did already but you said your email wasn’t set up. I had WeTransfer 'd it to rory@cabbageaudio.com. Where else coul I send it?

Can you not send me the link directly through private message? I don’t like sharing my email on a public forum…

I understand, sorry. Sent the link.

You csoundUnity object is not valid when you call setChannel(). A test prevents the bug:

if(csoundUnity)
	csoundUnity.setChannel ("frequency", 880.0f);	

I’m don’t know why it’s not valid, but it it works so long as you put in this test. That’s what’s odd. At some point the script is called and csoundUnity is not valid, but then the next time the script is executed it is? I didn’t really take a look at the project so I can’t be sure what’s going on. But at least this should get you over this particular road block.

Thanks for the workaround. I’ll keep it in mind and check for anomalies. (Maybe a multithreading issue?)

I added a check in the object’s Update() method as well as in the child Update() methods and find that very ofter (maybe always) the pointer to csoundUnity is NULL. Very strange, not sure what I can do to correct this…

That’s odd. Your csoundUnity object has to be valid at some point because the frequency does get set correctly. I’ll take another look tomorrow (I don’t have access to a Unity machine now). If you call the following in your update function without a test, does it crash?

csoundUnity.setChannel ("frequency", 880.0f);

I don’t expect it to, but if the object is NULL then it should. I have to say that I’m not a C# / .net expert by any stretch of the imagination. Maybe I’m overlooking something…

Yup, without the test it crashes. Not sure why the pointer wouldn’t persist. Maybe it never got set in the first place somehow. The program creates children, I passed the CsoundUnity pointer into the children, will try to see if it doesn’t get set somehow.


OK, I looked into the passing of the CsoundUnity reference to the children, and the reference pointer doesn’t get passed in the normal GetComponent() fashion called on the parent. I needed to just pass the initialized pointer. The next issue I have is how to instantiate an audio source to each child object on-the-fly and have it use it’s own .csd instance. Can I do this programmatically? Will look into the possibilities…

I didn’t notice any use of pointers at all in your code. I’ll take another look later but all I saw were references.

Pardon me for not taking more time to read through your code, but did you plan for each child to have control over one master instrument? It might get messy if you are using setChannel() as each channel would have to be totally unique. it’s is doable though. You could get each child to start an instrument using an sendScore() method. You could pass a unique p-field to each instance of the instrument. Then within the instrument you can create a unique channel name that matches the instance of the instrument.

On the other hand, you should also be able to set up as many CsoundUnity object as you wish. It looks like students from my 9.00 class partied a little too hard last night, so I might be able to look into this in the next hour…if no one shows up…

I’m just looking through your code again now. So you’re trying to get your fractal script to spawn instances of itself? I’m pretty sure this is the problem. When you stop the spawning there are no crashes.

I just tried to spawn each one with its own CsoundUnity object. I had to hack CsoundUnity.cs so it kept reading the same file. Yup, that quickly get ridiculous, but works. But grinds Unity to a halt. No surprise considering hundreds of Csound instance are being created. This approach definitely borders on cruelty to Csound!

Another approach would be to add the CsoundUnity script to your sphere object, and then access it from your fractal script using

void Awake() {
	csoundUnity =  GameObject.FindObjectOfType(typeof(CsoundUnity)) as CsoundUnity;
} 

void Start(){
    (...)
        if(csoundUnity)
    		csoundUnity.setChannel("frequency", Random.Range(100, 1000));	
    (...)
}

This works fine and doesn’t cause any crashes, even without any tests. The only thing is you are only using a single instance of an instrument. So each time you change the frequency it changes the overall frequency? But this is probably what you want. For what it’s worth, I did a quick test where each instance of fractal started a new instrument instance with a unique frequency. It works Ok too. So both approaches could be considered.

Thanks again for looking into this. I think the option of adding “notes” to a single instance of Csound would be the most efficient provided each note can be dynamically changed from each fractal instance. In this example there are I believe around 384 spawned objects and I want their position in space to be reflected in their pitches and filter settings and amplitudes. I will need to pass p-fields for each note. I can put an empty game object into the scene to start csound.
Glad I can be of assistance in pushing csound to its limits, a testament to its robustness!

I’m a little stymied. I want to do what you said, where each instance of a fractal starts a new instrument instance with a unique frequency. You say it works ok, but I don’t see how to spawn a new instrument in CSound on-the-fly and then be able to control it later. How can I create a new instrument and get a handle to it back to my program? I thought about creating a 2D array in Csound and plugging values into it when the instruments are created, but I don’t know how to spawn an instrument in csound. Can you point me to where to look? Also, can you tell me how you created a new instrument instance? This would be most appreciated, thanks!