-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrum-machine.js
More file actions
38 lines (31 loc) · 1.13 KB
/
Copy pathdrum-machine.js
File metadata and controls
38 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function playSound(selector, soundName) {
const audio = document.getElementById(selector);
if (!audio) return;
const pad = audio.parentElement;
const display = document.getElementById('display');
audio.currentTime = 0;
audio.play().catch(err => console.log("Playback prevented:", err));
display.innerText = soundName;
pad.classList.add('active');
setTimeout(() => {
pad.classList.remove('active');
}, 100);
}
const drumPads = document.querySelectorAll('.drum-pad');
drumPads.forEach(pad => {
const audioChild = pad.querySelector('.clip');
const soundDescription = pad.id;
const keyTrigger = audioChild.id;
pad.addEventListener('click', () => {
playSound(keyTrigger, soundDescription);
});
});
document.addEventListener('keydown', (event) => {
const key = event.key ? event.key.toUpperCase() : '';
const audioChild = document.getElementById(key);
if (audioChild) {
const pad = audioChild.parentElement;
const soundDescription = pad.id;
playSound(key, soundDescription);
}
});