Files
webspeak3/web/src/voice.ts
T
MoepchiandClaude Opus 5.5 341a1644a6 Mic clicks as regular sounds, own clicks off by default
The two clicks now live in the sound settings (toggle, test, custom file,
soundpack import via mic_click_on/off) instead of being fixed tones in the
voice player. Own mic clicks start off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-10-23 18:03:04 +00:00

521 lines
21 KiB
TypeScript

// Encoding/decoding and Web Audio plumbing for the voice feature.
//
// The connector always speaks 48kHz PCM in 20ms frames: mono for the
// microphone -> server direction (which it Opus-encodes), stereo mixed
// for the server -> speakers direction (already decoded/mixed for us).
import type { Aec3Instance } from "./aec3";
export const SAMPLE_RATE = 48000;
export const FRAME_SAMPLES = 960; // 20ms @ 48kHz
function int16ToBase64(samples: Int16Array): string {
const bytes = new Uint8Array(samples.buffer, samples.byteOffset, samples.byteLength);
let binary = "";
const chunkSize = 0x8000;
for (let i = 0; i < bytes.length; i += chunkSize) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
}
return btoa(binary);
}
function base64ToInt16(b64: string): Int16Array {
const binary = atob(b64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
return new Int16Array(bytes.buffer);
}
export interface MicCaptureOptions {
onFrame: (base64Pcm: string) => void;
onActivity?: (active: boolean) => void;
/** Reports the raw input RMS (roughly 0-1) on every processing tick, for live level meters. */
onLevel?: (rms: number) => void;
threshold?: number;
/** How long transmission continues after the level drops back below the threshold, so words aren't clipped. */
hangoverSeconds?: number;
/** `MediaDeviceInfo.deviceId` of the input device to use - omit for the system default. */
deviceId?: string;
echoCancellation?: boolean;
noiseSuppression?: boolean;
/** Automatic gain control. Exposed as its own toggle (rather than folded into
* echoCancellation) because AGC re-amplifying the mic signal after the
* browser's own AEC runs can bring back audible echo residue in speaker+mic
* (no headset) setups - some users get a cleaner result with it off. */
autoGainControl?: boolean;
/** When set, mic audio is run through this AEC3 instance (opts:
* captureNumChannels=1) before VAD/encoding, in place of relying solely on
* the browser's native echoCancellation. */
aec3?: Aec3Instance;
}
/**
* Captures the microphone and emits base64-encoded mono 16-bit PCM frames.
*
* Uses voice activation (like TeamSpeak's own "Sprachaktivierung"): frames are
* only sent while the input level is above `threshold`, plus a short hangover
* so the tail of a word isn't cut off. With `pushToTalk` set, only while
* `pttPressed` is. `onActivity` reports transitions so the
* UI can show a local "currently transmitting" indicator.
*/
export class MicCapture {
private stream: MediaStream | null = null;
private source: MediaStreamAudioSourceNode | null = null;
private merger: ChannelMergerNode | null = null;
private extra: MediaStreamAudioSourceNode | null = null;
private pendingExtra: number[] = [];
/** Loudest extra-source sample since setExtraAudio(), to spot a silent share. */
extraPeak = 0;
private processor: ScriptProcessorNode | null = null;
private silence: GainNode | null = null;
private monitorTarget: AudioNode | null = null;
private pending: number[] = [];
private active = false;
private activeUntil = 0;
private onFrame: (base64Pcm: string) => void;
private onActivity?: (active: boolean) => void;
private onLevel?: (rms: number) => void;
private deviceId?: string;
private echoCancellation: boolean;
private noiseSuppression: boolean;
private autoGainControl: boolean;
private aec3?: Aec3Instance;
private aec3Out: Float32Array[] | null = null;
private context: AudioContext;
threshold: number;
hangoverSeconds: number;
/** Push-to-talk: send while `pttPressed` instead of voice activation. */
pushToTalk = false;
pttPressed = false;
constructor(context: AudioContext, options: MicCaptureOptions) {
this.context = context;
this.onFrame = options.onFrame;
this.onActivity = options.onActivity;
this.onLevel = options.onLevel;
this.threshold = options.threshold ?? 0.02;
this.hangoverSeconds = options.hangoverSeconds ?? 0.3;
this.deviceId = options.deviceId;
this.echoCancellation = options.echoCancellation ?? true;
this.noiseSuppression = options.noiseSuppression ?? true;
this.autoGainControl = options.autoGainControl ?? true;
this.aec3 = options.aec3;
}
async start(): Promise<void> {
if (!navigator.mediaDevices) {
throw new Error("Microphone access requires HTTPS (or localhost) - the site is not a secure context.");
}
this.stream = await navigator.mediaDevices.getUserMedia({
audio: {
channelCount: 1,
// Requesting the pipeline's own rate (rather than leaving it to the
// device/OS default) avoids an extra internal resampling step ahead
// of the browser's own echo canceller - standard constraint, honored
// by both Chromium and Firefox, "ideal" so it's not fatal if a device
// genuinely can't provide it.
sampleRate: { ideal: SAMPLE_RATE },
echoCancellation: this.echoCancellation,
noiseSuppression: this.noiseSuppression,
autoGainControl: this.autoGainControl,
...(this.deviceId ? { deviceId: { exact: this.deviceId } } : {}),
},
});
// Granting the permission prompt counts as a user gesture, so this can
// succeed even when start() was triggered automatically on page load,
// before the AudioContext would otherwise be allowed to leave "suspended".
if (this.context.state === "suspended") await this.context.resume();
this.source = this.context.createMediaStreamSource(this.stream);
// Channel 0 is the mic, channel 1 the optional extra source (setExtraAudio).
this.merger = this.context.createChannelMerger(2);
this.processor = this.context.createScriptProcessor(2048, 2, 1);
this.processor.onaudioprocess = (event) => {
const raw = event.inputBuffer.getChannelData(0);
let input: Float32Array = raw;
if (this.aec3) {
const captureIn = [raw];
const outSize = this.aec3.processSize(captureIn);
if (!this.aec3Out || this.aec3Out[0].length !== outSize) this.aec3Out = [new Float32Array(outSize)];
this.aec3.process(this.aec3Out, captureIn);
input = this.aec3Out[0];
}
// Mirrors the (possibly AEC3-cancelled) signal to the output buffer, so
// setMonitoring() below can tap what's actually being sent rather than
// the raw pre-cancellation mic feed - a self-test through the raw feed
// always howls, whether or not AEC3 is on.
event.outputBuffer.getChannelData(0).set(input);
let sumSquares = 0;
for (let i = 0; i < input.length; i++) sumSquares += input[i] * input[i];
const rms = Math.sqrt(sumSquares / input.length);
this.onLevel?.(rms);
const now = this.context.currentTime;
if (rms >= this.threshold) this.activeUntil = now + this.hangoverSeconds;
const talking = this.pushToTalk ? this.pttPressed : now < this.activeUntil;
// Shared audio (music, a video) has its own pauses; keep sending throughout.
const shouldBeActive = this.extra !== null || talking;
if (shouldBeActive !== this.active) {
this.active = shouldBeActive;
this.onActivity?.(this.active);
if (!this.active) this.pending = [];
}
if (!this.active) return;
for (let i = 0; i < input.length; i++) this.pending.push(input[i]);
// Mixed in per frame rather than per tick: AEC3 may hand back a block of
// a different size than it got, and the extra source never went through
// it (it isn't an echo of anything).
if (this.extra) {
const extra = event.inputBuffer.getChannelData(1);
for (let i = 0; i < extra.length; i++) {
this.pendingExtra.push(extra[i]);
this.extraPeak = Math.max(this.extraPeak, Math.abs(extra[i]));
}
}
// AEC3's blocks make the two queues jitter against each other. Padding
// a short extra queue with silence clicks; skip it once instead, which
// leaves it a frame ahead from then on. Cap the lead to bound latency.
if (this.pendingExtra.length > 4 * FRAME_SAMPLES) this.pendingExtra.splice(0, this.pendingExtra.length - 2 * FRAME_SAMPLES);
while (this.pending.length >= FRAME_SAMPLES) {
const frame = this.pending.splice(0, FRAME_SAMPLES);
const extra = this.pendingExtra.length >= FRAME_SAMPLES ? this.pendingExtra.splice(0, FRAME_SAMPLES) : [];
const int16 = new Int16Array(FRAME_SAMPLES);
for (let i = 0; i < FRAME_SAMPLES; i++) {
// Shared music is mastered near full scale; at -6 dB it leaves room
// for the voice on top instead of clipping.
const clamped = Math.max(-1, Math.min(1, frame[i] + (extra[i] ?? 0) * 0.5));
int16[i] = clamped < 0 ? clamped * 32768 : clamped * 32767;
}
this.onFrame(int16ToBase64(int16));
}
};
// A ScriptProcessorNode only fires while connected to a destination; route
// it through a muted gain node so we don't hear our own mic echoed back.
this.silence = this.context.createGain();
this.silence.gain.value = 0;
this.source.connect(this.merger, 0, 0);
this.merger.connect(this.processor);
this.processor.connect(this.silence);
this.silence.connect(this.context.destination);
}
/** Mixes a second stream (e.g. a shared tab's audio) into what is sent, or
* removes it with null. The caller keeps ownership of the stream. */
setExtraAudio(stream: MediaStream | null): void {
this.extra?.disconnect();
this.extra = null;
this.pendingExtra = [];
this.extraPeak = 0;
if (stream && this.merger) {
this.extra = this.context.createMediaStreamSource(stream);
this.extra.connect(this.merger, 0, 1);
}
}
/** The raw mic input node, e.g. to additionally route it into a recorder. */
getSourceNode(): AudioNode | null {
return this.source;
}
/** Routes the (possibly AEC3-cancelled) mic signal to `destination` too, so
* you can hear yourself - a mic test - until disabled again. Taps the
* processor's output rather than the raw source, so the test actually
* reflects AEC3 when it's on, instead of always howling via the
* uncancelled raw feed. */
setMonitoring(enabled: boolean, destination: AudioNode): void {
if (!this.processor) return;
if (enabled && this.monitorTarget !== destination) {
if (this.monitorTarget) this.processor.disconnect(this.monitorTarget);
this.processor.connect(destination);
this.monitorTarget = destination;
} else if (!enabled && this.monitorTarget) {
this.processor.disconnect(this.monitorTarget);
this.monitorTarget = null;
}
}
stop(): void {
if (this.processor && this.monitorTarget) this.processor.disconnect(this.monitorTarget);
this.monitorTarget = null;
this.processor?.disconnect();
this.silence?.disconnect();
this.source?.disconnect();
this.extra?.disconnect();
this.merger?.disconnect();
this.extra = null;
this.merger = null;
this.pendingExtra = [];
this.stream?.getTracks().forEach((t) => t.stop());
this.processor = null;
this.silence = null;
this.source = null;
this.stream = null;
this.pending = [];
if (this.active) {
this.active = false;
this.onActivity?.(false);
}
}
}
function writeAsciiString(view: DataView, offset: number, text: string): void {
for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));
}
/** Encodes raw stereo float samples (range -1..1) as an uncompressed 16-bit PCM WAV file. */
export function encodeWavStereo(left: Float32Array, right: Float32Array, sampleRate: number): Blob {
const numChannels = 2;
const bytesPerSample = 2;
const blockAlign = numChannels * bytesPerSample;
const dataSize = left.length * blockAlign;
const buffer = new ArrayBuffer(44 + dataSize);
const view = new DataView(buffer);
writeAsciiString(view, 0, "RIFF");
view.setUint32(4, 36 + dataSize, true);
writeAsciiString(view, 8, "WAVE");
writeAsciiString(view, 12, "fmt ");
view.setUint32(16, 16, true);
view.setUint16(20, 1, true); // PCM
view.setUint16(22, numChannels, true);
view.setUint32(24, sampleRate, true);
view.setUint32(28, sampleRate * blockAlign, true);
view.setUint16(32, blockAlign, true);
view.setUint16(34, 16, true);
writeAsciiString(view, 36, "data");
view.setUint32(40, dataSize, true);
let offset = 44;
for (let i = 0; i < left.length; i++) {
const l = Math.max(-1, Math.min(1, left[i]));
const r = Math.max(-1, Math.min(1, right[i]));
view.setInt16(offset, l < 0 ? l * 0x8000 : l * 0x7fff, true);
view.setInt16(offset + 2, r < 0 ? r * 0x8000 : r * 0x7fff, true);
offset += blockAlign;
}
return new Blob([buffer], { type: "audio/wav" });
}
/** Output ("audiooutput") devices available for playback, e.g. for a device picker. */
export async function listAudioOutputDevices(): Promise<MediaDeviceInfo[]> {
if (!navigator.mediaDevices) return [];
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.filter((d) => d.kind === "audiooutput");
}
/** Input ("audioinput") devices available for the mic, e.g. for a device picker. */
export async function listAudioInputDevices(): Promise<MediaDeviceInfo[]> {
if (!navigator.mediaDevices) return [];
const devices = await navigator.mediaDevices.enumerateDevices();
return devices.filter((d) => d.kind === "audioinput");
}
type MediaDevicesWithPicker = MediaDevices & { selectAudioOutput?: () => Promise<MediaDeviceInfo> };
/** Whether the browser supports the native OS output-device picker (Chrome/Edge 105+). */
export function hasNativeOutputPicker(): boolean {
if (!navigator.mediaDevices) return false;
return typeof (navigator.mediaDevices as MediaDevicesWithPicker).selectAudioOutput === "function";
}
/**
* Opens the browser's native OS device chooser for audio output. Unlike
* `enumerateDevices()`, this lists every device the OS knows about (including
* ones like a USB headset that plain enumeration can otherwise omit) and asks
* the user to pick one, each call - no persistent listing to keep in sync.
*/
export async function pickAudioOutputDevice(): Promise<MediaDeviceInfo | null> {
if (!navigator.mediaDevices) return null;
const md = navigator.mediaDevices as MediaDevicesWithPicker;
if (typeof md.selectAudioOutput !== "function") return null;
return md.selectAudioOutput();
}
type SinkableElement = HTMLAudioElement & { setSinkId?: (id: string) => Promise<void> };
type SinkableContext = AudioContext & { setSinkId?: (id: string) => Promise<void> };
/**
* Schedules incoming base64-encoded stereo 16-bit PCM frames for gap-free
* playback, and supports switching output device across browsers.
*
* Playback is routed through a `MediaStreamAudioDestinationNode` into a
* hidden `<audio>` element rather than straight to `context.destination`,
* because device switching support is split across browsers:
* `AudioContext.setSinkId` (Chrome 110+) only retargets the context itself,
* while `HTMLMediaElement.setSinkId` (Chrome, and Firefox 130+) only works on
* a media element. Routing through the element lets `setOutputDevice` use
* whichever one the browser actually supports. Browsers without
* `HTMLMediaElement.setSinkId` (mobile ones) play straight to the context.
*/
export class AudioPlayer {
private nextTime = 0;
// null where media elements can't switch devices (Android Chrome/Firefox):
// there the detour buys nothing and its <audio> element stays silent.
private destination: MediaStreamAudioDestinationNode | null = null;
private element: SinkableElement | null = null;
private gain: GainNode;
// Voice frames only (not test tone, clicks or mic monitoring), so the
// leveler can sit between them and the volume gain.
private voice: GainNode;
private leveler: DynamicsCompressorNode;
private context: AudioContext;
private aecTap: ScriptProcessorNode | null = null;
private aecSink: GainNode | null = null;
constructor(context: AudioContext) {
this.context = context;
this.gain = context.createGain();
this.voice = context.createGain();
// "Automatic volume leveling": evens out loud and quiet speakers. Browsers
// add their own makeup gain, so quiet voices come up too.
// ponytail: one compressor for the whole mix, not per speaker like TS3.
this.leveler = context.createDynamicsCompressor();
this.leveler.threshold.value = -30;
this.leveler.knee.value = 10;
this.leveler.ratio.value = 4;
this.leveler.attack.value = 0.005;
this.leveler.release.value = 0.3;
this.leveler.connect(this.gain);
this.voice.connect(this.gain);
if (!("setSinkId" in HTMLMediaElement.prototype)) {
this.gain.connect(context.destination);
return;
}
this.destination = context.createMediaStreamDestination();
this.gain.connect(this.destination);
this.element = document.createElement("audio") as SinkableElement;
this.element.autoplay = true;
this.element.srcObject = this.destination.stream;
this.element.style.display = "none";
document.body.appendChild(this.element);
}
/** Mobile browsers ignore `autoplay` for audible media, so the hidden
* element stays paused (silent) until play() runs inside a user gesture. */
unlock(): void {
if (this.element?.paused) this.element.play().catch(() => {});
}
playFrame(base64Pcm: string): void {
const int16 = base64ToInt16(base64Pcm);
const frames = int16.length / 2;
if (frames <= 0) return;
const buffer = this.context.createBuffer(2, frames, SAMPLE_RATE);
const left = buffer.getChannelData(0);
const right = buffer.getChannelData(1);
for (let i = 0; i < frames; i++) {
left[i] = int16[i * 2] / 32768;
right[i] = int16[i * 2 + 1] / 32768;
}
const source = this.context.createBufferSource();
source.buffer = buffer;
source.connect(this.voice);
const now = this.context.currentTime;
if (this.nextTime < now + 0.02) {
// First frame, or we fell behind - restart just ahead of now instead of
// letting a backlog build up.
this.nextTime = now + 0.02;
}
source.start(this.nextTime);
this.nextTime += frames / SAMPLE_RATE;
}
reset(): void {
this.nextTime = 0;
}
/** Playback volume as linear gain (1 = unity/0dB). */
setVolume(gain: number): void {
this.gain.gain.value = gain;
}
setAutoVolume(enabled: boolean): void {
this.voice.disconnect();
this.voice.connect(enabled ? this.leveler : this.gain);
}
/** The node all voice/test audio ultimately reaches - use as a mic-monitoring target so it also respects volume/output device. */
getInputNode(): AudioNode {
return this.gain;
}
/** Plays a short sine-wave test tone through the same volume/output-device routing as voice playback. */
playTestTone(): void {
const osc = this.context.createOscillator();
osc.type = "sine";
osc.frequency.value = 440;
const toneGain = this.context.createGain();
toneGain.gain.value = 0.3;
osc.connect(toneGain);
toneGain.connect(this.gain);
const now = this.context.currentTime;
osc.start(now);
osc.stop(now + 0.6);
osc.onended = () => {
osc.disconnect();
toneGain.disconnect();
};
}
/** Feeds everything played back through this player into `aec3.analyze()` as
* the far-end/render reference, so a paired `MicCapture`'s aec3 `process()`
* calls can cancel it back out of the mic signal. Taps post-gain: what's
* actually reaching the speakers is what needs cancelling, not the
* pre-volume mix. */
attachAecRenderTap(aec3: Pick<Aec3Instance, "analyze">): void {
this.detachAecRenderTap();
const tap = this.context.createScriptProcessor(2048, 2, 2);
tap.onaudioprocess = (e) => {
aec3.analyze([e.inputBuffer.getChannelData(0), e.inputBuffer.getChannelData(1)]);
};
const sink = this.context.createGain();
sink.gain.value = 0;
this.gain.connect(tap);
tap.connect(sink);
sink.connect(this.context.destination);
this.aecTap = tap;
this.aecSink = sink;
}
detachAecRenderTap(): void {
if (this.aecTap) {
this.gain.disconnect(this.aecTap);
this.aecTap.disconnect();
this.aecTap = null;
}
this.aecSink?.disconnect();
this.aecSink = null;
}
/** Routes playback to a specific device (empty string = system default). */
async setOutputDevice(deviceId: string): Promise<void> {
if (typeof this.element?.setSinkId === "function") {
await this.element.setSinkId(deviceId);
return;
}
const ctx = this.context as SinkableContext;
if (typeof ctx.setSinkId === "function") {
await ctx.setSinkId(deviceId);
}
// Neither API is available - stays on the system default output.
}
dispose(): void {
this.detachAecRenderTap();
this.voice.disconnect();
this.leveler.disconnect();
this.gain.disconnect();
if (!this.element) return;
this.element.pause();
this.element.srcObject = null;
this.element.remove();
this.destination?.stream.getTracks().forEach((t) => t.stop());
}
}