Cabbage Logo
Back to Cabbage Site

CsoundUnity examples

In a Resources folder inside the folder of the sample folder we load through the package manager. The advantage of Resources folder is that we can create as many of them as we need.

Yeah, I was just doing that now but wonā€™t have time to finish it. It would be easiest I think to create a CsoundUnity method like:

public void CreateFunctionTable(AudioClip clip, int tableNumber)
{
       string createTableInstrument = String.Format(@"
schedule 9999, 0, 0
instr 9999
i1 ftgen {0}, 0, {1}, -2, 0, {1}, 0
endif", 
        tableNumber, file.length*AudioSettings.outputSampleRate);
   CompiledOrc(createTableInstrument);
   //wait some moments and then call:
   CopyTableIn(tableNumber, clip.getData());
}

Then users would only have to ensure that they start any instruments that use these tables a little after they create the tables.

Perfect!
Iā€™ll add this along the utility method to retrieve the samples of a clip as a MYFLT array.

Great. Iā€™ve no called compileOrc() from CsoundUnity but I think this would the cleanest way, although it will end up running an instrument that users will not have declaredā€¦?

Yes Iā€™ll add the CompileOrc method too.
Could schedule be called outside an instrument?

Yeah, it can be called like I have it in the rough example I sent. Easier than sending a score eventā€¦

Ok just pulled the update, later Iā€™m going to test it in the Sequencer scene!

Now that I have values inside the tables, and they have raw samples data, what opcode to use to read all audio channels accordingly?
Should we pass an argument to CsoundUnity.CreateTable method specifying the number of channels of the samples?
So maybe itā€™s better to create one or more tables based on the channel numbers, or is it ok to store raw data like I am doing now?
Because I didnā€™t find how to read data from the tables I created.
Tried with loscil3 but it says:

stereo loscil cannot read from mono ftable

I created the table with GEN02 with orc like this (I thought there was no reason to use an instrument since ftgen works also in global space):

gisampletable900 ftgen 900, 0, -165375, -2, 0, 0

this is the csound code to play a sample, I just changed yours

instr PlaySample
    a1, a2 loscil3 1.0, 1, giTable[p4]
    outs a1*.1, a2*.1
endin

:thinking:

EDIT:

I forgot:

giTable reads a float with the table number to read from:

giTable[0] chnget "sampletable900"

and I set a table and a channel for every AudioClip I have in Sequencer inspector:

var tn = 900 + count;
var res = csoundUnity.CreateTable(tn, samples);
csoundUnity.SetChannel($"sampletable{tn}", tn);

so when count is 0 tn is 900

EDIT of the EDIT:
I swear I read all this thread: Reading speed of a GEN table but thereā€™s no mention about a table filled externally. I canā€™t use GEN01!

Any table reading opcode should work once the vales are in the table?

Iā€™ll need to double check this. loscil uses GEN01, and Iā€™m not sure we can create a gen01 as we donā€™t have a file nam to use. We may just have to create a table for each channel in our sound file?

I actually donā€™t think it makes any difference which gen02 routine we use as we just overwrite the data anyway. One thing we need to check is that non power of2 sized tables are allowedā€¦

I wonā€™t get a chance to look into this till later, or most likely tomorrow at some point :frowning:

Yes but I suppose I should do some computation to retrieve the single channels, if multichannel

That seems the easiest way to do this.

True!

Yes this is allowed setting the value of the samples as a negative number
from: http://write.flossmanuals.net/csound/d-function-tables/

size : the size of the function table. A little care is required: in the early days of Csound only power-of-two sizes were possible for function tables (2, 4, 8, 16, ā€¦); nowadays almost all GEN Routines accepts other sizes, but these non-power-of-two sizes must be declared as negative numbers!

Iā€™ll try to do my best for a couple of hours, then Iā€™ll be busy until next tuesday.
We already did a lot, no hurry! :wink:

No problem. Thanks again for all the work youā€™ve done with this. Donā€™t forget to drop in every now and then in case I have a list of questions for you :laughing:

For sure, this is forum has become my favourite reading!
And itā€™s a pleasure working with you on CsoundUnity, something I wanted to do since I discovered it!
I have lots of projects in mind that will use it!

I did it with the perfect example I found here:
http://www.csounds.com/manual/html/nsamp.html
Iā€™ll push the updated Sequencer example in some minutes :wink:
Edit: done!
(maybe needs some polish with levels and playback duration of the sample)
Let me know!

nsamp. Thatā€™s a new one one me. Iā€™d have used ftlen but I see there are subtle differences between then. I hope to get a chance today to look at this.

I guess there are loads of updates we could make, but at some point we will ruin the simplicity of the demo? I had hoped to add a first person shooter mechanism so there is an element of play. It is a game engine after all :wink:

Hi @rorywalsh, @giovannibedetti, Iā€™m just finishing up the mechanics for my little example and about to move onto the CSound implementation. Iā€™m trying to use the JM_Sub synth by Karamel (which is awesome btw) but noticed it reads in HZ instead of MIDI notes which seems odd as it does work when using my midi controller in cabbage. Whatā€™s the best way to edit the instr to register midi notes?

There are various converters in csound between midi and frequency, have a look at http://www.csounds.com/manual/html/midinoteoncps.html
Or
http://www.csounds.com/manual/html/cpsmidi.html

But in general, there would be no problem in passing a frequency as a parameter of the score.
I cannot look at the csd now (I am travelling with train) but if you could explain what kind of problem you have Iā€™ll try to help if I can!

I can take a look later if @giovannibedetti canā€™t help you, sorry but Iā€™m not at home either and am writing on my phoneā€¦

Thatā€™s great, Iā€™ll take a look at those docs.
Iā€™d prefer to use MIDI notes because the game Iā€™m making will select notes mathematically, and it will make things a lot easier if I can stick to a 12 int octave.

Iā€™ve got the score event set to instrument 1 and get the correct sound result, but Iā€™m having to send it the note in frequency. So basically I just want to be able to send ā€œi1 0 1 60 1ā€ instead of ā€œi1 0 1 261.6 1ā€.

It should be a matter of adding the conversion from midi note to cps to p4 at line 902, 903, 905 and 906.
Sorry but I cannot help more than this now, maybe tomorrow Iā€™ll find some time!

1 Like

If you post the .csd file you are using I can update it later to work with midi notes. Or is it the same one @Karamel1 posted to the list here? That was a very nice synth!