How to play one audio file at a time?
in
Core Library Questions
•
1 year ago
The piece that I'm working on is designed so that certain sounds are played depending on a sonic range finder input. My current problem is that I'm not sure how prevent overlaying and letting one sound file finish before starting another sound file begins. Does anyone have advice on how to go about this?
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.AudioSample.*;
import processing.serial.*;
Minim minim;
AudioSample [] sounds = new AudioSample [3];
// width and height should be set here
int xWidth = 600;
int yHeight = 600;
// set framerate
int fr = 24;
// set up the display items you want by choosing true
boolean bck = true;
boolean grid = true;
boolean g_vert = true;
boolean g_horiz = true;
boolean g_values = true;
boolean output = true;
// these variables are for the serial port connection object
Serial port;
String portname = "COM8"; // find the name of your serial port in your system setup!
int baudrate = 9600; // set baudrate here
int value; // variables used to store value from serial port
String buf=""; // String buffer to store serial values
int value1; // value1 is the read value
// the serial event function takes the value of the event and store it in the corresponding variable
void serialEvent(int serial){
if(serial!=10) {
buf += char(serial);
} else {
//extract the value from the string 'buf'
buf = buf.substring(1,buf.length());
//cast the value to an integer
value1 = int(buf);
buf="";
}
}
// setup initializes displayItems and serial port objects
void setup(){
size(500, 400);
frameRate(fr);
minim = new Minim(this);
sounds [0] = minim.loadSample("sound0.mp3");
sounds [1] = minim.loadSample("sound1.mp3");
sounds [2] = minim.loadSample("sound2.mp3");
port = new Serial(this, portname, baudrate);
println(port);
}
void createSounds(){
//////////////////////////////////////////////
//closest to arduino 4
//Adding constraints to keep the circle within the framesize
if(value1 > 200 && value1 <300){
// value1 = width;
// rect(width/2,height/2,50,50);
int index = int( random(3) );
delay(300);
sounds[index].trigger();
sounds[index].trigger();
sounds[index].trigger();
}
else{//Draw the circle
// ellipse(width/2, height/2, value1, value1);
minim.stop();
}
}
// draw listens to serial port, draw
void draw(){
while(port.available() > 0){
value = port.read();
serialEvent(value);
}
noStroke();
fill(0, 0, 255);
createSounds();
if(output) println("Distance: "+value1);
delay(100);
}
void stop()
{
minim.stop();
super.stop();
}
1