1- /* global AudioContext, XMLHttpRequest, requestAnimationFrame */
1+ /* global AudioContext, XMLHttpRequest */
22
33function start ( ) {
44 window . AudioContext = window . AudioContext || window . webkitAudioContext
@@ -20,60 +20,53 @@ function start () {
2020 } )
2121 }
2222
23+ function playSound ( buffer , time ) {
24+ const source = context . createBufferSource ( )
25+ source . buffer = buffer
26+ source . connect ( context . destination )
27+ if ( ! source . start ) {
28+ source . start = source . noteOn
29+ }
30+ source . start ( time )
31+ }
32+
2333 let hithatBuffer
2434 let kickBuffer
2535 let snareBuffer
2636
27- loadAudio ( 'sounds/hihat.wav' ) . then ( ( buffer ) => {
28- hithatBuffer = buffer
29- } )
30- . then ( ( ) => {
31- return loadAudio ( 'sounds/kick.wav' ) . then ( ( buffer ) => {
32- kickBuffer = buffer
33- } )
34- } )
35- . then ( ( ) => {
36- return loadAudio ( 'sounds/snare.wav' ) . then ( ( buffer ) => {
37- snareBuffer = buffer
38- } )
39- } )
40- . then ( ( ) => {
41- function playSound ( buffer , time ) {
42- const source = context . createBufferSource ( )
43- source . buffer = buffer
44- source . connect ( context . destination )
45- if ( ! source . start ) {
46- source . start = source . noteOn
47- }
48- source . start ( time )
49- }
50-
51- const kick = kickBuffer
52- const snare = snareBuffer
53- const hihat = hithatBuffer
37+ function load ( ) {
38+ return Promise . all ( [
39+ loadAudio ( 'sounds/hihat.wav' ) . then ( ( buffer ) => { hithatBuffer = buffer } ) ,
40+ loadAudio ( 'sounds/kick.wav' ) . then ( ( buffer ) => { kickBuffer = buffer } ) ,
41+ loadAudio ( 'sounds/snare.wav' ) . then ( ( buffer ) => { snareBuffer = buffer } )
42+ ] )
43+ }
5444
55- // We'll start playing the rhythm 100 milliseconds from "now"
56- const startTime = context . currentTime
57- const tempo = 120 // BPM (beats per minute)
58- const eighthNoteTime = ( 60 / tempo ) / 2
45+ let play = ( ) => {
46+ // We'll start playing the rhythm 100 milliseconds from "now"
47+ const startTime = context . currentTime
48+ const tempo = 120 // BPM (beats per minute)
49+ const eighthNoteTime = ( 60 / tempo ) / 2
5950
60- // Play 2 bars of the following:
61- for ( let bar = 0 ; bar < 2 ; bar ++ ) {
62- const time = startTime + bar * 8 * eighthNoteTime
63- // Play the bass (kick) drum on beats 1, 5
64- playSound ( kick , time )
65- playSound ( kick , time + 4 * eighthNoteTime )
51+ // Play 2 bars of the following:
52+ for ( let bar = 0 ; bar < 2 ; bar ++ ) {
53+ const time = startTime + bar * 8 * eighthNoteTime
54+ // Play the bass (kick) drum on beats 1, 5
55+ playSound ( kickBuffer , time )
56+ playSound ( kickBuffer , time + 4 * eighthNoteTime )
6657
67- // Play the snare drum on beats 3, 7
68- playSound ( snare , time + 2 * eighthNoteTime )
69- playSound ( snare , time + 6 * eighthNoteTime )
58+ // Play the snare drum on beats 3, 7
59+ playSound ( snareBuffer , time + 2 * eighthNoteTime )
60+ playSound ( snareBuffer , time + 6 * eighthNoteTime )
7061
71- // Play the hi-hat every eighthh note.
72- for ( let i = 0 ; i < 8 ; ++ i ) {
73- playSound ( hihat , time + i * eighthNoteTime )
74- }
62+ // Play the hi-hat every eighthh note.
63+ for ( let i = 0 ; i < 8 ; ++ i ) {
64+ playSound ( hithatBuffer , time + i * eighthNoteTime )
7565 }
76- } )
66+ }
67+ }
68+
69+ load ( ) . then ( play )
7770}
7871
7972window . onload = start
0 commit comments