-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (154 loc) · 4.31 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
class DrumKit {
constructor() {
this.pads = document.querySelectorAll(".pad");
this.playBtn = document.querySelector(".play");
this.currentKick = "./sounds/kick-classic.wav";
this.currentSnare = "./sounds/snare-big.wav";
this.currentHihat = "./sounds/hihat-electro.wav";
this.kickAudio = document.querySelector(".kick-sound");
this.snareAudio = document.querySelector(".snare-sound");
this.hihatAudio = document.querySelector(".hihat-sound");
this.index = 0;
this.bpm = 180;
this.isPlaying = null;
this.selects = document.querySelectorAll("select");
this.muteBtns = document.querySelectorAll(".mute");
this.tempoSlider = document.querySelector(".tempo-slider");
}
activePad() {
this.classList.toggle("active");
}
repeat() {
let step = this.index % 8;
const activeBars = document.querySelectorAll(`.b${step}`);
//pads loop
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++;
}
start() {
const interval = (60 / this.bpm) * 1000;
//Check if it's playing
if (!this.isPlaying) {
this.isPlaying = setInterval(() => {
this.repeat();
}, interval);
} else {
//clear interval
clearInterval(this.isPlaying);
this.isPlaying = null;
this.index = 0;
}
}
updateBtn() {
if (!this.isPlaying) {
this.playBtn.innerText = "Stop";
this.playBtn.style.backgroundColor = "rgb(145, 13, 13)";
this.playBtn.classList.add("active");
} else {
this.playBtn.innerText = "Play";
this.playBtn.style.backgroundColor = "rgb(53, 48, 80)";
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");
this.bpm = e.target.value;
tempoText.innerText = e.target.value;
}
updateTempo() {
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);
});