browser audio processing phase 1
This commit is contained in:
@@ -1,31 +1,215 @@
|
||||
const t = window.BOOMCAST_I18N || {};
|
||||
import { BrowserSubmissionProcessor } from "./browser-submission-processor.js";
|
||||
|
||||
const form = document.querySelector('.upload-form');
|
||||
const dropZone = document.querySelector('#drop-zone');
|
||||
const fileInput = document.querySelector('#audio-file');
|
||||
const fileName = document.querySelector('#file-name');
|
||||
const status = document.querySelector('#upload-status');
|
||||
const showFile = (file) => { fileName.textContent = file ? file.name : (t.noFile || 'No file selected'); };
|
||||
const form = document.querySelector(".upload-form");
|
||||
|
||||
fileInput.addEventListener('change', () => showFile(fileInput.files[0]));
|
||||
['dragenter', 'dragover'].forEach((eventName) => dropZone.addEventListener(eventName, (event) => { event.preventDefault(); dropZone.classList.add('dragging'); }));
|
||||
['dragleave', 'drop'].forEach((eventName) => dropZone.addEventListener(eventName, (event) => { event.preventDefault(); dropZone.classList.remove('dragging'); }));
|
||||
dropZone.addEventListener('drop', (event) => { const [file] = event.dataTransfer.files; if (!file) return; fileInput.files = event.dataTransfer.files; showFile(file); });
|
||||
form.addEventListener('htmx:beforeRequest', () => { status.textContent = (t.analysing || 'Analysing your recording…'); form.setAttribute('aria-busy', 'true'); });
|
||||
form.addEventListener('htmx:afterRequest', () => { status.textContent = ''; form.removeAttribute('aria-busy'); });
|
||||
form.addEventListener('htmx:responseError', () => { status.textContent = (t.analysisFailed || 'Analysis could not be completed.'); });
|
||||
document.body.addEventListener('htmx:beforeSwap', (event) => {
|
||||
if (event.detail.xhr.status >= 400) {
|
||||
event.detail.shouldSwap = true;
|
||||
event.detail.isError = false;
|
||||
if (form) {
|
||||
const t = window.BOOMCAST_I18N || {};
|
||||
const dropZone = form.querySelector("#drop-zone");
|
||||
const fileInput = form.querySelector("#audio-file");
|
||||
const fileName = form.querySelector("#file-name");
|
||||
const circle = form.querySelector("#upload-progress-circle");
|
||||
const iconContent = form.querySelector("#upload-icon-content");
|
||||
const submit = form.querySelector('button[type="submit"]');
|
||||
const cancel = form.querySelector(".upload-cancel");
|
||||
const results = form.querySelector("#classification-results");
|
||||
const status = document.querySelector("#upload-status");
|
||||
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
|
||||
const states = ["idle", "file-selected", "uploading", "processing", "success", "error", "cancelled"];
|
||||
let request = null;
|
||||
let busy = false;
|
||||
let uploadComplete = false;
|
||||
let processingTimer = null;
|
||||
let preparing = false;
|
||||
const browserProcessor = new BrowserSubmissionProcessor();
|
||||
if (!browserProcessor.isEnabled()) {
|
||||
console.log("[BoomCast][BrowserBeta] Feature disabled; using legacy submission flow.");
|
||||
}
|
||||
|
||||
function setUploadState(state, { progress = null, message = "" } = {}) {
|
||||
for (const name of states) form.classList.toggle(`audio-upload--${name}`, name === state);
|
||||
const active = state === "uploading" || state === "processing";
|
||||
busy = active;
|
||||
submit.disabled = active;
|
||||
fileInput.disabled = active;
|
||||
cancel.hidden = !active;
|
||||
if (active) form.setAttribute("aria-busy", "true");
|
||||
else form.removeAttribute("aria-busy");
|
||||
|
||||
if (state === "uploading") {
|
||||
circle.setAttribute("role", "progressbar");
|
||||
circle.setAttribute("aria-label", t.uploadingAudio || "Uploading audio");
|
||||
circle.setAttribute("aria-valuemin", "0");
|
||||
circle.setAttribute("aria-valuemax", "100");
|
||||
if (progress === null) {
|
||||
circle.removeAttribute("aria-valuenow");
|
||||
circle.setAttribute("aria-valuetext", t.uploadingAudio || "Uploading audio");
|
||||
iconContent.textContent = "↑";
|
||||
} else {
|
||||
circle.setAttribute("aria-valuenow", String(progress));
|
||||
circle.setAttribute("aria-valuetext", `${t.uploadingAudio || "Uploading audio"}: ${progress}%`);
|
||||
circle.style.setProperty("--upload-progress", `${progress}%`);
|
||||
iconContent.textContent = `${progress}%`;
|
||||
}
|
||||
status.textContent = progress === null ? (t.uploadingAudio || "Uploading audio…") : "";
|
||||
} else {
|
||||
circle.removeAttribute("role");
|
||||
circle.removeAttribute("aria-label");
|
||||
circle.removeAttribute("aria-valuemin");
|
||||
circle.removeAttribute("aria-valuemax");
|
||||
circle.removeAttribute("aria-valuenow");
|
||||
circle.removeAttribute("aria-valuetext");
|
||||
iconContent.textContent = state === "processing" ? "◉" : state === "success" ? "✓" : state === "error" ? "!" : "↑";
|
||||
status.textContent = message || ({
|
||||
processing: t.uploadCompleteAnalysing || "Upload complete. Analysing audio…",
|
||||
success: t.analysisComplete || "Analysis complete.",
|
||||
cancelled: t.uploadCancelled || "Upload cancelled.",
|
||||
}[state] || "");
|
||||
}
|
||||
}
|
||||
|
||||
function showFile(file) {
|
||||
fileName.textContent = file ? file.name : (t.noFile || "No file selected");
|
||||
if (!busy) setUploadState(file ? "file-selected" : "idle");
|
||||
if (file) console.log("[boomcast-upload] file selected", { size: file.size, type: file.type });
|
||||
}
|
||||
|
||||
function fail(message, detail) {
|
||||
clearTimeout(processingTimer);
|
||||
console.error("[boomcast-upload] upload failed", detail);
|
||||
setUploadState("error", { message });
|
||||
request = null;
|
||||
}
|
||||
|
||||
function finishUpload(xhr) {
|
||||
if (uploadComplete) return;
|
||||
uploadComplete = true;
|
||||
console.log("[boomcast-upload] upload complete, awaiting classifier");
|
||||
// Give a real 100% progress event one paint before showing the separate server state.
|
||||
processingTimer = window.setTimeout(() => {
|
||||
if (request === xhr && busy) setUploadState("processing");
|
||||
}, 120);
|
||||
}
|
||||
|
||||
function handleResponse(xhr) {
|
||||
clearTimeout(processingTimer);
|
||||
request = null;
|
||||
let fragment;
|
||||
try {
|
||||
fragment = new DOMParser().parseFromString(xhr.responseText, "text/html");
|
||||
} catch (error) {
|
||||
fail(t.analysisFailed || "Analysis could not be completed.", error);
|
||||
return;
|
||||
}
|
||||
const card = fragment.querySelector(".results-card");
|
||||
if (!card) {
|
||||
fail(t.analysisFailed || "Analysis could not be completed.", `Unexpected response (HTTP ${xhr.status})`);
|
||||
return;
|
||||
}
|
||||
results.replaceChildren(document.importNode(card, true));
|
||||
window.htmx?.process(results);
|
||||
if (xhr.status >= 400 || card.classList.contains("error-card")) {
|
||||
const message = card.querySelector("p")?.textContent || (t.analysisFailed || "Analysis could not be completed.");
|
||||
console.error("[boomcast-upload] classification failed", { status: xhr.status, message });
|
||||
setUploadState("error", { message });
|
||||
return;
|
||||
}
|
||||
console.log("[boomcast-upload] classification response received", xhr.status);
|
||||
setUploadState("success");
|
||||
if (form.dataset.scrollToResults === "true") {
|
||||
results.scrollIntoView({ behavior: reducedMotion.matches ? "instant" : "smooth", block: "start" });
|
||||
}
|
||||
}
|
||||
|
||||
fileInput.addEventListener("change", () => showFile(fileInput.files[0]));
|
||||
["dragenter", "dragover"].forEach((name) => dropZone.addEventListener(name, (event) => {
|
||||
event.preventDefault();
|
||||
if (!busy) dropZone.classList.add("dragging");
|
||||
}));
|
||||
["dragleave", "drop"].forEach((name) => dropZone.addEventListener(name, (event) => {
|
||||
event.preventDefault();
|
||||
dropZone.classList.remove("dragging");
|
||||
}));
|
||||
dropZone.addEventListener("drop", (event) => {
|
||||
if (busy || !event.dataTransfer.files.length) return;
|
||||
fileInput.files = event.dataTransfer.files;
|
||||
showFile(fileInput.files[0]);
|
||||
});
|
||||
|
||||
function startUpload(file, metadata = null) {
|
||||
const formData = new FormData(form);
|
||||
formData.set("audio", file);
|
||||
if (metadata) formData.set("browser_processing_metadata", JSON.stringify(metadata));
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
request = xhr;
|
||||
uploadComplete = false;
|
||||
results.replaceChildren();
|
||||
setUploadState("uploading");
|
||||
console.log("[boomcast-upload] upload started", { size: file.size, type: file.type });
|
||||
xhr.open("POST", form.action);
|
||||
xhr.upload.addEventListener("progress", (progressEvent) => {
|
||||
if (uploadComplete) return;
|
||||
if (!progressEvent.lengthComputable || progressEvent.total <= 0) return;
|
||||
const percent = Math.min(100, Math.round(progressEvent.loaded / progressEvent.total * 100));
|
||||
if (percent !== Number(circle.getAttribute("aria-valuenow"))) {
|
||||
setUploadState("uploading", { progress: percent });
|
||||
console.log("[boomcast-upload] upload progress", percent);
|
||||
}
|
||||
if (progressEvent.loaded >= progressEvent.total && !uploadComplete) {
|
||||
finishUpload(xhr);
|
||||
}
|
||||
});
|
||||
window.addEventListener('pageshow', (event) => {
|
||||
if (event.persisted) {
|
||||
fileInput.value = '';
|
||||
showFile();
|
||||
status.textContent = '';
|
||||
document.querySelector('#classification-results').replaceChildren();
|
||||
}
|
||||
xhr.upload.addEventListener("load", () => {
|
||||
finishUpload(xhr);
|
||||
});
|
||||
|
||||
xhr.addEventListener("load", () => handleResponse(xhr));
|
||||
xhr.addEventListener("error", () => fail(t.uploadFailed || "Upload failed. Check your connection and try again.", "Network error"));
|
||||
xhr.addEventListener("timeout", () => fail(t.uploadFailed || "Upload failed. Check your connection and try again.", "Request timed out"));
|
||||
xhr.addEventListener("abort", () => {
|
||||
clearTimeout(processingTimer);
|
||||
request = null;
|
||||
setUploadState("cancelled");
|
||||
console.log("[boomcast-upload] upload cancelled");
|
||||
});
|
||||
xhr.send(formData);
|
||||
}
|
||||
|
||||
// Capture before HTMX's bubbling submit listener; the hx-* attributes remain a fallback.
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
if (busy || preparing) return;
|
||||
if (!form.reportValidity()) return;
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
if (!browserProcessor.isEnabled()) {
|
||||
startUpload(file);
|
||||
return;
|
||||
}
|
||||
preparing = true;
|
||||
submit.disabled = true;
|
||||
status.textContent = t.preparingRecording || "Preparing recording…";
|
||||
browserProcessor.prepare(file).then(({ file: preparedFile, metadata }) => {
|
||||
preparing = false;
|
||||
submit.disabled = false;
|
||||
startUpload(preparedFile || file, metadata);
|
||||
}).catch((error) => {
|
||||
console.error("[BoomCast][BrowserBeta] Initialisation failed:", error);
|
||||
browserProcessor.cleanup();
|
||||
console.log("[BoomCast][BrowserBeta] Restoring legacy submission flow.");
|
||||
preparing = false;
|
||||
submit.disabled = false;
|
||||
startUpload(file);
|
||||
});
|
||||
}, true);
|
||||
|
||||
cancel.addEventListener("click", () => request?.abort());
|
||||
window.addEventListener("pageshow", (event) => {
|
||||
if (event.persisted) {
|
||||
request?.abort();
|
||||
fileInput.value = "";
|
||||
results.replaceChildren();
|
||||
showFile();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
const PREFIX = "[BoomCast][BrowserBeta]";
|
||||
|
||||
export function readBrowserProcessingConfig() {
|
||||
const element = document.querySelector("#boomcast-browser-config");
|
||||
if (!element) return { browserProcessingBeta: false };
|
||||
try {
|
||||
return JSON.parse(element.textContent);
|
||||
} catch (error) {
|
||||
console.error(`${PREFIX} Configuration unavailable:`, error);
|
||||
return { browserProcessingBeta: false };
|
||||
}
|
||||
}
|
||||
|
||||
export class BrowserSubmissionProcessor {
|
||||
constructor(config = readBrowserProcessingConfig()) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
isEnabled() {
|
||||
return this.config.browserProcessingBeta === true;
|
||||
}
|
||||
|
||||
canProcess(file) {
|
||||
return this.isEnabled() && Boolean(file) && /^(audio|video)\//.test(file.type);
|
||||
}
|
||||
|
||||
async prepare(file) {
|
||||
if (!this.isEnabled()) return { file, metadata: null };
|
||||
console.log(`${PREFIX} Initialising beta submission pipeline.`);
|
||||
console.log(`${PREFIX} File selected:`, { type: file?.type || "", size: file?.size || 0 });
|
||||
if (!this.canProcess(file)) {
|
||||
console.log(`${PREFIX} Browser preprocessing unavailable; falling back.`);
|
||||
return { file, metadata: null };
|
||||
}
|
||||
let policyTimeout;
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
policyTimeout = setTimeout(() => controller.abort(), 5000);
|
||||
const response = await fetch(this.config.policyUrl, {
|
||||
cache: "no-store", credentials: "same-origin", signal: controller.signal,
|
||||
});
|
||||
if (!response.ok) throw new Error(`Policy HTTP ${response.status}`);
|
||||
const policy = await response.json();
|
||||
if (!policy.browser_processing_beta_enabled || policy.version !== this.config.policyVersion) {
|
||||
console.log(`${PREFIX} Browser beta disabled or policy changed; using legacy submission flow.`);
|
||||
return { file, metadata: null };
|
||||
}
|
||||
// Pass 1 has no local classifier or transcoder. Keep the original file.
|
||||
console.log(`${PREFIX} Browser processing shell ready; using original recording.`);
|
||||
return {
|
||||
file,
|
||||
metadata: {
|
||||
browser_processing_used: false,
|
||||
browser_processing_version: policy.version,
|
||||
browser_gate_result: "unavailable",
|
||||
browser_override: false,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`${PREFIX} Initialisation failed:`, error);
|
||||
this.cleanup();
|
||||
console.log(`${PREFIX} Restoring legacy submission flow.`);
|
||||
return { file, metadata: null };
|
||||
} finally {
|
||||
clearTimeout(policyTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
// Future passes can release workers, object URLs, and model tensors here.
|
||||
console.log("[BoomCast][Cleanup] Browser beta resources released.");
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,7 @@
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.upload-icon {
|
||||
.drop-zone .upload-icon {
|
||||
display: grid;
|
||||
width: 4rem;
|
||||
aspect-ratio: 1;
|
||||
@@ -140,6 +140,38 @@
|
||||
background: #4777c94d;
|
||||
}
|
||||
|
||||
.drop-zone .upload-icon__content { position: relative; z-index: 1; margin: 0; color: inherit; font-size: inherit; font-weight: 850; }
|
||||
.audio-upload--uploading .upload-icon,
|
||||
.audio-upload--processing .upload-icon,
|
||||
.audio-upload--success .upload-icon,
|
||||
.audio-upload--error .upload-icon { position: relative; width: 5.5rem; margin-bottom: .25rem; color: #fff; font-size: 1.25rem; box-shadow: 0 0 0 .35rem #334e8d70, 0 .6rem 2rem #101a3fa6; }
|
||||
.audio-upload--uploading .upload-icon { background: conic-gradient(#91baff var(--upload-progress, 0%), #334b82 0); }
|
||||
.audio-upload--processing .upload-icon { background: #405caa; }
|
||||
.audio-upload--processing .upload-icon::after { content: ""; position: absolute; inset: -.42rem; border: 3px solid #9fc5ff; border-right-color: transparent; border-radius: 50%; animation: spin 1.5s linear infinite; }
|
||||
.audio-upload--success .upload-icon { background: #147d6c; }
|
||||
.audio-upload--error .upload-icon { background: #9d385b; }
|
||||
.audio-upload--uploading .drop-zone,
|
||||
.audio-upload--processing .drop-zone { border-color: #7296d5; background: linear-gradient(135deg, #18284c, #242046); }
|
||||
.audio-upload--uploading .drop-zone strong,
|
||||
.audio-upload--processing .drop-zone strong,
|
||||
.audio-upload--uploading .drop-zone > span > span:not(.upload-icon),
|
||||
.audio-upload--processing .drop-zone > span > span:not(.upload-icon),
|
||||
.audio-upload--uploading .file-name,
|
||||
.audio-upload--processing .file-name { opacity: .35; }
|
||||
.audio-upload--uploading .drop-zone,
|
||||
.audio-upload--processing .drop-zone { cursor: default; }
|
||||
.audio-upload--uploading .drop-zone:hover,
|
||||
.audio-upload--processing .drop-zone:hover { transform: none; }
|
||||
.upload-cancel[hidden] { display: none; }
|
||||
#classification-results { scroll-margin-top: 1.5rem; }
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.audio-upload--processing .upload-icon::after { animation: none; }
|
||||
.audio-upload--uploading .drop-zone,
|
||||
.audio-upload--processing .drop-zone,
|
||||
.audio-upload--uploading .upload-icon,
|
||||
.audio-upload--processing .upload-icon { transition: none; }
|
||||
}
|
||||
|
||||
.drop-zone strong {
|
||||
display: block;
|
||||
font-size: 1.15rem;
|
||||
@@ -477,4 +509,3 @@
|
||||
transition-duration: .01ms !important;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,12 @@
|
||||
background: var(--theme-surface-raised) !important;
|
||||
}
|
||||
|
||||
:root[data-theme="light"] .audio-upload--uploading .drop-zone,
|
||||
:root[data-theme="light"] .audio-upload--processing .drop-zone {
|
||||
border-color: #7296d5 !important;
|
||||
background: #e4ebff !important;
|
||||
}
|
||||
|
||||
:root[data-theme="light"] input,
|
||||
:root[data-theme="light"] select,
|
||||
:root[data-theme="light"] textarea,
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import { BrowserSubmissionProcessor } from "../src/scripts/browser-submission-processor.js";
|
||||
|
||||
const file = { type: "audio/wav", size: 64 };
|
||||
|
||||
test("disabled beta returns the original file without loading policy", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = () => { throw new Error("fetch must not run"); };
|
||||
try {
|
||||
const processor = new BrowserSubmissionProcessor({ browserProcessingBeta: false });
|
||||
assert.equal(processor.isEnabled(), false);
|
||||
assert.equal(processor.canProcess(file), false);
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test("enabled shell checks current server policy and never claims processing occurred", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: true, version: 1 }) });
|
||||
try {
|
||||
const processor = new BrowserSubmissionProcessor({ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 });
|
||||
const result = await processor.prepare(file);
|
||||
assert.equal(result.file, file);
|
||||
assert.equal(result.metadata.browser_processing_used, false);
|
||||
assert.equal(result.metadata.browser_gate_result, "unavailable");
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test("admin-disabled policy and fetch failure both fall back to the original file", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
const processor = new BrowserSubmissionProcessor({ browserProcessingBeta: true, policyUrl: "/browser-policy/", policyVersion: 1 });
|
||||
try {
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ browser_processing_beta_enabled: false, version: 1 }) });
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
globalThis.fetch = async () => { throw new Error("offline"); };
|
||||
assert.deepEqual(await processor.prepare(file), { file, metadata: null });
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user