-
Notifications
You must be signed in to change notification settings - Fork 0
/
metronome.js
129 lines (119 loc) · 2.99 KB
/
metronome.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
import Timer from "./timer.js";
const tempoDisplay = document.querySelector(".tempo");
const tempoText = document.querySelector(".tempo-text");
const decreaseTempoBtn = document.querySelector(".decrease-tempo");
const increaseTempoBtn = document.querySelector(".increase-tempo");
const tempoSlider = document.querySelector(".slider");
const startStopBtn = document.querySelector(".start-stop");
const subtractBeats = document.querySelector(".subtract-beats");
const addBeats = document.querySelector(".add-beats");
const measureCount = document.querySelector(".measure-count");
const click1 = new Audio("click1.mp3");
const click2 = new Audio("click2.mp3");
let bpm = 90;
let beatsPerMeasure = 4;
let count = 0;
let isRunning = false;
let tempoTextString = "Medium";
decreaseTempoBtn.addEventListener("click", () => {
if (bpm <= 20) {
return;
}
bpm--;
validateTempo();
updateMetronome();
});
increaseTempoBtn.addEventListener("click", () => {
if (bpm >= 280) {
return;
}
bpm++;
validateTempo();
updateMetronome();
});
tempoSlider.addEventListener("input", () => {
bpm = tempoSlider.value;
validateTempo();
updateMetronome();
});
subtractBeats.addEventListener("click", () => {
if (beatsPerMeasure <= 2) {
return;
}
beatsPerMeasure--;
measureCount.textContent = beatsPerMeasure;
count = 0;
});
addBeats.addEventListener("click", () => {
if (beatsPerMeasure >= 12) {
return;
}
beatsPerMeasure++;
measureCount.textContent = beatsPerMeasure;
count = 0;
});
startStopBtn.addEventListener("click", () => {
count = 0;
if (!isRunning) {
metronome.start();
isRunning = true;
startStopBtn.textContent = "STOP";
} else {
metronome.stop();
isRunning = false;
startStopBtn.textContent = "START";
}
});
function updateMetronome() {
tempoDisplay.textContent = bpm;
tempoSlider.value = bpm;
metronome.timeInterval = 60000 / bpm;
if (bpm <= 40) {
tempoTextString = "Super Slow";
}
if (bpm > 40 && bpm < 80) {
tempoTextString = "Slow";
}
if (bpm > 80 && bpm < 120) {
tempoTextString = "Getting there";
}
if (bpm > 120 && bpm < 180) {
tempoTextString = "Nice and Steady";
}
if (bpm > 180 && bpm < 220) {
tempoTextString = "Rock n' Roll";
}
if (bpm > 220 && bpm < 240) {
tempoTextString = "Funky Stuff";
}
if (bpm > 240 && bpm < 260) {
tempoTextString = "Relax Dude";
}
if (bpm > 260 && bpm <= 280) {
tempoTextString = "Eddie Van Halen";
}
tempoText.textContent = tempoTextString;
}
function validateTempo() {
if (bpm <= 20) {
return;
}
if (bpm >= 280) {
return;
}
}
function playClick() {
console.log(count);
if (count === beatsPerMeasure) {
count = 0;
}
if (count === 0) {
click1.play();
click1.currentTime = 0;
} else {
click2.play();
click2.currentTime = 0;
}
count++;
}
const metronome = new Timer(playClick, 60000 / bpm, { immediate: true });