Skip to content

Instantly share code, notes, and snippets.

@ohiofi
Last active November 23, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohiofi/c6b2bca45c7a9aa1bc9741afc1f79973 to your computer and use it in GitHub Desktop.
Save ohiofi/c6b2bca45c7a9aa1bc9741afc1f79973 to your computer and use it in GitHub Desktop.
// STEP ONE: load all 10 of the sounds. For example…
// var soundA = new Audio("https://cdn.glitch.com/475828fb-3da7-4c90-8b9a-8217d8db7a15%2F0.mp3?1509508399315");
var soundA = new Audio(
"https://cdn.glitch.com/475828fb-3da7-4c90-8b9a-8217d8db7a15%2F0.mp3?1509508399315"
);
var soundB;
var soundC;
var soundD;
// STEP TWO: put the variable names for all of your sounds in yourSoundArray. For example: [sound1, sound2, sound3]
var yourSoundArray = [];
// For example, var yourSoundArray = [soundA, soundB, soundC, soundD];
// STEP THREE: call the playSong() function when your Play button is clicked. Must include your array name as the argument.
// For example, playSong(yourSoundArray)
function randomBgColor() {
// add code to randomize the document body style background color here
}
function playOne(soundArray,playThis) {
soundArray[playThis].play();
randomBgColor();
}
// play a 4 measure phrase of random sounds
function playRandomBeat(soundArray) {
// i is used to setTimeout. 300 is a quarter note
for (var i = 0; i <= 4000; i += 300) {
setTimeout(() => {
playOne(soundArray, Math.floor(Math.random() * soundArray.length));
}, i);
}
}
// play a 4 measure phrase of repeating measures
function playBeat(soundArray) {
var temp = [
Math.floor(Math.random() * soundArray.length),
Math.floor(Math.random() * soundArray.length),
Math.floor(Math.random() * soundArray.length),
Math.floor(Math.random() * soundArray.length)
];
setTimeout(() => {
playOne(soundArray,temp[0]);
}, 3600);
// i is used to setTimeout. 300 is a quarter note, 1200 is a 4/4 measure
for (var i = 0; i <= 3000; i = i + 1200) {
setTimeout(() => {
playOne(soundArray,temp[0]);
}, i);
setTimeout(() => {
playOne(soundArray,temp[1]);
}, i + 300);
setTimeout(() => {
playOne(soundArray,temp[2]);
}, i + 600);
setTimeout(() => {
playOne(soundArray,temp[3]);
}, i + 900);
}
}
// play 4 phrases to create a simple song
function playSong(soundArray) {
for (var loop = 0; loop <= 14400; loop = loop + 4800) {
var randomphrase = Math.floor(Math.random() * 4 + 1);
if (randomphrase <= 1) {
setTimeout(() => {
playRandomBeat(soundArray);
}, loop);
} else {
setTimeout(() => {
playBeat(soundArray);
}, loop);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment