38 lines
1.9 KiB
JavaScript
38 lines
1.9 KiB
JavaScript
// Per-device preferences. Missing devices retain their profile; choosing a new device uses template only.
|
|
export function deviceProfile(prefs, direction) {
|
|
const key = direction === 'input' ? 'inputDsp' : 'outputDsp';
|
|
const device = prefs[direction === 'input' ? 'inputDeviceId' : 'outputDeviceId'] || 'default';
|
|
return prefs[key].devices[device] ?? prefs[key].template;
|
|
}
|
|
export function updateDeviceProfile(prefs, direction, patch) {
|
|
const key = direction === 'input' ? 'inputDsp' : 'outputDsp';
|
|
const id = prefs[direction === 'input' ? 'inputDeviceId' : 'outputDeviceId'] || 'default';
|
|
return { [key]: { template: { ...prefs[key].template }, devices: { ...prefs[key].devices, [id]: { ...deviceProfile(prefs, direction), ...patch } } } };
|
|
}
|
|
/** Official denoiser level 0..3 of the capture device's profile, or null while noise reduction is off. */
|
|
export function captureDenoiseLevel(prefs) {
|
|
const input = deviceProfile(prefs, 'input');
|
|
return input.denoise ? input.denoiseLevel : null;
|
|
}
|
|
export function transmitConfig(prefs) {
|
|
const input = deviceProfile(prefs, 'input');
|
|
if (!input.vad) return { mode: 'direct' };
|
|
const config = { mode: 'vad', detector: input.vadMode };
|
|
if (input.vadMode !== 'volume_gate') config.sensitivity = input.vadSensitivity;
|
|
if (input.vadMode !== 'automatic') config.level_db = input.vadLevel;
|
|
return config;
|
|
}
|
|
export const transmitKey = (mode) => mode ? `${mode.mode}:${mode.detector ?? ''}:${mode.sensitivity ?? ''}:${mode.level_db ?? ''}` : '';
|
|
|
|
/** PTT release is the only delay here; privacy boundaries always hard-reset it. */
|
|
export class PushToTalkGate {
|
|
constructor(now = () => performance.now()) { this.now = now; this.down = false; this.until = 0; }
|
|
set(down, delay = 0) {
|
|
if (down === this.down) return;
|
|
this.down = down;
|
|
this.until = down ? 0 : this.now() + Math.max(0, delay);
|
|
}
|
|
reset() { this.down = false; this.until = 0; }
|
|
get open() { return this.down || this.now() < this.until; }
|
|
}
|