From bd33ef5dc96a938fe1886cc6775d7e38652c055d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 10 Sep 2022 22:10:36 +0200 Subject: keep mic_gain in sync with prefs --- client-web/source/preferences/mod.ts | 9 ++++++--- client-web/source/user/local.ts | 33 ++++++++++++++------------------- 2 files changed, 20 insertions(+), 22 deletions(-) (limited to 'client-web/source') diff --git a/client-web/source/preferences/mod.ts b/client-web/source/preferences/mod.ts index 5f238bb..d1f3635 100644 --- a/client-web/source/preferences/mod.ts +++ b/client-web/source/preferences/mod.ts @@ -17,14 +17,17 @@ type PrefMap = { [Key in keyof T type Optional = { [Key in keyof T]?: T[Key] } export const { prefs: PREFS, explicit: PREFS_EXPLICIT } = register_prefs(PREF_DECLS) const pref_change_handlers: Map unknown>> = new Map() -export const on_pref_changed = (key: keyof typeof PREFS, cb: () => unknown) => - (pref_change_handlers.get(key) +export const on_pref_changed = (key: keyof typeof PREFS, cb: () => unknown): (() => void) => { + const m = (pref_change_handlers.get(key) ?? (() => { const n = new Set<() => unknown>(); pref_change_handlers.set(key, n); return n })() - ).add(cb) + ) + m.add(cb) + return () => m.delete(cb) +} export function register_prefs>>(ds: T): { prefs: PrefMap, explicit: Optional> } { const prefs: PrefMap = {} as PrefMap diff --git a/client-web/source/user/local.ts b/client-web/source/user/local.ts index 4d11517..6fae710 100644 --- a/client-web/source/user/local.ts +++ b/client-web/source/user/local.ts @@ -1,7 +1,7 @@ /// import { log } from "../logger.ts"; -import { PREFS } from "../preferences/mod.ts"; +import { on_pref_changed, PREFS } from "../preferences/mod.ts"; import { RemoteUser } from "./remote.ts"; import { get_rnnoise_node } from "../rnnoise.ts"; import { Room } from "../room.ts"; @@ -12,8 +12,6 @@ import { ediv } from "../helper.ts"; import { ChatMessage } from "../../../common/packets.d.ts"; export class LocalUser extends User { - mic_gain?: GainNode - default_gain: number = PREFS.microphone_gain constructor(room: Room, id: number) { super(room, id) @@ -110,25 +108,22 @@ export class LocalUser extends User { async create_mic_track() { log("media", "requesting user media (audio)") - const audio_contraints = PREFS.rnnoise ? { - channelCount: { ideal: 1 }, - noiseSuppression: { ideal: false }, - echoCancellation: { ideal: true }, - autoGainControl: { ideal: true }, - } : { - channelCount: { ideal: 1 }, - noiseSuppression: { ideal: PREFS.native_noise_suppression }, - echoCancellation: { ideal: PREFS.echo_cancellation }, - autoGainControl: { ideal: PREFS.auto_gain_control }, - }; - - const user_media = await window.navigator.mediaDevices.getUserMedia({ audio: audio_contraints }) + const user_media = await window.navigator.mediaDevices.getUserMedia({ + audio: { + channelCount: { ideal: 1 }, + noiseSuppression: { ideal: PREFS.rnnoise ? false : PREFS.native_noise_suppression }, + echoCancellation: { ideal: PREFS.echo_cancellation }, + autoGainControl: { ideal: PREFS.auto_gain_control }, + } + }) const context = new AudioContext() const source = context.createMediaStreamSource(user_media) const destination = context.createMediaStreamDestination() const gain = context.createGain() - gain.gain.value = this.default_gain - this.mic_gain = gain + gain.gain.value = PREFS.microphone_gain + const clear_gain_cb = on_pref_changed("microphone_gain", () => { + gain.gain.value = PREFS.microphone_gain + }) let rnnoise: RNNoiseNode; if (PREFS.rnnoise) { @@ -146,8 +141,8 @@ export class LocalUser extends User { source.disconnect() if (rnnoise) rnnoise.disconnect() gain.disconnect() + clear_gain_cb() destination.disconnect() - this.mic_gain = undefined }) return t } -- cgit v1.2.3-70-g09d2