Cabbage Logo
Back to Cabbage Site

CsoundUnity CopyTableOut not working [SOLVED]

Hi all,

I’m making some experiments with the aim to create some examples for CsoundUnity, in this case i’m trying to transfer data using tables.
I’m trying to continuously fill a table with new values in Csound, and reading this changed values from Unity.
This is not working, just the first two values of the table are updated, whichever size it is (I tried creating it in the score with “f”, with ftgen, and also from CsoundUnity with CreateTable/CreateTableInstrument).
What I noticed is that if I create the function with “f” the len is always -1, meaning that the table doesn’t exist!

This is the code which should copy the content to a destination array:

public void TableCopyOut(int table, out MYFLT[] dest)
{
    int len = Csound6.NativeMethods.csoundTableLength(csound, table);
    if (len < 1)
    {
        dest = null;
        return;
    }

    dest = new MYFLT[len];
    IntPtr des = Marshal.AllocHGlobal(sizeof(MYFLT) * dest.Length);
    Csound6.NativeMethods.csoundTableCopyOut(csound, table, des);
    Marshal.Copy(des, dest, 0, len);
    Marshal.FreeHGlobal(des);
}

What could be wrong with this?
As far as I remember, this was working, we tested it in the past :thinking:

This is the Csound code I’m using:

instr 99

;generate the table
giFt      ftgen     100, 0, 256, -2, 0;

;test code to see if the table is updated with noise
kbeta line -0.9999, p3, 0.9999	;change beta value between -1 to 1
asig  noise .3, kbeta
asig  clip asig, 2, .9	;clip signal

ilen = ftlen(100)
prints "ilen 100: ", ilen

aph phasor sr / 256
;kpos = k(aph)
;printk2 kpos
;tablew	asig, aph, 100, 1
tabw asig, aph, 100

endin

Probably there is some mistake here so that the table is not updated, but printing kpos reports 8 values as expected (0.00000, 0.12500, 0.25000, 0.37500, 0.50000, 0.62500, 0.75000, 0.87500).
Any suggestion?

You using the dev branch yeah? You shouldn’t really call ftgen inside an instrument block, it should go into the header. In the Csound code listed you are only writing two values. If you want to use a phasor you should set the table mode to 1. Right now it is expecting values in the range of 0 to 256. You have done this in the commented tablew line, but not for the tabw one.

Thanks Rory!
I’m on the develop branch.
Yes I’m definitely writing just two values:

.
I tried with ftgen inside/outside the instr, it’s the same (but just read it’s not officially supported).
Tried adding 1 at the end and now it works. Tried again with tablew and it works too :thinking: (I’m a bit confused)
But at least the good news is that the table reading works as expected! :wink:

There are two modes for writing values to a table with the table opcodes. Mode 0 means you must pass absolute indices, 0 to the table length. Mode 1 means you can pass a normalised values between 0-1 instead, with 0 being the start of the table and 1 being the very end.

I’m glad it’s running now. :wink:

1 Like

I’m a bit confused because I tried creating a table with CreateTable and CreateTableInstrument and wasn’t able to write the values there with tablew or tabw. Are we forced to use gen02 for this?
In this case we could create a method for CsoundUnity: CreateEmptyTable (CreateTableInstrument creates an empty table but with gen 07), or add a parameter (with a default value) to CreateTableInstrument to choose the gen. Is this the case?

No, it shouldn’t matter what gen routine is used. You can write values directly to any table regardless of what GEN was used.

1 Like

Again on my experiments with tables :wink:
I’m now creating a table with CreateTableInstrument, without initialising its values. I then try to get a value from that table. Unity crashes miserably (this is expected, of course). I’m using CsoundUnity.GetTableSample, which calls the native function csoundTableGet (which states that the index should be valid, which it is, but I assume it needs to be initialised to be able to get it): is there a way to detect that the table has not been initialised, hence avoiding the crash? Getting its length works fine.

So you’re now checking the table length first before you call csoundGetTable()? And this works Ok?

Yes the length of the table returns correctly, while asking for a value at an index of the not initialised table causes Unity to crash with Attempt to access invalid address

1 Like