-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
198 lines (182 loc) · 5.91 KB
/
app.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
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
class DrumKit {
constructor() {
this.pads = document.querySelectorAll(".pad");
this.playBtn = document.querySelector(".play");
this.currentKick = "./sounds/kick-classic.wav";
this.currentSnare = "./sounds/snare-acoustic01.wav";
this.currentHihat = "./sounds/hihat.acoustic01.wav";
this.kickAudio = document.querySelector(".kick-sound");
this.snareAudio = document.querySelector(".snare-sound");
this.hihatAudio = document.querySelector(".hihat-sound");
this.index = 0;
this.bpm = 150;
this.isPlaying = null;
this.selects = document.querySelectorAll("select");
this.muteBtns = document.querySelectorAll(".mute");
this.tempoSlider = document.querySelector("#inputRange");
this.inputNum = document.querySelector("#inputNum");
}
activePad() {
this.classList.toggle("active");
}
repeat() {
let step = this.index % 8;
const activeBars = document.querySelectorAll(`.b${step}`);
//Loop over the pads
activeBars.forEach(bar => {
bar.style.animation = `playTrack 0.3s alternate ease-in-out 2`;
if (bar.classList.contains("active")) {
if (bar.classList.contains("kick-pad")) {
this.kickAudio.currentTime = 0;
this.kickAudio.play();
}
if (bar.classList.contains("snare-pad")) {
this.snareAudio.currentTime = 0;
this.snareAudio.play();
}
if (bar.classList.contains("hihat-pad")) {
this.hihatAudio.currentTime = 0;
this.hihatAudio.play();
}
}
});
this.index++;
if(this.index==8){
this.index=0;
}
}
start() {
const interval = (60 / this.bpm) * 1000;
//Check if it's playing
if (this.isPlaying) {
//Clear the interval
clearInterval(this.isPlaying);
console.log(this.isPlaying);
this.isPlaying = null;
} else {
this.isPlaying = setInterval(() => {
this.repeat();
}, interval);
}
}
updateBtn() {
//NULL
if (!this.isPlaying) {
this.playBtn.innerText = "Stop";
this.playBtn.classList.add("active");
} else {
this.playBtn.innerText = "Play";
this.playBtn.classList.remove("active");
}
}
changeSound(e) {
const selectionName = e.target.name;
const selectionValue = e.target.value;
switch (selectionName) {
case "kick-select":
this.kickAudio.src = selectionValue;
break;
case "snare-select":
this.snareAudio.src = selectionValue;
break;
case "hihat-select":
this.hihatAudio.src = selectionValue;
break;
}
}
mute(e) {
const muteIndex = e.target.getAttribute("data-track");
e.target.classList.toggle("active");
if (e.target.classList.contains("active")) {
switch (muteIndex) {
case "0":
this.kickAudio.volume = 0;
break;
case "1":
this.snareAudio.volume = 0;
break;
case "2":
this.hihatAudio.volume = 0;
break;
}
} else {
switch (muteIndex) {
case "0":
this.kickAudio.volume = 1;
break;
case "1":
this.snareAudio.volume = 1;
break;
case "2":
this.hihatAudio.volume = 1;
break;
}
}
}
changeTempo(e) {
const tempoText = document.querySelector(".tempo-nr");
tempoText.value = e.target.value;
}
updateTempo(e) {
this.bpm = e.target.value;
clearInterval(this.isPlaying);
this.isPlaying = null;
const playBtn = document.querySelector(".play");
if (playBtn.classList.contains("active")) {
this.start();
}
}
}
const drumKit = new DrumKit();
//Event Listeners
drumKit.pads.forEach(pad => {
pad.addEventListener("click", drumKit.activePad);
pad.addEventListener("animationend", function() {
this.style.animation = "";
});
});
drumKit.playBtn.addEventListener("click", function() {
drumKit.updateBtn();
drumKit.start();
});
drumKit.selects.forEach(select => {
select.addEventListener("change", function(e) {
drumKit.changeSound(e);
});
});
drumKit.muteBtns.forEach(btn => {
btn.addEventListener("click", function(e) {
drumKit.mute(e);
});
});
drumKit.tempoSlider.addEventListener("input", function(e) {
drumKit.changeTempo(e);
});
drumKit.tempoSlider.addEventListener("change", function(e) {
drumKit.updateTempo(e);
});
drumKit.inputNum.addEventListener("change" , function(e) {
drumKit.updateTempo(e);
});
//===========================================================
//////Styling:-
let inputRange = document.querySelector("#inputRange");
let inputNum = document.querySelector("#inputNum");
function numValChange(){
inputNum.value=inputRange.value;
}
function minMaxValidationFunction(value){
let min = 20;
let max = 300;
let val = parseInt(value);
if(val<min || isNaN(val)){
inputRange.value = min;
return min;
}else if(val>max){
inputRange.value = max;
return max;
}else {
inputRange.value = val;
return val;
}
}