Cabbage Logo
Back to Cabbage Site

Processing csoundo lib (p5.js related too)


I’ve been trying to get a simple example of a metro to triggering an instrument but also issuing an event via chnset to a chnGet in Processing to visually show the metro.

float trig = cs.getChn("onset"); if (trig == 1) { println(trig); fill(255); rect(random(width), random(height), 50, 50); }

The problem I’m having is what seems to be a discrepancy between Csound’s kr and the frame rate in Processing, and the metro impulses never aligning with the current draw frame.

I could, I guess, recreate the metro in Processing, but that kind of defeats the purpose of slaving visuals from the more accurate audio clock. Another option would be to use OSC messaging, but accessing the api directly would seem to be the preference in any case.

Am I missing an obvious trick here?

There is/should be a callback method that you can use. Tbh, it’s so long since I looked into it that I’d need to check the source. Let me take a look…

I see that there is some code to interface with Csound’s invalue/outvalue opcodes. These work with callback methods, so the idea would be when you use outvalue in Csound, it will trigger a callback method in your Processing sketch. The only issue is I don’t see any examples, so I might have to experiment a little with it. I’m not sure who contributed that bit of code, but I’ll see if I can figure something out. Leave it with me. I’ll try to take a look a little later today.

ahh thanks mate :slight_smile:

I figured that it would be something along those lines; something that runs out of the draw loop.

callbackEvent(String, double): void


outValueCallbackFunction(String, double): void


Looking through the git commit it seems I added those methods five years ago :joy: Oh why did I not commit an example along with them :confused:

[edit] It seems the methods I use to implement this have been removed from the current version of Csound. I can add them back in, but I want to find out first from the devs why they were removed. They usually have good reasons for such changes…

I added those method back to the Csound source and made a pull request. Hopefully they will work Ok now. I’ll let you know. You’re on OSX right?

Yes I saw the notification from GH. So the methods were commented out.

Am I in the minority of people wanting to use Csound with Processing? :smiley: The WASM option is equally cool. Hopefully write once and use on both (almost). Would the updates you’ve made now be applicable to the WASM/p5 versions?

Yep on macOS 10.12

Cheers mate

The WASM version should work just the same as the Csoundo version, although you will need to use p5js, so it will require some code changes. I’m just checking to see if those callback methods have been implemented in the WASM version. If so, that might be the way to go.

The updated Csound source builds ok on OSX and Linux, but not on Windows. That will need to be addressed before they will accept the PR :unamused:

It seems those callback methods are not implemented in the WASM build either. I’m going to test the changes I made in Linux. If it works there I can ask one of the Csound devs to prepare an OSX package for you.

I’m not sure we’ll be able to get these callback functions working, but I think we can get pretty god results without them. In your example you are checking to metro for a 1, but it would be better to simply increment a counter, and then check if it has changed. As you have seen, it’s near impossible to check every k-rate cycle because of the different update rates between Processing and Csound. The next best thing is to check if a change has taken place since the last draw. Something like this seems to work fairly well.

instr 1
kCnt init 0
if metro(4)==1 then  
   chnset kCnt, "counter"
   kCnt += 1 

And then in Processing do something like this:

import csoundo.*;

Csoundo cs;
int cnt=0;
float x,y,amp;
boolean setup = false;
int counter = 0;

void setup() {
    size(400, 400);
    cs = new Csoundo(this, "Random.csd");;
    setup = true;

void draw() {
  if (cs.getChn("counter") != counter)
     if (cs.getChn("counter") % 2 == 0)
       ellipse(width/2, height/2, 200, 200);
     counter = int(cs.getChn("counter")); 

I’ve attached a zipped example. (11.0 KB)

For what it’s worth, I dropped Processing for p5js some time ago. The WASM version of Csound is very usable in p5js. There is an example in the csoundo git repo. It’s already a little dated, but still runs fine. In fact, it’s based on this example, which might actually be a little more up to date in terms of the Csound version it ships with. A sample of pure JS examples can be found here.

Here’s a silly game I wrote with it some time back.

Ah of course! setting up a in intermediary counter to bring the ‘control rate’ down to a level Processing can work with. Thanks for looking in to this man, appreciate it. I toyed with OSC communication and actually that is decent enough as well. Good to know.

Funnily enough I was using p5.js a lot before picking up Processing again. I figured it would be easier to record the visual and audio more easily than from the browser. I guess I need to investigate more. Horses for courses - it’s useful to be acquainted with both.


It’s a pity that these callback methods aren’t available. I started my Csound/Processing journey with OSC quite a few years ago. Then moved on to csoundo and now p5js. I get frustrated with the syncing between them at times due to the different update rates, but usually find a way around it in the end.