Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
216 lines (188 loc) · 6.24 KB
/
Copy pathscript.js
File metadata and controls
216 lines (188 loc) · 6.24 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
'use strict';
// ============================================================
// Calcolatore Delay — dal BPM ai tempi di delay/riverbero.
// Tutto client-side, nessun dato esce dal dispositivo.
// ms di una nota = (60000 / BPM) * (4 / denominatore)
// puntato = ms * 1.5 terzina = ms * 2/3
// Hz = 1000 / ms
// ============================================================
var MIN_BPM = 20;
var MAX_BPM = 300;
// Righe della tabella: etichetta, nome esteso, denominatore (rispetto al 4/4).
var NOTES = [
{ label: '1/1', denom: 1 },
{ label: '1/2', denom: 2 },
{ label: '1/4', denom: 4 },
{ label: '1/8', denom: 8 },
{ label: '1/16', denom: 16 },
{ label: '1/32', denom: 32 }
];
// --- Riferimenti DOM ---
var bpmInput = document.getElementById('bpm');
var minusBtn = document.getElementById('minusBtn');
var plusBtn = document.getElementById('plusBtn');
var tapBtn = document.getElementById('tapBtn');
var tapHint = document.getElementById('tapHint');
var advBtn = document.getElementById('advBtn');
var advPanel = document.getElementById('advanced');
var rowsEl = document.getElementById('rows');
var unitBtns = document.querySelectorAll('.unit');
var toastEl = document.getElementById('toast');
var unit = 'ms'; // 'ms' | 'hz'
// --- Stato BPM ---
function getBpm() {
var v = parseFloat(bpmInput.value);
if (!isFinite(v)) v = 120;
return v;
}
function clamp(v) {
v = Math.round(v);
if (v < MIN_BPM) v = MIN_BPM;
if (v > MAX_BPM) v = MAX_BPM;
return v;
}
function setBpm(v) {
bpmInput.value = clamp(v);
render();
}
// --- Calcolo ---
function noteMs(bpm, denom, kind) {
var ms = (60000 / bpm) * (4 / denom);
if (kind === 'dot') ms *= 1.5;
else if (kind === 'trip') ms *= 2 / 3;
return ms;
}
// Formatta un valore in ms oppure nel suo equivalente in Hz.
function fmt(ms) {
if (unit === 'hz') {
var hz = 1000 / ms;
return (hz >= 100 ? hz.toFixed(0) : hz.toFixed(2)) + ' Hz';
}
return (ms >= 100 ? ms.toFixed(0) : ms.toFixed(1)) + ' ms';
}
// --- Costruzione tabella ---
function buildRows() {
rowsEl.innerHTML = '';
NOTES.forEach(function (n) {
var row = document.createElement('div');
row.className = 'grid-row';
var note = document.createElement('span');
note.className = 'note';
note.textContent = n.label;
row.appendChild(note);
['straight', 'dot', 'trip'].forEach(function (kind) {
var cell = document.createElement('button');
cell.type = 'button';
cell.className = 'cell';
cell.dataset.denom = n.denom;
cell.dataset.kind = kind;
cell.addEventListener('click', function () { copyCell(cell); });
row.appendChild(cell);
});
rowsEl.appendChild(row);
});
}
// Aggiorna solo i numeri (senza ricostruire il DOM).
function render() {
var bpm = getBpm();
var cells = rowsEl.querySelectorAll('.cell');
cells.forEach(function (cell) {
var ms = noteMs(bpm, parseFloat(cell.dataset.denom), cell.dataset.kind);
cell.textContent = fmt(ms);
});
}
// --- Copia negli appunti ---
function copyCell(cell) {
var text = cell.textContent;
var done = function () {
cell.classList.add('copied');
setTimeout(function () { cell.classList.remove('copied'); }, 350);
showToast('Copiato: ' + text);
};
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(done, function () { fallbackCopy(text, done); });
} else {
fallbackCopy(text, done);
}
}
function fallbackCopy(text, done) {
var ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); done(); } catch (e) {}
document.body.removeChild(ta);
}
var toastTimer = null;
function showToast(msg) {
toastEl.textContent = msg;
toastEl.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(function () { toastEl.classList.remove('show'); }, 1200);
}
// --- Batti il tempo ---
var taps = [];
var tapReset = null;
function onTap() {
var now = performance.now();
// Dopo 2s di pausa ricomincio una nuova serie.
if (taps.length && now - taps[taps.length - 1] > 2000) taps = [];
taps.push(now);
// Feedback visivo a ogni battito.
tapBtn.classList.add('beat');
setTimeout(function () { tapBtn.classList.remove('beat'); }, 90);
if (taps.length >= 2) {
// Media degli ultimi intervalli (max 8 battiti) per stabilità.
var recent = taps.slice(-8);
var sum = 0;
for (var i = 1; i < recent.length; i++) sum += recent[i] - recent[i - 1];
var avg = sum / (recent.length - 1);
var bpm = clamp(60000 / avg);
bpmInput.value = bpm;
render();
tapHint.textContent = bpm + ' BPM da ' + recent.length + ' battiti';
} else {
tapHint.textContent = 'Continua a battere…';
}
clearTimeout(tapReset);
tapReset = setTimeout(function () {
tapHint.textContent = 'Tocca a ritmo per ricavare il BPM.';
taps = [];
}, 2500);
}
// --- Eventi UI ---
minusBtn.addEventListener('click', function () { setBpm(getBpm() - 1); });
plusBtn.addEventListener('click', function () { setBpm(getBpm() + 1); });
bpmInput.addEventListener('input', render);
bpmInput.addEventListener('change', function () { setBpm(getBpm()); });
// Le frecce su/giù muovono il BPM anche tenendo il focus nel campo.
bpmInput.addEventListener('keydown', function (e) {
if (e.key === 'ArrowUp') { e.preventDefault(); setBpm(getBpm() + 1); }
if (e.key === 'ArrowDown') { e.preventDefault(); setBpm(getBpm() - 1); }
});
tapBtn.addEventListener('click', onTap);
// La barra spaziatrice batte il tempo (comodo da tastiera).
window.addEventListener('keydown', function (e) {
if (e.code === 'Space' && document.activeElement !== bpmInput) {
e.preventDefault();
onTap();
}
});
unitBtns.forEach(function (btn) {
btn.addEventListener('click', function () {
unit = btn.dataset.unit;
unitBtns.forEach(function (b) { b.classList.toggle('active', b === btn); });
render();
});
});
// --- Toggle Avanzate (animato) ---
advBtn.addEventListener('click', function () {
var open = advPanel.classList.toggle('open');
tapBtn.classList.toggle('compact', open);
advBtn.textContent = open ? 'Chiudi' : 'Avanzate';
});
// --- Avvio ---
buildRows();
render();