xxxxxxxxxx
91
// Array of sounds
let guitars = [];
let drums = [];
let triangle;
let beat = 60;
let rhythm = 10;
// Load all the sounds
function preload() {
//load guitars
for (let g = 0; g < 6; g++) {
guitars.push(loadSound("guitar" + g + ".mp3"));
guitars[g].setVolume(0.1);
}
//load drums
for (let d = 0; d < 4; d++) {
drums.push(loadSound("drum" + d + ".mp3"));
drums[d].setVolume(0.2);
}
triangle = loadSound("triangle0.mp3");
}
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
//timer for the beat
let time = floor(frameCount / 60);
// Play instrument
for (let g = 0; g < guitars.length; g++) {
//between 0 & 5 seconds, play every second
if (time >= 0 && time < 10 && frameCount % beat == 0) {
guitars[5].play();
//guitars[5].setVolume(0.2);
}
//between 5 & 20 seconds, play every second
if (time >= 5 && time <= 20 && frameCount % beat == 0) {
drums[0].play();
}
//between 5 & 25 seconds, play half a second
if (time >= 5 && time <= 25 && frameCount % beat == beat / 2) {
drums[2].play();
}
if (time >= 10 && time <= 25 && frameCount % (beat * 2) == 4) {
drums[3].play();
}
//after 15 seconds, play every 5 seconds
if (time >= 15 && frameCount % 300 == 0) {
triangle.play();
}
if (time >= 10 && time <= 20 && frameCount % (beat * g) == 0) {
guitars[g].play();
//guitars[g].setVolume(0.6);
}
if (time >= 30 && frameCount % floor((beat * rhythm) / g) == 0) {
// Pick a random harmonic ratio for melody
guitars[g].play();
guitars[g].setVolume(0.6);
//increase guitar play rate
rhythm -= 0.2;
if (rhythm < 2) {
rhythm = 2;
}
}
if (time >= 45 && time <= 60 && frameCount % 15 == 0) {
drums[0].play();
}
if (time >= 40 && time <= 60 && frameCount % beat == beat / 2) {
drums[2].play();
}
if (time >= 60) {
guitars[g].stop();
drums[0].stop();
drums[2].stop();
drums[3].stop();
triangle.stop();
}
}
}