rorywalsh / CsoundUnity

GNU Lesser General Public License v2.1
70 stars 19 forks source link

Adjusting ksmps and sr #24

Closed Despair-Bear closed 3 years ago

Despair-Bear commented 3 years ago

Hey there. Sorry for bombarding you all with questions haha. I was messing around and figured a good way to potentially optimize csounds in Unity is to allow varying levels of sample rates so objects which don't necessarily require maximum quality can be reduced but I noticed that in the profiler, when I decrease ksmps and sr in my .CSD file I see that the DSP CPU consumption actually goes up instead of down. I'm assuming that something is set within the CsoundsUnity initialization that hooks into Unity's AudioSource object that may force the sample rate to match Unity's?

I know this is probably beyond the initial scope of this project so I'm mostly looking to be pointed in the right direction as I don't mind mucking around with the source to get this working if it is currently not possible.

giovannibedetti commented 3 years ago

Yes they are overridden with SetParams https://github.com/rorywalsh/CsoundUnity/blob/8fb1cf9795f33360349d1fbc3a8e7bf046ad2dda/Runtime/CsoundUnityBridge.cs#L94

Try commenting this line to use the sr and kr from your csd. We found that just some combinations of sr/kr are working (or clicks and pops appear, probably because of thread desync), and so decided to override them. Also notice that not having the same sample rate of the Unity Audio Project Settings causes audio to be played faster/slower and with changed pitch (typical sampling rate issues).

We could add a toggle to disable overriding them, and so be able to set the desired values, at your own risk ;)