xxxxxxxxxx
50
let sounds = [];
let currentFile;
let hue = 360;
let lastFrameKeyPressed = false;
function preload() {
for (let i = 0; i < 5; i++) {
let fileName = "sounds/" + i + ".mp3";
print(fileName);
sounds[i] = loadSound(fileName);
}
}
function setup() {
createCanvas(600, 600);
currentFile = sounds[Math.floor(random(5))];
colorMode(HSB, 360, 100, 100);
}
function draw() {
background(0);
if (currentFile.isPlaying()) {
background(hue, 100, 100);
}
// if calling play repeatedly you'll get noise
// checking to see if file is finished playing before triggering again is one solution
if (keyIsPressed && !currentFile.isPlaying()) {
currentFile = sounds[Math.floor(random(5))];
currentFile.play();
hue = random(360);
}
// another option is making sure that the last frame was not pressed
// if (keyIsPressed && !lastFrameKeyPressed) {
// currentFile = sounds[Math.floor(random(5))];
// currentFile.play();
// hue = random(360);
// }
lastFrameKeyPressed = keyIsPressed;
}
function mousePressed() {
currentFile = sounds[Math.floor(random(5))];
currentFile.play();
hue = random(360);
}