mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +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 VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
|
||||
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 DESIGN_THEME_KEY = "webspeak3:design-theme";
|
||||
// The user's actual pick - "standard" | "nova" | "greenteaspeak" | "custom:<id>".
|
||||
@@ -5549,6 +5551,10 @@ interface AudioSettings {
|
||||
onVadHangoverChange: (v: number) => void;
|
||||
pushToTalk: boolean;
|
||||
onPushToTalkChange: (on: boolean) => void;
|
||||
continuous: boolean;
|
||||
onContinuousChange: (on: boolean) => void;
|
||||
typingAttenuation: boolean;
|
||||
onTypingAttenuationChange: (on: boolean) => void;
|
||||
pttBinding: PttBinding | null;
|
||||
onPttBindingChange: (binding: PttBinding) => void;
|
||||
noiseSuppressionEnabled: boolean;
|
||||
@@ -5785,15 +5791,26 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
|
||||
</>
|
||||
)}
|
||||
<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")}
|
||||
</label>
|
||||
<label className="ts-options-radio">
|
||||
<input
|
||||
type="radio"
|
||||
name="activation"
|
||||
checked={!audio.pushToTalk}
|
||||
onChange={() => audio.onPushToTalkChange(false)}
|
||||
checked={!audio.pushToTalk && !audio.continuous}
|
||||
onChange={() => {
|
||||
audio.onPushToTalkChange(false);
|
||||
audio.onContinuousChange(false);
|
||||
}}
|
||||
/>
|
||||
{t("recording.voiceActivation")}
|
||||
</label>
|
||||
@@ -5832,7 +5849,11 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
|
||||
<legend>{t("recording.dsp")}</legend>
|
||||
<div className="ts-options-dsp-grid">
|
||||
<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")}
|
||||
</label>
|
||||
<label className="ts-options-checkbox">
|
||||
@@ -7286,6 +7307,11 @@ function AppInner() {
|
||||
const [vadThreshold, setVadThreshold] = useState(0.02);
|
||||
const [vadHangover, setVadHangover] = useState(() => loadNumberPref(VAD_HANGOVER_KEY, 0.3));
|
||||
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 [pttPressed, setPttPressed] = useState(false);
|
||||
// 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");
|
||||
}, [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(() => {
|
||||
pttPressedRef.current = pttPressed;
|
||||
if (micCaptureRef.current) micCaptureRef.current.pttPressed = pttPressed;
|
||||
@@ -9339,6 +9384,8 @@ function AppInner() {
|
||||
micCaptureRef.current?.stop();
|
||||
micCaptureRef.current = mic;
|
||||
mic.pushToTalk = pushToTalkRef.current;
|
||||
mic.continuous = continuousRef.current;
|
||||
mic.typingAttenuation = typingAttenuationRef.current;
|
||||
mic.pttPressed = pttPressedRef.current;
|
||||
mic.setExtraAudio(tabAudioRef.current);
|
||||
setMicOn(true);
|
||||
@@ -11206,7 +11253,7 @@ function AppInner() {
|
||||
>
|
||||
🗣️
|
||||
</button>
|
||||
) : (
|
||||
) : continuous ? null : (
|
||||
<label className="ts-icon-slider" title={t("toolbar.vadSensitivity")}>
|
||||
🎚️
|
||||
<input
|
||||
@@ -11677,6 +11724,10 @@ function AppInner() {
|
||||
vadHangover,
|
||||
onVadHangoverChange: setVadHangover,
|
||||
pushToTalk,
|
||||
continuous,
|
||||
onContinuousChange: setContinuous,
|
||||
typingAttenuation,
|
||||
onTypingAttenuationChange: setTypingAttenuation,
|
||||
onPushToTalkChange: setPushToTalk,
|
||||
pttBinding,
|
||||
onPttBindingChange: setPttBinding,
|
||||
|
||||
+16
-1
@@ -87,6 +87,12 @@ export class MicCapture {
|
||||
/** Push-to-talk: send while `pttPressed` instead of voice activation. */
|
||||
pushToTalk = 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) {
|
||||
this.context = context;
|
||||
@@ -139,6 +145,10 @@ export class MicCapture {
|
||||
this.aec3.process(this.aec3Out, captureIn);
|
||||
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
|
||||
// setMonitoring() below can tap what's actually being sent rather than
|
||||
// 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;
|
||||
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.
|
||||
const shouldBeActive = this.extra !== null || talking;
|
||||
if (shouldBeActive !== this.active) {
|
||||
@@ -201,6 +211,11 @@ export class MicCapture {
|
||||
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
|
||||
* removes it with null. The caller keeps ownership of the stream. */
|
||||
setExtraAudio(stream: MediaStream | null): void {
|
||||
|
||||
Reference in new issue
Block a user