-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
44 lines (33 loc) · 982 Bytes
/
script.js
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
39
40
41
42
43
44
document.body.addEventListener('keyup', (event)=>{
playSound(event.code.toLowerCase());
});
document.querySelector('.composer button').addEventListener('click', () => {
let song = document.querySelector('#input').value;
if(song !== '') {
let songArray = song.split('');
playComposition(songArray);
}
});
function playSound(sound) {
let audioElement = document.querySelector(`#s_${sound}`);
let keyElement = document.querySelector(`div[data-key="${sound}"]`);
if(audioElement) {
audioElement.currentTime = 0;
audioElement.play();
}
if(keyElement) {
keyElement.classList.add('active');
setTimeout(()=>{
keyElement.classList.remove('active');
}, 300);
}
}
function playComposition(songArray) {
let wait = 0;
for(let songItem of songArray) {
setTimeout(()=>{
playSound(`key${songItem}`);
}, wait);
wait += 250;
}
}