browser audio detection phase 4

This commit is contained in:
Joren de Graaf
2026-10-02 21:45:24 +02:00
parent 1de4b8a0b3
commit 46ad514a70
20 changed files with 903 additions and 31 deletions
+111 -28
View File
@@ -1,5 +1,3 @@
import { BrowserSubmissionProcessor } from "./browser-submission-processor.js";
const form = document.querySelector(".upload-form");
if (form) {
@@ -19,21 +17,49 @@ if (form) {
const gateOverride = form.querySelector("#browser-gate-override");
const status = document.querySelector("#upload-status");
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
const browserToggle = form.querySelector("#browser-processing-toggle");
const configElement = document.querySelector("#boomcast-browser-config");
let browserConfig = { browserProcessingBeta: false };
try { browserConfig = JSON.parse(configElement?.textContent || "{}"); }
catch (error) { console.error("[BoomCast][BrowserBeta] Configuration unavailable:", error); }
const browserAvailable = browserConfig.browserProcessingBeta === true && Boolean(browserToggle);
if (browserToggle) browserToggle.checked = false;
const audioAccept = fileInput.accept;
const videoAccept = ",video/*,.mp4,.mov,.m4v,.webm,.mkv,.avi";
const browserModeEnabled = () => browserAvailable && browserToggle.checked;
const isVideoFile = (file) => Boolean(file) &&
(file.type.startsWith("video/") || (!file.type && /\.(mp4|mov|m4v|webm|mkv|avi)$/i.test(file.name || "")));
let browserProcessor = null;
let browserProcessorPromise = null;
let sourceFile = null;
async function getBrowserProcessor() {
if (browserProcessor) return browserProcessor;
browserProcessorPromise ||= import("./browser-submission-processor.js").then(({ BrowserSubmissionProcessor }) => {
browserProcessor = new BrowserSubmissionProcessor(browserConfig);
console.log("[BoomCast][BrowserBeta] Coordinator initialised.");
return browserProcessor;
}).catch((error) => {
browserProcessorPromise = null;
throw error;
});
return browserProcessorPromise;
}
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;
let selectionId = 0;
let selectionController = null;
let selectedResult = null;
let preparationPending = false;
let preparationFailed = false;
let gateBlocked = false;
const browserProcessor = new BrowserSubmissionProcessor();
if (!browserProcessor.isEnabled()) {
console.log("[BoomCast][BrowserBeta] Feature disabled; using legacy submission flow.");
if (!browserAvailable) {
console.log("[BoomCast][BrowserBeta] Feature disabled globally; using existing server flow.");
} else {
console.log("[BoomCast][BrowserBeta] Browser processing disabled by user; using existing server flow.");
}
function setUploadState(state, { progress = null, message = "" } = {}) {
@@ -42,6 +68,7 @@ if (form) {
busy = active;
submit.disabled = active;
fileInput.disabled = active;
if (browserToggle) browserToggle.disabled = active;
cancel.hidden = !active;
if (active) form.setAttribute("aria-busy", "true");
else form.removeAttribute("aria-busy");
@@ -134,33 +161,50 @@ if (form) {
function selectFile(file) {
selectionId += 1;
selectionController?.abort();
browserProcessor?.cancel();
selectionController = null;
selectedResult = null;
preparationPending = false;
preparationFailed = false;
gateBlocked = false;
gatePanel.hidden = true;
if (!busy) results.replaceChildren();
showFile(file);
if (!busy) { submit.disabled = false; cancel.hidden = true; }
if (!file || !browserProcessor.canProcess(file)) return;
if (!file) return;
console.log("[BoomCast][BrowserBeta] File selected.", { mimeType: file.type, sizeBytes: file.size });
if (!browserModeEnabled()) {
console.log("[BoomCast][BrowserBeta] Server mode selected.");
if (isVideoFile(file)) {
preparationFailed = true;
submit.disabled = true;
status.textContent = t.serverAudioOnly || "Server processing currently accepts audio recordings. Choose an audio file or enable browser processing.";
}
return;
}
console.log("[BoomCast][BrowserBeta] Browser mode selected.");
const id = selectionId;
const controller = new AbortController();
selectionController = controller;
preparationPending = true;
submit.disabled = true;
cancel.hidden = false;
status.textContent = BrowserSubmissionProcessor.isVideo(file)
status.textContent = isVideoFile(file)
? (t.extractingAudio || "Extracting audio from video…")
: (t.preparingRecording || "Preparing recording…");
browserProcessor.prepare(file, { signal: controller.signal, onState: (state) => {
if (id !== selectionId) return;
status.textContent = state === "checking" ? (t.checkingRecording || "Checking recording…")
: (t.extractingAudio || "Extracting audio from video…");
} }).then((result) => {
if (id !== selectionId || controller.signal.aborted) return;
getBrowserProcessor().then((processor) => {
if (id !== selectionId || controller.signal.aborted || !browserModeEnabled()) return null;
processor.setEnabled(true);
return processor.process(file, { signal: controller.signal, onState: (state) => {
if (id !== selectionId) return;
status.textContent = state === "checking" ? (t.checkingRecording || "Checking recording…")
: (t.extractingAudio || "Extracting audio from video…");
} });
}).then((result) => {
if (!result || id !== selectionId || controller.signal.aborted || !browserModeEnabled()) return;
preparationPending = false;
cancel.hidden = true;
if (BrowserSubmissionProcessor.isVideo(file) && result.file === file) {
if (isVideoFile(file) && result.file === file) {
preparationFailed = true;
submit.disabled = true;
status.textContent = t.videoExtractionFailed || "Could not extract audio from this video. Please choose an audio recording.";
@@ -177,31 +221,64 @@ if (form) {
: (t.irrelevantGateMessage || "BoomCast could not identify a sound this service currently looks for. You can still submit it for manual review.");
gatePanel.hidden = false;
submit.disabled = true;
status.textContent = (t.gateChooseMessage || "Recording checked. Choose another recording or submit for manual review.");
status.textContent = t.gateChooseMessage || "Recording checked. Choose another recording or submit for manual review.";
} else {
submit.disabled = false;
status.textContent = BrowserSubmissionProcessor.isVideo(file)
? (t.videoAudioOnly || "Video processed on this device. Only extracted audio will be sent if you submit.")
: (t.readyToSubmit || "Ready to submit.");
status.textContent = result.gate === "unavailable"
? (t.localUnavailable || "Local checking was unavailable. BoomCast will use normal server processing.")
: isVideoFile(file)
? (t.videoAudioOnly || "Video processed on this device. Only extracted audio will be sent if you submit.")
: (t.readyToSubmit || "Ready to submit.");
}
}).catch((error) => {
if (id !== selectionId) return;
console.error("[BoomCast][BrowserBeta] Preparation failed:", error);
if (id !== selectionId || !browserModeEnabled()) return;
console.error("[BoomCast][BrowserBeta] Unexpected browser pipeline error:", error);
preparationPending = false;
cancel.hidden = true;
if (BrowserSubmissionProcessor.isVideo(file)) {
if (isVideoFile(file)) {
preparationFailed = true;
submit.disabled = true;
status.textContent = t.videoExtractionFailed || "Could not extract audio from this video. Please choose an audio recording.";
} else {
console.warn("[BoomCast][BrowserBeta] Browser pipeline unavailable; falling back to server.");
selectedResult = { file, metadata: null, gate: "unavailable" };
submit.disabled = false;
status.textContent = t.readyToSubmit || "Ready to submit.";
status.textContent = t.localUnavailable || "Local checking was unavailable. BoomCast will use normal server processing.";
}
});
}
fileInput.addEventListener("change", () => selectFile(fileInput.files[0]));
function restoreOriginalFile() {
if (!sourceFile || fileInput.files[0] === sourceFile) return;
try {
const transfer = new DataTransfer();
transfer.items.add(sourceFile);
fileInput.files = transfer.files;
} catch (error) {
console.error("[BoomCast][Cleanup] Could not restore original selection:", error);
fileInput.value = "";
sourceFile = null;
}
}
browserToggle?.addEventListener("change", () => {
if (browserModeEnabled()) {
console.log("[BoomCast][BrowserBeta] Browser processing enabled by user.");
fileInput.accept = audioAccept + videoAccept;
selectFile(sourceFile || fileInput.files[0]);
} else {
console.log("[BoomCast][BrowserBeta] Switching to server processing.");
console.log("[BoomCast][Cleanup] Cancelling active browser processing job.");
selectionController?.abort();
browserProcessor?.setEnabled(false);
browserProcessor?.cleanup();
fileInput.accept = audioAccept;
restoreOriginalFile();
selectFile(sourceFile || fileInput.files[0]);
}
});
fileInput.addEventListener("change", () => { sourceFile = fileInput.files[0] || null; selectFile(sourceFile); });
["dragenter", "dragover"].forEach((name) => dropZone.addEventListener(name, (event) => {
event.preventDefault();
if (!busy) dropZone.classList.add("dragging");
@@ -213,7 +290,8 @@ if (form) {
dropZone.addEventListener("drop", (event) => {
if (busy || !event.dataTransfer.files.length) return;
fileInput.files = event.dataTransfer.files;
selectFile(fileInput.files[0]);
sourceFile = fileInput.files[0] || null;
selectFile(sourceFile);
});
function startUpload(file, metadata = null) {
@@ -279,15 +357,15 @@ if (form) {
if (!form.reportValidity()) return;
const file = fileInput.files[0];
if (!file) return;
if (!browserModeEnabled()) { startUpload(file); return; }
if (selectedResult) { startUpload(selectedResult.file, selectedResult.metadata); return; }
if (!browserProcessor.isEnabled()) { startUpload(file); return; }
// Unknown types retain the existing server flow.
// Unknown audio types retain the existing server flow.
startUpload(file);
}, true);
gateChoose.addEventListener("click", () => fileInput.click());
gateOverride.addEventListener("click", () => {
if (!gateBlocked || !selectedResult || busy || preparationPending) return;
if (!browserModeEnabled() || !gateBlocked || !selectedResult || busy || preparationPending) return;
console.warn("[BoomCast][Gate] User explicitly overrode local gate.");
console.log("[BoomCast][Gate] Submission will enter manual review.");
const metadata = { ...selectedResult.metadata, browser_override: true };
@@ -299,6 +377,7 @@ if (form) {
cancel.addEventListener("click", () => {
if (preparationPending) {
selectionController?.abort();
browserProcessor?.cancel();
selectionId += 1;
preparationPending = false;
preparationFailed = true;
@@ -311,8 +390,12 @@ if (form) {
if (event.persisted) {
request?.abort();
selectionController?.abort();
browserProcessor?.cancel();
selectionId += 1;
selectedResult = null;
sourceFile = null;
if (browserToggle) { browserToggle.checked = false; fileInput.accept = audioAccept; }
browserProcessor?.setEnabled(false);
preparationPending = false;
preparationFailed = false;
gateBlocked = false;
@@ -15,9 +15,39 @@ export class BrowserSubmissionProcessor {
this.config = config;
this.extractVideo = extractVideo;
this.dependencies = dependencies;
this.userEnabled = false;
this.activeController = null;
}
isEnabled() { return this.config.browserProcessingBeta === true; }
isAvailable() { return this.config.browserProcessingBeta === true; }
isEnabled() { return this.isAvailable() && this.userEnabled; }
setEnabled(enabled) {
if (!enabled) this.cancel();
this.userEnabled = this.isAvailable() && enabled === true;
console.log(`${PREFIX} ${this.userEnabled ? "Browser processing enabled by user." : "Browser processing disabled by user; using existing server flow."}`);
}
cancel() {
this.activeController?.abort();
this.activeController = null;
}
async process(file, { signal, onState } = {}) {
this.cancel();
const controller = new AbortController();
this.activeController = controller;
const abort = () => controller.abort();
signal?.addEventListener("abort", abort, { once: true });
if (signal?.aborted) controller.abort();
try {
return await this.prepare(file, { signal: controller.signal, onState });
} finally {
signal?.removeEventListener("abort", abort);
if (this.activeController === controller) this.activeController = null;
}
}
canProcess(file) {
return this.isEnabled() && Boolean(file) && (BrowserSubmissionProcessor.isAudio(file) || BrowserSubmissionProcessor.isVideo(file));
@@ -102,5 +132,8 @@ export class BrowserSubmissionProcessor {
}
}
cleanup() { console.log("[BoomCast][Cleanup] Browser beta resources released."); }
cleanup() {
this.cancel();
console.log("[BoomCast][Cleanup] Browser beta resources released.");
}
}
@@ -515,3 +515,8 @@
.browser-gate p { margin: .45rem 0 1rem; }
.browser-gate-actions { display: flex; gap: .65rem; flex-wrap: wrap; }
.browser-gate-actions button { cursor: pointer; }
.browser-mode { margin: 1rem 0; }
.browser-mode__label { display: inline-flex; align-items: center; gap: .55rem; font-weight: 700; cursor: pointer; }
.browser-mode__label input { width: 1.1rem; height: 1.1rem; margin: 0; accent-color: #91baff; }
.browser-mode p { margin: .35rem 0 0 1.65rem; font-size: .9rem; opacity: .85; }