Construindo um Beatbox Manipulável com JS
<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> <div id="track1" rel="prato"> <h2>Prato</h2> </div> <div id="track2" rel="prato-fechado"> <h2>Prato-fechado</h2> </div> <div id="track3" rel="bumbo"> <h2>Bumbo</h2> </div> <script> const numItems = 10 function generationTrack(trackId){ const track = document.getElementById('track'+trackId) for (let i=0; i<10; i++){ track.innerHTML += '<input type="checkbox id="i-'+trackId+'-'+i+'" />' } } generateTrack(1) generateTrack(2) generateTrack(2) </script> </body> <html>
<script> //gera o check-box const numItems = 10 function generationTrack(trackId){ const track = document.getElementById('track'+trackId) for (let i=0; i<10; i++){ track.innerHTML += '<input type="checkbox id="i-'+trackId+'-'+i+'" />' } } generateTrack(1) generateTrack(2) generateTrack(2) //fim gerar check-box const bpm = 90 let current = 0 function tick(){ const currentItem = current++ % numItems console.log(currentItem) } setInterval(tick, (60*1000)/bpm) </script>
<script> const bpm = 90 let current = 0 let isPlaying = false let timer = null function tick(){ const currentItem = current++ % numItems for(let i=1; i<=3; i++){ const item = document.getElementById('i-'+i+'-'+currentItem) const track = document.getElementById('track' +i) if (item.checked){ const audio = document.getElementById(track.attributes.rel.value) audio.currentTime = 0 audio.play() } } } const play = document.getElementById('play') play.addEventListener('click', function(){ if (isPlaying){ clearInterval(timer) play.innerHTML = 'Play' } else { timer = setInterval(tick, (60*1000)/bpm) play.innerHTML = 'Stop' } isPlaying = !isPlaying } </script>