-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
149 lines (126 loc) · 3.43 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
// variables
const AccentClick = new Howl({
src: ["clicks/AccentClick.mp3"],
html5: true,
});
const RegularClick = new Howl({
src: ["clicks/RegularClick.mp3"],
html5: true,
});
const circles = document.querySelectorAll(".circle");
const tempoDisplay = document.querySelector(".tempo");
const tempoText = document.querySelector(".tempo-text");
const increaseTempoButton = document.querySelector(".increase-tempo");
const decreaseTempoButton = document.querySelector(".decrease-tempo");
const tempoSlider = document.querySelector(".slider");
const startStopButton = document.querySelector(".start-stop");
const addBeatsButton = document.querySelector(".add-beats");
const subtractBeatsButton = document.querySelector(".subtract-beats");
const measuresDisplay = document.querySelector(".measure-count");
let tempo = 150;
let measures = 4;
let tempoTextString = "Moderate";
// event listeners section
// +/- BPM buttons
increaseTempoButton.addEventListener("click", () => {
if (tempo >= 280) {
return;
}
tempo += 1;
updateMetronome();
});
decreaseTempoButton.addEventListener("click", () => {
if (tempo <= 20) {
return;
}
tempo -= 1;
updateMetronome();
});
// slider handling
tempoSlider.addEventListener("input", () => {
tempo = +tempoSlider.value;
updateMetronome();
});
// +/- measures buttons
addBeatsButton.addEventListener("click", () => {
if (measures >= 12) {
return;
}
measures += 1;
measuresDisplay.textContent = measures;
console.log(measures);
});
subtractBeatsButton.addEventListener("click", () => {
if (measures <= 1) {
return;
}
measures -= 1;
measuresDisplay.textContent = measures;
console.log(measures);
});
// descriptions according to BPM
function updateMetronome() {
if (tempo >= 20 && tempo <= 45) {
tempoTextString = "Grave";
}
if (tempo > 45 && tempo <= 60) {
tempoTextString = "Lento";
}
if (tempo > 60 && tempo <= 66) {
tempoTextString = "Larghetto";
}
if (tempo > 66 && tempo <= 76) {
tempoTextString = "Adagio";
}
if (tempo > 76 && tempo <= 108) {
tempoTextString = "Andante";
}
if (tempo > 108 && tempo <= 120) {
tempoTextString = "Moderato";
}
if (tempo > 120 && tempo <= 156) {
tempoTextString = "Allegro";
}
if (tempo > 156 && tempo <= 176) {
tempoTextString = "Vivace";
}
if (tempo > 176 && tempo <= 200) {
tempoTextString = "Presto";
}
if (tempo > 200 && tempo <= 280) {
tempoTextString = "Prestissimo";
}
tempoDisplay.textContent = tempo;
tempoSlider.value = tempo;
tempoText.textContent = tempoTextString;
console.log(tempo);
}
// Start/stop handling
let isPlaying = false;
let timeout;
let lastClick = performance.now();
let circleIndex = 0;
function playMetronome() {
const now = performance.now();
RegularClick.play();
const expected = 60000 / tempo;
const actual = now - lastClick;
const diff = new Intl.NumberFormat("en-US", {
signDisplay: "exceptZero",
}).format(actual - expected);
console.log("click", { expected, actual, diff });
circles.forEach((circle) => circle.classList.remove("active"));
circles[circleIndex].classList.add("active");
circleIndex = (circleIndex + 1) % circles.length;
lastClick = now;
timeout = setTimeout(playMetronome, 60000 / tempo);
}
startStopButton.addEventListener("click", () => {
startStopButton.classList.toggle("active");
isPlaying = !isPlaying;
if (isPlaying) {
playMetronome();
} else {
clearTimeout(timeout);
}
});