Hey!
I’m trying to make a sketch that plays a random sound out of a pre-loaded AudioPlayer array in certain for loop conditions.
My problem is that for some reason ALL 3 items in the array play at once when I use – (int) random(3) –
Any suggestions?
Code is as follows:
import ddf.minim.*;
import ddf.minim.effects.*;
float start, runtime;
PImage earth, angry, happy, neutral;
PShape globe;
Minim minim;
AudioPlayer[] playlistGood;
AudioPlayer[] playlistBad;
void setup(){
size(1200,800, P3D);
background(32);
start = millis();
minim = new Minim(this);
playlistGood = new AudioPlayer[3];
playlistGood[0] = minim.loadFile("bestfriends2.mp3");
playlistGood[1] = minim.loadFile("changeworld.mp3");
playlistGood[2] = minim.loadFile("bus.mp3");
playlistBad = new AudioPlayer[3];
playlistBad[0] = minim.loadFile("acting2.mp3");
playlistBad[1] = minim.loadFile("composting.mp3");
playlistBad[2] = minim.loadFile("grandchildren.mp3");
angry = loadImage("angry.JPG");
happy = loadImage("happy.JPG");
neutral = loadImage("neutral.JPG");
}
void draw(){
background(32);
//smooth();
lights();
runtime = millis() - start;
//sound/visual changes depending on time and position of person
if (mouseX > 1100) {
earth = angry;
playlistGood[0].rewind();
playlistGood[1].rewind();
playlistGood[2].rewind();
playlistBad[(int) random(3)].play();
} else if ((mouseX < 1100) && (runtime > 5000)){
earth = happy;
playlistBad[0].rewind();
playlistBad[1].rewind();
playlistBad[2].rewind();
playlistGood[(int)random(3)].play();
} else {
println("walking");
earth = neutral;
}
//reset timer after 15 seconds of inaction
if (runtime >= 20000){
start = millis();
}
println("runtime:" + runtime);
//DRAW GLOBE
translate(width/2, height/2);
rotateX(0.2);
//follow mouse movements
rotateY(mouseX * 0.002);
globe = createShape(SPHERE, 300);
globe.setTexture(earth);
globe.setStroke(false);
shape(globe);
}