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.
Page Index Toggle Pages: 1
Help with OSC (Read 480 times)
Help with OSC
Jan 5th, 2010, 4:29pm
 
Hi, im trying to use OSC to have processing and super collider communicate with each other. I have them communicating so far as when something happens within processing a sound is generated in super collider. This im ok with, the problem im having is that I want a sound to be generated only when the line comes in contact with an ellipse within the first section, so the first 100 pixels on the y axis. Below is my code from both processing and super collider.
Any help would be greatly appreciated
cheers


Code:
ArrayList balls;
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int numBalls = 100;
int currentBall = 0;
int ballWidth = 25;
float linex = 0.0;
boolean ball = false;
boolean start = false;
PFont font;
int ballX = 0;
int ballY = 0;




void setup(){
 size(500,550);
 smooth();
 oscP5 = new OscP5(this,12000);
 myRemoteLocation = new NetAddress("127.0.0.1",57120);
 balls = new ArrayList();
 font = createFont("GillSans-48.vlw",42);
 textFont(font);
 }
 
 


void draw(){
 background(0);
 stroke(247,15,15,200);
 strokeWeight(8);
 
 
 
 if(start == true){
 linex = linex + 2.0;
 line(linex,0.0,linex,500);
 if(linex>=500){
   linex=0;
 }
 }



 stroke(255);
 strokeWeight(4);
 line(0,100,500,100);
 line(0,200,500,200);
 line(0,300,500,300);
 line(0,400,500,400);


 fill(250,25,25);
 noStroke();
 rect(0,500,500,500);
   if((keyPressed == true) && (key == 's')){
   start = true;
 }
 
 
 fill(255);
 text("Press 's' to start",10,545);
 
 
 
 for(int i = balls.size()-1; i>=0;i--){
   Ball ball = (Ball) balls.get(i);
   ball.display();
 }
 }
 

 
void mousePressed(){
  ball =true;
  ballX = mouseX;
  ballY = mouseY;
  balls.add(new Ball(ballX, ballY,ballWidth));
}


BALL CLASS CODE:
Code:
 class Ball{
 float x,y,w;
 

 Ball(float tempX, float tempY, float tempW){
   x = tempX;
   y = tempY;
   w = tempW;

 }
 
 void display(){
   fill(255);
   noStroke();
   
   if(dist(x,0,linex,0)<=ballWidth/2)fill(255,0,0);
   if(dist(x,0,linex,0)>=ballWidth/2)fill(255);
   ellipse(x,y,w,w);
   if(dist(x,0,linex,0)<=ballWidth/2){
    OscMessage myMessage = new OscMessage("/synth");
   oscP5.send(myMessage,myRemoteLocation);  
 }
 }
}


SUPERCOLLIDER CODE:

Code:
(
SynthDef("synth",{ arg freq = 440, note = 50, amp = 0.3, dur = 1;
var sig;
sig = SinOsc.ar(freq, 0, amp)
* EnvGen.kr(Env.perc(0.01,dur),doneAction:2);
Out.ar(0, sig ! 2);
}).send(s);
)

(
var node;
o = OSCresponder(nil, '/synth', {arg time, responder, msg, addr;
Synth(\synth,
[\freq, 440, \dur, 1 ]);
}).add;
)

o = remove;

Re: Help with OSC
Reply #1 - Jan 6th, 2010, 12:22pm
 
i'd take the OSC-functionality out of the display function to simplify things a bit:

Code:

class Ball{
float x,y,w;
boolean playing;


Ball(float tempX, float tempY, float tempW){
x = tempX;
y = tempY;
w = tempW;
playing = false;
}

void display(){
noStroke();
if(abs(x-linex)<=ballWidth/2)
{
fill(255,0,0);
}
else
{
fill(255);
}
ellipse(x,y,w,w);
}

void playNote()
{
OscMessage myMessage = new OscMessage("/synth");
oscP5.send(myMessage,myRemoteLocation);
println("bleep!");
}

void check()
{
if(abs(x-linex)<=ballWidth/2 && y < 100)
{
if(!playing) playNote();
playing = true;
}
else
{
playing = false;
}
}
}


now the display function just does what it says... display the ball.

The check() function (which has to be called in draw) checks
a) if the playhead touches the ball and if it's in the first section (<100)
b) if there's already a note playing (the way you had it set up it would retrigger OSC messages as long as the playhead is near the ball)

so if this ball has not been triggered yet, and if it's in the right position, call playNote() to send a message once.
Page Index Toggle Pages: 1