2026-06-29 23:54:46 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2026 Lotus Guild
|
|
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
type LocalAudioTrack,
|
|
|
|
|
ParticipantEvent,
|
|
|
|
|
type Room as LivekitRoom,
|
|
|
|
|
Track,
|
|
|
|
|
} from "livekit-client";
|
|
|
|
|
import { logger } from "matrix-js-sdk/lib/logger";
|
|
|
|
|
|
|
|
|
|
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
|
|
|
|
|
import { lotusFlag, lotusParam } from "./lotusWidget";
|
|
|
|
|
import {
|
|
|
|
|
type LotusDenoiseConfig,
|
|
|
|
|
LotusDenoiseProcessor,
|
|
|
|
|
} from "./lotusDenoiseProcessor";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Apply Lotus ML noise suppression to the local mic track as a first-class
|
|
|
|
|
* Element Call audio TrackProcessor (#1), replacing the host's build-time
|
|
|
|
|
* `getUserMedia` monkeypatch.
|
|
|
|
|
*
|
|
|
|
|
* Opt-in via `lotusDenoise=ml` (+ `lotusModel`, `lotusGate`,
|
|
|
|
|
* `lotusGateThreshold`, `lotusDenoiseBase`). Because the processor is attached
|
|
|
|
|
* to the mic publication and re-attached on every (re)publish, denoise
|
|
|
|
|
* survives EC's mid-call reconnect — fixing the A7 "mic dead after reconnect"
|
|
|
|
|
* bug. Additive: no-op without the flag.
|
|
|
|
|
*/
|
2026-06-30 00:00:40 -04:00
|
|
|
/**
|
|
|
|
|
* Resolve the denoise asset base, forcing it to be SAME-ORIGIN. The base is fed
|
|
|
|
|
* to `audioWorklet.addModule()`, which executes the target as code in the
|
|
|
|
|
* worklet scope (processing the live mic) — so a cross-origin base from a
|
|
|
|
|
* crafted call link would be arbitrary code execution. Any non-same-origin or
|
|
|
|
|
* malformed value falls back to the bundled "./denoise/".
|
|
|
|
|
*/
|
|
|
|
|
function safeAssetBase(raw: string | null): string {
|
2026-06-30 00:37:51 -04:00
|
|
|
// Resolve to an ABSOLUTE same-origin href against the document. Absolute is
|
|
|
|
|
// required because native dynamic `import()` (DTLN/DeepFilterNet) resolves a
|
|
|
|
|
// relative specifier against the JS chunk's URL, not the document — so a
|
|
|
|
|
// relative "./denoise/" would 404. addModule()/fetch() work with absolute
|
|
|
|
|
// too, so this keeps all three asset-load paths consistent.
|
|
|
|
|
const fallback = new URL("./denoise/", window.location.href).href;
|
2026-06-30 00:00:40 -04:00
|
|
|
if (!raw) return fallback;
|
|
|
|
|
try {
|
|
|
|
|
const u = new URL(raw, window.location.href);
|
|
|
|
|
if (u.origin !== window.location.origin) return fallback;
|
|
|
|
|
return u.href.endsWith("/") ? u.href : `${u.href}/`;
|
|
|
|
|
} catch {
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-29 23:54:46 -04:00
|
|
|
export function startLotusDenoise(vm: CallViewModel): () => void {
|
2026-06-30 00:49:11 -04:00
|
|
|
// Gate on a DEDICATED flag — NOT the existing `lotusDenoise=ml` that the
|
|
|
|
|
// host's build-time getUserMedia shim already uses. Otherwise simply shipping
|
|
|
|
|
// this fork (while the host still injects its shim and still sets
|
|
|
|
|
// lotusDenoise=ml) would denoise twice. The host opts into the in-source
|
|
|
|
|
// engine with `lotusDenoiseSource=1` AND stops injecting the shim at the same
|
|
|
|
|
// time. Default off ⇒ the fork is inert and behaviour is unchanged.
|
|
|
|
|
if (!lotusFlag("lotusDenoiseSource")) return () => undefined;
|
2026-06-29 23:54:46 -04:00
|
|
|
|
2026-06-30 00:28:10 -04:00
|
|
|
const requested = lotusParam("lotusModel");
|
|
|
|
|
const model: LotusDenoiseConfig["model"] =
|
|
|
|
|
requested === "speex" ||
|
|
|
|
|
requested === "dtln" ||
|
|
|
|
|
requested === "deepfilternet"
|
|
|
|
|
? requested
|
|
|
|
|
: "rnnoise";
|
2026-06-30 00:15:59 -04:00
|
|
|
|
|
|
|
|
const rawThreshold = lotusParam("lotusGateThreshold");
|
2026-07-01 00:16:23 -04:00
|
|
|
const rawFloor = lotusParam("lotusDenoiseFloor");
|
2026-06-29 23:54:46 -04:00
|
|
|
const config: LotusDenoiseConfig = {
|
2026-06-30 00:15:59 -04:00
|
|
|
model,
|
2026-06-30 00:00:40 -04:00
|
|
|
assetBase: safeAssetBase(lotusParam("lotusDenoiseBase")),
|
2026-06-29 23:54:46 -04:00
|
|
|
gate: lotusFlag("lotusGate"),
|
2026-06-30 00:15:59 -04:00
|
|
|
// Default -45 (matches the reference shim); accept an explicit 0 (don't
|
|
|
|
|
// coerce it away via `|| default`).
|
|
|
|
|
gateThreshold:
|
|
|
|
|
rawThreshold !== null && Number.isFinite(Number(rawThreshold))
|
|
|
|
|
? Number(rawThreshold)
|
|
|
|
|
: -45,
|
2026-07-01 00:16:23 -04:00
|
|
|
// Dry/wet attenuation floor. Default 0.15 (~-16 dB) tames the
|
|
|
|
|
// over-suppression "underwater"/pumping artifact; host can tune via
|
|
|
|
|
// `lotusDenoiseFloor` (0 = full suppression, no floor).
|
|
|
|
|
floor:
|
|
|
|
|
rawFloor !== null && Number.isFinite(Number(rawFloor))
|
|
|
|
|
? Math.min(0.5, Math.max(0, Number(rawFloor)))
|
|
|
|
|
: 0.15,
|
2026-06-29 23:54:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const micOf = (room: LivekitRoom): LocalAudioTrack | undefined =>
|
|
|
|
|
room.localParticipant.getTrackPublication(Track.Source.Microphone)
|
|
|
|
|
?.track as LocalAudioTrack | undefined;
|
|
|
|
|
|
|
|
|
|
const apply = (room: LivekitRoom): void => {
|
|
|
|
|
const mic = micOf(room);
|
|
|
|
|
if (mic && !mic.getProcessor()) {
|
|
|
|
|
void mic
|
|
|
|
|
.setProcessor(new LotusDenoiseProcessor(config))
|
|
|
|
|
.catch((e) => logger.warn("[lotus] denoise setProcessor failed", e));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const roomListeners = new Map<LivekitRoom, () => void>();
|
|
|
|
|
let rooms: LivekitRoom[] = [];
|
|
|
|
|
|
2026-06-30 23:27:44 -04:00
|
|
|
// Drive activation off the LOCAL participant's connection(s), not
|
|
|
|
|
// `livekitRoomItems$` — that stream excludes the local participant and only
|
|
|
|
|
// surfaces rooms with ≥1 remote member, so it wouldn't denoise you while
|
|
|
|
|
// you're alone and is a fragile coupling to a remote-render concern.
|
|
|
|
|
const sub = vm.allConnections$.subscribe((data) => {
|
|
|
|
|
const next = data.getConnections().map((c) => c.livekitRoom);
|
2026-06-29 23:54:46 -04:00
|
|
|
rooms = next;
|
|
|
|
|
for (const [room, off] of roomListeners) {
|
|
|
|
|
if (!next.includes(room)) {
|
|
|
|
|
off();
|
|
|
|
|
roomListeners.delete(room);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const room of next) {
|
|
|
|
|
if (!roomListeners.has(room)) {
|
|
|
|
|
// Re-attach on every (re)publish — this is what makes denoise survive
|
|
|
|
|
// reconnects (A7), unlike the old getUserMedia patch.
|
|
|
|
|
const onPublished = (): void => apply(room);
|
|
|
|
|
room.localParticipant.on(
|
|
|
|
|
ParticipantEvent.LocalTrackPublished,
|
|
|
|
|
onPublished,
|
|
|
|
|
);
|
|
|
|
|
roomListeners.set(room, () =>
|
|
|
|
|
room.localParticipant.off(
|
|
|
|
|
ParticipantEvent.LocalTrackPublished,
|
|
|
|
|
onPublished,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
apply(room);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
sub.unsubscribe();
|
|
|
|
|
for (const off of roomListeners.values()) off();
|
|
|
|
|
roomListeners.clear();
|
|
|
|
|
for (const room of rooms) {
|
|
|
|
|
const mic = micOf(room);
|
|
|
|
|
if (mic?.getProcessor()) void mic.stopProcessor();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|