-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
175 lines (158 loc) · 5.32 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
class DrumKit {
constructor() {
this.pads = document.querySelectorAll('.pad');
this.playBtn = document.querySelector('.play');
this.currentKick = './sounds/kick-classic.mp4';
this.currentSnare = './sounds/snare-acoustic01.mp4';
this.currentHihat = './sounds/hihat-acoustic01.mp4';
this.kickAudio = document.querySelector('.kick-sound');
this.snareAudio = document.querySelector('.snare-sound');
this.hihatAudio = document.querySelector('.hihat-sound');
this.index = 0;
this.isPlaying = null;
this.bpm = 150; // modify for slow/faster bmp
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}`);
// Loop over the pads
activeBars.forEach((bar) => {
bar.style.animation = 'playTrack 0.3s alternate ease-in-out 2';
// Check if pads are active
if (bar.classList.contains('active')) {
// Check each sound
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;
// If it's not playing then play it
if (!this.isPlaying) {
this.isPlaying = setInterval(() => {
this.repeat();
}, interval);
} else {
// If it's already playing then just clear the interval
clearInterval(this.isPlaying);
this.isPlaying = null;
}
}
updateBtn() {
// If it's not null then it means it's already playing
if (!this.isPlaying) {
this.playBtn.innerText = 'Stop';
this.playBtn.classList.add('active');
} else {
this.playBtn.innerText = 'Start';
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) {
// Indicates which mute btn was clicked
const muteIndex = e.target.getAttribute('data-track');
e.target.classList.toggle('active');
// If it's active mute the specified track using its index
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) {
// Change the text and bpm to its corresponding tempo via the slider
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 = '';
})
});
// this will refer to the btn if not placed in a callback
drumKit.playBtn.addEventListener('click', function () {
drumKit.updateBtn();
drumKit.start();
});
drumKit.selects.forEach((select) => {
select.addEventListener('change', (e) => {
drumKit.changeSound(e)
})
})
drumKit.muteBtns.forEach((btn) => {
btn.addEventListener('click', (e) => {
drumKit.mute(e);
});
});
drumKit.tempoSlider.addEventListener('input', (e) => {
drumKit.changeTempo(e);
});
drumKit.tempoSlider.addEventListener('change', (e) => {
drumKit.updateTempo(e);
});