-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
201 lines (171 loc) · 5.67 KB
/
index.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Drum Kit</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="saved-sounds">
</div>
<div class="keys">
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<div data-key="83" class="key">
<kbd>S</kbd>
<span class="sound">hihat</span>
</div>
<div data-key="68" class="key">
<kbd>D</kbd>
<span class="sound">kick</span>
</div>
<div data-key="70" class="key">
<kbd>F</kbd>
<span class="sound">openhat</span>
</div>
<div data-key="71" class="key">
<kbd>G</kbd>
<span class="sound">boom</span>
</div>
<div data-key="72" class="key">
<kbd>H</kbd>
<span class="sound">ride</span>
</div>
<div data-key="74" class="key">
<kbd>J</kbd>
<span class="sound">snare</span>
</div>
<div data-key="75" class="key">
<kbd>K</kbd>
<span class="sound">tom</span>
</div>
<div data-key="76" class="key">
<kbd>L</kbd>
<span class="sound">tink</span>
</div>
</div>
<form class="options">
<select name="sound" id="selectSound" class="key sounds-list">
<option value="65">Clap</option>
<option value="83">Hihat</option>
<option value="68">Kick</option>
<option value="70">Openhat</option>
<option value="71">Boom</option>
<option value="72">Ride</option>
<option value="74">Snare</option>
<option value="75">Tom</option>
<option value="76">Tink</option>
</select>
<select name="repeat" id="selectRepeatInterval" class="key repeat-list">
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="16">16</option>
</select>
<button class="key" id="saveBeat"><span class="sound"><b>Save beat</b></span></button>
<button class="key" id="play"><span class="sound"><b>PLAY</b></span></button>
<button class="key" id="stop"><span class="sound"><b>STOP</b></span></button>
<button class="key" id="clear"><span class="sound"><b>CLEAR</b></span></button>
</form>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
<audio data-key="70" src="sounds/openhat.wav"></audio>
<audio data-key="71" src="sounds/boom.wav"></audio>
<audio data-key="72" src="sounds/ride.wav"></audio>
<audio data-key="74" src="sounds/snare.wav"></audio>
<audio data-key="75" src="sounds/tom.wav"></audio>
<audio data-key="76" src="sounds/tink.wav"></audio>
<script>
const savedSounds = document.getElementById("saved-sounds");
const playBtn = document.getElementById("play");
const stopBtn = document.getElementById("stop");
const clearBtn = document.getElementById("clear");
const audio = document.getElementsByTagName("audio");
const divs = document.getElementsByClassName("key");
document.addEventListener("keydown", e => {
for(const el of audio){
if(el.dataset.key == e.which){
el.currentTime = 0;
el.play();
}
}
for(const el of divs){
if(el.dataset.key == e.which){
el.classList.add("playing");
setTimeout(()=>{
el.classList.remove("playing");
},300)
}
}
});
function PlaySound(soundCode, interval){
this.soundCode = soundCode;
this.interval = interval*200;
}
PlaySound.prototype.play = function(){
for(const el of audio){
if(el.dataset.key == this.soundCode){
var interval = setInterval(()=>{
el.play();
},this.interval);
stopBtn.addEventListener("click", (e)=>{
e.preventDefault();
clearInterval(interval);
});
}
}
}
PlaySound.prototype.pause = function(){
for(const el of audio){
if(el.dataset.key == this.soundCode){
el.pause();
el.currentTime = 0;
}
}
}
let sound = new PlaySound("65",2);
let sound2 = new PlaySound("83",8);
let sound3 = new PlaySound("71",1);
//sound.play();
//sound2.play();
//sound3.play();
var selectedSounds = [];
saveBtn = document.getElementById("saveBeat");
saveBtn.addEventListener("click", (e) => {
e.preventDefault();
const selectSound = document.getElementById("selectSound");
const selectInterval = document.getElementById("selectRepeatInterval");
const selectedSound = selectSound.options[selectSound.selectedIndex].value;
const selectedSoundText = selectSound.options[selectSound.selectedIndex].text;
const selectedInterval = parseInt(selectInterval.options[selectInterval.selectedIndex].value);
const newSound= new PlaySound(selectedSound,selectedInterval);
const newElem = document.createElement("p");
newElem.classList.add("key","soundDescr");
newElem.appendChild(document.createTextNode(`Sound: ${selectedSoundText} Repeat: ${selectedInterval}/16`));
savedSounds.appendChild(newElem);
selectedSounds.push(newSound);
});
playBtn.addEventListener("click", (e)=>{
e.preventDefault();
for(const sound of selectedSounds){
sound.play();
}
});
stopBtn.addEventListener("click", (e)=>{
e.preventDefault();
for(const sound of selectedSounds){
clearInterval(sound);
}
});
clearBtn.addEventListener("click", (e)=>{
stopBtn.onclick();
selectedsounds = [];
savedSounds.innerHTML="";
});
</script>
</body>
</html>