Cabbage Logo
Back to Cabbage Site

Optimization tips for Csound in Unity

Hey all,

So I’ve been messing around with using the ‘scanu’ opcode with instruments in Unity. I’ve been hitting some serious CPU walls, and am wondering the things I should be considering with optimizing Unity, as well as the Csound instrument itself.

I’ve been using a sketch written by Takahiko Tsuchiya called “Spinning Clock”. It’s likely the case that this instrument will just be too much, because it is a very dense (and beautiful) design.

But the instrument itself can run fine when untouched. The problem comes in when I try to control a ‘kRate’ input parameter of the ‘scanu’ opcode. It’s being controlled by the Csound object’s ‘position.y’. I’m using the CsoundSender component, and a CsoundTransformandPhysics sender.

Other than taking from the instrument’s quality, I’m wondering what things I can do to help Unity out? I have a screenshot of the profiler as I try to input into the kRate parameter attached, that may help. It’s interesting that ‘Physics.Processing’ spikes up, along with the "EditorLoop’.

comp_P1_01.csd (12.5 KB)

[EDIT]

Also, I should mention that when I run the instrument in cabbage and aggressively scrub through the kRate parameter channel I have mapped it runs great.

As I mentioned in another thread I’ve found those opcodes to be quite CPU hungry. I"m not familiar with the CsoundSender or CsoundTransformedPhysics sender. Do you also get problem if you just use the basic channel methods?

1 Like

Sorry for the late reply, are those components from Mateo&Caio’s collection of tools?
It would be nice to have a look at them, I never had the chance :wink:
Can you send them via pm maybe? not sure if they want them to be public yet.
Also, the csd you sent has no Cabbage widgets, how are you controlling it on Cabbage?

Edit: btw I like the premises though :joy::
; Please be warned that the scanned synth in this piece is not completely stable.
; Your headphones / speakers may explode.

1 Like

Ah yes sorry! I had uploaded the original file on accident without the widget channels included.
Here is correct file.
comp_P1_01.csd (13.8 KB)
Also, yes that is from Mateo and Caio’s collection! Very useful stuff they’ve been making. I can show you that on PM.

Yeah the premises were quite concerning. It did, in fact, blow my speakers up occasionally.

Also, let me try the basic method and see if that helps anything.

I am getting better results from a simpler channel setting script, but not much better.

I guess I’m just confused because the instrument really does run flawlessly in Cabbage, even with me aggressively changing the channels. This makes me think I can be doing something differently with how I am sending the data over. Is it possibly too precise(float)? Is there a way to optimize a channel that’s at kRate?

I had a quick look at the scripts you’re using but really I cannot make assumptions without seeing the whole setup.
Also those scripts refer to other scripts, so I cannot really tell what can be slowing down things.
If you want send me a zip of your project / or a minimized version that reproduces the issue.
I suspect there is something that gets called too many times per frame than what is needed.
There is no reason why setting a position causes a spike in the physics :face_with_monocle:
Do you have a rigidbody on your gameobject?
Because the rigidbody could try to move the object down because of gravity, and you try to move it up with the script. That could probably cause some slowdowns?
If you have a rigidbody on a gameobject and want to move it, either set the rigidbody to Kinematic, or use one of the methods on the rigidbody like move or simply set its position: https://docs.unity3d.com/ScriptReference/Rigidbody.html

https://drive.google.com/file/d/11TPgGcK18XcXChLdWDEgJC8bLUB9ZzE3/view?usp=sharing

Here’s a link to the project file. The scene I’m talking about is called “Drop_Ball_On_Floor”. To move the ‘Csound’ ball up and down, you use the arrow keys or w/s. Hopefully this sends over correctly. I have been using a rigid body but just took and off and no difference. Hopefully this sends over alright!

Thanks a ton for checking it out.

It crashes as soon as I hit play, after copying the matrxB files (btw I had to add the extensions there, they were missing)
Mm not sure why it crashes, but yes you are setting the position of a gameobject that has a rigidbody attached.
That causes concurrent behaviours, so the rigidbody tries to go down because of physics/gravity, and you are setting the transform. You should never set the transform of a rigidbody, only if it is set as Kinematic (so it will trigger collisions but it’s position won’t change because of physics)
[EDIT]
Which version of Unity are you using?
Btw I won’t have time to have a closer look soon (I’m in the middle of a release), maybe during the weekend but I’m not sure I’m free.
In the meantime try to remove the rigidbody from the object or set it to kinematic.
Or maybe ask Caio/Mateo to add a method to set the position of a rigidbody instead of a transform?
It could be useful I think.

@Marshall_Chad Regarding the precision of the k rate channels, you can always try changing ksmps to something bigger. That should slow down the updates.

Btw, keep in mind that Unity is a beast in comparison to Cabbage. An instrument that works flawlessly in Cabbage might not run as well in Unity.