-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmetronome.js
172 lines (136 loc) · 4.47 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
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
/**
* How often we should beep
*/
var beepInterval;
const context = new (window.AudioContext || window.webkitAudioContext)();
/**
* Low: The beep that is made on every beat but the main beat
* High: The beep that is made on the first beat of the bar
*/
const frequencies = {
low: 880.0,
high: 1760.0
};
const elements = {
noteType: document.getElementById("note-type"),
beatType: document.getElementById("beat-type"),
tempo: document.getElementById("tempo"),
tempoValue: document.getElementById("tempo-value"),
toggleButton: document.getElementById("toggle-button"),
beatCounter: document.getElementById("beat-counter"),
toggleOptions: document.getElementById("toggle-options"),
closeOptions: document.getElementById("close-options"),
options: document.getElementById("options"),
volume: document.getElementById("volume"),
waveform: document.getElementById("waveform"),
tapButton: document.getElementById("tap-button")
};
/**
* timesThrough: The amount of beeps made. This is counted so
* we can find out the first beat of the bar.
* playSound: Whether or not we should be beeping
*/
const settings = {
timesThrough: -1,
playSound: false
};
elements.toggleButton.addEventListener('click', togglePlay);
elements.toggleOptions.addEventListener('click', function () {
elements.options.classList.toggle('hidden');
});
elements.beatType.addEventListener('input', update);
// tempo: update display value while dragged and update beat when release
elements.tempo.addEventListener('input', updateTempoValue);
elements.tempo.addEventListener('change', update);
elements.closeOptions.addEventListener('click', (e) => {
elements.options.classList.toggle('hidden');
});
elements.tapButton.addEventListener('click', updateTapTempo);
function updateTempoValue() {
elements.tempoValue.innerText = `at ${elements.tempo.value} bpm`;
}
function togglePlay() {
settings.playSound = !settings.playSound;
update(settings.playSound);
}
function updateBeatCounter() {
const val = elements.noteType.value;
elements.beatCounter.innerText = `${(settings.timesThrough % val) + 1}`;
}
/**
* Updates the text of the button.
* @param {Boolean} shouldPlaySound
*/
function updateToggleButtonText(shouldPlaySound) {
let buttonText = "start";
if (shouldPlaySound) {
buttonText = "stop";
}
return buttonText;
}
function update(shouldPlaySound) {
updateTempoValue();
updateBeatCounter();
elements.toggleButton.innerText = updateToggleButtonText(shouldPlaySound);
clearInterval(beepInterval);
if (shouldPlaySound) {
// Tick once before starting the interval, to make the metronome
// start immediately when pressing play.
tick();
return updateBeepInterval(elements.tempo.value, elements.beatType.value);
}
settings.timesThrough = -1;
}
var lastTap;
function updateTapTempo() {
var tap = new Date();
lastTap = lastTap || tap;
var diffInMillis = Math.abs((lastTap - tap) / 1000);
lastTap = tap;
var bpm = 60 / diffInMillis;
elements.tempo.value = bpm;
tick();
update();
updateTempoValue();
}
function updateBeepInterval(tempo, beatType) {
if (tempo > 0) {
const interval = parseInt(bpmToMs(tempo, beatType));
beepInterval = setInterval(tick, interval);
}
}
function bpmToMs(beatsPerMinute, beatType) {
const noteDurations = {
1: beatsPerMinute / 4,
2: beatsPerMinute / 2,
4: beatsPerMinute,
8: beatsPerMinute * 2,
16: beatsPerMinute * 4,
32: beatsPerMinute * 8
};
const milliseconds = (60000 / noteDurations[beatType]);
return milliseconds;
}
function shouldBeep (timesThrough, noteType) {
return timesThrough % noteType === 0;
}
function tick() {
settings.timesThrough++;
updateBeatCounter();
const oscillator = context.createOscillator();
const gain = context.createGain();
gain.gain.value = elements.volume.value;
oscillator.type = elements.waveform.value;
oscillator.frequency.value = frequencies.low;
oscillator.connect(gain);
gain.connect(context.destination);
timeToBeep = shouldBeep(settings.timesThrough, elements.noteType.value)
if (timeToBeep) {
oscillator.frequency.value = frequencies.high
}
oscillator.start();
oscillator.stop(context.currentTime + 0.1);
if (gain.gain.value > 0) {
gain.gain.exponentialRampToValueAtTime(0.00001, context.currentTime + .10)
}
}