Tuesday, 9 November 2010

Yup - works pretty well with Ess:

//based on Graph // by David A. Mellis
import processing.serial.*;import krister.Ess.*; //import this sound library//declare 3 channels for 3 sound files:AudioChannel myChannel, myChannel2, myChannel3;
Serial port;String buff = "";String buff1 = "";String buff2 = "";//String buff3 = ""; //meint index = 0;int NEWLINE = 10;
// Store the last 256 values received so we can graph them.int[] valuesx = new int[256];int[] valuesy = new int[256];//int[] button_value = new int[256];//mevoid setup(){ size(512, 512); Ess.start(this);
port = new Serial(this, "COM7", 9600); //selection of drumb sounds: //myChannel=new AudioChannel("bass.wav"); myChannel=new AudioChannel("tamb.wav"); // myChannel=new AudioChannel("bass2.wav"); // myChannel=new AudioChannel("bass3.wav"); // myChannel=new AudioChannel("bass4.wav"); // myChannel=new AudioChannel("bass5.wav"); // myChannel=new AudioChannel("bass6.wav");
myChannel2=new AudioChannel("bass6.wav"); myChannel3=new AudioChannel("bass2.wav");
// If you know the name of the port used by the Arduino board, you // can specify it directly like this. //port = new Serial(this, "COM1", 9600);
}
void draw(){ background(250);
stroke(0);
// Graph the stored values by drawing a lines between them. for (int i = 0; i <> 0){ serialEvent(port.read()); } // image(etch,0, 0, width, height);}
void serialEvent(int serial){ if (serial != NEWLINE) { // Store all the characters on the line. buff += char(serial); } else { // The end of each line is marked by two characters, a carriage // return and a newline. We're here because we've gotten a newline, // but we still need to strip off the carriage return. buff = buff.substring(0, buff.length()-1); index = buff.indexOf(","); buff1 = buff.substring(0, index); buff2 = buff.substring(index+1, buff.length());
// Parse the String into an integer. We divide by 4 because // analog inputs go from 0 to 1023 while colors in Processing // only go from 0 to 255. int x = Integer.parseInt(buff1)/2; int y = Integer.parseInt(buff2)/2;
// Clear the value of "buff" buff = "";
// Shift over the existing values to make room for the new one. for (int i = 0; i < 255; i++) { valuesx[i] = valuesx[i + 1]; valuesy[i] = valuesy[i + 1]; }
// Add the received value to the array. valuesx[255] = x; valuesy[255] = y; if(x >= 10){ myChannel.play(); println(" "+x + "x "); }
else if(y >=60){
myChannel2.play(); println(" "+y + "y "); } }

}
public void stop() { Ess.stop(); super.stop();}

No comments:

Post a Comment