Hi,
I would like to trigger sound when I receive data=0.
But sometimes It works, sometimes it doesn’t.
Maybe because my program doesn’t actualise itself enough quickly?
here my program
import processing.serial.*; // import data from Arduino
Serial myPort; // The serial port
float v0,v1,v2,v3,v4;
int x;
import ddf.minim.*;
Minim minim;
AudioSample kick;
AudioSample snare;
void setup()
{
background (255);
size(1000,800);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[4], 115200);
// Read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
minim = new Minim(this);
// load BD.wav from the data folder
kick = minim.loadSample( "BD.mp3", // filename
512 // buffer size
);
// An AudioSample will spawn its own audio processing Thread,
// and since audio processing works by generating one buffer
// of samples at a time, we can specify how big we want that
// buffer to be in the call to loadSample. k
// above, we requested a buffer size of 512 because
// this will make the triggering of the samples sound more responsive.
// on some systems, this might be too small and the audio
// will sound corrupted, in that case, you can just increase
// the buffer size.
// if a file doesn't exist, loadSample will return null
if ( kick == null ) println("Didn't get kick!");
// load SD.wav from the data folder
snare = minim.loadSample("SD.wav", 512);
if ( snare == null ) println("Didn't get snare!");
}
void draw()
{
background(0);
stroke(255);
// use the mix buffer to draw the waveforms.
for (int i = 0; i < kick.bufferSize() - 1; i++)
{
float x1 = map(i, 0, kick.bufferSize(), 0, width);
float x2 = map(i+1, 0, kick.bufferSize(), 0, width);
line(x1, 50 - kick.mix.get(i)*50, x2, 50 - kick.mix.get(i+1)*50);
line(x1, 150 - snare.mix.get(i)*50, x2, 150 - snare.mix.get(i+1)*50);
}
// Draw circles
fill(25);
ellipse(x, v0+250, 5, 5);
fill(50);
ellipse(x, v1+350, 5, 5);
fill(75);
ellipse(x, v2+450, 5, 5);
fill(100);
ellipse(x, v3+550, 5, 5);
fill (125);
ellipse(x, v4+650, 5, 5);
// Update x position
x++;
// Refresh screen
if (x > width) {
background(255);
x = 0;
}
}
void keyPressed()
{
if ( key == 's' ) snare.trigger();
if ( key == 'k' ) kick.trigger();
if ( key == 'j' ) kick.trigger();
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
//println(myString);
// split the string at the commas
// and convert the sections into integers:
float values[] = float(split(myString, ','));
if (values.length > 0) {
// ATTENTION LANCE ARDUINO pendant 5 sec puis Processing
v0= map (values[0], 0, 2774, 0, 36);
v1= map (values[1], 0, 2774, 0, 36);
v2= map (values[2], 0, 2774, 0, 36);
v3= map (values[3], 0, 2774, 0, 36);
v4= map (values[4], 0, 2774, 0, 36);
println (v0);
println (v1);
println (v2);
println (v3);
}
if (int (v0)==0) snare.trigger();
if (int (v1)==0) snare.trigger();
}
I send this from Arduino
for(uint8_t i = 0; i < NBMOTEURS; i++) {
Serial.print(position[i]);
Serial.print(", ");
} //Serial.println("");
// */
Serial.println();
}