mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-06 21:42:50 +08:00
Resolve noise suppression through one plan with four modes (off, browser, rnnoise, gtcrn, default gtcrn): model engines disable the browser suppressor, preload their WASM and worklet before getUserMedia, and fall back to browser denoising (no-worklet, sample-rate, load-failed) without a second permission prompt. Add an 80 Hz high-pass ahead of the model, persist the mode beside the legacy level mirror, and let the microphone test record raw and processed takes side by side.
156 lines
5.0 KiB
TypeScript
156 lines
5.0 KiB
TypeScript
export interface MicrophoneTestStreams {
|
|
/** Raw getUserMedia stream, before any denoising. */
|
|
dry: MediaStream;
|
|
/** Graph output that is actually sent to the room. */
|
|
processed: MediaStream;
|
|
}
|
|
|
|
interface MicrophoneTestOptions {
|
|
prepare(): Promise<MicrophoneTestStreams | null>;
|
|
onActive(active: boolean): void;
|
|
onUrl(url: string): void;
|
|
onDryUrl(url: string): void;
|
|
onStopped(): void;
|
|
onError(error: unknown): void;
|
|
}
|
|
|
|
type RecordingKind = "dry" | "processed";
|
|
|
|
interface RecorderSlot {
|
|
kind: RecordingKind;
|
|
recorder: MediaRecorder;
|
|
chunks: Blob[];
|
|
stopped: boolean;
|
|
}
|
|
|
|
interface Recording {
|
|
slots: RecorderSlot[];
|
|
timer: number | null;
|
|
stopped: boolean;
|
|
}
|
|
|
|
// Capture belongs to the caller. This controller owns only the test operation,
|
|
// recorders, deadline and both playback URLs, including results queued after stop().
|
|
export function createMicrophoneTest(options: MicrophoneTestOptions) {
|
|
let current: Recording | null = null;
|
|
const urls: Record<RecordingKind, string> = { dry: "", processed: "" };
|
|
|
|
function clearUrl(kind: RecordingKind): void {
|
|
const url = urls[kind];
|
|
if (url) URL.revokeObjectURL(url);
|
|
urls[kind] = "";
|
|
if (kind === "dry") options.onDryUrl("");
|
|
else options.onUrl("");
|
|
}
|
|
|
|
function clearUrls(): void {
|
|
clearUrl("dry");
|
|
clearUrl("processed");
|
|
}
|
|
|
|
function finishCapture(recording: Recording): void {
|
|
if (recording.timer !== null) window.clearTimeout(recording.timer);
|
|
recording.timer = null;
|
|
if (recording.stopped) return;
|
|
recording.stopped = true;
|
|
options.onActive(false);
|
|
options.onStopped();
|
|
}
|
|
|
|
function dispose(): void {
|
|
const recording = current;
|
|
current = null;
|
|
if (recording) {
|
|
finishCapture(recording);
|
|
for (const slot of recording.slots) {
|
|
if (slot.recorder.state !== "inactive") {
|
|
try { slot.recorder.stop(); } catch { /* already ended */ }
|
|
}
|
|
slot.chunks.length = 0;
|
|
}
|
|
recording.slots.length = 0;
|
|
}
|
|
clearUrls();
|
|
}
|
|
|
|
function stop(): void {
|
|
const recording = current;
|
|
if (!recording) return;
|
|
// A pending permission or device enumeration must not create a recorder.
|
|
if (!recording.slots.length) current = null;
|
|
finishCapture(recording);
|
|
for (const slot of recording.slots) {
|
|
if (slot.recorder.state === "inactive") continue;
|
|
try { slot.recorder.stop(); }
|
|
catch (error) { dispose(); options.onError(error); return; }
|
|
}
|
|
}
|
|
|
|
function finalizeSlot(recording: Recording, slot: RecorderSlot): void {
|
|
slot.stopped = true;
|
|
if (current !== recording) return;
|
|
if (!recording.slots.every(candidate => candidate.stopped || candidate.recorder.state === "inactive")) return;
|
|
current = null;
|
|
finishCapture(recording);
|
|
for (const candidate of recording.slots) {
|
|
if (!candidate.chunks.length) continue;
|
|
try {
|
|
const url = URL.createObjectURL(new Blob(candidate.chunks, { type: candidate.recorder.mimeType || "audio/webm" }));
|
|
clearUrl(candidate.kind);
|
|
urls[candidate.kind] = url;
|
|
if (candidate.kind === "dry") options.onDryUrl(url);
|
|
else options.onUrl(url);
|
|
} catch (error) { options.onError(error); }
|
|
candidate.chunks.length = 0;
|
|
}
|
|
}
|
|
|
|
async function start(): Promise<void> {
|
|
dispose();
|
|
const recording: Recording = { slots: [], timer: null, stopped: false };
|
|
current = recording;
|
|
options.onActive(true);
|
|
try {
|
|
const streams = await options.prepare();
|
|
if (current !== recording) return;
|
|
// Keep the existing level-meter-only test on browsers without recording.
|
|
if (typeof MediaRecorder === "undefined" || !streams) return;
|
|
let startError: unknown = null;
|
|
for (const kind of ["processed", "dry"] as const) {
|
|
try {
|
|
const recorder = new MediaRecorder(streams[kind]);
|
|
const slot: RecorderSlot = { kind, recorder, chunks: [], stopped: false };
|
|
recorder.ondataavailable = event => {
|
|
if (current === recording && event.data.size) slot.chunks.push(event.data);
|
|
};
|
|
recorder.onstop = () => finalizeSlot(recording, slot);
|
|
recorder.onerror = event => {
|
|
if (current !== recording) return;
|
|
dispose();
|
|
options.onError("error" in event ? event.error : new Error("Microphone recording failed"));
|
|
};
|
|
recorder.start();
|
|
recording.slots.push(slot);
|
|
} catch (error) {
|
|
// One path failing must not disable the other; A/B degrades to mono.
|
|
startError ??= error;
|
|
}
|
|
}
|
|
// Nothing could record: surface the recorder failure, not a silent test.
|
|
if (!recording.slots.length) {
|
|
if (startError) throw startError;
|
|
return;
|
|
}
|
|
recording.timer = window.setTimeout(() => {
|
|
if (current === recording) stop();
|
|
}, 5_000);
|
|
} catch (error) {
|
|
if (current !== recording) return;
|
|
dispose();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return { start, stop, dispose };
|
|
}
|