Hello everyone, my first post here. I’ve been wrestling with this code for a while now, I have managed to get it working to some degree, but I’ve run into an issue I can’t get my head around.
I want load sounds into an array. Then at set times play a random mp3 from the array, currently at 5, 6 and 7 second intervals.
The issue is the sounds play fine and at the right times first time through the code, then it starts to become overlapped like the millis(count) has caught up with itself and is no longer playing and counting in the order I want.
I’m looking for a way to maybe reset the count / millis back to 0 and run through the program again so it only plays sounds at the requested intervals.
Here is the code below. Thank you in advance for any help on the matter.
(understandably there are libraries and mp3 files not included my apologies for that)
//variables for millis //
int lasttimecheck;
int lasttimenew;
int lasttimenow;
// variables for amount of time. 3000 = 3 seconds for example //
int timecheck;
int timenew;
int timenow;
import processing.sound.*;
AudioDevice device;
SoundFile[] file;
void setup() {
size(640, 360);
background(255);
lasttimecheck = millis();
lasttimenew = millis();
lasttimenow = millis();
// timings//
timecheck = 5000; // 5 seonds
timenew = 6000; // 6 seonds
timenow = 7000; // 7 seconds
//number of items in the array
file = new SoundFile[24];
//loop through all items in the array and look for file types //
for (int i = 0; i < file.length; i++) {
file[i] = new SoundFile(this, (i) + ".aiff");
}
}
// first array list 5 seconds //
void timechange() {
if (millis() > lasttimecheck + timecheck) {
lasttimecheck = millis();
file[int(random(0, 7))].play();
}
}
// second array list 6 sceonds //
void timeextra() {
if (millis() > lasttimenew + timenew) {
lasttimenew = millis();
file[int(random(8, 15))].play();
}
}
// second array list 7 seconds //
void timedone() {
if (millis() > lasttimenow + timenow) {
lasttimenow = millis();
file[int(random(16, 23))].play();
}
}
// play sounds //
void draw() {
background(0);
timeextra();
timechange();
timedone();
}