[Csnd] chnset understanding problems

hello everybody,

i have a problem with using the chnset and chnget opcodes.

i want to send an array of k values with chnset to a destination which also expects a k-array.
but the console tells me i do something wrong with at leats chnset and the examples in the manual did not help me.

this is my sketch. what do i do wrong?

// sending
  kMidiData[] ctrlsave 1,\
    0,1,2,3,4,5,6,7,8,9,10,\
    11,12,13,14,15
  chnset kMidiData, "midiData_faderfoxec4“

// receiving
  kPreset ctrlpreset p4, chnget("midiData_faderfoxec4")

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

i don't think you can send / receive an array.
rory added the array version, but is has different names:
https://csound.com/docs/manual/chnset.html

best -
  joachim

The test csd is here:

https://github.com/csound/csound/blob/develop/tests/soak/channelArrayOpcodes.csd

From that .csd:

instr 1; send k-values
SChan init 2
SChan[0] = “kChan1”
SChan[1] = “kChan2”
kArr fillarray 1, 2
chnsetk kArr, SChan
endin

If I understand correctly the example above arrays are “unsymmetric”. Is it so that an array “kArr” in Csound requires separate
scalar channels “kChan1” and “kChan2” as a counterpart in e.g. java-code ?

Is it possible that both sides, Csound and e.g. java, could be arrays ?

–Risto

ke 9. lokak. 2024 klo 18.17 Rory Walsh (rorywalsh@ear.ie) kirjoitti:

The test csd is here:

https://github.com/csound/csound/blob/develop/tests/soak/channelArrayOpcodes.csd

i don’t think you can send / receive an array.
rory added the array version, but is has different names:
https://csound.com/docs/manual/chnset.html

best -
joachim

hello everybody,

i have a problem with using the chnset and chnget opcodes.

i want to send an array of k values with chnset to a destination which also expects a k-array.
but the console tells me i do something wrong with at leats chnset and the examples in the manual did not help me.

this is my sketch. what do i do wrong?

// sending
kMidiData ctrlsave 1,
0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15
chnset kMidiData, "midiData_faderfoxec4“

// receiving
kPreset ctrlpreset p4, chnget(“midiData_faderfoxec4”)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

I made an opcode pair (ref/deref) which can be used to pass an array by reference (https://csound-plugins.github.io/csound-plugins/opcodes/ref.html). In this case you can use a scalar channel:

--nosound sr = 44100 ksmps = 64 nchnls = 2 0dbfs = 1

instr 1
karr[] fillarray 1, 2, 3, 4
chnset ref(karr), “mychan”
turnoff
endin

instr 2
karr2[] deref chnget(“mychan”)
printarray karr2
turnoff
endin

i 1 0 1
i 2 0 1

These opcodes are using normal control channels. In Csound 7 there are array channels, so complete arrays can be passed in and out of channels.

Prof. Victor Lazzarini
Maynooth University
Ireland

Thanks Eduardo
This seems to be half of the solution. But what does this mean in java-code …

I suppose you can do almost any kind of tricks utilising pointers and casting in c-code.

I’m not sure how to do the same in java…
Here are some pieces of code in java and the question after that.

First object creation:

CsoundMYFLTArray CtNbrChannel = new CsoundMYFLTArray();

c.GetChannelPtr(CtNbrChannel.GetPtr(), “CtNbr”, …

And usage, setting a single value e.g.:

CtNbrChannel.SetValue(0, (double) 0);

As you can guess the Channel requires double as the actual value java data-type and some
integer argument 0.
The java-help (in Eclipse) tells about that method:
void csnd6.CsoundMYFLTArray.SetValue(int arg0, double arg1)

So now the question is how is it possible to feed a double-array as an argument instead of a double scalar here ?

One guess that I have in mind is that it could be enough to use the 1’st element of a double array as the value-argument.
If the internal behaviour of the API is such that only pointers are in use, then referring to the first element of a double-array
would point to the whole array at the same time …hmmm, just guessing.

Then I could have in java:

// an array definition

double Cnbr = { 71,72,73,74,75,76,77,78 };

And usage:

CtNbrChannel.SetValue(0, Cnbr[0]);

Still wondering what should the value of the arg0 ?

Does anyone have better information about this ?

–Risto

pe 11. lokak. 2024 klo 0.54 Eduardo Moguillansky (eduardo.moguillansky@gmail.com) kirjoitti:

I made an opcode pair (ref/deref) which can be used to pass an array by reference (https://csound-plugins.github.io/csound-plugins/opcodes/ref.html). In this case you can use a scalar channel:

--nosound sr = 44100 ksmps = 64 nchnls = 2 0dbfs = 1

instr 1
karr fillarray 1, 2, 3, 4
chnset ref(karr), “mychan”
turnoff
endin

instr 2
karr2 deref chnget(“mychan”)
printarray karr2
turnoff
endin

i 1 0 1
i 2 0 1

hello everybody,

i have a problem with using the chnset and chnget opcodes.

i want to send an array of k values with chnset to a destination which also expects a k-array.
but the console tells me i do something wrong with at leats chnset and the examples in the manual did not help me.

this is my sketch. what do i do wrong?

// sending
kMidiData ctrlsave 1,
0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15
chnset kMidiData, "midiData_faderfoxec4“

// receiving
kPreset ctrlpreset p4, chnget(“midiData_faderfoxec4”)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Thanks Eduardo for this link!
Looks very interesting!

Does this mean i can pass a array to an UDO and change this array outside and get these changes updated inside an UDO?

Do i need to deref the array inside the UDO?

Greetings,
Philipp

Thanks Eduardo for this link!
Looks very interesting!

Does this mean i can pass a array to an UDO and change this array outside and get these changes updated inside an UDO?

Yes, they share the same memory. In particular, any change to the array inside the UDO will be reflected in the original array.

opcode arrayadd_inplace, 0, ii
  ; in place 
  iref, ix xin
  iIn[] deref iref
  iIn += ix
endop

You would call this with

iMyArray[] fillarray 0, 1, 2, 3

arrayadd_inplace(ref(iMyArray), 5)

After this iMyArray would hold 5, 6, 7, 8

Do i need to deref the array inside the UDO?

Greetings,
Philipp

I made an opcode pair (ref/deref) which can be used to pass an array by reference (https://csound-plugins.github.io/csound-plugins/opcodes/ref.html). In this case you can use a scalar channel:

--nosound sr = 44100 ksmps = 64 nchnls = 2 0dbfs = 1

instr 1
karr fillarray 1, 2, 3, 4
chnset ref(karr), “mychan”
turnoff
endin

instr 2
karr2 deref chnget(“mychan”)
printarray karr2
turnoff
endin

i 1 0 1
i 2 0 1

hello everybody,

i have a problem with using the chnset and chnget opcodes.

i want to send an array of k values with chnset to a destination which also expects a k-array.
but the console tells me i do something wrong with at leats chnset and the examples in the manual did not help me.

this is my sketch. what do i do wrong?

// sending
kMidiData ctrlsave 1,
0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15
chnset kMidiData, "midiData_faderfoxec4“

// receiving
kPreset ctrlpreset p4, chnget(“midiData_faderfoxec4”)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

FYI: the equivalent code in CS7 using new-style UDOs which implement
pass-by-ref:

opcode arrayadd_inplace(arrayRef:i[], ix):void
  arrayRef += ix
endop

instr 1
  myArray:i[] = [0, 1, 2, 3]
  printarray(myArray)
  arrayadd_inplace(myArray, 5)
  printarray(myArray)
endin

prints the following:

0.0000 1.0000 2.0000 3.0000
5.0000 6.0000 7.0000 8.0000

thanks for the very instructive example!!
  j

ah, csound 7 looks very promissing!
looking forward to the release!

Steven, thanks for the clarification regarding pass by ref in csound7. Is there any reference regarding this anywhere? In particular, are return arrays also passed by ref? What happens if an udo changes the size of an array? And the last: is there any mechanism to pass an array to an instr?

That reduction in boilerplate is great! The code looks nice and succinct.

Is there any form of documentation for csound7 yet I can take a look at?

Cheers

I don’t think this is exactly reduction in boilerplate but something that offers other
styles of programming which we did not before. It is important to know that with this
we have a situation where side-effects are explicit and it may require some careful
thought.

Regarding documentation, we don’t have anything beyond the CSD examples under
tests/commandline but hopefully we will have something in the new manual that is
being worked at slowly.

Hi Eduardo,

At the moment the implementation is merged into the develop branch and
can be used with CS7 build. There's an open issue to optimize the
parsing out of xin/xout for pass-by-ref UDOs, but the main mechanism
to search opcodes for vars that match xin/xout and set the pointers is
working and should work for returned arrays. I have not tested for
array size changes but *should* work fine. I think at this point it's
in a state where getting users to test it out and help with
identifying any remaining issues would be invaluable.

As of the moment, pass-by-ref will be used when using new-style UDOs
with the exception that if there is a difference in local sr or ksmps
compared to the parent (i.e., calling code), it will fall back to pass
by value. There's an opportunity to do some optimization there but it
was the safest thing to do right now considering the complexity in
that area of processing.

As for passing arrays to instruments, there's no mechanism at the
moment. There's been a long-standing todo to modify the event data
structure to use CS_VAR_MEM instead of numeric pointers (+ magic code
for strings) for pfields. I'm unsure about the implications for memory
allocations and runtime but we would need to go this route if we want
to pass any data type as a pfield argument.

Let me know if you have any further questions!

Steven

Hey Steven

If in any case you’d want to pass an array by value to a UDO in CS7, would you just use the old-style UDO?

This is a great development in any case!

Cheers