The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
6.1 KiB
JavaScript
189 lines
6.1 KiB
JavaScript
// Audio modal: live beat indicators and level from the controller's microphone detector, plus a
|
|
// simulated beat (a fixed BPM sent to the drivers, with the microphone ignored while it runs).
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const modal = document.getElementById('audio-modal');
|
|
const openBtn = document.getElementById('audio-btn');
|
|
if (!modal || !openBtn) return;
|
|
|
|
const el = (id) => document.getElementById(id);
|
|
const beatBtn = el('audio-modal-beat-sync');
|
|
const bpmValue = el('audio-bpm-value');
|
|
const beatReadout = el('audio-modal-beat-readout');
|
|
const barPhase = el('audio-bar-phase-value');
|
|
const levelBar = el('audio-input-level-bar');
|
|
const levelReadout = el('audio-level-readout');
|
|
const simInput = el('audio-simulated-bpm');
|
|
const simStatus = el('audio-sim-status');
|
|
const simStart = el('audio-sim-start-btn');
|
|
const simStop = el('audio-sim-stop-btn');
|
|
|
|
const SIM_MIN = 60;
|
|
const SIM_MAX = 200;
|
|
let events = null;
|
|
let pollTimer = null;
|
|
let simRunning = false;
|
|
let simBpm = 120;
|
|
let simTimer = null;
|
|
let simTick = 0;
|
|
|
|
const clampSim = (n) => Math.max(SIM_MIN, Math.min(SIM_MAX, Math.round(Number(n) || 120)));
|
|
|
|
function flash(simulated) {
|
|
const cls = simulated ? 'flash-simulated' : 'flash';
|
|
beatBtn.classList.add(cls);
|
|
setTimeout(() => beatBtn.classList.remove(cls), 90);
|
|
}
|
|
|
|
function showBar(bar, meter, sure) {
|
|
if (!bar) {
|
|
barPhase.textContent = '';
|
|
return;
|
|
}
|
|
barPhase.textContent = `${bar}/${meter}`;
|
|
barPhase.classList.toggle('is-downbeat', bar === 1);
|
|
beatBtn.classList.toggle('guess', !sure);
|
|
}
|
|
|
|
function showBpm(bpm, simulated) {
|
|
bpmValue.textContent = bpm ? bpm.toFixed(simulated ? 0 : 1) : '--';
|
|
beatBtn.classList.toggle('audio-simulated', simulated);
|
|
}
|
|
|
|
// --- detector events -------------------------------------------------------------------
|
|
function openEvents() {
|
|
if (events || !window.EventSource) return;
|
|
events = new EventSource('/beats');
|
|
events.addEventListener('beat', (e) => {
|
|
if (simRunning) return;
|
|
const s = JSON.parse(e.data);
|
|
flash(false);
|
|
showBpm(s.bpm, false);
|
|
beatReadout.textContent = `#${s.n}`;
|
|
showBar(s.bar, s.meter, s.sure);
|
|
});
|
|
events.addEventListener('tempo', (e) => {
|
|
if (simRunning) return;
|
|
const s = JSON.parse(e.data);
|
|
showBpm(s.locked ? s.bpm : 0, false);
|
|
});
|
|
}
|
|
|
|
function closeEvents() {
|
|
if (events) {
|
|
events.close();
|
|
events = null;
|
|
}
|
|
}
|
|
|
|
// --- level and simulation state --------------------------------------------------------
|
|
async function poll() {
|
|
try {
|
|
const s = await (await fetch('/status', { cache: 'no-store' })).json();
|
|
const pct = Math.max(0, Math.min(100, ((s.mic_rms_dbfs + 90) / 90) * 100));
|
|
levelBar.style.width = `${pct}%`;
|
|
levelBar.parentElement.setAttribute('aria-valuenow', String(Math.round(pct)));
|
|
levelReadout.textContent = `${s.mic_rms_dbfs.toFixed(1)} dBFS · ${s.mic_spl_db.toFixed(0)} dB`;
|
|
const t = await (await fetch('/test/beats', { cache: 'no-store' })).json();
|
|
setSimRunning(!!t.running);
|
|
} catch (e) {
|
|
/* controller briefly unreachable */
|
|
}
|
|
if (modal.classList.contains('active')) pollTimer = setTimeout(poll, 500);
|
|
}
|
|
|
|
function setSimRunning(on) {
|
|
if (on === simRunning && (on === (simTimer !== null))) return;
|
|
simRunning = on;
|
|
simStart.disabled = on;
|
|
simStop.disabled = !on;
|
|
simStatus.textContent = on ? `Sending ${simBpm} BPM to the drivers; the microphone is ignored.` : '';
|
|
clearInterval(simTimer);
|
|
simTimer = null;
|
|
if (on) {
|
|
// The controller does not echo generated beats back, so the indicator runs on its own clock
|
|
simTick = 0;
|
|
showBpm(simBpm, true);
|
|
simTimer = setInterval(() => {
|
|
simTick++;
|
|
flash(true);
|
|
beatReadout.textContent = `#${simTick}`;
|
|
showBar(((simTick - 1) % 4) + 1, 4, true);
|
|
}, 60000 / simBpm);
|
|
} else {
|
|
showBpm(0, false);
|
|
beatReadout.textContent = '';
|
|
showBar(0);
|
|
}
|
|
}
|
|
|
|
async function startSim() {
|
|
simBpm = clampSim(simInput.value);
|
|
simInput.value = simBpm;
|
|
simStatus.textContent = '';
|
|
const res = await fetch('/test/beats', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ bpm: simBpm, count: 0 }),
|
|
});
|
|
if (!res.ok) {
|
|
simStatus.textContent = 'The controller refused that BPM.';
|
|
return;
|
|
}
|
|
setSimRunning(false);
|
|
setSimRunning(true);
|
|
fetch('/settings/settings', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ audio_simulated_bpm: simBpm }),
|
|
}).catch(() => {});
|
|
}
|
|
|
|
async function stopSim() {
|
|
await fetch('/test/beats/stop', { method: 'POST' });
|
|
setSimRunning(false);
|
|
}
|
|
|
|
function tap() {
|
|
fetch('/tap', { method: 'POST' }).catch(() => {});
|
|
}
|
|
|
|
async function loadSimBpm() {
|
|
try {
|
|
const s = await (await fetch('/settings', { cache: 'no-store' })).json();
|
|
if (s.audio_simulated_bpm != null) simInput.value = clampSim(s.audio_simulated_bpm);
|
|
} catch (e) {
|
|
/* keep the default */
|
|
}
|
|
}
|
|
|
|
// --- open and close --------------------------------------------------------------------
|
|
function open() {
|
|
modal.classList.add('active');
|
|
simStop.disabled = true;
|
|
loadSimBpm();
|
|
openEvents();
|
|
clearTimeout(pollTimer);
|
|
poll();
|
|
}
|
|
|
|
function close() {
|
|
modal.classList.remove('active');
|
|
closeEvents();
|
|
clearTimeout(pollTimer);
|
|
// The simulation keeps running on the controller; only the local indicator stops
|
|
clearInterval(simTimer);
|
|
simTimer = null;
|
|
}
|
|
|
|
openBtn.addEventListener('click', open);
|
|
el('audio-close-btn').addEventListener('click', close);
|
|
simStart.addEventListener('click', startSim);
|
|
simStop.addEventListener('click', stopSim);
|
|
el('audio-tap-btn').addEventListener('click', tap);
|
|
beatBtn.addEventListener('click', tap);
|
|
document.addEventListener('keydown', (e) => {
|
|
if (!modal.classList.contains('active') || e.repeat) return;
|
|
if ((e.key === 't' || e.key === 'T') && !/INPUT|TEXTAREA|SELECT/.test(e.target.tagName)) tap();
|
|
});
|
|
});
|