We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › SuperCollider and Processing Example Code
Page Index Toggle Pages: 1
SuperCollider and Processing Example Code (Read 12085 times)
SuperCollider and Processing Example Code
Dec 4th, 2005, 5:15am
 
Hi Guys,
I found the examples in the discourse very helpful on other topics so I thought I might try and post an example file for those who want a jump start integrating SuperCollider and Processing. It's remarkably easy one you get your bearings:

Just cut and paste both of these into the respective programs and you should have an OSC socket going down!

Supercollider:

/////////
(
SynthDef("rand_osc4", {

arg pick, freq, trig;

var control;
// D-----------F#----------A-------------- is DM here 1,3,5
pick = Dxrand([86,74,62,50,90,78,66,54,93,81,69,57],inf);  

// converts the midi numbers to a frequency
freq = Demand.kr(trig,0,pick).midicps;

a = 0.25; // attack time (in seconds)
d = 1; // decay time
control = EnvGen.ar(Env([0, 0.1, 0], [a,d]), Trig.ar(trig,a+d));

//final output to the channels (is this stereo?)
Out.ar(0, SinOsc.ar(freq) * control);

}).send(s);
)
/////////

Processing  (you will need to download the OSC library and put it in the processing folder under Applications)


///////// just a little sample of a circle drag  ////

import osc.*;
import oscP5.*;


OscP5 oscP5;
int sendToPort;
int receiveAtPort;
String host;
String oscP5event;
int num_nodes = 20;
int start_node = 4000;
int current_node = start_node;

int r = 155;
int g = 100;
int b = 100;

float bx;
float by;
int bs = 10;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;  
float bdify = 0.0;  


void setup()  
{
 size(300, 300);
 bx = width/2.0;
 by = height/2.0;
 smooth();
 ellipseMode(CENTER_RADIUS);  
 framerate(25);
 initOsc();
 Setup_Nodes(num_nodes, start_node);
}



void Setup_Nodes(int num, int startin) {
//should setup a number of nodes within Supercollider
 for(int i=start_node; i<startin + num; i++) {
   OscMessage oscMsg = oscP5.newMsg("/s_new");
   oscMsg.add("rand_osc4");
   oscMsg.add(i);
   oscMsg.add(0);
   oscMsg.add(1);
   oscMsg.add("pick");
   oscMsg.add(1);
   // send the osc message
   // via the oscP5 bundle
   oscP5.sendMsg(oscMsg);
 }
}


void oscEvent(OscIn theOscIn) {
 println("recieved");
}


// setup osc functioniality

void initOsc() {
 receiveAtPort = 12000;
 sendToPort = 57110; // supercollider  
 host = "127.0.0.1";  //change to whatever the IP want to recieve
 oscP5event = "oscEvent";  
 oscP5 = new OscP5(this, host, sendToPort, receiveAtPort, oscP5event);
}

void draw()  
{  
 background(0);

 // Test if the cursor is over the box  
 if (mouseX > bx-bs && mouseX < bx+bs &&  
   mouseY > by-bs && mouseY < by+bs) {
   bover = true;  
   if(!locked) {  
     stroke(255);  
     fill(153);
   }  
 }
 else {
   stroke(153);
   fill(153);
   bover = false;
 }

 // Draw the box
 ellipse(bx, by, bs, bs);
}

void SoundOscMessage() {
//make sure the current node never exceeds nodes created during setup
if(current_node == (start_node + num_nodes - 1)) {
 current_node = start_node - 1;
 }
 current_node++;
 // Reset the Syth
 OscMessage oscReset = oscP5.newMsg("/n_set");
 oscReset.add(current_node);
 oscReset.add("trig");
 oscReset.add(0);
 // send the osc message
 // via the oscP5 bundle
 oscP5.sendMsg(oscReset);
 
 // Trigger a new sound Sound
 OscMessage oscTrig = oscP5.newMsg("/n_set");
 oscTrig.add(current_node);
 oscTrig.add("trig");
 oscTrig.add(1);
 // send the osc message
 // via the oscP5 bundle
 oscP5.sendMsg(oscTrig);
}


void mousePressed() {
 if(bover) {  
   locked = true;  
   fill(255, 255, 255);
 }
 else {
   locked = false;
 }
 bdifx = mouseX-bx;  
 bdify = mouseY-by;  

}

void mouseDragged() {
 if(locked) {
   bx = mouseX-bdifx;  
   by = mouseY-bdify;  

   Object[] xyObj;
   xyObj = new Object[] {
     new String("4001")    };

   //oscP5.sendMsg("/n_set", xyObj);
   SoundOscMessage();
   // println(bx);
   // println(by);
 }
}

void mouseReleased() {
 locked = false;
}

best,
Seth
Re: SuperCollider and Processing Example Code
Reply #1 - Dec 4th, 2005, 2:17pm
 
Seth, thank you for posting this tutorial, I'm going to blog it over on Generator.x. Am I correct to assume this will only work on MacOS
Re: SuperCollider and Processing Example Code
Reply #2 - Dec 4th, 2005, 3:02pm
 
Ok, I managed to find a Win32 port (link is in the blog post) and tried your example with Processing. It seems that there is indeed OSC communication between SC and Processing, but it keeps giving me these messages:

FAILURE /s_new SynthDef not found
FAILURE /n_set Node not found

I've never tried Supercollider before, so it might easily be something trivial. Any tips
Re: SuperCollider and Processing Example Code
Reply #3 - Dec 4th, 2005, 3:46pm
 
hi,
in order to start the SynthDef you need to boot the supercollider server and activate the SynthDef.
doubleclick next to the first ( in the code example below. everything in between the open open bracket and the close bracket should be highlighted now. if so, press enter (not return).
the supercollider server should start running now. start the processing sketch seth posted and move the circle. can you hear it?
Code:

// processing to supercollider
// in the next line, doubleclick next to the (  and press enter
(
s = Server.local;
s.boot;


SynthDef("rand_osc4",{
arg pick,freq,trig;
var control;
// D-----------F#----------A-------------- is DM here 1,3,5  
pick = Dxrand([86,74,62,50,90,78,66,54,93,81,69,57],inf);    
 
// converts the midi numbers to a frequency
freq = Demand.kr(trig,0,pick).midicps;
a = 0.25;
d = 1;
control = EnvGen.ar(Env([0,0.1,0],[a,d]),Trig.ar(trig,a+d));
Out.ar(0,SinOsc.ar(freq)*control);
}).send(s);
)
Re: SuperCollider and Processing Example Code
Reply #4 - Dec 5th, 2005, 3:07pm
 
OK, figured out to run the SynthDef (ctrl-return), so now I don't get any error message. But no sound either. There is definitely OSC communication though, and I can get sound with SinOsc commands.

Did you get it running on a Mac or PC? Is there an easy way to print debug messages from SC, i.e. what the values of freq and pick etc are?
Re: SuperCollider and Processing Example Code
Reply #5 - Dec 5th, 2005, 8:46pm
 
hi marius,
works fine here on osx.

may i suggest to first try out the synth from within sc itself.  select and evaluate (ctrl+ret) these lines one at a time (er, after the code sojamo posted that prepares/boots the server and sends the synthdef of course)...

a= Synth("rand_osc4", [\trig, 1])  //start synth
b= Synth("rand_osc4", [\trig, 1])
c= Synth("rand_osc4", [\trig, 1])
a.free   //remove synth
b.free
c.free

this should make some sound and produce no errors.  yes/no?

if you get "FAILURE /s_new SynthDef not found" then the synthdef didn't get sent to the server.

if that all works then run this...

s.dumpOSC(1)  //start posting incoming osc

and start the processing code.  playing with the circle and sc should spit out something like below.  else you've got problems with osc.

[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/s_new", "rand_osc4", 4000, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4001, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4002, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4003, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4004, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4005, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4006, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4007, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4008, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4009, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4010, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4011, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4012, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4013, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4014, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4015, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4016, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4017, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4018, 0, 1, "pick", 1 ]
[ "/s_new", "rand_osc4", 4019, 0, 1, "pick", 1 ]
[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/status", ]
[ "/n_set", 4001, "trig", 0 ]
[ "/n_set", 4001, "trig", 1 ]
[ "/n_set", 4002, "trig", 0 ]
[ "/n_set", 4002, "trig", 1 ]
[ "/n_set", 4003, "trig", 0 ]
[ "/n_set", 4003, "trig", 1 ]
[ "/n_set", 4004, "trig", 0 ]
[ "/n_set", 4004, "trig", 1 ]
[ "/n_set", 4005, "trig", 0 ]
[ "/n_set", 4005, "trig", 1 ]
[ "/n_set", 4006, "trig", 0 ]
[ "/n_set", 4006, "trig", 1 ]
[ "/n_set", 4007, "trig", 0 ]
[ "/n_set", 4007, "trig", 1 ]

Re: SuperCollider and Processing Example Code
Reply #6 - Dec 11th, 2005, 9:31am
 
Hi Guys,
Nice to see people playing with the code. I used a Mac - actually two macs - one with supercollider and one with processing. Supercollider needs to have its local server booted AND you have to build the Synth before you run the script in processing. See the supercollider tutorial under Help to get started. It's pretty good as far as tutorials go. Here is updated Supercollider code, with test lines at the bottom so you can make sure things are working there first.

Seth



/////////
(
SynthDef("rand_osc4", {

arg pick, freq, trig;

var control;
// D-----------F#----------A-------------- is DM here 1,3,5
//pick = Drand([86,74,62,50,90,78,66,54,93,81,69,57],inf);  
//pick = Drand([86,74,90,78,93,81],inf);
pick = Drand([110,98,86,74,102,90,78,105,93,81,69],inf);


// converts the midi numbers to a frequency
freq = Demand.kr(trig,0,pick).midicps;

a = 0.02; // attack time (in seconds)
d = 0.5; // decay time
control = EnvGen.ar(Env([0, 0.05, 0], [a,d]), Trig.ar(trig,a+d));


//final output to the channels (is this stereo?)
Out.ar(0, SinOsc.ar(freq) * control);

}).send(s);
)
/////////


//Example messages to send to the  stars:

s.sendMsg("/s_new", "rand_osc4", 4000, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4001, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4002, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4003, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4004, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4005, 0, 1, "pick", 1); // create synth
s.sendMsg("/s_new", "rand_osc4", 4006, 0, 1, "pick", 1); // create synth


s.sendMsg("/n_set", 4000, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4001, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4002, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4003, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4004, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4005, "trig", 1);  // send a trigger of 1
s.sendMsg("/n_set", 4006, "trig", 1);  // send a trigger of 1

s.sendMsg("/n_set", 4000, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4001, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4002, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4003, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4004, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4005, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  
s.sendMsg("/n_set", 4006, "trig", 0);  // send trigger 0. This important because Trig works on 0 to  

s.sendMsg("/n_free", 4000);  // kill synth
s.sendMsg("/n_free", 4001);  // kill synth
s.sendMsg("/n_free", 4002);  // kill synth
s.sendMsg("/n_free", 4003);  // kill synth
s.sendMsg("/n_free", 4004);  // kill synth
s.sendMsg("/n_free", 4005);  // kill synth
s.sendMsg("/n_free", 4006);  // kill synth

Re: SuperCollider and Processing Example Code
Reply #7 - Jan 16th, 2007, 9:51pm
 
just tried ou the example above. in order to make it work i needed to change the following within the processing part of the code:

set 'import osc.*' to 'import netP5.*'
change any occurence of 'sendMsg' to 'send'

i'm working on the latest version of processing and added 'oscP5' from http://www.sojamo.de/iv/index.php?n=11

nice thing to have supercollider working together with processing ...

best, stefan
Re:Collider &Processing Code+Arduino and LM35
Reply #8 - Nov 10th, 2009, 12:39pm
 
Hi guy, i got alredy running processing with Supercollider, Now what i want to do is to connect:

LM35(temp sensor)---> Arduino--->Processing--->SuperCollider

so i can get  a sound signal trigerred by a temperature measure.
So i have already running LM35+ Arduino+Processing, and seperately i have Processing and SC. But i Want all of them running, lets said that when peopleo touch temp sensor (LM35), it sends a value to Processing and process, via OSC send that signal tu Supercollider, So when somebody touch sensor gets sound variations.
Well here i have the code for Lm35+Arduino +Processing separately from the code of Processing+SC:

LM35+ARDUINO+PROCESSING:

//import Serial communication library
import processing.serial.*;

//init variables
Serial commPort;
float tempC;
float tempF;
int yDist;
PFont font12;
PFont font24;
float[] tempHistory = new float[100];

void setup()
{
 //setup fonts for use throughout the application
 font12 = loadFont("AmericanTypewriter-23.vlw");
 font24 = loadFont("AmericanTypewriter-23.vlw");
 
 //set the size of the window
 size(610, 600);
 
 //init serial communication port
 commPort = new Serial(this, "/dev/cu.usbserial-A5002tHf", 9600);
 
 //fill tempHistory with default temps
 for(int index = 0; index<100; index++)
   tempHistory[index] = 0;
}

void draw()
{
 //get the temp from the serial port
 while (commPort.available() > 0)
 {
   tempC = commPort.read();
 
   //refresh the background to clear old data
   background(123);

   //draw the temp rectangle
   colorMode(RGB, 160);  //use color mode sized for fading
   stroke (0);
   rect (100,70,73,213);
   //fade red and blue within the rectangle
   for (int colorIndex = 0; colorIndex <= 160; colorIndex++)
   {
     stroke(160 - colorIndex, 0, colorIndex);
     line(50, colorIndex + 20, 70, colorIndex + 20);
   }
   
   //draw graph
   stroke(0);
   fill(255,255,255);
   rect(200,190,210,210);
   for (int index = 0; index<100; index++)
   {  
     if(index == 99)
       tempHistory[index] = tempC;
     else
       tempHistory[index] = tempHistory[index + 1];
     point(90 + index, 180 - tempHistory[index]);
   }
 
   //write reference values
   fill(0,0,0);
   textFont(font12);
   textAlign(CENTER);
   text("212 F",70, 25);
   text("32 F", 70, 187);
 
   //draw triangle pointer
   yDist = int(160 - (160 * (tempC * 0.01)));
   stroke(0);
   triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
 
   //write the temp in C and F
   fill(0,0,0);
   textFont(font24);
   textAlign(CENTER);
   text(str(int(tempC)) + " C", 115, 37);
   tempF = ((tempC*9)/5) + 32;
   text(str(int(tempF)) + " F", 115, 65);
 }
}
===============================
PROCESSING+SC:

import supercollider.*;


import netP5.*;
import oscP5.*;


OscP5 oscP5;
int sendToPort;
int receiveAtPort;
String host;
String oscP5event;
int num_nodes = 20;
int start_node = 4000;
int current_node = start_node;

int r = 155;
int g = 100;
int b = 100;

float bx;
float by;
int bs = 10;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;  
float bdify = 0.0;  


void setup()  
{
size(300, 300);
bx = width/2.0;
by = height/2.0;
smooth();
ellipseMode(CENTER_RADIUS);  
frameRate(25);
initOsc();
Setup_Nodes(num_nodes, start_node);
}



void Setup_Nodes(int num, int startin) {
//should setup a number of nodes within Supercollider
for(int i=start_node; i<startin + num; i++) {
  OscMessage oscMsg = oscP5.newMsg("/s_new");
  oscMsg.add("rand_osc4");
  oscMsg.add(i);
  oscMsg.add(0);
  oscMsg.add(1);
  oscMsg.add("pick");
  oscMsg.add(1);
  // send the osc message
  // via the oscP5 bundle
  oscP5.send(oscMsg);
}
}


void oscEvent(OscIn theOscIn) {
println("recieved");
}


// setup osc functioniality

void initOsc() {
receiveAtPort = 12000;
sendToPort = 57110; // supercollider  
host = "127.0.0.1";  //change to whatever the IP want to recieve
oscP5event = "oscEvent";  
oscP5 = new OscP5(this, host, sendToPort, receiveAtPort, oscP5event);
}

void draw()  
{  
background(0);

// Test if the cursor is over the box  
if (mouseX > bx-bs && mouseX < bx+bs &&  
  mouseY > by-bs && mouseY < by+bs) {
  bover = true;  
  if(!locked) {  
    stroke(255);  
    fill(153);
  }  
}
else {
  stroke(153);
  fill(153);
  bover = false;
}

// Draw the box
ellipse(bx, by, bs, bs);
}

void SoundOscMessage() {
//make sure the current node never exceeds nodes created during setup
if(current_node == (start_node + num_nodes - 1)) {
current_node = start_node - 1;
}
current_node++;
// Reset the Syth
OscMessage oscReset = oscP5.newMsg("/n_set");
oscReset.add(current_node);
oscReset.add("trig");
oscReset.add(0);
// send the osc message
// via the oscP5 bundle
oscP5.send(oscReset);

// Trigger a new sound Sound
OscMessage oscTrig = oscP5.newMsg("/n_set");
oscTrig.add(current_node);
oscTrig.add("trig");
oscTrig.add(1);
// send the osc message
// via the oscP5 bundle
oscP5.send(oscTrig);
}


void mousePressed() {
if(bover) {  
  locked = true;  
  fill(255, 255, 255);
}
else {
  locked = false;
}
bdifx = mouseX-bx;  
bdify = mouseY-by;  

}

void mouseDragged() {
if(locked) {
  bx = mouseX-bdifx;  
  by = mouseY-bdify;  

  Object[] xyObj;
  xyObj = new Object[] {
    new String("4001")    };

  //oscP5.sendMsg("/n_set", xyObj);
  SoundOscMessage();
  // println(bx);
  // println(by);
}
}

void mouseReleased() {
locked = false;
}
===================================

Well hope to get recomendationss
R.
Re: SuperCollider and Processing Example Code+Arduino
Reply #9 - Nov 10th, 2009, 12:42pm
 
Forgot to write Arduino's Code:


#include "SimpleMessageSystem.h"
int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

void setup()
{
Serial.begin(9600); // start serial communication
}

void loop()
{


for(i = 0;i<=7;i++){ // gets 8 samples of temperature

samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);

}

tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit

if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature

Serial.print(tempc,DEC);
Serial.print(" Celsius, ");

Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

tempc = 0;

delay(1000); // delay before loop
}
there you go
Re: SuperCollider and Processing Example Code
Reply #10 - Nov 15th, 2009, 2:23pm
 
Hi, that example is cool, now how can i use it but, get the sound from data from serial, not from the mouse movement. i mean  info from a sensor, any change of sensor read change or trigg the sound from collider
Greetingss
R.
Page Index Toggle Pages: 1