feat(stt): add energy-based VAD with silence gate and auto-stop (#249)

* loop: stt-warmup completed after 6 iterations

* refactor: extract STT startup helpers and simplify audio recorder

* feat(stt): add energy-based VAD with silence gate and auto-stop

- New VoiceActivityDetector class: adaptive noise floor, per-frame RMS
  classification, trailing-silence endpointing, configurable thresholds
- AudioWorkletProcessor computes RMS in existing flush loop and posts
  {samples, rms} instead of a bare Int16Array
- AudioRecorder integrates VAD via handleVadFrame() in both worklet and
  ScriptProcessor paths; auto-calls stop() on trailing silence; silence
  gate in processRecording() fires onNoSpeech instead of sending empty
  audio to the transcription API (fixes Whisper hallucination bug)
- useSTT wires setOnNoSpeech to reset isProcessing/isRecording cleanly
  without surfacing an error
- computeRms() exported helper for ScriptProcessor fallback path

* fix(stt): clear processing state when stopping a silent recording

stopRecording() called recorder.stop() before setIsProcessing(true). For
a silent take the recorder synchronously fires onNoSpeech (which sets
processing false), so the subsequent setIsProcessing(true) won and the
voice overlay was stuck in the processing state forever.

Set processing before stop() so the recorder callback (onNoSpeech or
onDataAvailable) has the final say on the processing flag.

* fix(stt): always emit a terminal signal so the voice overlay never sticks

Two remaining stuck paths after the silent-stop fix:

- processRecording() early-returned without firing onNoSpeech when a take
  had no captured audio (e.g. a quick start/stop under one worklet flush,
  totalSamples === 0). Now any non-emitting path (empty audio or no
  detected speech) fires onNoSpeech, while a genuine abort still returns
  silently.
- stopRecording() set isProcessing true even when the recorder was no
  longer recording (double-stop or post auto-stop), where stop() skips
  processRecording entirely and nothing clears the flag. Guard the
  external path on recorder state before entering processing.

* loop: mediarecorder-stt completed after 6 iterations

* refactor(stt): extract shared helpers and cleanup

* refactor(stt): add separate arm/disarm thresholds for voice swipe

* fix(stt): prevent default touch behavior on voice gesture handlers

* feat(stt): add audio-level speech detection before emitting data
This commit is contained in:
Chris Scott authored and GitHub committed 2026-06-02 12:30:33 -04:00
1 parent ab624a0970
commit 2b2cb6d23d
13 files changed
+1625 -549

No files matched your search

@@ -47,7 +47,8 @@ const revokeBlobUrls = (attachments: ImageAttachment[]) => {
const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"]
const VOICE_SEND_SWIPE_THRESHOLD = 30
const VOICE_SEND_SWIPE_ARM_THRESHOLD = 24
const VOICE_SEND_SWIPE_DISARM_THRESHOLD = 8
type VoiceButtonVariant = 'desktop' | 'mobile'
@@ -496,6 +497,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
return
}
event.preventDefault()
voiceGestureStartYRef.current = event.clientY
voiceSwipeArmedRef.current = false
setIsVoiceSwipeArmed(false)
@@ -510,8 +512,11 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
return
}
event.preventDefault()
const deltaY = voiceGestureStartYRef.current - event.clientY
const nextIsSwipeArmed = deltaY >= VOICE_SEND_SWIPE_THRESHOLD
const nextIsSwipeArmed = voiceSwipeArmedRef.current
? deltaY >= VOICE_SEND_SWIPE_DISARM_THRESHOLD
: deltaY >= VOICE_SEND_SWIPE_ARM_THRESHOLD
if (nextIsSwipeArmed !== voiceSwipeArmedRef.current) {
voiceSwipeArmedRef.current = nextIsSwipeArmed
@@ -532,6 +537,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
return
}
event.preventDefault()
voiceGestureStartYRef.current = null
if (canceled || !voiceSwipeArmedRef.current) {
@@ -1096,7 +1102,7 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
: 'bg-muted hover:bg-muted-foreground/20 text-muted-foreground hover:text-foreground border-border active:bg-muted-foreground/30 active:scale-95'
}`
const containerClassName = isDesktop ? 'relative hidden md:block' : 'relative flex w-full'
const containerClassName = isDesktop ? 'relative hidden md:block' : 'relative flex w-full touch-none select-none'
return (
<div
@@ -1104,6 +1110,9 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
className={containerClassName}
{...voiceGestureHandlers}
>
{!isDesktop && showVoiceFeedback && (
<div aria-hidden="true" className="absolute inset-x-0 bottom-full z-20 h-44 touch-none" />
)}
<VoiceStatusOverlay show={!isDesktop && showVoiceFeedback} label={voiceFeedbackLabel} state={voiceFeedbackState} />
<button
type="button"
@@ -48,7 +48,7 @@ export function VoiceStatusOverlay({ show, label, state }: VoiceStatusOverlayPro
className="pointer-events-none absolute inset-x-0 bottom-0 z-10"
>
<span className="sr-only">{label}</span>
<div className="relative flex h-36 w-full flex-col items-center justify-between overflow-hidden rounded-xl border border-green-300/70 bg-gradient-to-t from-green-700 via-green-500 to-emerald-400 px-1 py-3 text-white shadow-lg shadow-green-500/40">
<div className="animate-voice-overlay-in relative flex h-44 w-full flex-col items-center justify-between overflow-hidden rounded-xl border border-green-300/70 bg-gradient-to-t from-green-700 via-green-500 to-emerald-400 px-1 py-4 text-white shadow-lg shadow-green-500/40">
<div className="absolute inset-x-1 top-1 h-10 rounded-full bg-white/20 blur-sm" />
<div className="relative flex flex-1 flex-col items-center justify-center gap-1">
{isLoading ? (