Make the three playback options work

Automatic volume leveling runs incoming voice through a compressor,
own mic clicks tick when you start/stop transmitting, and other clients'
mic clicks tick when someone else does. Defaults match TS3 (on, on, off)
and are stored like the other audio settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5.5 committed 2026-10-23 17:59:12 +00:00
1 parent d64b25f22f
commit 97421ff2de
2 files changed
+102 -15

No files matched your search

+66 -11
View File
@@ -398,6 +398,9 @@ const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume";
const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression";
const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation";
const AUTO_GAIN_CONTROL_KEY = "webspeak3:auto-gain-control";
const AUTO_VOLUME_KEY = "webspeak3:auto-volume";
const OWN_MIC_CLICKS_KEY = "webspeak3:own-mic-clicks";
const OTHER_MIC_CLICKS_KEY = "webspeak3:other-mic-clicks";
const AEC3_ENABLED_KEY = "webspeak3:aec3-enabled";
const VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
const PUSH_TO_TALK_KEY = "webspeak3:push-to-talk";
@@ -5525,6 +5528,12 @@ interface AudioSettings {
onRefreshOutputDevices: () => void;
playbackVolume: number;
onPlaybackVolumeChange: (v: number) => void;
autoVolume: boolean;
onAutoVolumeChange: (v: boolean) => void;
ownMicClicks: boolean;
onOwnMicClicksChange: (v: boolean) => void;
otherMicClicks: boolean;
onOtherMicClicksChange: (v: boolean) => void;
onPlayTestTone: () => void;
inputDevices: MediaDeviceInfo[];
inputDeviceId: string;
@@ -5711,15 +5720,15 @@ function WiedergabePanel({ audio }: { audio: AudioSettings }) {
<fieldset className="ts-options-fieldset">
<legend>{t("playback.options")}</legend>
<label className="ts-options-checkbox">
<input type="checkbox" checked disabled readOnly />
<input type="checkbox" checked={audio.autoVolume} onChange={(e) => audio.onAutoVolumeChange(e.target.checked)} />
{t("playback.autoVolume")}
</label>
<label className="ts-options-checkbox">
<input type="checkbox" checked disabled readOnly />
<input type="checkbox" checked={audio.ownMicClicks} onChange={(e) => audio.onOwnMicClicksChange(e.target.checked)} />
{t("playback.ownMicClicks")}
</label>
<label className="ts-options-checkbox">
<input type="checkbox" disabled readOnly />
<input type="checkbox" checked={audio.otherMicClicks} onChange={(e) => audio.onOtherMicClicksChange(e.target.checked)} />
{t("playback.otherMicClicks")}
</label>
</fieldset>
@@ -7299,6 +7308,13 @@ function AppInner() {
// Off by default: a heavier, opt-in alternative/supplement to native AEC for
// speaker+mic (no headset) setups (github.com/WebSpeak3/webspeak3 issue #2).
const [aec3Enabled, setAec3Enabled] = useState(() => loadBoolPref(AEC3_ENABLED_KEY, false));
// Playback options, defaults as in TS3.
const [autoVolume, setAutoVolume] = useState(() => loadBoolPref(AUTO_VOLUME_KEY, true));
const [ownMicClicks, setOwnMicClicks] = useState(() => loadBoolPref(OWN_MIC_CLICKS_KEY, true));
const [otherMicClicks, setOtherMicClicks] = useState(() => loadBoolPref(OTHER_MIC_CLICKS_KEY, false));
// Read from the socket handler, which only sees the first render's state.
const otherMicClicksRef = useRef(otherMicClicks);
const talkersRef = useRef<Set<number>>(new Set());
const [aec3Loading, setAec3Loading] = useState(false);
const aec3Ref = useRef<Aec3Instance | null>(null);
// Mirrors aec3Enabled but updates immediately, for startMic() calls made in
@@ -7564,6 +7580,20 @@ function AppInner() {
localStorage.setItem(PLAYBACK_VOLUME_KEY, String(playbackVolume));
}, [playbackVolume]);
useEffect(() => {
audioPlayerRef.current?.setAutoVolume(autoVolume);
localStorage.setItem(AUTO_VOLUME_KEY, autoVolume ? "1" : "0");
}, [autoVolume]);
useEffect(() => {
localStorage.setItem(OWN_MIC_CLICKS_KEY, ownMicClicks ? "1" : "0");
}, [ownMicClicks]);
useEffect(() => {
otherMicClicksRef.current = otherMicClicks;
localStorage.setItem(OTHER_MIC_CLICKS_KEY, otherMicClicks ? "1" : "0");
}, [otherMicClicks]);
useEffect(() => {
localStorage.setItem(NOISE_SUPPRESSION_KEY, noiseSuppressionEnabled ? "1" : "0");
}, [noiseSuppressionEnabled]);
@@ -7698,6 +7728,7 @@ function AppInner() {
// that existed at the time - a fresh one (first use, or after
// disposeAudioIfIdle) has to be brought up to the current settings.
player.setVolume(playbackVolume);
player.setAutoVolume(autoVolume);
if (outputDeviceId) {
player.setOutputDevice(outputDeviceId).catch((error: Error) =>
appendLog({ text: `Output device error: ${error.message}`, kind: "error" }),
@@ -7791,7 +7822,8 @@ function AppInner() {
setPokes(parked.pokes);
setActiveFavoriteId(parked.favoriteId);
setActiveTab(parked.chatTab);
setTalkers(new Set(parked.talkers));
talkersRef.current = new Set(parked.talkers);
setTalkers(talkersRef.current);
setWhisperChannelIds(new Set(parked.whisperChannelIds));
setWhisperClientIds(new Set(parked.whisperClientIds));
setWhisperLog(parked.whisperLog);
@@ -7833,7 +7865,8 @@ function AppInner() {
setPmThreads({});
setPokes([]);
setActiveTab("channel");
setTalkers(new Set());
talkersRef.current = new Set();
setTalkers(talkersRef.current);
setWhisperChannelIds(new Set());
setWhisperClientIds(new Set());
setWhisperLog([]);
@@ -8195,9 +8228,18 @@ function AppInner() {
// Audio only for the active session tab.
if (!outputMutedRef.current) audioPlayerRef.current?.playFrame(data.pcm);
break;
case "talkers":
setTalkers(new Set<number>(data.clients));
case "talkers": {
const next = new Set<number>(data.clients);
const prev = talkersRef.current;
if (otherMicClicksRef.current && !outputMutedRef.current) {
// One click per update, even if several people start at once.
if ([...next].some((id) => !prev.has(id))) audioPlayerRef.current?.playMicClick(true);
else if ([...prev].some((id) => !next.has(id))) audioPlayerRef.current?.playMicClick(false);
}
talkersRef.current = next;
setTalkers(next);
break;
}
case "reconnecting":
// The gateway lost the TS server and is reconnecting by itself.
appendLog({ text: t("log.serverReconnecting"), kind: "error" });
@@ -10367,10 +10409,17 @@ function AppInner() {
const isAway = ownClient?.away ?? false;
const inputMuted = ownClient?.inputMuted ?? false;
const outputMuted = ownClient?.outputMuted ?? false;
const displayTalkers =
selfActive && !inputMuted && ownClient?.hasTalkPower
? new Set(talkers).add(ownClient.id)
: talkers;
const selfTalking = Boolean(selfActive && !inputMuted && ownClient?.hasTalkPower);
const displayTalkers = selfTalking && ownClient ? new Set(talkers).add(ownClient.id) : talkers;
const selfTalkingSeenRef = useRef(false);
useEffect(() => {
// Skip the initial render so opening the client doesn't click.
if (!selfTalkingSeenRef.current) {
selfTalkingSeenRef.current = true;
return;
}
if (ownMicClicks && !outputMuted) audioPlayerRef.current?.playMicClick(selfTalking);
}, [selfTalking]); // eslint-disable-line react-hooks/exhaustive-deps
const showServerQueryClients =
activeFavoriteId != null &&
favorites.some((f) => f.id === activeFavoriteId && f.showServerQueryClients);
@@ -11608,6 +11657,12 @@ function AppInner() {
onRefreshOutputDevices: refreshOutputDevices,
playbackVolume,
onPlaybackVolumeChange: setPlaybackVolume,
autoVolume,
onAutoVolumeChange: setAutoVolume,
ownMicClicks,
onOwnMicClicksChange: setOwnMicClicks,
otherMicClicks,
onOtherMicClicksChange: setOtherMicClicks,
onPlayTestTone: handlePlayTestTone,
inputDevices,
inputDeviceId,
+36 -4
View File
@@ -357,6 +357,10 @@ export class AudioPlayer {
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;
@@ -364,6 +368,18 @@ export class AudioPlayer {
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;
@@ -398,7 +414,7 @@ export class AudioPlayer {
const source = this.context.createBufferSource();
source.buffer = buffer;
source.connect(this.gain);
source.connect(this.voice);
const now = this.context.currentTime;
if (this.nextTime < now + 0.02) {
@@ -419,6 +435,11 @@ export class AudioPlayer {
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;
@@ -426,16 +447,25 @@ export class AudioPlayer {
/** Plays a short sine-wave test tone through the same volume/output-device routing as voice playback. */
playTestTone(): void {
this.beep(440, 0.6, 0.3);
}
/** Short tick when someone starts (higher) or stops (lower) talking. */
playMicClick(start: boolean): void {
this.beep(start ? 1400 : 900, 0.03, 0.15);
}
private beep(freq: number, duration: number, volume: number): void {
const osc = this.context.createOscillator();
osc.type = "sine";
osc.frequency.value = 440;
osc.frequency.value = freq;
const toneGain = this.context.createGain();
toneGain.gain.value = 0.3;
toneGain.gain.value = volume;
osc.connect(toneGain);
toneGain.connect(this.gain);
const now = this.context.currentTime;
osc.start(now);
osc.stop(now + 0.6);
osc.stop(now + duration);
osc.onended = () => {
osc.disconnect();
toneGain.disconnect();
@@ -487,6 +517,8 @@ export class AudioPlayer {
dispose(): void {
this.detachAecRenderTap();
this.voice.disconnect();
this.leveler.disconnect();
this.gain.disconnect();
if (!this.element) return;
this.element.pause();