xxxxxxxxxx
74
let triSound, dingSound, mutiSound, snapSound, dogSound, dogSound2, catSound;
let beatInterval;
let beatIndex = 0;
function preload() {
triSound = loadSound("trisound.wav");
dingSound = loadSound("dingsound.wav");
mutiSound = loadSound("mutisound.wav");
snapSound = loadSound("snapsound.wav");
dogSound = loadSound("dog1.mp3");
dogSound2 = loadSound("dog2.mp3");
catSound = loadSound("cat4.mp3");
}
function setup() {
createCanvas(400, 400);
textAlign(CENTER, CENTER);
textSize(20);
text("Press 'p' to play background track", width / 2, height / 2);
}
function draw() {
background(220);
}
function keyPressed() {
if (key === "p") {
startBackgroundTrack();
} else if (key === "s") {
stopBackgroundTrack();
}
}
function startBackgroundTrack() {
if (!beatInterval) {
beatInterval = setInterval(playBeat, 300); // Adjust the interval for tempo
}
}
function stopBackgroundTrack() {
clearInterval(beatInterval);
beatInterval = null;
}
function playBeat() {
// Mimic a pattern with variations
switch (beatIndex % 9) {
case 0:
triSound.play();
break;
case 1:
dingSound.play();
break;
case 2:
mutiSound.play();
break;
case 4:
snapSound.play();
break;
case 6:
triSound.play();
dingSound.play();
break;
case 7:
mutiSound.play();
break;
case 8:
dogSound.play();
catSound.play();
break;
}
beatIndex++;
}