Cabbage Logo
Back to Cabbage Site

CsoundUnity Package (UPM) development

I’m getting File Changed messages, but should the CSD that is displayed in the editor also change?

Yes it should be updated!

I’m afraid that’s not happening for me. AFAIK, it works fine in my CsoundUnity test project, but not in this new project. It’s not a big deal, I just have to drag the .csd over each time I update it.

Ok!
Yes that will work. Will try to look at, it maybe next week :thinking:

You should check if this gets called, line 148:

csound.SetCsd(csound.csoundFileGUID);

and the action on line 86

They don’t seem to be getting hit here on Windows. I put breakpoints on both, but neither get triggered…

Ok so it’s serious. I’ll have to look at it!

Hi @giovannibedetti. I’m looking to add -d -n to the compile time args to Csound. Is it ok to go ahead and just do:

Csound6.NativeMethods.csoundSetOption(csound, "-n");
Csound6.NativeMethods.csoundSetOption(csound, "-d");

We are not using SetOption() anywhere in the current branch, so I just wanted to make sure it’s ok. Also, the following is commented out too:

//Csound6.NativeMethods.csoundSetOption(csound, $"--sample-rate={AudioSettings.outputSampleRate}");
//Csound6.NativeMethods.csoundSetOption(csound, "--ksmps=32");

Whatever about the ksmps stuff, i think it’s important to use the same SR as the game?

I’ve modified the current source to support the writing of the number of channels to the first index of our tables. I also have it writing a single channel to the table, if users don’t write the number of channels. Only, we should let users determine which channel to write? Currently, it only writes the left channel. It’s easy to sort this, but I’m worried about our method signature! It currently looks like this:

public static MYFLT[] GetSamples(string source, SamplesOrigin origin, bool writeChannelData = false)

Adding a channel number parameter seems useless unless writeChannelData is false. So do you think it would be better to split this up into two new methods:

public static MYFLT[] GetStereoSamples(string source, SamplesOrigin origin)
public static MYFLT[] GetMonoSamples(string source, SamplesOrigin origin, int channelNumber)

I’m leaning towards the two methods. I think it’s cleaner. I’m not going to push anything for a while. Let me know what you think when you get a chance. Btw, it’s working fine now. No need to speed up table reading by 2x any more. :+1:

I’m sorry, I’m really busy today :sweat_smile:

We have a AudioChannels enum in CsoundUnityChild

public enum AudioChannels { MONO = 1, STEREO = 2/*, QUAD?, FIVE_PLUS_ONE???*/}

I think it would be better if it resides in CsoundUnity, so we have an easy way to pass this as a parameter, default would be MONO

like this:

public static MYFLT[] GetSamples(string source, SamplesOrigin origin, AudioChannels channels = AudioChannels.MONO)

so you can also call

GetSamples(source, SamplesOrigin.Resources);

What do you think?

Cool, I didn’t know we already had that. But we also need something to specify the channel we wish to write to the table? We might not always want to write just the let channel. The function signature you present doesn’t allow the users the option of writing the number of channels to disk? Or do you suggest we do this automatically if channel type is set to stereo? That would work, but we would still need something like this:

public static MYFLT[] GetSamples(string source, SamplesOrigin origin, AudioChannels channels = AudioChannels.MONO, int channelNumber=1, bool writeChannelsToArray)

It kind of gets messy don’t you think? That’s why I was thinking of having two unique public methods, for mono and stereo files. The private method can have an ugly signature. That’s Ok.

The two methods

are ok for me!
But I think we can also have a more complete signature like:

public static MYFLT[] GetSamples(string source, SamplesOrigin origin, AudioChannels channels = AudioChannels.MONO, writeChannelData = false, int channelNumber = 0)

so GetMonoSamples calls:

GetSamples(source, origin, AudioChannels.MONO, true, channelNumber);

and GetStereoSamples calls:

GetSamples(source, origin, AudioChannels.STEREO, true, 0); 
//the channel number could be used also here? 

would it be too much? maybe confusing!?

To be honest we could need also a version with this signature:

public static MYFLT[][] GetSamples(string source, SamplesOrigin origin, AudioChannels channels = AudioChannels.MONO, writeChannelData = false, int channelNumber = 0)

(and so on…) to get all the separated channels

How many channels do we plan to support?
I am sorry if I am overthinking, not very focused right now!

OK for the -d, could the -n go in the Csound Settings? Could be useful to see tables ;D

This is commented because I tried some old code and the --ksmps=32 sometimes was breaking the audio result. I saw that it was sounding better if the csds were setting the SR and ksmps, so I left it commented.

I don’t know, would there be cases in which I want to use a sample + csd at a different SR from the game? I didn’t test this really, so I’m not aware of any problems.
So could it be useful to make the user choose, adding a SampleRate and KSMPS setting in the Inspector? (with default settings of course)
Again, is this too much?
Just thinking :thinking:

Yeah, this is how the internal private method will probably look. And we just expose simple wrappers to it.

That’s exactly what I was thinking.

I think for now, mono and stereo. If users want multichannel they can ask us, or go about it themselves. The Csound/C# code is there. If they implement it well they can make a PR :laughing:

I’m not sure we want to give people the chance to launch FLTK stuff from Unity. Half the time one can’t even close the flkt window :confounded:

I can’t think of any good reason to use different sampling rates. Can you imagine the number of potential threads with subjects of ‘samples playing back at wrong pitch’. In fact, that’s another thing. diskin2 will do sample rate conversion on the fly, but our new table reading UDOs won’t. I’ve put in a note saying audio rates for files must match that the project and .csd, otherwise you will have problems with playback rates.

This seems fine to me. We set it up by default, but allow power-users the chance to disable it if they need to. I might get some time on this tomorrow again. I’ll keep you posted.

1 Like

Btw, I just pushed some stuff through, even though it’s not quite finished yet. I wanted to show Steven and some of the other Csound guys the work we’ve been doing. I just wanted to let you know…

1 Like

In the end I didn’t get to show them, but the table read example is now committed, so you can check that out. I did ask about any way to create a reference to a sound file that exists only in memory, but it seems like it might too specific for a general opcode. A custom opcode might be the way to go. But I think for now, so long as you don’t mix and match sampling rates between the project and your audio files, it should be Ok. Actually, I don’t even know if Unity does this kind of automatic sample rate conversion? If you try to play a 48kHz sample in a project that is 44kHz, does unity automatically correct the rate?

It’s great to know what the Csound devs think about the whole project and how it could be improved!
Happy if they give us some feedback.
We surely have to focus on usability, the file loading thing is a crucial aspect of the package.
I’ll try the sample as soon as I can.

Yes a custom UDO like yours is the answer. Should we integrate it inside the CsoundUnity class as a static method maybe, or is it better to have it as a utility? Or As a menu item? Don’t know. I’d like to have it as a drag&drop feature or something. Maybe a generic script that does all the loading combinations and sends the tables to CsoundUnity could be the key.

Yes, you can mix audio files with different samples rates and they will play at the correct rate.

Fixed the CsoundFileWatcher issue, just a path problem ("\" vs “/”)

1 Like

Now I’m trying to build on my brand new Oculus Quest!
It builds (I had to do just some minor changes), but we have a problem with SetInputSample (if the Process Clip Audio is enabled) but also with SetOutputSample, and then the app crashes:

2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #02 CsoundUnity:SetInputSample (int,int,single) <0x4b>

SET INPUT SAMPLE

0001/01/01 00:00:00.000 -1 -1 Info --------- beginning of crash
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime FATAL EXCEPTION: UnityMain
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Process: com.Csound.CsoundPackage_DevelopmentProject, PID: 15406
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime java.lang.Error: FATAL EXCEPTION [UnityMain]
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Unity version : 2019.3.9f1
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Device model : Oculus Quest
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Device fingerprint: oculus/vr_monterey/monterey:7.1.1/NGI77B/655140.23520.0:user/release-keys
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Caused by: java.lang.Error: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Version ‘2019.3.9f1 (e6e740a1c473)’, Build type ‘Release’, Scripting Backend ‘mono’, CPU ‘armeabi-v7a’
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Build fingerprint: ‘oculus/vr_monterey/monterey:7.1.1/NGI77B/655140.23520.0:user/release-keys’
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Revision: ‘0’
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime ABI: ‘arm’
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Timestamp: 2020-06-16 16:31:59+0200
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime pid: 15406, tid: 15446, name: FMOD mixer thre >>> com.Csound.CsoundPackage_DevelopmentProject <<<
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime uid: 10068
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime Cause: null pointer dereference
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime r0 00000000 r1 00000000 r2 00001e28 r3 00000000
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime r4 00000000 r5 d5827f70 r6 d41ff178 r7 00000000
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime r8 d30d3634 r9 d30ed090 r10 d356df28 r11 d41febf8
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime ip 40e77000 sp d41febf8 lr d2613c24 pc ced600fe
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime backtrace:
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #00 pc 001ca0fe /data/app/com.Csound.CsoundPackage_DevelopmentProject-1/lib/arm/libcsoundandroid.so (csoundSetSpinSample+14) (BuildId: 2a9cb8e8297b697cdf82f7cf85f873f20ac88815)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #01 pc 0000bc22 anonymous:d2608000
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime managed backtrace:
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #00 (wrapper managed-to-native) csoundcsharp.Csound6/NativeMethods:csoundSetSpinSample (intptr,int,int,single)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #01 CsoundUnityBridge:SetSpinSample (int,int,single) <0x43>
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #02 CsoundUnity:SetInputSample (int,int,single) <0x4b>
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #03 CsoundUnity:ProcessBlock (single[],int) <0x283>
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #04 CsoundUnity:OnAudioFilterRead (single[],int) <0x33>
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime #05 (wrapper runtime-invoke) :runtime_invoke_void__this___object_int (object,intptr,intptr,intptr)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at libcsoundandroid.csoundSetSpinSample(csoundSetSpinSample:14)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at csoundcsharp.Csound6.NativeMethods.csoundSetSpinSample (intptr,int,int,single)(Native Method)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at CsoundUnityBridge.SetSpinSample (int,int,single)(0x43:67)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at CsoundUnity.SetInputSample (int,int,single)(0x4b:75)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at CsoundUnity.ProcessBlock (single[],int)(0x283:643)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at CsoundUnity.OnAudioFilterRead (single[],int)(0x33:51)
2020/06/16 16:31:59.646 15406 15420 Error AndroidRuntime at .runtime_invoke_void__this___object_int (object,intptr,intptr,intptr)(Native Method)

2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #02 CsoundUnity:GetOutputSample (int,int) <0x2f>

SET OUTPUT SAMPLE

2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime FATAL EXCEPTION: UnityMain
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Process: com.Csound.CsoundPackage_DevelopmentProject, PID: 16623
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime java.lang.Error: FATAL EXCEPTION [UnityMain]
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Unity version : 2019.3.9f1
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Device model : Oculus Quest
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Device fingerprint: oculus/vr_monterey/monterey:7.1.1/NGI77B/655140.23520.0:user/release-keys
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Caused by: java.lang.Error: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Version ‘2019.3.9f1 (e6e740a1c473)’, Build type ‘Release’, Scripting Backend ‘mono’, CPU ‘armeabi-v7a’
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Build fingerprint: ‘oculus/vr_monterey/monterey:7.1.1/NGI77B/655140.23520.0:user/release-keys’
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Revision: ‘0’
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime ABI: ‘arm’
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Timestamp: 2020-06-16 17:03:14+0200
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime pid: 16623, tid: 16741, name: FMOD mixer thre >>> com.Csound.CsoundPackage_DevelopmentProject <<<
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime uid: 10068
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime Cause: null pointer dereference
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime r0 00000000 r1 00000000 r2 00001e2c r3 00000002
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime r4 00000000 r5 d5827f70 r6 d95ff178 r7 00000000
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime r8 d301fff4 r9 d30f4090 r10 d34f0f28 r11 d95fed50
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime ip d96f7d48 sp d95fec40 lr d96f7d58 pc d937815a
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime backtrace:
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #00 pc 00e1015a anon_inode:dmabuf
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime managed backtrace:
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #00 (wrapper managed-to-native) csoundcsharp.Csound6/NativeMethods:csoundGetSpoutSample (intptr,int,int)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #01 CsoundUnityBridge:GetSpoutSample (int,int) <0x27>
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #02 CsoundUnity:GetOutputSample (int,int) <0x2f>
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #03 CsoundUnity:ProcessBlock (single[],int) <0x307>
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #04 CsoundUnity:OnAudioFilterRead (single[],int) <0x33>
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime #05 (wrapper runtime-invoke) :runtime_invoke_void__this___object_int (object,intptr,intptr,intptr)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at anon_inode:dmabuf.0xe1015a(Native Method)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at csoundcsharp.Csound6.NativeMethods.csoundGetSpoutSample (intptr,int,int)(Native Method)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at CsoundUnityBridge.GetSpoutSample (int,int)(0x27:39)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at CsoundUnity.GetOutputSample (int,int)(0x2f:47)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at CsoundUnity.ProcessBlock (single[],int)(0x307:775)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at CsoundUnity.OnAudioFilterRead (single[],int)(0x33:51)
2020/06/16 17:03:14.291 16623 16637 Error AndroidRuntime at .runtime_invoke_void__this___object_int (object,intptr,intptr,intptr)(Native Method)

Is this only happening when you have process audio clip enabled?