mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 09:52:50 +08:00
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:
1 parent
d64b25f22f
commit
97421ff2de
2 files changed
+102
-15
No files matched your search
+66
-11
@@ -398,6 +398,9 @@ const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume";
|
|||||||
const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression";
|
const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression";
|
||||||
const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation";
|
const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation";
|
||||||
const AUTO_GAIN_CONTROL_KEY = "webspeak3:auto-gain-control";
|
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 AEC3_ENABLED_KEY = "webspeak3:aec3-enabled";
|
||||||
const VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
|
const VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
|
||||||
const PUSH_TO_TALK_KEY = "webspeak3:push-to-talk";
|
const PUSH_TO_TALK_KEY = "webspeak3:push-to-talk";
|
||||||
@@ -5525,6 +5528,12 @@ interface AudioSettings {
|
|||||||
onRefreshOutputDevices: () => void;
|
onRefreshOutputDevices: () => void;
|
||||||
playbackVolume: number;
|
playbackVolume: number;
|
||||||
onPlaybackVolumeChange: (v: number) => void;
|
onPlaybackVolumeChange: (v: number) => void;
|
||||||
|
autoVolume: boolean;
|
||||||
|
onAutoVolumeChange: (v: boolean) => void;
|
||||||
|
ownMicClicks: boolean;
|
||||||
|
onOwnMicClicksChange: (v: boolean) => void;
|
||||||
|
otherMicClicks: boolean;
|
||||||
|
onOtherMicClicksChange: (v: boolean) => void;
|
||||||
onPlayTestTone: () => void;
|
onPlayTestTone: () => void;
|
||||||
inputDevices: MediaDeviceInfo[];
|
inputDevices: MediaDeviceInfo[];
|
||||||
inputDeviceId: string;
|
inputDeviceId: string;
|
||||||
@@ -5711,15 +5720,15 @@ function WiedergabePanel({ audio }: { audio: AudioSettings }) {
|
|||||||
<fieldset className="ts-options-fieldset">
|
<fieldset className="ts-options-fieldset">
|
||||||
<legend>{t("playback.options")}</legend>
|
<legend>{t("playback.options")}</legend>
|
||||||
<label className="ts-options-checkbox">
|
<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")}
|
{t("playback.autoVolume")}
|
||||||
</label>
|
</label>
|
||||||
<label className="ts-options-checkbox">
|
<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")}
|
{t("playback.ownMicClicks")}
|
||||||
</label>
|
</label>
|
||||||
<label className="ts-options-checkbox">
|
<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")}
|
{t("playback.otherMicClicks")}
|
||||||
</label>
|
</label>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
@@ -7299,6 +7308,13 @@ function AppInner() {
|
|||||||
// Off by default: a heavier, opt-in alternative/supplement to native AEC for
|
// Off by default: a heavier, opt-in alternative/supplement to native AEC for
|
||||||
// speaker+mic (no headset) setups (github.com/WebSpeak3/webspeak3 issue #2).
|
// speaker+mic (no headset) setups (github.com/WebSpeak3/webspeak3 issue #2).
|
||||||
const [aec3Enabled, setAec3Enabled] = useState(() => loadBoolPref(AEC3_ENABLED_KEY, false));
|
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 [aec3Loading, setAec3Loading] = useState(false);
|
||||||
const aec3Ref = useRef<Aec3Instance | null>(null);
|
const aec3Ref = useRef<Aec3Instance | null>(null);
|
||||||
// Mirrors aec3Enabled but updates immediately, for startMic() calls made in
|
// Mirrors aec3Enabled but updates immediately, for startMic() calls made in
|
||||||
@@ -7564,6 +7580,20 @@ function AppInner() {
|
|||||||
localStorage.setItem(PLAYBACK_VOLUME_KEY, String(playbackVolume));
|
localStorage.setItem(PLAYBACK_VOLUME_KEY, String(playbackVolume));
|
||||||
}, [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(() => {
|
useEffect(() => {
|
||||||
localStorage.setItem(NOISE_SUPPRESSION_KEY, noiseSuppressionEnabled ? "1" : "0");
|
localStorage.setItem(NOISE_SUPPRESSION_KEY, noiseSuppressionEnabled ? "1" : "0");
|
||||||
}, [noiseSuppressionEnabled]);
|
}, [noiseSuppressionEnabled]);
|
||||||
@@ -7698,6 +7728,7 @@ function AppInner() {
|
|||||||
// that existed at the time - a fresh one (first use, or after
|
// that existed at the time - a fresh one (first use, or after
|
||||||
// disposeAudioIfIdle) has to be brought up to the current settings.
|
// disposeAudioIfIdle) has to be brought up to the current settings.
|
||||||
player.setVolume(playbackVolume);
|
player.setVolume(playbackVolume);
|
||||||
|
player.setAutoVolume(autoVolume);
|
||||||
if (outputDeviceId) {
|
if (outputDeviceId) {
|
||||||
player.setOutputDevice(outputDeviceId).catch((error: Error) =>
|
player.setOutputDevice(outputDeviceId).catch((error: Error) =>
|
||||||
appendLog({ text: `Output device error: ${error.message}`, kind: "error" }),
|
appendLog({ text: `Output device error: ${error.message}`, kind: "error" }),
|
||||||
@@ -7791,7 +7822,8 @@ function AppInner() {
|
|||||||
setPokes(parked.pokes);
|
setPokes(parked.pokes);
|
||||||
setActiveFavoriteId(parked.favoriteId);
|
setActiveFavoriteId(parked.favoriteId);
|
||||||
setActiveTab(parked.chatTab);
|
setActiveTab(parked.chatTab);
|
||||||
setTalkers(new Set(parked.talkers));
|
talkersRef.current = new Set(parked.talkers);
|
||||||
|
setTalkers(talkersRef.current);
|
||||||
setWhisperChannelIds(new Set(parked.whisperChannelIds));
|
setWhisperChannelIds(new Set(parked.whisperChannelIds));
|
||||||
setWhisperClientIds(new Set(parked.whisperClientIds));
|
setWhisperClientIds(new Set(parked.whisperClientIds));
|
||||||
setWhisperLog(parked.whisperLog);
|
setWhisperLog(parked.whisperLog);
|
||||||
@@ -7833,7 +7865,8 @@ function AppInner() {
|
|||||||
setPmThreads({});
|
setPmThreads({});
|
||||||
setPokes([]);
|
setPokes([]);
|
||||||
setActiveTab("channel");
|
setActiveTab("channel");
|
||||||
setTalkers(new Set());
|
talkersRef.current = new Set();
|
||||||
|
setTalkers(talkersRef.current);
|
||||||
setWhisperChannelIds(new Set());
|
setWhisperChannelIds(new Set());
|
||||||
setWhisperClientIds(new Set());
|
setWhisperClientIds(new Set());
|
||||||
setWhisperLog([]);
|
setWhisperLog([]);
|
||||||
@@ -8195,9 +8228,18 @@ function AppInner() {
|
|||||||
// Audio only for the active session tab.
|
// Audio only for the active session tab.
|
||||||
if (!outputMutedRef.current) audioPlayerRef.current?.playFrame(data.pcm);
|
if (!outputMutedRef.current) audioPlayerRef.current?.playFrame(data.pcm);
|
||||||
break;
|
break;
|
||||||
case "talkers":
|
case "talkers": {
|
||||||
setTalkers(new Set<number>(data.clients));
|
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;
|
break;
|
||||||
|
}
|
||||||
case "reconnecting":
|
case "reconnecting":
|
||||||
// The gateway lost the TS server and is reconnecting by itself.
|
// The gateway lost the TS server and is reconnecting by itself.
|
||||||
appendLog({ text: t("log.serverReconnecting"), kind: "error" });
|
appendLog({ text: t("log.serverReconnecting"), kind: "error" });
|
||||||
@@ -10367,10 +10409,17 @@ function AppInner() {
|
|||||||
const isAway = ownClient?.away ?? false;
|
const isAway = ownClient?.away ?? false;
|
||||||
const inputMuted = ownClient?.inputMuted ?? false;
|
const inputMuted = ownClient?.inputMuted ?? false;
|
||||||
const outputMuted = ownClient?.outputMuted ?? false;
|
const outputMuted = ownClient?.outputMuted ?? false;
|
||||||
const displayTalkers =
|
const selfTalking = Boolean(selfActive && !inputMuted && ownClient?.hasTalkPower);
|
||||||
selfActive && !inputMuted && ownClient?.hasTalkPower
|
const displayTalkers = selfTalking && ownClient ? new Set(talkers).add(ownClient.id) : talkers;
|
||||||
? new Set(talkers).add(ownClient.id)
|
const selfTalkingSeenRef = useRef(false);
|
||||||
: talkers;
|
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 =
|
const showServerQueryClients =
|
||||||
activeFavoriteId != null &&
|
activeFavoriteId != null &&
|
||||||
favorites.some((f) => f.id === activeFavoriteId && f.showServerQueryClients);
|
favorites.some((f) => f.id === activeFavoriteId && f.showServerQueryClients);
|
||||||
@@ -11608,6 +11657,12 @@ function AppInner() {
|
|||||||
onRefreshOutputDevices: refreshOutputDevices,
|
onRefreshOutputDevices: refreshOutputDevices,
|
||||||
playbackVolume,
|
playbackVolume,
|
||||||
onPlaybackVolumeChange: setPlaybackVolume,
|
onPlaybackVolumeChange: setPlaybackVolume,
|
||||||
|
autoVolume,
|
||||||
|
onAutoVolumeChange: setAutoVolume,
|
||||||
|
ownMicClicks,
|
||||||
|
onOwnMicClicksChange: setOwnMicClicks,
|
||||||
|
otherMicClicks,
|
||||||
|
onOtherMicClicksChange: setOtherMicClicks,
|
||||||
onPlayTestTone: handlePlayTestTone,
|
onPlayTestTone: handlePlayTestTone,
|
||||||
inputDevices,
|
inputDevices,
|
||||||
inputDeviceId,
|
inputDeviceId,
|
||||||
|
|||||||
+36
-4
@@ -357,6 +357,10 @@ export class AudioPlayer {
|
|||||||
private destination: MediaStreamAudioDestinationNode | null = null;
|
private destination: MediaStreamAudioDestinationNode | null = null;
|
||||||
private element: SinkableElement | null = null;
|
private element: SinkableElement | null = null;
|
||||||
private gain: GainNode;
|
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 context: AudioContext;
|
||||||
private aecTap: ScriptProcessorNode | null = null;
|
private aecTap: ScriptProcessorNode | null = null;
|
||||||
private aecSink: GainNode | null = null;
|
private aecSink: GainNode | null = null;
|
||||||
@@ -364,6 +368,18 @@ export class AudioPlayer {
|
|||||||
constructor(context: AudioContext) {
|
constructor(context: AudioContext) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.gain = context.createGain();
|
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)) {
|
if (!("setSinkId" in HTMLMediaElement.prototype)) {
|
||||||
this.gain.connect(context.destination);
|
this.gain.connect(context.destination);
|
||||||
return;
|
return;
|
||||||
@@ -398,7 +414,7 @@ export class AudioPlayer {
|
|||||||
|
|
||||||
const source = this.context.createBufferSource();
|
const source = this.context.createBufferSource();
|
||||||
source.buffer = buffer;
|
source.buffer = buffer;
|
||||||
source.connect(this.gain);
|
source.connect(this.voice);
|
||||||
|
|
||||||
const now = this.context.currentTime;
|
const now = this.context.currentTime;
|
||||||
if (this.nextTime < now + 0.02) {
|
if (this.nextTime < now + 0.02) {
|
||||||
@@ -419,6 +435,11 @@ export class AudioPlayer {
|
|||||||
this.gain.gain.value = gain;
|
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. */
|
/** The node all voice/test audio ultimately reaches - use as a mic-monitoring target so it also respects volume/output device. */
|
||||||
getInputNode(): AudioNode {
|
getInputNode(): AudioNode {
|
||||||
return this.gain;
|
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. */
|
/** Plays a short sine-wave test tone through the same volume/output-device routing as voice playback. */
|
||||||
playTestTone(): void {
|
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();
|
const osc = this.context.createOscillator();
|
||||||
osc.type = "sine";
|
osc.type = "sine";
|
||||||
osc.frequency.value = 440;
|
osc.frequency.value = freq;
|
||||||
const toneGain = this.context.createGain();
|
const toneGain = this.context.createGain();
|
||||||
toneGain.gain.value = 0.3;
|
toneGain.gain.value = volume;
|
||||||
osc.connect(toneGain);
|
osc.connect(toneGain);
|
||||||
toneGain.connect(this.gain);
|
toneGain.connect(this.gain);
|
||||||
const now = this.context.currentTime;
|
const now = this.context.currentTime;
|
||||||
osc.start(now);
|
osc.start(now);
|
||||||
osc.stop(now + 0.6);
|
osc.stop(now + duration);
|
||||||
osc.onended = () => {
|
osc.onended = () => {
|
||||||
osc.disconnect();
|
osc.disconnect();
|
||||||
toneGain.disconnect();
|
toneGain.disconnect();
|
||||||
@@ -487,6 +517,8 @@ export class AudioPlayer {
|
|||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
this.detachAecRenderTap();
|
this.detachAecRenderTap();
|
||||||
|
this.voice.disconnect();
|
||||||
|
this.leveler.disconnect();
|
||||||
this.gain.disconnect();
|
this.gain.disconnect();
|
||||||
if (!this.element) return;
|
if (!this.element) return;
|
||||||
this.element.pause();
|
this.element.pause();
|
||||||
|
|||||||
Reference in new issue
Block a user