|
1 | 1 | <!DOCTYPE html>
|
2 | 2 | <html lang="en">
|
| 3 | + |
3 | 4 | <head>
|
4 | 5 | <meta charset="UTF-8">
|
5 | 6 | <title>JS Drum Kit</title>
|
6 | 7 | <link rel="stylesheet" href="style.css">
|
7 | 8 | </head>
|
| 9 | + |
8 | 10 | <body>
|
9 | 11 |
|
10 | 12 |
|
|
47 | 49 | </div>
|
48 | 50 | </div>
|
49 | 51 |
|
| 52 | + <input type="button" value="Start Audio" id="playAudioButton"></input> |
| 53 | + |
50 | 54 | <audio data-key="65" src="sounds/clap.wav"></audio>
|
51 |
| - <audio data-key="83" src="sounds/hihat.wav"></audio> |
| 55 | + <audio data-key="83" src="sounds/closedhat.wav"></audio> |
52 | 56 | <audio data-key="68" src="sounds/kick.wav"></audio>
|
53 | 57 | <audio data-key="70" src="sounds/openhat.wav"></audio>
|
54 | 58 | <audio data-key="71" src="sounds/boom.wav"></audio>
|
55 | 59 | <audio data-key="72" src="sounds/ride.wav"></audio>
|
56 | 60 | <audio data-key="74" src="sounds/snare.wav"></audio>
|
57 | 61 | <audio data-key="75" src="sounds/tom.wav"></audio>
|
58 |
| - <audio data-key="76" src="sounds/tink.wav"></audio> |
59 |
| - |
60 |
| -<script> |
61 |
| - |
62 |
| - /************************************************************************************************ |
63 |
| - GOAL: When a user opens this page and presses a key that corresponds with |
64 |
| - one of our div elements, we should play the audio clip associated with |
65 |
| - the keypress, add a class to the specific element that matches with the keypress, |
66 |
| - and then remove that class in order to reset the element to it's original state. |
67 |
| - **************************************************************************************************/ |
68 |
| - |
69 |
| - function removeTransition(e) { |
70 |
| - // Skip if it's not a transform event |
71 |
| - if (e.propertyName !== 'transform') return; |
72 |
| - e.target.classList.remove('playing'); |
73 |
| - } |
74 |
| - |
75 |
| - function playSound(e) { |
76 |
| - const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); |
77 |
| - const key = document.querySelector(`div[data-key="${e.keyCode}"]`); |
78 |
| - |
79 |
| - // Stop function from running if key pressed doesn't match up with our elements data-key value |
80 |
| - if (!audio) return; |
81 |
| - key.classList.add('playing'); |
82 |
| - |
83 |
| - // Play sound clip from start every time a corresponding key is pressed |
84 |
| - audio.currentTime = 0; |
85 |
| - audio.play(); |
86 |
| - } |
87 |
| - |
88 |
| - // Find all elements in the document with a class 'key' |
89 |
| - const keys = Array.from(document.querySelectorAll('.key')); |
90 |
| - keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
91 |
| - window.addEventListener('keydown', playSound); |
92 |
| - |
93 |
| -</script> |
| 62 | + <audio data-key="76" src="sounds/tinkbell.wav"></audio> |
| 63 | + |
| 64 | + <script> |
| 65 | + |
| 66 | + /************************************************************************************************ |
| 67 | + GOAL: When a user opens this page and presses a key that corresponds with |
| 68 | + one of our div elements, we should play the audio clip associated with |
| 69 | + the keypress, add a class to the specific element that matches with the keypress, |
| 70 | + and then remove that class in order to reset the element to it's original state. |
| 71 | + **************************************************************************************************/ |
| 72 | + |
| 73 | + function removeTransition(e) { |
| 74 | + // Skip if it's not a transform event |
| 75 | + if (e.propertyName !== 'transform') return; |
| 76 | + e.target.classList.remove('playing'); |
| 77 | + } |
| 78 | + |
| 79 | + function playSound(e) { |
| 80 | + |
| 81 | + // If space key is pressed then start the auto drum notes |
| 82 | + if (e.code === 'Space') { |
| 83 | + play(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); |
| 88 | + const key = document.querySelector(`div[data-key="${e.keyCode}"]`); |
| 89 | + |
| 90 | + // Stop function from running if key pressed doesn't match up with our elements data-key value |
| 91 | + if (!audio) return; |
| 92 | + key.classList.add('playing'); |
| 93 | + |
| 94 | + // Play sound clip from start every time a corresponding key is pressed |
| 95 | + audio.currentTime = 0; |
| 96 | + audio.play(); |
| 97 | + } |
| 98 | + |
| 99 | + // Find all elements in the document with a class 'key' |
| 100 | + const keys = Array.from(document.querySelectorAll('.key')); |
| 101 | + keys.forEach(key => key.addEventListener('transitionend', removeTransition)); |
| 102 | + window.addEventListener('keydown', playSound); |
| 103 | + |
| 104 | + |
| 105 | + /************************************************************************************************ |
| 106 | + Play some auto drum notes |
| 107 | + ************************************************************************************************/ |
| 108 | + let playing = false; |
| 109 | + let interval; |
| 110 | + const totalSteps = 16; |
| 111 | + let currentStep = 1; |
| 112 | + let fullSteps = Array.from({ length: 16 }, () => []); |
| 113 | + const kick = 68; |
| 114 | + const hihat = 83; |
| 115 | + const snare = 74; |
| 116 | + const tink = 76; |
| 117 | + let bpm = 12; // increase or decrease this 12 param to see change in beats per minute |
| 118 | + let count = 0; |
| 119 | + |
| 120 | + // Add the click event listener to our play button |
| 121 | + const playAudioButton = document.querySelector('#playAudioButton'); |
| 122 | + playAudioButton.addEventListener('click', play); |
| 123 | + |
| 124 | + // Toggle the play button and start/stop the audio based on current state |
| 125 | + function play(e) { |
| 126 | + playAudioButton.value = playing ? 'Start Audio' : 'Stop Audio'; |
| 127 | + if (playing) { |
| 128 | + stopAudio(); |
| 129 | + } else { |
| 130 | + startAudio(); |
| 131 | + } |
| 132 | + playing = !playing; |
| 133 | + } |
| 134 | + |
| 135 | + // Play the instrument that is set for current step |
| 136 | + function playStep(step) { |
| 137 | + return Array.from(fullSteps[step]).map((keyCode) => playSound({ keyCode })); |
| 138 | + } |
| 139 | + |
| 140 | + // Start the audio, if play button is clicked |
| 141 | + function startAudio() { |
| 142 | + interval = setInterval(() => { |
| 143 | + |
| 144 | + // Add hihat after 2nd round |
| 145 | + if (++count === 32) { |
| 146 | + createNotes(hihat, [1, 3, 5, 7, 9, 11, 13, 15]); |
| 147 | + } |
| 148 | + |
| 149 | + // Add tink after 4th round |
| 150 | + if (count === 32 * 2) { |
| 151 | + createNotes(tink, [2, 4, 7, 12]); |
| 152 | + } |
| 153 | + if (count === 32 * 4) { |
| 154 | + createNotes(tink, [3, 5, 8, 13]); |
| 155 | + } |
| 156 | + |
| 157 | + playStep(currentStep - 1); |
| 158 | + currentStep = (currentStep < totalSteps) ? currentStep + 1 : 1; |
| 159 | + if (!playing) stopAudio(); |
| 160 | + }, ~~(1500 / bpm)); |
| 161 | + } |
| 162 | + |
| 163 | + // Stop the audio, if stop button is clicked |
| 164 | + function stopAudio() { |
| 165 | + clearInterval(interval); |
| 166 | + currentStep = 1; |
| 167 | + count = 0; |
| 168 | + fullSteps = Array.from({ length: 16 }, () => []); |
| 169 | + createFullNotes(); |
| 170 | + keys.forEach(key => key.classList.remove('playing')); |
| 171 | + } |
| 172 | + |
| 173 | + // Create setup for what instrument needs to be played at what step |
| 174 | + function createNotes(instrument, steps) { |
| 175 | + return Array.from(steps).map((step) => { |
| 176 | + if (!Array.isArray(fullSteps[step - 1])) { |
| 177 | + fullSteps[step - 1] = []; |
| 178 | + } |
| 179 | + fullSteps[step - 1].push(instrument) |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + // Create all the notes needed to play the initial audio |
| 184 | + function createFullNotes() { |
| 185 | + createNotes(kick, [1, 4, 7, 11]); |
| 186 | + createNotes(snare, [5, 13]); |
| 187 | + } |
| 188 | + |
| 189 | + // Initialize the initial audio notes |
| 190 | + createFullNotes(); |
| 191 | + </script> |
94 | 192 |
|
95 | 193 |
|
96 | 194 | </body>
|
| 195 | + |
97 | 196 | </html>
|
0 commit comments