A free, open source, and extensible speech-to-text application that works completely offline.
add a better viz for mic waiting
CJ Pais549cbde3ebb7
diff --git a/src-tauri/src/actions.rs b/src-tauri/src/actions.rsindex 0aa3b50e..bdf05f12 100644--- a/src-tauri/src/actions.rs+++ b/src-tauri/src/actions.rs@@ -539,46 +539,60 @@ impl ShortcutAction for TranscribeAction {debug!("Microphone mode - always_on: {}", is_always_on);let mut recording_error: Option<String> = None;- if is_always_on {- // Always-on mode: Play audio feedback immediately, then apply mute after sound finishes- debug!("Always-on mode: Playing audio feedback immediately");- let rm_clone = Arc::clone(&rm);- let app_clone = app.clone();- // The blocking helper exits immediately if audio feedback is disabled,- // so we can always reuse this thread to ensure mute happens right after playback.- std::thread::spawn(move || {- play_feedback_sound_blocking(&app_clone, SoundType::Start);- rm_clone.apply_mute();- });-- if let Err(e) = rm.try_start_recording(&binding_id, vad_policy) {- debug!("Recording failed: {}", e);- recording_error = Some(e);- }- } else {- // On-demand mode: Start recording first, then play audio feedback, then apply mute- // This allows the microphone to be activated before playing the sound- debug!("On-demand mode: Starting recording first, then audio feedback");- let recording_start_time = Instant::now();- match rm.try_start_recording(&binding_id, vad_policy) {- Ok(()) => {- debug!("Recording started in {:?}", recording_start_time.elapsed());- // Small delay to ensure microphone stream is active- let app_clone = app.clone();- let rm_clone = Arc::clone(&rm);- std::thread::spawn(move || {- std::thread::sleep(std::time::Duration::from_millis(100));- debug!("Handling delayed audio feedback/mute sequence");- // Helper handles disabled audio feedback by returning early, so we reuse it- // to keep mute sequencing consistent in every mode.+ let recording_start_time = Instant::now();+ match rm.try_start_recording(&binding_id, vad_policy) {+ Ok(readiness) => {+ debug!(+ "Recording request accepted in {:?}; waiting for first microphone samples",+ recording_start_time.elapsed()+ );+ let generation = readiness.generation();+ let app_clone = app.clone();+ let rm_clone = Arc::clone(&rm);+ std::thread::spawn(move || {+ if !readiness.wait() {+ debug!("Microphone readiness wait ended without receiving samples");+ return;+ }++ // Development-only preview hook for evaluating the brief+ // arming animation on hardware that normally starts too fast+ // to make it visible.+ #[cfg(debug_assertions)]+ if let Ok(delay_ms) = std::env::var("HANDY_DEBUG_MIC_READY_DELAY_MS")+ .unwrap_or_default()+ .parse::<u64>()+ {+ let delay_ms = delay_ms.min(10_000);+ if delay_ms > 0 {+ debug!("Delaying microphone-ready cue by {delay_ms}ms for UI preview");+ std::thread::sleep(Duration::from_millis(delay_ms));+ }+ }++ if !rm_clone.is_recording_readiness_current(generation) {+ debug!("Microphone became ready for an inactive recording");+ return;+ }++ debug!("Microphone is receiving samples; recording is ready");+ utils::emit_recording_ready(&app_clone);++ // The start chime is a readiness cue, so it must follow the+ // first real input callback rather than Stream::play() or a+ // fixed delay. The helper returns immediately when feedback+ // is disabled; mute still follows the same readiness point.+ if rm_clone.is_recording_readiness_current(generation) {play_feedback_sound_blocking(&app_clone, SoundType::Start);+ }+ if rm_clone.is_recording_readiness_current(generation) {rm_clone.apply_mute();- });- }- Err(e) => {- debug!("Failed to start recording: {}", e);- recording_error = Some(e);- }+ }+ });+ }+ Err(e) => {+ debug!("Failed to start recording: {}", e);+ recording_error = Some(e);}}@@ -616,6 +630,11 @@ impl ShortcutAction for TranscribeAction {}fn stop(&self, app: &AppHandle, binding_id: &str, _shortcut_str: &str) {+ // Prevent a slow microphone from emitting a ready event or start chime+ // after the user has already requested stop.+ app.state::<Arc<AudioRecordingManager>>()+ .invalidate_recording_readiness();+// Unregister the cancel shortcut when transcription stopsshortcut::unregister_cancel_shortcut(app);diff --git a/src-tauri/src/audio_toolkit/audio/recorder.rs b/src-tauri/src/audio_toolkit/audio/recorder.rsindex 08a44304..8e6c3959 100644--- a/src-tauri/src/audio_toolkit/audio/recorder.rs+++ b/src-tauri/src/audio_toolkit/audio/recorder.rs@@ -21,9 +21,9 @@ use crate::audio_toolkit::{enum Cmd {/// Begin capturing. Carries the send timestamp so the consumer can log how- /// long the command sat in the channel (and how much audio was dropped- /// before it was seen).- Start(VadPolicy, Instant),+ /// long the command sat in the channel, plus a one-shot acknowledgement+ /// sent only after the first microphone sample chunk is processed.+ Start(VadPolicy, Instant, mpsc::Sender<()>),Stop(mpsc::Sender<Vec<f32>>),Shutdown,}@@ -355,11 +355,21 @@ impl AudioRecorder {}}- pub fn start(&self, vad_policy: VadPolicy) -> Result<(), Box<dyn std::error::Error>> {- if let Some(tx) = &self.cmd_tx {- tx.send(Cmd::Start(vad_policy, Instant::now()))?;- }- Ok(())+ /// Queue a recording start and return a one-shot receiver that resolves only+ /// after the first real microphone sample chunk has entered the capture path.+ /// `Stream::play()` returning is not sufficient: some Bluetooth and USB+ /// devices take much longer to begin delivering callbacks.+ pub fn start(+ &self,+ vad_policy: VadPolicy,+ ) -> Result<mpsc::Receiver<()>, Box<dyn std::error::Error>> {+ let tx = self+ .cmd_tx+ .as_ref()+ .ok_or_else(|| Error::other("Recorder is not open"))?;+ let (ready_tx, ready_rx) = mpsc::channel();+ tx.send(Cmd::Start(vad_policy, Instant::now(), ready_tx))?;+ Ok(ready_rx)}pub fn stop(&self) -> Result<Vec<f32>, Box<dyn std::error::Error>> {@@ -623,6 +633,7 @@ fn run_consumer(// when Cmd::Start lands.let mut first_chunk_logged = false;let mut awaiting_first_captured_chunk: Option<Instant> = None;+ let mut capture_ready_tx: Option<mpsc::Sender<()>> = None;// ---------- spectrum visualisation setup ---------------------------- //const BUCKETS: usize = 16;@@ -689,12 +700,13 @@ fn run_consumer(let mut pending = Some(chunk);while let Ok(cmd) = cmd_rx.try_recv() {match cmd {- Cmd::Start(policy, sent_at) => {+ Cmd::Start(policy, sent_at, ready_tx) => {log::debug!("Cmd::Start processed {:?} after send; capture begins with the in-flight chunk",sent_at.elapsed());awaiting_first_captured_chunk = Some(Instant::now());+ capture_ready_tx = Some(ready_tx);stop_flag.store(false, Ordering::Relaxed);vad_policy = policy;processed_samples.clear();@@ -714,6 +726,10 @@ fn run_consumer(}Cmd::Stop(reply_tx) => {recording = false;+ // If Stop was queued before the first chunk, dropping this+ // sender prevents a stale ready UI event or start chime.+ capture_ready_tx = None;+ awaiting_first_captured_chunk = None;stop_flag.store(true, Ordering::Relaxed);// The chunk in hand arrived before the stop; it belongs to@@ -833,6 +849,12 @@ fn run_consumer(started.elapsed());}+ if let Some(ready_tx) = capture_ready_tx.take() {+ // Signal only after this chunk has passed through the visualizer+ // and resampler. Silence still counts: readiness means the host+ // is delivering samples, not that VAD has detected speech.+ let _ = ready_tx.send(());+ }}}}diff --git a/src-tauri/src/managers/audio.rs b/src-tauri/src/managers/audio.rsindex 8cf50a2e..980c68c7 100644--- a/src-tauri/src/managers/audio.rs+++ b/src-tauri/src/managers/audio.rs@@ -13,7 +13,7 @@ use crate::utils;use log::{debug, error, info, trace, warn};use std::path::Path;use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};-use std::sync::{Arc, Mutex};+use std::sync::{mpsc, Arc, Mutex};use std::time::{Duration, Instant};use tauri::Manager;@@ -306,6 +306,23 @@ fn create_audio_recorder(/* ──────────────────────────────────────────────────────────────── */+/// One recording session's first-sample notification. Waiting on this never+/// blocks the shortcut coordinator: callers hand it to a dedicated worker.+pub struct RecordingReadiness {+ receiver: mpsc::Receiver<()>,+ generation: u64,+}++impl RecordingReadiness {+ pub fn wait(self) -> bool {+ self.receiver.recv().is_ok()+ }++ pub fn generation(&self) -> u64 {+ self.generation+ }+}+#[derive(Clone)]pub struct AudioRecordingManager {/// Never assign through this directly — route every write through@@ -327,6 +344,10 @@ pub struct AudioRecordingManager {/// the main/webview thread when a worker holds `state` across a slow/// CoreAudio open/close.recording_active: Arc<AtomicBool>,+ /// Invalidates asynchronous first-sample UI/chime work when a recording is+ /// stopped or cancelled. This prevents a slow device from producing a late+ /// "ready" indication for a session the user already ended.+ capture_generation: Arc<AtomicU64>,/// Resolution of a *named* microphone (selected or clamshell) to its cpal/// device, cached so on-demand recording starts skip the full device/// enumeration (~40-110ms). Keyed by the resolved name, so a settings@@ -363,6 +384,7 @@ impl AudioRecordingManager {cancel_generation: Arc::new(AtomicU64::new(0)),stream_router,recording_active: Arc::new(AtomicBool::new(false)),+ capture_generation: Arc::new(AtomicU64::new(0)),cached_device: Arc::new(Mutex::new(None)),};@@ -707,7 +729,7 @@ impl AudioRecordingManager {&self,binding_id: &str,vad_policy: VadPolicy,- ) -> Result<(), String> {+ ) -> Result<RecordingReadiness, String> {let mut state = self.state.lock().unwrap();if let RecordingState::Idle = *state {@@ -726,16 +748,23 @@ impl AudioRecordingManager {}if let Some(rec) = self.recorder.lock().unwrap().as_ref() {- if rec.start(vad_policy).is_ok() {- *self.is_recording.lock().unwrap() = true;- self.set_state(- &mut state,- RecordingState::Recording {- binding_id: binding_id.to_string(),- },- );- debug!("Recording started for binding {binding_id}");- return Ok(());+ match rec.start(vad_policy) {+ Ok(receiver) => {+ let generation = self.capture_generation.fetch_add(1, Ordering::AcqRel) + 1;+ *self.is_recording.lock().unwrap() = true;+ self.set_state(+ &mut state,+ RecordingState::Recording {+ binding_id: binding_id.to_string(),+ },+ );+ debug!("Recording requested for binding {binding_id}");+ return Ok(RecordingReadiness {+ receiver,+ generation,+ });+ }+ Err(error) => return Err(format!("Failed to start recorder: {error}")),}}Err("Recorder not available".to_string())@@ -791,6 +820,16 @@ impl AudioRecordingManager {Ok(())}+ /// Invalidate pending first-sample UI and audio-feedback work immediately.+ /// Called at the beginning of stop, before the slower capture drain starts.+ pub fn invalidate_recording_readiness(&self) {+ self.capture_generation.fetch_add(1, Ordering::AcqRel);+ }++ pub fn is_recording_readiness_current(&self, generation: u64) -> bool {+ self.capture_generation.load(Ordering::Acquire) == generation+ }+pub fn cancel_generation(&self) -> u64 {self.cancel_generation.load(Ordering::Acquire)}@@ -800,6 +839,7 @@ impl AudioRecordingManager {}pub fn stop_recording(&self, binding_id: &str, cancel_generation: u64) -> Option<Vec<f32>> {+ self.invalidate_recording_readiness();let mut state = self.state.lock().unwrap();match *state {@@ -886,6 +926,7 @@ impl AudioRecordingManager {/// Cancel any ongoing recording without returning audio samplespub fn cancel_recording(&self) {+ self.invalidate_recording_readiness();self.cancel_generation.fetch_add(1, Ordering::AcqRel);let mut state = self.state.lock().unwrap();diff --git a/src-tauri/src/overlay.rs b/src-tauri/src/overlay.rsindex 655c5e2e..34e5e46f 100644--- a/src-tauri/src/overlay.rs+++ b/src-tauri/src/overlay.rs@@ -582,6 +582,23 @@ fn show_overlay_state_on_main(app_handle: &AppHandle, state: &str) {}}+/// Notify the visible recording overlay that the input stream has delivered its+/// first sample chunk. Audio feedback uses the same backend readiness signal,+/// but this targeted event is skipped when overlays are disabled.+pub fn emit_recording_ready(app_handle: &AppHandle) {+ if !OVERLAY_ENABLED.load(Ordering::Relaxed) {+ return;+ }++ // Showing the overlay is also queued onto the main thread. Queue readiness+ // there as well so a very fast always-on stream cannot overtake show-overlay+ // and then get reset back to the arming state by the frontend.+ let handle = app_handle.clone();+ let _ = app_handle.run_on_main_thread(move || {+ let _ = handle.emit_to("recording_overlay", "recording-ready", ());+ });+}+/// Shows the recording overlay window with fade-in animationpub fn show_recording_overlay(app_handle: &AppHandle) {show_overlay_state(app_handle, "recording");diff --git a/src/overlay/RecordingOverlay.css b/src/overlay/RecordingOverlay.cssindex e8ce0bef..d369d0c2 100644--- a/src/overlay/RecordingOverlay.css+++ b/src/overlay/RecordingOverlay.css@@ -310,10 +310,23 @@width: 7px;height: 7px;border-radius: 50%;- background: var(--s-accent);flex: none;+ box-sizing: border-box;+ transition:+ background-color 180ms ease,+ opacity 180ms ease;+}+.sdot.ready {+ background: var(--s-accent);+ opacity: 1;animation: sdot-pulse 1.9s infinite;}+/* A quiet neutral dot while the host has not delivered its first sample yet. */+.sdot.arming {+ background: var(--s-muted);+ opacity: 0.45;+ animation: none;+}@keyframes sdot-pulse {0% {box-shadow: 0 0 0 0 var(--s-accent-soft);@@ -334,7 +347,9 @@opacity: 1;}-/* Waveform — simple reactive bars, themed; heights set inline from mic levels. */+/* Waveform — reactive only after the first microphone callback. While arming,+ a muted travelling pulse acknowledges the shortcut without pretending that+ audio levels are already being received. */.swave {display: flex;align-items: center;@@ -349,7 +364,42 @@max-height: 18px;border-radius: 2px;background: var(--s-accent);- transition: height 80ms linear;+ transition:+ height 80ms linear,+ background-color 180ms ease,+ opacity 180ms ease;+}+.swave.arming i {+ height: 6px !important;+ background: var(--s-muted);+ opacity: 0.35;+ animation: swave-arming 900ms ease-in-out infinite;+}+.swave.arming i:nth-child(2),+.swave.arming i:nth-child(8) {+ animation-delay: 75ms;+}+.swave.arming i:nth-child(3),+.swave.arming i:nth-child(7) {+ animation-delay: 150ms;+}+.swave.arming i:nth-child(4),+.swave.arming i:nth-child(6) {+ animation-delay: 225ms;+}+.swave.arming i:nth-child(5) {+ animation-delay: 300ms;+}+@keyframes swave-arming {+ 0%,+ 100% {+ transform: scaleY(0.55);+ opacity: 0.25;+ }+ 50% {+ transform: scaleY(1.5);+ opacity: 0.7;+ }}.sx {width: 22px;diff --git a/src/overlay/RecordingOverlay.tsx b/src/overlay/RecordingOverlay.tsxindex c32f617d..dbab41e1 100644--- a/src/overlay/RecordingOverlay.tsx+++ b/src/overlay/RecordingOverlay.tsx@@ -22,6 +22,10 @@ const RecordingOverlay: React.FC = () => {const { t } = useTranslation();const [isVisible, setIsVisible] = useState(false);const [state, setState] = useState<OverlayState>("recording");+ // `Stream::play()` returning does not mean hardware callbacks are flowing.+ // Stay visually in an arming state until the backend processes the first+ // actual microphone sample chunk.+ const [captureReady, setCaptureReady] = useState(false);const [levels, setLevels] = useState<number[]>(Array(WAVE_BARS).fill(0));const [streamText, setStreamText] = useState<StreamTextEvent>({committed: "",@@ -51,6 +55,17 @@ const RecordingOverlay: React.FC = () => {useEffect(() => {const setupEventListeners = async () => {const unlistenShow = await listen("show-overlay", async (event) => {+ const overlayState = event.payload as OverlayState;+ // Reset synchronously before settings I/O. A fast microphone can emit+ // recording-ready while the awaits below are in flight; resetting after+ // them would overwrite that event and leave the overlay stuck arming.+ if (overlayState === "recording" || overlayState === "streaming") {+ setCaptureReady(false);+ smoothedLevelsRef.current = Array(16).fill(0);+ setLevels(Array(WAVE_BARS).fill(0));+ setStreamText({ committed: "", tentative: "" });+ }+await syncLanguageFromSettings();// The Live panel flows downward from a top overlay and upward from a// bottom one; read the placement so the layout can flip to match.@@ -64,11 +79,7 @@ const RecordingOverlay: React.FC = () => {} catch {// Keep the previous/default placement if settings can't be read.}- const overlayState = event.payload as OverlayState;setState(overlayState);- if (overlayState === "recording" || overlayState === "streaming") {- setStreamText({ committed: "", tentative: "" });- }if (overlayState === "streaming") {setPhase("listening");setWorkKind("transcribing");@@ -80,6 +91,12 @@ const RecordingOverlay: React.FC = () => {const unlistenHide = await listen("hide-overlay", () => {setIsVisible(false);+ setCaptureReady(false);+ });++ const unlistenReady = await listen("recording-ready", () => {+ setElapsed(0);+ setCaptureReady(true);});const unlistenLevel = await listen<number[]>("mic-level", (event) => {@@ -107,6 +124,7 @@ const RecordingOverlay: React.FC = () => {return () => {unlistenShow();unlistenHide();+ unlistenReady();unlistenLevel();unlistenStream();unlistenPhase();@@ -116,12 +134,12 @@ const RecordingOverlay: React.FC = () => {setupEventListeners();}, []);- // Elapsed timer while the Live overlay is visible.+ // Elapsed capture timer starts only once microphone samples are flowing.useEffect(() => {- if (state !== "streaming" || !isVisible) return;+ if (state !== "streaming" || !isVisible || !captureReady) return;const id = setInterval(() => setElapsed((e) => e + 1), 1000);return () => clearInterval(id);- }, [state, isVisible]);+ }, [state, isVisible, captureReady]);// Stick to the bottom as text streams in — but only while pinned, so a user who// has scrolled up to read history isn't yanked back down by the next chunk.@@ -153,7 +171,7 @@ const RecordingOverlay: React.FC = () => {// ---- Shared building blocks (one visual language for every overlay form) ----const waveform = (- <div className="swave">+ <div className={`swave ${captureReady ? "ready" : "arming"}`}>{levels.map((v, i) => (<ikey={i}@@ -187,7 +205,7 @@ const RecordingOverlay: React.FC = () => {const listeningRow = (showTimer: boolean, showCancel: boolean) => (<div className="sbase"><div className="sbase-l">- <span className="sdot" />+ <span className={`sdot ${captureReady ? "ready" : "arming"}`} /></div>{waveform}<div className="sbase-r">