Cabbage Logo
Back to Cabbage Site

Panning from Csound, relationship to AudioSource in Unity

I am having some trouble with getting the Csound opcode pan2 to work properly in Unity3D. I get good results running the patch in Csound by itself, but running it in Unity messes up the panning. I can control other things without problems from Unity (Pitch, volumes, etc.) but not pan. Is there a recommended way to set up the Audio Source parameters to allow transparency through Unity?

(As an aside, I found that using some of Unity’s audio effects through it’s mixers is MUCH more efficient than using Csound’s, probably due to summing of all sources vs. each voice running it’s own effect, provided Unity provides an alternative. This suggests porting all favorite Csound effects into Unity as plug-ins to the mixer).

Thanks for any help!

Are you sure you’re using the latest scripts? Grab them from github and overwrite the current ones. We fixed some channel issues a while back. I did create a CsoundUnity mixer plugin. It was just a proof of concept though, it needs more work. And maybe the unity sdk is better now. I’d need to take a look.

Just to be sure, I downloaded the UnityPackage from the OSX link on this website - the latest scripts are included in this package, right? Panning still doesn’t work for me, unfortunately.

CsoundUnity, or linked from here:
http://rorywalsh.github.io/CsoundUnity/docs.html

I started a new unity project, put in one game object, added a csoundunity script and just added the file below. The sound just increases in loudness in my left ear but doesn’t pan. Based on the csound below, shouldn’t it pan, even without a unity script sending commands?

<Cabbage>
form caption("Test My Pan"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d		; for unity
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs  = 1

giSine ftgen 0, 0, 2^10, 10, 1

instr 1

kline	line	0, p3, 1     ; straight line

ain	oscili	.6, 440, giSine ; audio signal..
aL,aR	pan2	ain, kline   ; sent across image
	outs	aL, aR

endin
</CsInstruments>
<CsScore>
f0 z
f1 0 32768 10 1

i1 0 20	; pan across the stereo field for 20 seconds
</CsScore>
</CsoundSynthesizer>

Can you grab the scripts from here and replace the ones if your project with them:

p.s. if you don’t format your code using the format code button, or Ctrl+alt+c, it doesn’t appear properly in the forum…

Thanks. I just overwrote the CsoundUnityScripts with these scripts and have the following error:

Assets/Scripts/CsoundUnityScripts/CsoundCsharp.cs(97,24): error CS0103: The name `_dllVersionCsUnity’ does not exist in the current context

Do I need to refresh some other files as well?

Can you try replacing all the files with the one on git? I don’t think the OSX packages is up to date. Actually, forget that. Just install the usual package, then replace the CsoundUnity.cs file b with the one in git. That’s the one that fixed the issue. I’ll be able to prepare a new package for osx next week but am not in a position to do so right now…

I thought I replaced that one when I copied all the script files into the CsoundUnityScripts folder.
I can’t run or test because of the error I mentioned.
Do I need an additional DLL file?

Do you have the latest version of Csound installed on your machine? I will be back in the land of OSX on Wednesday, I can take a look and see what’s up.

I’m not sure whether the Csound used by Unity is the same as the Csound used by CsoundQT, but here’s what I have in CsoundQT:
Using Csound version:6080 Precision:double (64-bit)

is this the latest? is it what Unity uses?
what I see in the CsoundLib64.framework version folder is 6.0, both for libcsnd6.6.0 and libCsoundAC.6.0

If there is a discrepancy between what CsoundQT uses and Unity I assume that things are re-compiled/interpreted in the Unity Csound environment so that compatibility is a non-issue unless there were new Unity opcodes in CsounQT that were not present in the Unity libs.

Hi Thomas, if Just tried the very latest OSX package and I’m not having any issues?

All of the current Csound6 packages are presented with 6.0, if you run Csound from the terminal you should get the version number. I’m running 6.09.

Normally no as both will use the Csound runtime library. But each time the Csound API is bumped it can cause some issues with pacakges that expect to see the older library. This version of CsoundUnity comes with a precompiled native dll/.dylib that links to Csound 6.09. I had to do this to allow processing of AudioSource clips. My preference is always to avoid having any precompiled plugins and just use scripts instead, but it wasn’t possible here. It will be however once the next version of Csound is released.

I’m not sure what to do then, I definitely can’t pan audio properly, even using the Unity panner on a Csound source. The pan2 program I posted previously doesn’t work. I assume you are using a Mac. The CsoundUnity package I’m using I downloaded from your link on the Cabbage site. Could you send me an example Unity project that uses panning? Any help is most appreciated.

Is this the one you’re installing?
https://github.com/rorywalsh/CsoundUnity/releases/download/v2.3/CsoundUnityOSX.unitypackage

Thanks, I’ll check when I get back to my computer.

Yes!!! Downloaded the latest and pan now works! Somehow I wasn’t getting the latest before.

Great. But I wonder how you were managing to download the wrong package. I hope I don’t have bugs links lying around?

I think that I originally found this package in the Unity Store. I thought I was able to update it from your Cabbage page but I guess not…

I don’t think there ever was one there. Anyway, it’s all good now. You should also be able to process audio clips quite easily, and all sounds are sent through the Unity3d sound system.

Now I have to figure out how to update my earlier Unity projects without breaking something.