Cabbage Logo
Back to Cabbage Site

CsoundUnity Package (UPM) development

Yes, but I added this basically to test if the FileWatcher was working. Then adding the button to save was a matter of seconds. It is just a fast way to read and edit the csd directly in Unity.

Good idea. Yes, itā€™s cool, letā€™s keep it :wink:

I tried a simple command line application to test csoundGet0dbfs() and it works fine. :thinking: We must be doing something wrongā€¦

[edit] we should probably add --realtime when compiling Csound? Seems like itā€™s something that should always be on?

:laughing: Itā€™s because we are overriding 0dbfs. No need to do that anymore. I think we scale all Csound samples by zerbdfs right?

Iā€™m almost sure it doesnā€™t work also if itā€™s not override by SetParams. But I could be wrong. Great if itā€™s working.
We are now scaling everything by 0dbfs but we are not scaling the AudioChannels, we are getting the samples with

public MYFLT[] GetAudioChannel(string name)

do you think we should scale also this?

We scale them here:

switch (AudioChannelsSetting)
                {
                    case AudioChannels.MONO:
                        samples[i + channel] = (float)(namedAudioChannelData[0][sampleIndex] / zerodbfs);
                        break;
                    case AudioChannels.STEREO:
                        samples[i + channel] = (float)(namedAudioChannelData[(int)channel][sampleIndex] / zerodbfs);
                        break;
                }

That should be fine. So if someone does send a sample with amp 32000, it will get scaled accordingly right?

Btw, I just pushed the 0dbfs thingā€¦

Fine, I just looked at the CsoundUnity part, instead of looking at the child :see_no_evil:
But maybe is more correct to have it scaled on the CsoundUnity part? the user could want to obtain the AudioChannel for other computations (see visuals), maybe itā€™s better having all normalised?

Good point. I think youā€™re right :wink:

1 Like

I have another point ā€¢ :grin:
We should find an easy way to load samples, the table thing is great, but I am having some difficulties using that table because itā€™s not recognized as stereo by the table reading opcodes, so I have to calculate the reading speed and position multiplying the table length by the channels.
Also, the table is not accepting long files (Iā€™ll tell the max value), and this is a limitation.
Now a Start method of a user script could be like this:

IEnumerator Start()
{
    csound = GetComponent<CsoundUnity>();
    if (!csound)
        Debug.LogWarning("Csound not found?");

    while (!csound.IsInitialized)
    {
        yield return null; //waiting for initialization
    }
    // this gets the samples from the AudioClip field
    var samples = CsoundUnity.GetSamples(source.name, CsoundUnity.SamplesOrigin.Resources);
    // this creates the table with those raw samples (no info about channels)
    csound.CreateTable(100, samples);
}

and this is great.
On the Csound part I have to do something like this:

    instr 2
	  
  
		ifn   = p5
		ilen = ftlen(ifn)	 	 
		itrns = nchnls  ; no transposition
		itrns *= i(gkspeed)		
		ilps  =  int(p4 * sr * nchnls) % ilen ;start
		idursam = p3 * sr * nchnls            ;duration in samples
		ilpe  =  int(ilps + idursam) % ilen   ;end	
        imode =  1	; loops forward
 		istrt =  ilps		
 		;lphasor provides index into f1 			
 		alphs lphasor itrns, ilps, ilpe, imode, istrt
 		atab  table  alphs, ifn
 		aenv  linseg 0,	p3/10,	p6, (p3 * 4 / 5),	p6,	p3 / 10,	0
 		atab *= aenv		 		   
       
        outs atab, atab
 	
endin

which is fine as well, but for example loscilx http://www.csounds.com/manual/html/loscilx.html isnā€™t working with the table, telling that the table has no channels. Lots of opcodes that read audio files can produce multiple audio vars based on channels. And lots of code should be adapted.
Also pitch shifting is not available, or I couldnā€™t find an easy way without having lots of noise.
What do you think?
What can we do to produce tables with channels? I didnā€™t find anything in the docs.

As far as I know, none of the table reading opcodes will read in in stereo. But I see your point. However, I donā€™t know of any way to dynamically create a GEN01 table from data. I could write a new opcode for this? But it would be best to find a native solution. Let me ask on the Csound slack channel. Steven Yi is usually pretty quick to reply, and he is aware of what weā€™re atā€¦

loscilx ā€” Read multi-channel sampled sound from a table.

Description

Read sampled sound (up to 16 channels) from a table, with optional sustain and release looping.

I never used it, but it seems good!
Should work with GEN01

Greetings to Steven!

Before I ask Steven, couldnā€™t we do this ourselves, without using loscilx?

Letā€™s say we use the first index of our table to denote the number of channels. After that we write our interleaved data from Unity to the table. Our instrument reads the first index of the table to get the number of channels and then after that parses the data accordingly, outputting channels data for each channel?

It would need to wrapped up as a UDO, but we can dynamically add the UDO to the orchestra when we compile Csound?

Yes great!
What would be the max duration of the file?
To be honest, the easy way already exists: put the AudioClip in the AudioSource, enable ProcessClipAudio, and in Csound:

ainL, ainR inch 1, 2

But this gives no control on fine audio playback

I think tables have a fixed upper limit, but Iā€™m not sure what it is.

True. But to build samples and things like that itā€™s not so useful. I think this way should work. I will take a look at it when I get a chance. Probably tomorrow morning. :wink:

Ok no hurry, Iā€™m just having fun creating an example about Granular Synthesis :slight_smile:

Here is a test:
A stereo file of 2 mins would be 10584000 samples.
When I try to load with create table I obtain maximum 6008184 samples, which is about 68 seconds.

Strange. Iā€™ll check with the Csound devsā€¦

Maybe thereā€™s some error with the ftgen code I use to create it (using negative GEN2)
The table is created and then the samples are overwritten by CopyTableIn :thinking:

Can you try with another GEN routine? Say GEN07?

Just tried with this code:

public int CreateTableInstrument(int tableNumber, int tableLength/*, int nChannels*/)
{
    string createTableInstrument = String.Format(@"gisampletable{0} ftgen {0}, 0, {1}, -7, 0, 0", tableNumber, -tableLength /** AudioSettings.outputSampleRate*/);
    // Debug.Log("orc to create table: \n" + createTableInstrument);
    return CompileOrc(createTableInstrument);
}

in the logs I have:

ftable 100:	6008184 points, scalemax 0.000

printing table length:

[2.000000] new instr 2 instance ilen 6008184, P2 2.000000, P3 0.100000, P4 2.000000, P5 100.000000, P6 1.000000

Edit, I think itā€™s my problem, let me investigate
PS Thatā€™s the length I pass to the function :see_no_evil: