mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Make continuous transmission and typing attenuation work
Continuous transmission is a third activation mode next to push-to-talk and voice activation (the toolbar hides the VAD slider then). Typing attenuation damps the mic to 10% for 0.2 s after each keystroke, so typing in the client doesn't trigger voice activation. Both off by default. 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
d860aa61e5
commit
ced63fdb2a
2 files changed
+72
-6
No files matched your search
+56
-5
@@ -404,6 +404,8 @@ 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";
|
||||||
|
const CONTINUOUS_KEY = "webspeak3:continuous-transmission";
|
||||||
|
const TYPING_ATTENUATION_KEY = "webspeak3:typing-attenuation";
|
||||||
const PTT_BINDING_KEY = "webspeak3:ptt-key";
|
const PTT_BINDING_KEY = "webspeak3:ptt-key";
|
||||||
const DESIGN_THEME_KEY = "webspeak3:design-theme";
|
const DESIGN_THEME_KEY = "webspeak3:design-theme";
|
||||||
// The user's actual pick - "standard" | "nova" | "greenteaspeak" | "custom:<id>".
|
// The user's actual pick - "standard" | "nova" | "greenteaspeak" | "custom:<id>".
|
||||||
@@ -5549,6 +5551,10 @@ interface AudioSettings {
|
|||||||
onVadHangoverChange: (v: number) => void;
|
onVadHangoverChange: (v: number) => void;
|
||||||
pushToTalk: boolean;
|
pushToTalk: boolean;
|
||||||
onPushToTalkChange: (on: boolean) => void;
|
onPushToTalkChange: (on: boolean) => void;
|
||||||
|
continuous: boolean;
|
||||||
|
onContinuousChange: (on: boolean) => void;
|
||||||
|
typingAttenuation: boolean;
|
||||||
|
onTypingAttenuationChange: (on: boolean) => void;
|
||||||
pttBinding: PttBinding | null;
|
pttBinding: PttBinding | null;
|
||||||
onPttBindingChange: (binding: PttBinding) => void;
|
onPttBindingChange: (binding: PttBinding) => void;
|
||||||
noiseSuppressionEnabled: boolean;
|
noiseSuppressionEnabled: boolean;
|
||||||
@@ -5785,15 +5791,26 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<label className="ts-options-radio">
|
<label className="ts-options-radio">
|
||||||
<input type="radio" name="activation" disabled readOnly />
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="activation"
|
||||||
|
checked={!audio.pushToTalk && audio.continuous}
|
||||||
|
onChange={() => {
|
||||||
|
audio.onPushToTalkChange(false);
|
||||||
|
audio.onContinuousChange(true);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
{t("recording.continuous")}
|
{t("recording.continuous")}
|
||||||
</label>
|
</label>
|
||||||
<label className="ts-options-radio">
|
<label className="ts-options-radio">
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="activation"
|
name="activation"
|
||||||
checked={!audio.pushToTalk}
|
checked={!audio.pushToTalk && !audio.continuous}
|
||||||
onChange={() => audio.onPushToTalkChange(false)}
|
onChange={() => {
|
||||||
|
audio.onPushToTalkChange(false);
|
||||||
|
audio.onContinuousChange(false);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
{t("recording.voiceActivation")}
|
{t("recording.voiceActivation")}
|
||||||
</label>
|
</label>
|
||||||
@@ -5832,7 +5849,11 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
|
|||||||
<legend>{t("recording.dsp")}</legend>
|
<legend>{t("recording.dsp")}</legend>
|
||||||
<div className="ts-options-dsp-grid">
|
<div className="ts-options-dsp-grid">
|
||||||
<label className="ts-options-checkbox">
|
<label className="ts-options-checkbox">
|
||||||
<input type="checkbox" disabled readOnly />
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={audio.typingAttenuation}
|
||||||
|
onChange={(e) => audio.onTypingAttenuationChange(e.target.checked)}
|
||||||
|
/>
|
||||||
{t("recording.typingAttenuation")}
|
{t("recording.typingAttenuation")}
|
||||||
</label>
|
</label>
|
||||||
<label className="ts-options-checkbox">
|
<label className="ts-options-checkbox">
|
||||||
@@ -7286,6 +7307,11 @@ function AppInner() {
|
|||||||
const [vadThreshold, setVadThreshold] = useState(0.02);
|
const [vadThreshold, setVadThreshold] = useState(0.02);
|
||||||
const [vadHangover, setVadHangover] = useState(() => loadNumberPref(VAD_HANGOVER_KEY, 0.3));
|
const [vadHangover, setVadHangover] = useState(() => loadNumberPref(VAD_HANGOVER_KEY, 0.3));
|
||||||
const [pushToTalk, setPushToTalk] = useState(() => loadBoolPref(PUSH_TO_TALK_KEY, false));
|
const [pushToTalk, setPushToTalk] = useState(() => loadBoolPref(PUSH_TO_TALK_KEY, false));
|
||||||
|
// Only matters while pushToTalk is off: send all the time instead of voice activation.
|
||||||
|
const [continuous, setContinuous] = useState(() => loadBoolPref(CONTINUOUS_KEY, false));
|
||||||
|
const continuousRef = useRef(continuous);
|
||||||
|
const [typingAttenuation, setTypingAttenuation] = useState(() => loadBoolPref(TYPING_ATTENUATION_KEY, false));
|
||||||
|
const typingAttenuationRef = useRef(typingAttenuation);
|
||||||
const [pttBinding, setPttBinding] = useState<PttBinding | null>(loadPttBinding);
|
const [pttBinding, setPttBinding] = useState<PttBinding | null>(loadPttBinding);
|
||||||
const [pttPressed, setPttPressed] = useState(false);
|
const [pttPressed, setPttPressed] = useState(false);
|
||||||
// Read when a new MicCapture is created, which happens outside render.
|
// Read when a new MicCapture is created, which happens outside render.
|
||||||
@@ -7523,6 +7549,25 @@ function AppInner() {
|
|||||||
localStorage.setItem(PUSH_TO_TALK_KEY, pushToTalk ? "1" : "0");
|
localStorage.setItem(PUSH_TO_TALK_KEY, pushToTalk ? "1" : "0");
|
||||||
}, [pushToTalk]);
|
}, [pushToTalk]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
continuousRef.current = continuous;
|
||||||
|
if (micCaptureRef.current) micCaptureRef.current.continuous = continuous;
|
||||||
|
localStorage.setItem(CONTINUOUS_KEY, continuous ? "1" : "0");
|
||||||
|
}, [continuous]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
typingAttenuationRef.current = typingAttenuation;
|
||||||
|
if (micCaptureRef.current) micCaptureRef.current.typingAttenuation = typingAttenuation;
|
||||||
|
localStorage.setItem(TYPING_ATTENUATION_KEY, typingAttenuation ? "1" : "0");
|
||||||
|
if (!typingAttenuation) return;
|
||||||
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
|
// The push-to-talk key is not typing.
|
||||||
|
if (e.code !== pttBinding?.code) micCaptureRef.current?.noteTyping();
|
||||||
|
};
|
||||||
|
window.addEventListener("keydown", onKeyDown, true);
|
||||||
|
return () => window.removeEventListener("keydown", onKeyDown, true);
|
||||||
|
}, [typingAttenuation, pttBinding]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
pttPressedRef.current = pttPressed;
|
pttPressedRef.current = pttPressed;
|
||||||
if (micCaptureRef.current) micCaptureRef.current.pttPressed = pttPressed;
|
if (micCaptureRef.current) micCaptureRef.current.pttPressed = pttPressed;
|
||||||
@@ -9339,6 +9384,8 @@ function AppInner() {
|
|||||||
micCaptureRef.current?.stop();
|
micCaptureRef.current?.stop();
|
||||||
micCaptureRef.current = mic;
|
micCaptureRef.current = mic;
|
||||||
mic.pushToTalk = pushToTalkRef.current;
|
mic.pushToTalk = pushToTalkRef.current;
|
||||||
|
mic.continuous = continuousRef.current;
|
||||||
|
mic.typingAttenuation = typingAttenuationRef.current;
|
||||||
mic.pttPressed = pttPressedRef.current;
|
mic.pttPressed = pttPressedRef.current;
|
||||||
mic.setExtraAudio(tabAudioRef.current);
|
mic.setExtraAudio(tabAudioRef.current);
|
||||||
setMicOn(true);
|
setMicOn(true);
|
||||||
@@ -11206,7 +11253,7 @@ function AppInner() {
|
|||||||
>
|
>
|
||||||
🗣️
|
🗣️
|
||||||
</button>
|
</button>
|
||||||
) : (
|
) : continuous ? null : (
|
||||||
<label className="ts-icon-slider" title={t("toolbar.vadSensitivity")}>
|
<label className="ts-icon-slider" title={t("toolbar.vadSensitivity")}>
|
||||||
🎚️
|
🎚️
|
||||||
<input
|
<input
|
||||||
@@ -11677,6 +11724,10 @@ function AppInner() {
|
|||||||
vadHangover,
|
vadHangover,
|
||||||
onVadHangoverChange: setVadHangover,
|
onVadHangoverChange: setVadHangover,
|
||||||
pushToTalk,
|
pushToTalk,
|
||||||
|
continuous,
|
||||||
|
onContinuousChange: setContinuous,
|
||||||
|
typingAttenuation,
|
||||||
|
onTypingAttenuationChange: setTypingAttenuation,
|
||||||
onPushToTalkChange: setPushToTalk,
|
onPushToTalkChange: setPushToTalk,
|
||||||
pttBinding,
|
pttBinding,
|
||||||
onPttBindingChange: setPttBinding,
|
onPttBindingChange: setPttBinding,
|
||||||
|
|||||||
+16
-1
@@ -87,6 +87,12 @@ export class MicCapture {
|
|||||||
/** Push-to-talk: send while `pttPressed` instead of voice activation. */
|
/** Push-to-talk: send while `pttPressed` instead of voice activation. */
|
||||||
pushToTalk = false;
|
pushToTalk = false;
|
||||||
pttPressed = false;
|
pttPressed = false;
|
||||||
|
/** Send all the time, no voice activation (ignored while pushToTalk). */
|
||||||
|
continuous = false;
|
||||||
|
/** Damp the mic right after a keystroke, so typing doesn't trigger
|
||||||
|
* voice activation and is quieter while talking. See noteTyping(). */
|
||||||
|
typingAttenuation = false;
|
||||||
|
private typingUntil = 0;
|
||||||
|
|
||||||
constructor(context: AudioContext, options: MicCaptureOptions) {
|
constructor(context: AudioContext, options: MicCaptureOptions) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
@@ -139,6 +145,10 @@ export class MicCapture {
|
|||||||
this.aec3.process(this.aec3Out, captureIn);
|
this.aec3.process(this.aec3Out, captureIn);
|
||||||
input = this.aec3Out[0];
|
input = this.aec3Out[0];
|
||||||
}
|
}
|
||||||
|
if (this.typingAttenuation && this.context.currentTime < this.typingUntil) {
|
||||||
|
// Copy first: `raw` is the input buffer, not ours to change.
|
||||||
|
input = input.map((v) => v * 0.1);
|
||||||
|
}
|
||||||
// Mirrors the (possibly AEC3-cancelled) signal to the output buffer, so
|
// Mirrors the (possibly AEC3-cancelled) signal to the output buffer, so
|
||||||
// setMonitoring() below can tap what's actually being sent rather than
|
// setMonitoring() below can tap what's actually being sent rather than
|
||||||
// the raw pre-cancellation mic feed - a self-test through the raw feed
|
// the raw pre-cancellation mic feed - a self-test through the raw feed
|
||||||
@@ -152,7 +162,7 @@ export class MicCapture {
|
|||||||
|
|
||||||
const now = this.context.currentTime;
|
const now = this.context.currentTime;
|
||||||
if (rms >= this.threshold) this.activeUntil = now + this.hangoverSeconds;
|
if (rms >= this.threshold) this.activeUntil = now + this.hangoverSeconds;
|
||||||
const talking = this.pushToTalk ? this.pttPressed : now < this.activeUntil;
|
const talking = this.pushToTalk ? this.pttPressed : this.continuous || now < this.activeUntil;
|
||||||
// Shared audio (music, a video) has its own pauses; keep sending throughout.
|
// Shared audio (music, a video) has its own pauses; keep sending throughout.
|
||||||
const shouldBeActive = this.extra !== null || talking;
|
const shouldBeActive = this.extra !== null || talking;
|
||||||
if (shouldBeActive !== this.active) {
|
if (shouldBeActive !== this.active) {
|
||||||
@@ -201,6 +211,11 @@ export class MicCapture {
|
|||||||
this.silence.connect(this.context.destination);
|
this.silence.connect(this.context.destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Call on every keystroke; with typingAttenuation on, the next ~0.2 s are damped. */
|
||||||
|
noteTyping(): void {
|
||||||
|
this.typingUntil = this.context.currentTime + 0.2;
|
||||||
|
}
|
||||||
|
|
||||||
/** Mixes a second stream (e.g. a shared tab's audio) into what is sent, or
|
/** Mixes a second stream (e.g. a shared tab's audio) into what is sent, or
|
||||||
* removes it with null. The caller keeps ownership of the stream. */
|
* removes it with null. The caller keeps ownership of the stream. */
|
||||||
setExtraAudio(stream: MediaStream | null): void {
|
setExtraAudio(stream: MediaStream | null): void {
|
||||||
|
|||||||
Reference in new issue
Block a user