[Csnd] wonky array access

The API is written in C and this is the reference

https://csound.com/docs/api/index.html

There is a thin C++ wrapper to these functions

https://github.com/csound/csound/blob/csound6/include/csound.hpp

There are Java bindings to the C/C++ API generated by SWIG. Since Java cannot deal with things like pointers and callbacks, there is extra code to wrap these in cs_glue.hpp/cpp. These are not part of the API and are not documented beyond the comments in the header file.

The problem I pointed out in the code you asked about is that you were not using the wrapper class correctly. This was indicated by the error message

If you look at the header file you should be able to see what you need to make it work. If you want to know the parameters for csoundScoreEvent() you look at the API reference. The types reported in Eclipse are SWIG-created to accommodate the bindings,
and if there are C pointers involved as in this case, you will need to use the wrapper.

CsoundMYFLTArray::GetPtr(int index) is probably what you need, but don’t take my word for it, check the header files.

Aside from this, a look at Java examples in the CsoundAPIExamples repository seems to indicate that using ReadScore() is preferred to using ScoreEvent() in Java. Possibly because of the extra complication of using arrays/pointers.

Prof. Victor Lazzarini
Maynooth University
Ireland

Thanks for answering and patience !

I noticed somewhere that SWIG is in use. Because I’m not familiar with
that tool I didn’t realize that there is no explicit java-code for the interface at all.
The whole java-side is generated.
I have been working earlier in my work-life only in such cases where c-code has
been tied to java by JNI with explicit bindings in java-code.

I actually succeeded to get the whole code working by this way
(in case anyone is interested … probably not).
The ScoreEvent below is inside a loop in the actual code.

CsoundMYFLTArray pp = new CsoundMYFLTArray(3); // p1,p2,p3

pp.SetValue(0, 2); // the instr 2
pp.SetValue(1, 0); // immediately, no delay
pp.SetValue(2, 0.011); // duration 11ms, (note! krate = 100 (=10ms))

SWIGTYPE_p_double aa = pp.GetPtr(0); // 0 was the important index number

pt.ScoreEvent(0, ‘i’,3, aa ); // 0 is important, the action happens immediately

The actual question is still open related to the duration parameter, which is here 11ms, just over 10ms.
I asked about the possibility get exactly one krate length execution of the instrument,
but I think I can live without this possibility by adding some more logic in csound-part.

–Risto

ke 5.3.2025 klo 19.51 Victor Lazzarini (000010b17ddd988e-dmarc-request@listserv.heanet.ie) kirjoitti:

The API is written in C and this is the reference

https://csound.com/docs/api/index.html

There is a thin C++ wrapper to these functions

https://github.com/csound/csound/blob/csound6/include/csound.hpp

There are Java bindings to the C/C++ API generated by SWIG. Since Java cannot deal with things like pointers and callbacks, there is extra code to wrap these in cs_glue.hpp/cpp. These are not part of the API and are not documented beyond the comments in the header file.

The problem I pointed out in the code you asked about is that you were not using the wrapper class correctly. This was indicated by the error message

If you look at the header file you should be able to see what you need to make it work. If you want to know the parameters for csoundScoreEvent() you look at the API reference. The types reported in Eclipse are SWIG-created to accommodate the bindings,
and if there are C pointers involved as in this case, you will need to use the wrapper.

CsoundMYFLTArray::GetPtr(int index) is probably what you need, but don’t take my word for it, check the header files.

Aside from this, a look at Java examples in the CsoundAPIExamples repository seems to indicate that using ReadScore() is preferred to using ScoreEvent() in Java. Possibly because of the extra complication of using arrays/pointers.

Prof. Victor Lazzarini
Maynooth University
Ireland

I don’t know if you noticed but i wrote e.g. ScoreEvent.
The comment was a general one for all java-methods.

I can see the type of each argument of any method in Eclipse,
but what is the purpose of each argument ?
Even the names of the arguments could help to me guess what
and where to put which values.

–Risto

P.S. Concerning this specific argument I suppose that
I need to use index value 0 to point in the first element of the array
in order to get the correct pointer value.

ke 5.3.2025 klo 16.58 Victor Lazzarini (000010b17ddd988e-dmarc-request@listserv.heanet.ie) kirjoitti:

Your problem is not with ScoreEvent, it is because you are not using CsoundMYFLTArray correctly. This is
in cs_glue.hpp.

========================
Prof. Victor Lazzarini
Maynooth University
Ireland

OK, to get the whole Csound seemed to be very simple. Just download the appropriate .gz-file and so on.
(So I didn’t really do a checkout)

Now if I try to find what are the bindings from e.g. ScoreEvent java-method to the corresponding c+±functions
I still can’t find anything. It seems that csnd6.jar java source code is not here, but where is that ?
For a person that understands a bit of java the java source code would probably clarify things a bit
more than c++ only.
Neither cs_glue.hpp or .cpp includes anything related to ScoreEvent.

–Risto

ke 5.3.2025 klo 11.52 vlz (viclazzarini@gmail.com) kirjoitti:
Well, learning how to use git is a good idea since it’s ubiquitous, and checking out the sources is not that much of an effort and it doesn’t take too much space
either.

Alternatively, you can look at the file online, https://github.com/csound/csound/blob/csound6/interfaces/cs_glue.hpp

Or you can do neither of these things, it’s up to you. You asked for help and I am suggesting where you could search for it.

Still scratching my head …
At https://github.com/csound/csound I found:
" Anyone seeking the latest 6.x version please checkout the csound6 branch (or the master branch, containing the latest and final release of this version"

Literally this means I should checkout the whole csound 6.x source-code to my PC in order
to just get usage information of the java-interface… really?

I don’t have any experience on working in github-enviroment. I used to work in svn-environment still a few years ago,
but I suppose that doesn’t help me at all here.

–Risto

ke 5.3.2025 klo 8.43 Victor Lazzarini (000010b17ddd988e-dmarc-request@listserv.heanet.ie) kirjoitti:
I mean ./interfaces in the csound github repo,
csound6 branch.

Prof. Victor Lazzarini
Maynooth University
Ireland

./interfaces ?
Do you mean https://csound.com/docs/api/interfaces.html or what ? (this doesn’t exist)

At the main page https://csound.com/docs/api/index.html there is the following text:

“The Csound Application Programming Interface (API) reference is contained herein. The Csound API actually consists of several APIs:
• The basic Csound C API. Include csound.h and link with libcsound.a. This also includes the Cscore API (see below).
• The basic Csound C++ API. Include csound.hpp and link with libcsound.a.
• The interfaces API, includes a number of auxiliary C++ classes, which add functionality and support the wrapping of the Csound API by various languages (e.g. Python, Java, Lua).”
Unfortunately only the first two items have a link forward, but the “interfaces API” doesn’t have any.
I’m a bit confused where to go now ?

–Risto

ti 4.3.2025 klo 22.58 Victor Lazzarini (000010b17ddd988e-dmarc-request@listserv.heanet.ie) kirjoitti:
it’s in ./interfaces. Look there you’ll get the relevant method.

Prof. Victor Lazzarini
Maynooth University
Ireland

Sorry, but I really don’t understand:
a) Where can I find that c++ file (c++ I suppose), this page
https://csound.com/docs/api/files.html
lists only these files:

cscore.h
csdebug.h
csdl.h
csound.h
csound.hpp
csound_threaded.hpp
csoundCore.h

b) Now I noticed that to get java to know that the type SWIGTYPE_p_double exists, this additional import was required
(how could I know this, because it can’t be found in any of the examples)

import csnd6.SWIGTYPE_p_double;

Now I can do something like:
CsoundMYFLTArray pp = new CsoundMYFLTArray(3);
pp.SetValue(0, 2);
pp.SetValue(1, 0);
pp.SetValue(2, 3);

SWIGTYPE_p_double aa = pp.GetPtr(3);

and then use that this way:

pt.ScoreEvent(0, ‘i’,3, aa );

There is still one question mark here, the value 3. Is it correct in .GetPtr(3) (= number of elements) or should it
“point” to the fist element of the pp array ?

c) Any idea why Edge-Copilot could not give a correct example here ? Maybe this type of NHI is
not intelligent enough :slight_smile:

–Risto

ti 4.3.2025 klo 19.50 Victor Lazzarini (000010b17ddd988e-dmarc-request@listserv.heanet.ie) kirjoitti:
pp is a wrapper around the array, the API function take a pointer instead. Look at cs_glue.hpp to see how to get it.

Prof. Victor Lazzarini
Maynooth University
Ireland

Warning
This email originated from outside of Maynooth University’s Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.
I tried to get an answer from HI (human intelligence) how to use ScoreEvent-method but
because of poor success I turned to NHI (non-human intelligence) in this case Edge-Copilot.

  1. I asked the following question:
    “can you find any example of java-code using csound-api ScoreEvent method?”
    And this was the answer:

import csnd6.csnd6;
import csnd6.Csound;
import csnd6.CsoundMYFLTArray;

public class CsoundExample {
public static void main(String args) {
csnd6.csoundInitialize(csnd6.CSOUNDINIT_NO_ATEXIT);

Csound csound = new Csound();
csound.CompileOrc(“instr 1\n a1 oscil p4, p5, 1\n out a1\n endin”);
csound.ReadScore(“i1 0 1 0.5 440”);

csound.Start();
csound.PerformKsmps();

// Create a score event
CsoundMYFLTArray pFields = new CsoundMYFLTArray(5);
pFields.SetValue(0, ‘i’);
pFields.SetValue(1, 1);
pFields.SetValue(2, 0);
pFields.SetValue(3, 1);
pFields.SetValue(4, 0.5);
pFields.SetValue(5, 440);

csound.ScoreEvent(‘i’, pFields, 6);

csound.PerformKsmps();
csound.Stop();
csound.Cleanup();
}
}

That looks actually quite clear, but it is related to Csound-type object.

  1. So asked another question:

“can you find any example of java-code using csound-api ScoreEvent method in the case of Performance thread ?”

Now the answer was this:

import csnd6.csnd6;
import csnd6.Csound;
import csnd6.CsoundPerformanceThread;
import csnd6.CsoundMYFLTArray;

public class CsoundPerformanceThreadExample {
public static void main(String args) {
csnd6.csoundInitialize(csnd6.CSOUNDINIT_NO_ATEXIT);

Csound csound = new Csound();
csound.CompileOrc(“instr 1\n a1 oscil p4, p5, 1\n out a1\n endin”);
csound.ReadScore(“i1 0 1 0.5 440”);

CsoundPerformanceThread perfThread = new CsoundPerformanceThread(csound);
perfThread.Play();

// Create a score event
CsoundMYFLTArray pFields = new CsoundMYFLTArray(5);
pFields.SetValue(0, ‘i’);
pFields.SetValue(1, 1);
pFields.SetValue(2, 0);
pFields.SetValue(3, 1);
pFields.SetValue(4, 0.5);
pFields.SetValue(5, 440);

perfThread.ScoreEvent(‘i’, pFields, 6);

// Wait for the performance to finish
perfThread.Join();
csound.Cleanup();
}
}

The actual syntax of the ScoreEvent-call seems to be the same in both cases.
It looks like Copilot gives here a wrong answer because there should be one additional
parameter in the call in the Performance-case.

Of course I tried both cases in java-code but without success.
I can define those p-field arrays but the problem is the actual method call.
When I try to utilise CsoundMYFLTArray-type parameter in the code:

CsoundMYFLTArray pp = new CsoundMYFLTArray(3);pp.SetValue(0, 2);
pp.SetValue(1, 0);
pp.SetValue(2, 3);

And then use the actual call like this (c is of type Csound):
c.ScoreEvent(‘i’, pp, 3 );

Now i get this error-message in Eclipse:

“The method ScoreEvent(char, SWIGTYPE_p_double, int) in the type Csound is not applicable
for the arguments (char, CsoundMYFLTArray, int)”

This clearly indicates that for some reason the definition in the API for the ScoreEvent is
somehow wrong. Copilot suggested Csound MYFLTArray, but it has been defined as SWIGTYPE_p_double in csnd6.jar.
I can’t understand how any kind of test could have passed java-compiler if tihs method
had been used in java-code.

–Risto
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 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
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

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

1/kr or ksmps/sr gives you 1 kcycle exactly.

Prof. Victor Lazzarini
Maynooth University
Ireland