-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtap-tempo-calculator.html
More file actions
334 lines (288 loc) · 12.3 KB
/
Copy pathtap-tempo-calculator.html
File metadata and controls
334 lines (288 loc) · 12.3 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tap Tempo BPM Calculator - Real-time Beat Detection</title>
<meta name="description" content="Tap tempo BPM calculator with configurable sample size and timeout. Detect beats per minute in real-time by tapping a button. Perfect for musicians and DJs.">
<meta name="keywords" content="tap tempo, bpm calculator, beats per minute, tempo calculator, music tempo, rhythm calculator, metronome">
<meta name="author" content="guyru">
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website">
<meta property="og:url" content="https://guyru.github.io/calculators/tap-tempo-calculator.html">
<meta property="og:title" content="Tap Tempo BPM Calculator - Real-time Beat Detection">
<meta property="og:description" content="Tap tempo BPM calculator with configurable sample size and timeout. Detect beats per minute in real-time by tapping a button.">
<!-- Twitter -->
<meta property="twitter:card" content="summary">
<meta property="twitter:url" content="https://guyru.github.io/calculators/tap-tempo-calculator.html">
<meta property="twitter:title" content="Tap Tempo BPM Calculator - Real-time Beat Detection">
<meta property="twitter:description" content="Tap tempo BPM calculator with configurable sample size and timeout. Detect beats per minute in real-time by tapping a button.">
<link rel="stylesheet" href="shared.css">
<style>
.tap-button {
width: 100%;
min-height: 200px;
font-size: 1.5rem;
font-weight: 600;
background: var(--color-primary);
color: white;
border: none;
border-radius: var(--radius-md);
cursor: pointer;
transition: all 0.15s ease;
margin: var(--spacing-md) 0;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
-webkit-tap-highlight-color: transparent;
letter-spacing: 0.025em;
}
.tap-button:hover {
background: var(--color-primary-hover);
}
.tap-button:active,
.tap-button.active {
transform: scale(0.98);
background: var(--color-primary-hover);
}
.bpm-display {
text-align: center;
margin: var(--spacing-md) 0;
}
.bpm-value {
font-size: 3rem;
font-weight: 700;
color: var(--color-text);
margin: var(--spacing-xs) 0;
letter-spacing: -0.025em;
}
.bpm-label {
font-size: 1rem;
color: var(--color-text-secondary);
margin-bottom: var(--spacing-xs);
font-weight: 500;
}
.tap-count {
font-size: 0.875rem;
color: var(--color-text-secondary);
margin-top: var(--spacing-xs);
}
.settings-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: var(--spacing-md);
margin-bottom: var(--spacing-md);
}
.reset-button {
background: var(--color-surface);
color: var(--color-text);
border: 1px solid var(--color-border);
padding: 0.625rem 1.25rem;
border-radius: var(--radius-md);
cursor: pointer;
font-size: 0.9375rem;
transition: all 0.15s ease;
margin-top: var(--spacing-xs);
font-weight: 500;
}
.reset-button:hover {
border-color: var(--color-primary);
color: var(--color-primary);
}
@media (max-width: 768px) {
.settings-grid {
grid-template-columns: 1fr;
gap: var(--spacing-sm);
}
.tap-button {
min-height: 150px;
font-size: 1.25rem;
}
.bpm-value {
font-size: 2.5rem;
}
}
</style>
</head>
<body>
<div class="calculator-container">
<div class="header">
<h1>Tap Tempo BPM Calculator</h1>
<p>Tap the button to detect beats per minute in real-time</p>
</div>
<button class="tap-button" id="tapButton"
aria-label="Tap this button to the beat to calculate BPM">
TAP HERE
</button>
<div class="bpm-display">
<div class="bpm-label">Beats Per Minute</div>
<div class="bpm-value" id="bpmValue" aria-live="polite">--</div>
<div class="tap-count" id="tapCount" aria-live="polite">Tap to start</div>
<button class="reset-button" id="resetButton" aria-label="Reset all taps and start over">
Reset
</button>
</div>
<form id="calculatorForm">
<div class="settings-grid">
<div class="form-group">
<label for="sampleSize">Sample Size</label>
<input type="number" id="sampleSize" min="2" max="20" value="5"
aria-describedby="sampleSizeHelp" aria-label="Number of taps to average for BPM calculation">
<div class="help-text" id="sampleSizeHelp">Number of taps to average (smoothing factor)</div>
</div>
<div class="form-group">
<label for="timeout">Timeout (seconds)</label>
<input type="number" id="timeout" min="1" max="30" value="5"
aria-describedby="timeoutHelp" aria-label="Seconds before clearing tap history">
<div class="help-text" id="timeoutHelp">Seconds of inactivity before clearing taps</div>
</div>
</div>
</form>
<div class="github-links">
<a href="index.html" aria-label="Back to calculators index">← Back to Calculators</a>
<a href="https://github.com/guyru/calculators" target="_blank" rel="noopener noreferrer">View Source</a>
<a href="https://github.com/guyru/calculators/issues" target="_blank" rel="noopener noreferrer">Report Issue</a>
</div>
</div>
<script>
class TapTempo {
constructor(sampleSize = 5, timeoutSeconds = 5) {
this.sampleSize = sampleSize;
this.timeout = timeoutSeconds * 1000; // Convert to milliseconds
this.timestamps = [];
}
updateSettings(sampleSize, timeoutSeconds) {
this.sampleSize = sampleSize;
this.timeout = timeoutSeconds * 1000;
// Clear timestamps when settings change to avoid inconsistency
this.timestamps = [];
}
tap() {
const now = Date.now();
// Remove timestamps older than sample size
if (this.timestamps.length >= this.sampleSize) {
this.timestamps = this.timestamps.slice(0, this.sampleSize - 1);
}
// Reset timestamps if the duration since the last timestamp exceeds the timeout
if (this.timestamps.length > 0) {
const lastTimestamp = this.timestamps[0];
if (now - lastTimestamp > this.timeout) {
this.timestamps = [];
}
}
// Store the timestamp at the beginning of the array (most recent first)
this.timestamps.unshift(now);
// Calculate average duration between taps
if (this.timestamps.length > 1) {
const totalDuration = this.timestamps[0] - this.timestamps[this.timestamps.length - 1];
const averageDuration = totalDuration / (this.timestamps.length - 1);
const bpm = 60000 / averageDuration; // Convert to BPM
return {
bpm: bpm,
tapCount: this.timestamps.length
};
}
return {
bpm: null,
tapCount: this.timestamps.length
};
}
reset() {
this.timestamps = [];
}
}
// DOM elements
const tapButton = document.getElementById('tapButton');
const bpmValue = document.getElementById('bpmValue');
const tapCount = document.getElementById('tapCount');
const resetButton = document.getElementById('resetButton');
const sampleSizeInput = document.getElementById('sampleSize');
const timeoutInput = document.getElementById('timeout');
// URL fragment state management
function parseHash() {
const hash = window.location.hash.slice(1);
const params = {};
if (hash) {
hash.split('&').forEach(part => {
const [key, value] = part.split('=');
if (key && value !== undefined) {
params[decodeURIComponent(key)] = decodeURIComponent(value);
}
});
}
return params;
}
function updateHash() {
const params = {};
if (sampleSizeInput.value && sampleSizeInput.value !== '5') {
params.sampleSize = sampleSizeInput.value;
}
if (timeoutInput.value && timeoutInput.value !== '5') {
params.timeout = timeoutInput.value;
}
const hashString = Object.entries(params)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
history.replaceState(null, '', hashString ? '#' + hashString : window.location.pathname);
}
function loadFromHash() {
const params = parseHash();
if (Object.keys(params).length === 0) return;
if (params.sampleSize) sampleSizeInput.value = params.sampleSize;
if (params.timeout) timeoutInput.value = params.timeout;
}
// Load settings from URL hash
loadFromHash();
// Initialize the tap tempo calculator with current settings
let tapTempo = new TapTempo(
parseInt(sampleSizeInput.value) || 5,
parseInt(timeoutInput.value) || 5
);
// Handle tap button clicks
tapButton.addEventListener('click', function() {
const result = tapTempo.tap();
if (result.bpm !== null) {
bpmValue.textContent = Math.round(result.bpm); // Round to integer
tapCount.textContent = '';
} else {
bpmValue.textContent = '--';
tapCount.textContent = result.tapCount === 1 ? 'Tap again for BPM' : 'Keep tapping';
}
});
// Handle reset button
resetButton.addEventListener('click', function() {
tapTempo.reset();
bpmValue.textContent = '--';
tapCount.textContent = 'Tap to start';
});
// Handle settings changes
function updateSettings() {
const sampleSize = parseInt(sampleSizeInput.value);
const timeout = parseInt(timeoutInput.value);
if (sampleSize >= 2 && sampleSize <= 20 && timeout >= 1 && timeout <= 30) {
tapTempo.updateSettings(sampleSize, timeout);
// Reset display when settings change
bpmValue.textContent = '--';
tapCount.textContent = 'Tap to start';
updateHash();
}
}
sampleSizeInput.addEventListener('input', updateSettings);
timeoutInput.addEventListener('input', updateSettings);
// Add keyboard support for spacebar tapping
document.addEventListener('keydown', function(event) {
if (event.code === 'Space') {
event.preventDefault(); // Prevent page scrolling
tapButton.classList.add('active');
tapButton.click();
}
});
document.addEventListener('keyup', function(event) {
if (event.code === 'Space') {
tapButton.classList.remove('active');
}
});
</script>
</body>
</html>