How to add a Sound Trigger?
in
Programming Questions
•
1 year ago
Hi everyone,
Im new to Processing, and am currently working on a project in which I have collected a large amount of motion data from the traffic outside my window. I currently have a sketch that reads in this data, and plays it back. All the motion is represented by various sized rectangles. Once a rectangle passes through the centre point of the screen, it changes to a shade of yellow to show it has passed through this point;
What I would like to do is add sound to my sketch, so that once a rectangle passes through the centre point, a simple tone is triggered and played. I would like this to happen every time a new rectangle passes through this point, in order to create a sort of abstract piece of music. Any help would be greatly appreciated;
The Sketch
String line;
String type;
int id;
int x;
int y;
int w;
int h;
int year;
int month;
int day;
int hour;
int minute;
int second;
int millis;
BufferedReader reader;
void setup(){
size(640,480);
frameRate(60);
reader = createReader("Blobs-2011-11-29-17-42-34.txt");
}
void parseData (String datastr){
type = null;
id = 0;
x = 0;
y = 0;
w = 0;
h = 0;
year = 0;
month = 0;
day = 0;
hour = 0;
minute = 0;
second = 0;
millis = 0;
// check there is some data to parse
if (datastr != null){
String[] list = split(datastr, ',');
type = list[0]; // find out the first item in the line
// parse the blob data
if (type.equals("Blob") && list.length == 13){
id = int(list[1]);
x = int(list[2]);
y = int(list[3]);
w = int(list[4]);
h = int(list[5]);
year = int(list[6]);
month = int(list[7]);
day = int(list[8]);
hour = int(list[9]);
minute = int(list[10]);
second = int(list[11]);
millis = int(list[12]);
} else if (type.equals("Blob") && list.length != 13){
println("invalid blob list length");
}
}
}
void readFromFile(int skipLines){
try {
int skip = skipLines;
while (skip>0){
line = reader.readLine();
skip--;
}
} catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line != null){
parseData(line);
}
}
void draw(){
fill(150,150,150,50);
rect(0, 0, width, height);
boolean newFrame = false;
while (!newFrame){
readFromFile(5); // pass a number to skip lines in the file
if (type.equals("Blob")){
// do other code here based on x,y,w,h
// Colour the rects that are inbetween 200-300 vertical screen position
if (y >200 && y < 300){
fill(255,255,0);
} else {
fill(255,255,255);
}
// draw all rects smaller than 100px
if (w<100 && h<100){
rect(x, y, w, h);
}
}
// Clear the screen each frame (or decrement the frame refresh counter)
if (type.equals("Frame")){
newFrame = true;
}
}
}
1