151 lines
7.3 KiB
JavaScript
151 lines
7.3 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Voice preferences that follow the official TS6 audio settings model (client_ui/main.js, AudioSettings):
|
|
* - push to talk is global (`ptt.active`, release delay Off/100/300/500 ms, stored ms default 300);
|
|
* - capture DSP is per input device: agc, denoise on/off + `denoiseLevel` (the official denoiserLevel
|
|
* 0..3 = Low/Moderate/High/Very High, default 1 Moderate), typing suppression, VAD on/off, VAD mode
|
|
* (automatic | volume_gate | hybrid), VAD level -50..50 dB (default -30);
|
|
* - playback DSP is per output device: aec (echo cancellation), agc (level other users), comfort noise
|
|
* on/off + level -97..-23 dB (default -60), echo reduction/ducking -40..0 dB (default 0);
|
|
* - master output level and notification level are dB on the official fader (-200..+10, 0.5 steps),
|
|
* notification device 'voice' (follow playback device, default), '' (system default), 'none' or an id.
|
|
* Profiles live under `template` (used for every device without its own entry) and `devices` (by the
|
|
* capture/playback device id; a device's entry is created by its first change).
|
|
*
|
|
* TeamsPeach extension kept from earlier versions: `vadSensitivity` of the automatic/hybrid detector.
|
|
*
|
|
* Fresh profiles get the official defaults. A profile written by an earlier TeamsPeach version is migrated
|
|
* in memory from its legacy keys (transmitMode, vadSensitivity, echoCancellation, noiseSuppression,
|
|
* autoGainControl, voiceVolume): every stored choice keeps its effect; controls that did not exist before
|
|
* (playback AGC, comfort noise, ducking, typing attenuation, VAD level, noise reduction level) start at the
|
|
* official defaults. Input profiles stored before `denoiseLevel` existed keep every stored field, including
|
|
* their denoise on/off choice, and gain the default level. The file itself is only rewritten — without the
|
|
* legacy keys — by the user's next settings change.
|
|
*/
|
|
|
|
const VAD_MODES = ['automatic', 'volume_gate', 'hybrid'];
|
|
const SENSITIVITIES = ['high', 'normal', 'low'];
|
|
const RELEASE_DELAYS = [100, 300, 500];
|
|
const MAX_DEVICE_PROFILES = 64;
|
|
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
|
|
const isBool = (v) => typeof v === 'boolean';
|
|
const intIn = (lo, hi) => (v) => Number.isInteger(v) && v >= lo && v <= hi;
|
|
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
|
|
|
/** Official fader modifier: finite, -200..+10, on the 0.5 dB grid. */
|
|
function validDb(v) {
|
|
return typeof v === 'number' && Number.isFinite(v) && v >= -200 && v <= 10 && Number.isInteger(v * 2);
|
|
}
|
|
|
|
const IN_FIELDS = {
|
|
agc: isBool,
|
|
denoise: isBool,
|
|
denoiseLevel: intIn(0, 3),
|
|
typing: isBool,
|
|
vad: isBool,
|
|
vadMode: (v) => VAD_MODES.includes(v),
|
|
vadLevel: intIn(-50, 50),
|
|
vadSensitivity: (v) => SENSITIVITIES.includes(v),
|
|
};
|
|
|
|
const OUT_FIELDS = {
|
|
aec: isBool,
|
|
agc: isBool,
|
|
comfortNoise: isBool,
|
|
comfortNoiseLevel: intIn(-97, -23),
|
|
echoReduction: intIn(-40, 0),
|
|
};
|
|
|
|
// Official defaults (main.js class k / class T).
|
|
const IN_DEFAULT = Object.freeze({ agc: true, denoise: true, denoiseLevel: 1, typing: false, vad: true, vadMode: 'automatic', vadLevel: -30, vadSensitivity: 'normal' });
|
|
const OUT_DEFAULT = Object.freeze({ aec: false, agc: true, comfortNoise: false, comfortNoiseLevel: -60, echoReduction: 0 });
|
|
|
|
function validProfile(fields) {
|
|
return (p) => isPlainObject(p)
|
|
&& Object.keys(p).length === Object.keys(fields).length
|
|
&& Object.entries(fields).every(([k, check]) => Object.prototype.hasOwnProperty.call(p, k) && check(p[k]));
|
|
}
|
|
|
|
function validDsp(fields) {
|
|
const profile = validProfile(fields);
|
|
return (v) => isPlainObject(v)
|
|
&& Object.keys(v).length === 2
|
|
&& profile(v.template)
|
|
&& isPlainObject(v.devices)
|
|
&& Object.keys(v.devices).length <= MAX_DEVICE_PROFILES
|
|
&& Object.entries(v.devices).every(([id, p]) => id.length >= 1 && id.length <= 512 && !CONTROL.test(id) && profile(p));
|
|
}
|
|
|
|
const VOICE_SCHEMA = {
|
|
pttEnabled: { default: false, check: isBool },
|
|
pttReleaseDelay: {
|
|
default: { active: false, ms: 300 },
|
|
check: (v) => isPlainObject(v) && Object.keys(v).length === 2 && isBool(v.active) && RELEASE_DELAYS.includes(v.ms),
|
|
},
|
|
inputDsp: { default: { template: { ...IN_DEFAULT }, devices: {} }, check: validDsp(IN_FIELDS) },
|
|
outputDsp: { default: { template: { ...OUT_DEFAULT }, devices: {} }, check: validDsp(OUT_FIELDS) },
|
|
outputLevelDb: { default: 0, check: validDb },
|
|
notificationDeviceId: { default: 'voice', check: (v) => typeof v === 'string' && v.length <= 512 && !CONTROL.test(v) },
|
|
notificationLevelDb: { default: -23, check: validDb },
|
|
};
|
|
|
|
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
|
|
/** Legacy voice volume percent (0..200) → dB modifier on the 0.5 dB grid. */
|
|
function percentToDb(pct) {
|
|
if (!(pct > 0)) return -200;
|
|
const db = Math.round(2 * 20 * Math.log10(pct / 100)) / 2;
|
|
return Math.max(-200, Math.min(10, db));
|
|
}
|
|
|
|
/** An input DSP value stored before `denoiseLevel` existed, with the default level added to every profile. */
|
|
function withDenoiseLevel(dsp) {
|
|
if (!isPlainObject(dsp) || !isPlainObject(dsp.devices)) return null;
|
|
const upgrade = (p) => (isPlainObject(p) && !has(p, 'denoiseLevel') ? { ...p, denoiseLevel: IN_DEFAULT.denoiseLevel } : p);
|
|
return { ...dsp, template: upgrade(dsp.template), devices: Object.fromEntries(Object.entries(dsp.devices).map(([id, p]) => [id, upgrade(p)])) };
|
|
}
|
|
|
|
/**
|
|
* Fills voice keys missing from a stored profile (`raw`, as read from disk) from its legacy keys, into
|
|
* `values` (already holding validated stored keys and defaults). A stored valid key is never touched; a stored
|
|
* input DSP that predates `denoiseLevel` is kept with that one field added.
|
|
*/
|
|
function migrateVoicePrefs(raw, values) {
|
|
if (!isPlainObject(raw)) return values;
|
|
if (has(raw, 'inputDsp') && !VOICE_SCHEMA.inputDsp.check(raw.inputDsp)) {
|
|
const upgraded = withDenoiseLevel(raw.inputDsp);
|
|
if (upgraded && VOICE_SCHEMA.inputDsp.check(upgraded)) values.inputDsp = upgraded;
|
|
}
|
|
// A first-run or unrelated preference object is not a legacy audio profile.
|
|
if (!['transmitMode', 'vadSensitivity', 'autoGainControl', 'noiseSuppression', 'echoCancellation', 'voiceVolume'].some((key) => has(raw, key))) return values;
|
|
const legacy = (key, check, fallback) => (has(raw, key) && check(raw[key]) ? raw[key] : fallback);
|
|
const mode = legacy('transmitMode', (v) => v === 'ptt' || v === 'open' || v === 'vad', 'ptt');
|
|
if (!has(raw, 'pttEnabled')) values.pttEnabled = mode === 'ptt';
|
|
if (!has(raw, 'inputDsp')) {
|
|
values.inputDsp = {
|
|
template: {
|
|
...IN_DEFAULT,
|
|
agc: legacy('autoGainControl', isBool, true),
|
|
denoise: legacy('noiseSuppression', isBool, true),
|
|
vad: mode === 'vad',
|
|
vadSensitivity: legacy('vadSensitivity', (v) => SENSITIVITIES.includes(v), 'normal'),
|
|
},
|
|
devices: {},
|
|
};
|
|
}
|
|
if (!has(raw, 'outputDsp')) {
|
|
// Only echo cancellation existed before; playback controls new to TeamsPeach start at the official
|
|
// defaults (release notes say so) — no setting the user made is changed.
|
|
values.outputDsp = {
|
|
template: { ...OUT_DEFAULT, aec: legacy('echoCancellation', isBool, true) },
|
|
devices: {},
|
|
};
|
|
}
|
|
if (!has(raw, 'outputLevelDb')) values.outputLevelDb = percentToDb(legacy('voiceVolume', intIn(0, 200), 100));
|
|
return values;
|
|
}
|
|
|
|
module.exports = { VOICE_SCHEMA, migrateVoicePrefs, percentToDb, IN_DEFAULT, OUT_DEFAULT, MAX_DEVICE_PROFILES };
|