Como construir um beatbox com JavaScript
<html> <head> <tittle>beat-box</tittle> </head> <body> <button id="play">Play</button> <audio src="prato.wav" id="prato"></audio> <audio src="prato-fechado.wav" id="prato-fechado"></audio> <audio src="bumbo.wav" id="bumbo"></audio> <audio src="boca.wav" id="boca"></audio> <audio src="caixa.wav" id="caixa"></audio> <script></script> </body> <html>
<script> const play = constGetElementById('play') let timer = null let currentAudio = 0 const bpm = 90 const music = ['prato', 'bumbo', 'boca', 'caixa'] function tick(){ console.log(music[(currentAudio++)%music.length]) } play.addEventListener('click', function(){ timer = setInterval(tick, (60 *1000 / bpm) }) </script>
function tick(){ const audioID = (music[(currentAudio++)%music.length] const audio = document.getElementById(audioID) audio.play() }
<script> const play = constGetElementById('play') let timer = null let currentAudio = 0 const bpm = 90 const music = \[ \['bumbo', 'bumbo', 'bumbo', 'bumbo', \], \['prato', 'prato-fechado', 'boca', 'caixa'\] \] function tick(){ const current = (music\[(currentAudio++)%music\[0\].length\] const audioID1 = music\[0\]\[current\] const audioID2 = music\[1\]\[current\] const audio1 = document.getElementById(audioID1) audio1.play() audio1.currentTime = 0 const audio2 = document.getElementById(audioID2) audio2.play() audio2.currentTime = 0 } play.addEventListener('click', function(){ timer = setInterval(tick, (60*1000/bpm) }) </script>