diff --git a/simulator/static/app.js b/simulator/static/app.js index dca6b71..7cba5a1 100644 --- a/simulator/static/app.js +++ b/simulator/static/app.js @@ -920,8 +920,29 @@ function devLiveReload() { // (e.g. on an altitude change). audio on = the current scale's soundtrack. const aud = $("aud"); let audUrl = null; // soundtrack url currently loaded (null = silent) +let audLastErr = ""; // last play() rejection / element error (for the readout) const FADE_MS = 500; +// --- Live audio status readout (diagnostic) — so a "no sound" report is legible --- +const AUD_ERR = ["", "aborted", "network", "decode", "format-not-supported"]; +function audioStatusText() { + if (!$("audio").checked) return "audio: off"; + if (aud.error) return `audio: MEDIA ERROR ${aud.error.code} (${AUD_ERR[aud.error.code] || "?"})`; + if (audLastErr) return "audio: " + audLastErr; + if (aud.paused) return `audio: on, PAUSED (readyState ${aud.readyState})`; + return `audio: ▶ playing ${aud.currentTime.toFixed(1)}s · vol ${Math.round(aud.volume * 100)}% · rs ${aud.readyState}`; +} +function updateAudioStatus() { + const el = $("audio-status"); + if (!el) return; + el.textContent = audioStatusText(); + const playing = $("audio").checked && !aud.paused && !aud.error && !audLastErr; + el.classList.toggle("playing", playing); + el.classList.toggle("blocked", !!aud.error || !!audLastErr); +} +aud.addEventListener("error", () => { audLastErr = ""; updateAudioStatus(); }); +setInterval(updateAudioStatus, 400); + function fadeVolume(el, to, ms, done) { // setInterval (not requestAnimationFrame): rAF throttles/pauses when the tab is // backgrounded, which would stall the gain ramp; a timer fires regardless. @@ -947,8 +968,12 @@ function soundtrackUrl() { function playUrl(url) { aud.src = mediaUrl(url); aud.volume = 0; + audLastErr = ""; const pr = aud.play(); - if (pr) pr.catch(() => {}); + if (pr) { + pr.then(() => { audLastErr = ""; updateAudioStatus(); }) + .catch((e) => { audLastErr = "play BLOCKED: " + ((e && e.name) || e); updateAudioStatus(); }); + } fadeVolume(aud, 1, FADE_MS); } diff --git a/simulator/static/index.html b/simulator/static/index.html index 974f3e1..7f33646 100644 --- a/simulator/static/index.html +++ b/simulator/static/index.html @@ -40,6 +40,15 @@ Audio +