use std::collections::{BTreeMap, HashSet}; use std::sync::Arc; use std::sync::Mutex; use std::sync::Once; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::mpsc::{self, Receiver, SyncSender, TrySendError}; use std::thread; use std::time::Instant; use color_eyre::eyre::{Result, WrapErr, eyre}; use transcribe_cpp::{ Backend, ExtSlot, Model, ModelOptions, RunExtension, RunOptions, TimestampKind, Transcript, WhisperRunOptions, sys::TRANSCRIBE_EXT_KIND_WHISPER_RUN, }; use crate::context::ContextSnapshot; use crate::dictation::{DictationClip, DictationProtocol, pad_for_parakeet}; use crate::dictation_processor::ProcessingObservation; use crate::gguf_session::OfflineGgufSession; use crate::history::{History, HistoryDraft, HistoryKind}; use crate::meeting::{self, TranscriptEntry, TranscriptPublication}; use crate::paste::{PasteMode, Paster}; use crate::suppression::InputActivity; use crate::transcription::WarmTranscriber; use crate::transcription_engine::{PendingTranscript, TranscriptionEngine}; use crate::transcription_models::{ TranscriptionModelId, TranscriptionSelection, model_path, validate, }; pub struct Parakeet { session: OfflineGgufSession, options: RunOptions, name: String, selection: Option, max_audio_samples: Option, stream: Option, } static TRANSCRIBE_LOGGING: Once = Once::new(); const TRANSCRIPTION_SAMPLES_PER_MS: usize = 16; const MAX_PENDING_OUTPUTS: usize = 16; pub enum WorkerEvent { ModelFailed(String), Completed { job_id: DictationJobId, target: TranscriptionTarget, result: Result, processing: Option, }, Stage { job_id: DictationJobId, stage: DictationJobStage, }, Cancelled { job_id: DictationJobId, }, Pasted { kind: PasteKind, result: Result, }, } #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub struct DictationJobId(u64); impl DictationJobId { pub const fn value(self) -> u64 { self.0 } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum DictationJobStage { Transcribing, Processing, } #[derive(Clone, Copy, Debug)] pub enum PasteKind { LastTranscript, MeetingDelta, } #[derive(Clone, Copy, Debug)] pub enum TranscriptionTarget { Paste, Send, VoiceAction, Service, } struct InferenceJob { job_id: DictationJobId, control: Arc, submitted_at: Instant, transcript: PendingTranscript, audio_ms: u64, prepare_ms: u128, target: TranscriptionTarget, protocol: Option>, context: ContextSnapshot, } struct ProcessorJob { job_id: DictationJobId, control: Arc, target: TranscriptionTarget, text: String, context: ContextSnapshot, timings: JobTimings, } /// Pipeline timings carried from inference through output for the final log. #[derive(Clone, Copy)] struct JobTimings { total_started: Instant, queue_ms: u128, audio_ms: u64, prepare_ms: u128, inference_ms: u128, } struct CompletedTranscript { text: String, /// Corrected local transcript before mode processing. raw: String, application: Option, timings: JobTimings, processing: Option, } enum OutputJob { PreparePaste, Completed { job_id: DictationJobId, control: Arc, target: TranscriptionTarget, result: Box>, }, Cancelled { job_id: DictationJobId, }, Paste { sequence: u64, kind: PasteKind, }, } impl OutputJob { fn sequence(&self) -> u64 { match self { Self::PreparePaste => u64::MAX, Self::Completed { job_id, .. } | Self::Cancelled { job_id } => job_id.0, Self::Paste { sequence, .. } => *sequence, } } } #[derive(Default)] struct JobControl { cancelled: AtomicBool, output_started: Mutex, } impl JobControl { fn cancel(&self) -> bool { let output_started = self .output_started .lock() .unwrap_or_else(|error| error.into_inner()); if *output_started { return false; } !self.cancelled.swap(true, Ordering::AcqRel) } fn is_cancelled(&self) -> bool { self.cancelled.load(Ordering::Acquire) } fn begin_output(&self) -> bool { let mut output_started = self .output_started .lock() .unwrap_or_else(|error| error.into_inner()); if self.is_cancelled() { return false; } *output_started = true; true } } #[derive(Default)] struct OrderedOutputs { waiting: BTreeMap, next_sequence: u64, } impl OrderedOutputs { fn push(&mut self, job: OutputJob) -> Vec { if job.sequence() < self.next_sequence { return Vec::new(); } self.waiting.insert(job.sequence(), job); let mut ready = Vec::new(); while let Some(job) = self.waiting.remove(&self.next_sequence) { ready.push(job); self.next_sequence += 1; } ready } } pub struct DictationWorker { engine: Option>, inference_jobs: Option>, output_jobs: Option>, events: Receiver, event_sender: mpsc::Sender, state: Arc>, inference_worker: Option>, processor_workers: Vec>, output_worker: Option>, } struct WorkerState { next_sequence: u64, jobs: BTreeMap>, pending_pastes: usize, } impl WorkerState { fn next_output_sequence(&self) -> Result { // Count accepted work even after it leaves a channel for ordered buffering. if self.jobs.len() + self.pending_pastes >= MAX_PENDING_OUTPUTS { return Err("dictation queue is full"); } Ok(self.next_sequence) } fn cancel_latest(&self) -> Option { self.jobs .iter() .rev() .find_map(|(&job_id, control)| control.cancel().then_some(job_id)) } } impl DictationWorker { pub fn start( activity: InputActivity, transformations: Arc, history: Option, ) -> Self { const PROCESSOR_WORKERS: usize = 2; // Admission caps accepted jobs, so a submitted job always has a slot. let (inference_jobs, inference_receiver) = mpsc::sync_channel::(MAX_PENDING_OUTPUTS); let (processor_jobs, processor_receiver) = mpsc::sync_channel::(4); let (output_jobs, output_receiver) = mpsc::sync_channel::(8); let (event_sender, events) = mpsc::channel(); let state = Arc::new(Mutex::new(WorkerState { next_sequence: 0, jobs: BTreeMap::new(), pending_pastes: 0, })); let engine = match TranscriptionEngine::start(WarmTranscriber::default()) { Ok(engine) => { let events = event_sender.clone(); let _ = engine.run(move |model| { prioritize_inference_thread(); match WarmTranscriber::load() { Ok(loaded) => { tracing::info!("transcription model loaded"); *model = loaded; } Err(error) => { let _ = events.send(WorkerEvent::ModelFailed(error.to_string())); } } }); Some(Arc::new(engine)) } Err(error) => { let _ = event_sender.send(WorkerEvent::ModelFailed(error.to_string())); None } }; let output_worker = thread::spawn({ let state = state.clone(); let events = event_sender.clone(); move || run_output_worker(output_receiver, activity, history, &state, &events) }); let processor_receiver = Arc::new(Mutex::new(processor_receiver)); let processor_workers = (0..PROCESSOR_WORKERS) .map(|_| { let jobs = processor_receiver.clone(); let output = output_jobs.clone(); let events = event_sender.clone(); let transformations = transformations.clone(); thread::spawn(move || { run_processor_worker(&jobs, &output, &events, &transformations) }) }) .collect(); let inference_worker = thread::spawn({ let output = output_jobs.clone(); let events = event_sender.clone(); move || run_inference_worker(inference_receiver, &processor_jobs, &output, &events) }); Self { engine, inference_jobs: Some(inference_jobs), output_jobs: Some(output_jobs), events, event_sender, state, inference_worker: Some(inference_worker), processor_workers, output_worker: Some(output_worker), } } /// Hands the clip to the engine at once, so its live stream finishes /// before any later capture begins one; the job then waits in order. pub fn transcribe( &self, mut clip: DictationClip, target: TranscriptionTarget, protocol: Option>, context: ContextSnapshot, ) -> Result { let mut state = self.state.lock().unwrap_or_else(|error| error.into_inner()); let job_id = DictationJobId(state.next_output_sequence()?); let (Some(engine), Some(jobs)) = (&self.engine, &self.inference_jobs) else { return Err("dictation worker is unavailable"); }; let control = Arc::new(JobControl::default()); let (_, selection) = crate::app_settings::transcription_selection(); let submitted_at = Instant::now(); let audio_ms = clip.duration_ms(); // A voice-delimited clip is retranscribed without its control phrase, // which a stream cannot do. let stream = clip.take_stream().filter(|_| protocol.is_none()); let samples = clip.into_transcription_samples(); crate::dictation_diagnostics::persist(&samples); let prepare_ms = submitted_at.elapsed().as_millis(); let offline = { let control = control.clone(); let protocol = protocol.clone(); Box::new(move |model: &mut WarmTranscriber, samples: Vec| { if control.is_cancelled() { return Err("dictation was cancelled".to_string()); } let _busy = DictationInference::begin(); let transcriber = model .activate(&selection) .map_err(|error| error.to_string())?; match protocol.as_deref() { Some(protocol) => transcriber.transcribe_voice(samples, protocol), None => transcriber.transcribe(samples), } .map_err(|error| error.to_string()) }) }; let transcript = engine .finish_with(stream, samples, offline) .map_err(|_| "dictation worker is unavailable")?; jobs.try_send(InferenceJob { job_id, control: control.clone(), submitted_at, transcript, audio_ms, prepare_ms, target, protocol, context, }) .map(|()| { state.next_sequence += 1; state.jobs.insert(job_id, control); job_id }) .map_err(queue_error) } /// The engine that streams hotkey captures while they are recorded. pub fn engine(&self) -> Option> { self.engine.clone() } pub fn prepare_paste(&self) { let state = self.state.lock().unwrap_or_else(|error| error.into_inner()); if !state.jobs.is_empty() || state.pending_pastes > 0 { return; } drop(state); let Some(output_jobs) = &self.output_jobs else { return; }; let _ = output_jobs.try_send(OutputJob::PreparePaste); } /// Switches the warm model after the work already handed to the engine. pub fn reload(&self, selection: TranscriptionSelection) -> Result<(), &'static str> { let events = self.event_sender.clone(); self.engine .as_ref() .ok_or("dictation worker is unavailable")? .run(move |model| { let _busy = DictationInference::begin(); if let Err(error) = model.activate(&selection) { let _ = events.send(WorkerEvent::ModelFailed(error.to_string())); } }) .map_err(|_| "dictation worker is unavailable") } pub fn paste_last(&self) -> Result<(), &'static str> { self.submit_paste(PasteKind::LastTranscript) } pub fn paste_meeting(&self) -> Result<(), &'static str> { self.submit_paste(PasteKind::MeetingDelta) } fn submit_paste(&self, kind: PasteKind) -> Result<(), &'static str> { let mut state = self.state.lock().unwrap_or_else(|error| error.into_inner()); let sequence = state.next_output_sequence()?; self.output_jobs .as_ref() .ok_or("dictation worker is unavailable")? .try_send(OutputJob::Paste { sequence, kind }) .map(|()| { state.next_sequence += 1; state.pending_pastes += 1; }) .map_err(queue_error) } pub fn try_recv(&self) -> Option { self.events.try_recv().ok() } pub fn is_busy(&self) -> bool { let state = self.state.lock().unwrap_or_else(|error| error.into_inner()); !state.jobs.is_empty() || state.pending_pastes > 0 } pub fn pending_count(&self) -> usize { self.state .lock() .unwrap_or_else(|error| error.into_inner()) .jobs .len() } pub fn cancel_latest(&self) -> Option { let state = self.state.lock().unwrap_or_else(|error| error.into_inner()); let job_id = state.cancel_latest()?; drop(state); let _ = self .output_jobs .as_ref()? .try_send(OutputJob::Cancelled { job_id }); Some(job_id) } fn shutdown(&mut self) { // Accepted jobs still finish: the inference worker drains its queue. self.inference_jobs.take(); join_worker(self.inference_worker.take(), "dictation inference"); for worker in self.processor_workers.drain(..) { join_worker(Some(worker), "dictation processing"); } self.output_jobs.take(); join_worker(self.output_worker.take(), "dictation output"); } } impl Drop for DictationWorker { fn drop(&mut self) { self.shutdown(); } } fn run_output_worker( jobs: Receiver, activity: InputActivity, history: Option, state: &Mutex, events: &mpsc::Sender, ) { let mut paster = Paster::new(activity); let mut last_transcript = None; let mut meeting_cursor = MeetingPasteCursor::default(); let mut ordered = OrderedOutputs::default(); while let Ok(job) = jobs.recv() { if matches!(job, OutputJob::PreparePaste) { paster.prepare(); continue; } for job in ordered.push(job) { let event = finish_output( job, &mut |text, mode, commit| paster.paste(text, mode, commit), &mut last_transcript, &mut meeting_cursor, history.as_ref(), ); let mut state = state.lock().unwrap_or_else(|error| error.into_inner()); match &event { WorkerEvent::Completed { job_id, .. } | WorkerEvent::Cancelled { job_id } => { state.jobs.remove(job_id); } WorkerEvent::Pasted { .. } => { state.pending_pastes = state.pending_pastes.saturating_sub(1); } WorkerEvent::ModelFailed(_) | WorkerEvent::Stage { .. } => {} } drop(state); if events.send(event).is_err() { return; } } } } fn run_processor_worker( jobs: &Mutex>, output: &SyncSender, events: &mpsc::Sender, transformations: &crate::personal_commands::TransformationClient, ) { loop { let job = jobs .lock() .unwrap_or_else(|error| error.into_inner()) .recv(); let Ok(job) = job else { break }; if job.control.is_cancelled() { let _ = output.send(OutputJob::Cancelled { job_id: job.job_id }); continue; } let mut processed = process_job_text( &job, crate::config::dictation_profiles, || { crate::dictation_processor::process_voice_action_cancellable( &job.text, job.context.selected_text.as_deref(), &job.context, &job.control.cancelled, ) }, || { let _ = events.send(WorkerEvent::Stage { job_id: job.job_id, stage: DictationJobStage::Processing, }); }, ); processed.apply_transformations(transformations, &job.context, &job.control.cancelled); if job.control.is_cancelled() { let _ = output.send(OutputJob::Cancelled { job_id: job.job_id }); continue; } if let Some(observation) = &processed.observation && let Some(error) = &observation.fallback { if matches!(job.target, TranscriptionTarget::VoiceAction) { tracing::warn!(%error, "voice action processing failed"); } else { tracing::warn!( profile = observation.profile, %error, "dictation processing fell back to the previous pipeline output" ); } } if output .send(OutputJob::Completed { job_id: job.job_id, control: job.control, target: job.target, result: Box::new(Ok(CompletedTranscript { text: processed.text, raw: job.text, application: job.context.application, timings: job.timings, processing: processed.observation, })), }) .is_err() { break; } } } /// Select processing when the worker reaches the job, keeping Voice Action /// independent of mode snapshots. The stage precedes processing in either path. fn process_job_text( job: &ProcessorJob, profiles: impl FnOnce() -> crate::dictation_processor::Profiles, voice_action: impl FnOnce() -> crate::dictation_processor::Processed, processing: impl FnOnce(), ) -> crate::dictation_processor::Processed { if matches!(job.target, TranscriptionTarget::VoiceAction) { processing(); voice_action() } else { let profiles = profiles(); if profiles.processes(&job.context) { processing(); } profiles.process_cancellable(&job.text, &job.context, &job.control.cancelled) } } /// Waits for each accepted job's transcript in submission order, then hands /// it to processing. The engine thread runs the inference itself. fn run_inference_worker( jobs: Receiver, processor_jobs: &SyncSender, output: &SyncSender, events: &mpsc::Sender, ) { while let Ok(job) = jobs.recv() { if job.control.is_cancelled() { let _ = output.send(OutputJob::Cancelled { job_id: job.job_id }); continue; } let _busy = DictationInference::begin(); let _ = events.send(WorkerEvent::Stage { job_id: job.job_id, stage: DictationJobStage::Transcribing, }); let total_started = job.submitted_at; let queue_ms = total_started.elapsed().as_millis(); let inference_started = Instant::now(); let audio_ms = job.audio_ms; let result = job.transcript.wait().map(|transcribed| { tracing::info!( source = transcribed.source.label(), finish_ms = transcribed.finish_ms, audio_ms, "transcribed dictation" ); let text = transcribed.text; let corrected = if matches!(job.target, TranscriptionTarget::Service) { text.clone() } else { strip_transcript_protocol(&text, job.protocol.as_deref()) }; tracing::debug!( raw_transcript = text, corrected_transcript = corrected, "prepared local transcript" ); corrected }); let timings = JobTimings { total_started, queue_ms, audio_ms, prepare_ms: job.prepare_ms, inference_ms: inference_started.elapsed().as_millis(), }; if job.control.is_cancelled() { let _ = output.send(OutputJob::Cancelled { job_id: job.job_id }); continue; } match result { Ok(text) if matches!( job.target, TranscriptionTarget::Paste | TranscriptionTarget::Send | TranscriptionTarget::VoiceAction ) && !text.trim().is_empty() => { if processor_jobs .send(ProcessorJob { job_id: job.job_id, control: job.control, target: job.target, text, context: job.context, timings, }) .is_err() { break; } } result => { let application = job.context.application; let result = result.map(|text| CompletedTranscript { raw: text.clone(), text, application, timings, processing: None, }); if output .send(OutputJob::Completed { job_id: job.job_id, control: job.control, target: job.target, result: Box::new(result), }) .is_err() { break; } } } } } fn join_worker(worker: Option>, name: &str) { if let Some(worker) = worker && worker.join().is_err() { tracing::error!(worker = name, "worker panicked during shutdown"); } } #[cfg(target_os = "macos")] pub(crate) fn prioritize_inference_thread() { // ggml worker pthreads start at default QoS instead of inheriting this // class; GGUF sessions use one decoder thread so decoding stays here. const QOS_CLASS_USER_INTERACTIVE: u32 = 0x21; unsafe extern "C" { fn pthread_set_qos_class_self_np(qos_class: u32, relative_priority: i32) -> i32; } // SAFETY: This configures only the calling worker thread using the public // macOS pthread QoS API. Relative priority zero is valid for this class. let status = unsafe { pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0) }; if status == 0 { tracing::info!("dictation inference worker uses user-interactive QoS"); } else { tracing::warn!(status, "could not raise dictation inference worker QoS"); } } #[cfg(not(target_os = "macos"))] pub(crate) fn prioritize_inference_thread() {} static DICTATION_INFERENCE: AtomicUsize = AtomicUsize::new(0); struct DictationInference; impl DictationInference { fn begin() -> Self { DICTATION_INFERENCE.fetch_add(1, Ordering::AcqRel); Self } } impl Drop for DictationInference { fn drop(&mut self) { DICTATION_INFERENCE.fetch_sub(1, Ordering::AcqRel); } } /// Whether dictation is capturing or running inference. Meeting /// transcription waits for this to clear before each chunk so it never /// competes with a dictation job for the model hardware. pub(crate) fn dictation_busy() -> bool { DICTATION_INFERENCE.load(Ordering::Acquire) > 0 || crate::recording_environment::intentional_recording_active() } /// Runs the calling thread at utility QoS for background transcription. pub(crate) fn deprioritize_background_thread() { const QOS_CLASS_UTILITY: u32 = 0x11; unsafe extern "C" { fn pthread_set_qos_class_self_np(qos_class: u32, relative_priority: i32) -> i32; } // SAFETY: This configures only the calling thread using the public macOS // pthread QoS API. Relative priority zero is valid for this class. let status = unsafe { pthread_set_qos_class_self_np(QOS_CLASS_UTILITY, 0) }; if status != 0 { tracing::warn!(status, "could not lower background transcription QoS"); } } fn queue_error(error: TrySendError) -> &'static str { match error { TrySendError::Full(_) => "dictation queue is full", TrySendError::Disconnected(_) => "dictation worker is unavailable", } } fn finish_output( job: OutputJob, paste: &mut impl FnMut(&str, PasteMode, &dyn Fn() -> bool) -> Result<()>, last_transcript: &mut Option, meeting_cursor: &mut MeetingPasteCursor, history: Option<&History>, ) -> WorkerEvent { match job { OutputJob::PreparePaste => unreachable!("paste preparation bypasses ordered output"), OutputJob::Completed { job_id, control, target, result, } if !control.is_cancelled() => { let result = *result; let processing = result .as_ref() .ok() .and_then(|result| result.processing.clone()); let result = result.and_then(|completed| { if matches!(target, TranscriptionTarget::VoiceAction) && let Some(error) = completed .processing .as_ref() .and_then(|processing| processing.fallback.clone()) { return Err(error); } if control.is_cancelled() { return Err("voice action was cancelled".into()); } let paste_started = Instant::now(); if !completed.text.trim().is_empty() { let commit = || control.begin_output(); match target { TranscriptionTarget::Paste => { paste(&completed.text, PasteMode::Continue, &commit) } TranscriptionTarget::Send => { paste(&completed.text, PasteMode::Send, &commit) } TranscriptionTarget::VoiceAction => { paste(&completed.text, PasteMode::Standalone, &commit) } TranscriptionTarget::Service => commit() .then_some(()) .ok_or_else(|| eyre!("dictation was cancelled")), } .map_err(|error| error.to_string())?; if matches!( target, TranscriptionTarget::Paste | TranscriptionTarget::Send ) { *last_transcript = Some(completed.text.clone()); } if let Some(history) = history { record_history(history, target, &completed); } } tracing::info!( audio_ms = completed.timings.audio_ms, queue_ms = completed.timings.queue_ms, prepare_ms = completed.timings.prepare_ms, inference_ms = completed.timings.inference_ms, paste_ms = paste_started.elapsed().as_millis(), total_ms = completed.timings.total_started.elapsed().as_millis(), "dictation pipeline completed" ); Ok(completed.text) }); if control.is_cancelled() { WorkerEvent::Cancelled { job_id } } else { WorkerEvent::Completed { job_id, target, result, processing, } } } OutputJob::Completed { job_id, .. } | OutputJob::Cancelled { job_id } => { WorkerEvent::Cancelled { job_id } } OutputJob::Paste { kind, .. } => { let result = match kind { PasteKind::LastTranscript => last_transcript .as_deref() .ok_or_else(|| "no previous transcript is available".to_string()) .and_then(|text| { paste(text, PasteMode::Continue, &|| true) .map_err(|error| error.to_string())?; Ok(text.to_string()) }), PasteKind::MeetingDelta => { paste_meeting_delta(paste, meeting_cursor).map_err(|error| error.to_string()) } }; WorkerEvent::Pasted { kind, result } } } } /// Record one successfully pasted result. History failures must never fail /// the paste that already happened. fn record_history(history: &History, target: TranscriptionTarget, completed: &CompletedTranscript) { let kind = match target { TranscriptionTarget::Paste => HistoryKind::Dictation, TranscriptionTarget::Send => HistoryKind::Send, TranscriptionTarget::VoiceAction => HistoryKind::VoiceAction, TranscriptionTarget::Service => return, }; let draft = HistoryDraft { kind, raw_text: completed.raw.clone(), final_text: completed.text.clone(), application: completed.application.clone(), processing: completed.processing.clone(), audio_ms: completed.timings.audio_ms, inference_ms: completed.timings.inference_ms as u64, total_ms: completed.timings.total_started.elapsed().as_millis() as u64, }; if let Err(error) = history.record(draft) { tracing::warn!(%error, "could not record dictation history"); } } fn strip_transcript_protocol(text: &str, protocol: Option<&DictationProtocol>) -> String { protocol.map_or_else(|| text.trim().to_string(), |protocol| protocol.strip(text)) } #[derive(Default)] struct MeetingPasteCursor { meeting_id: Option, publication: Option, seen: HashSet, } fn paste_meeting_delta( paste: &mut impl FnMut(&str, PasteMode, &dyn Fn() -> bool) -> Result<()>, cursor: &mut MeetingPasteCursor, ) -> Result { let meetings = meeting::list()?; let selected = meeting::active_or_latest(&meetings) .ok_or_else(|| eyre!("no meeting transcript is available"))?; // Live and final ASR use different segment boundaries, so switching a // cursor between them could repeat or skip an overlapping segment. let same_meeting = cursor.meeting_id.as_deref() == Some(&selected.id); let empty = HashSet::new(); let (publication, seen) = if same_meeting { (cursor.publication, &cursor.seen) } else { (None, &empty) }; let transcript = meeting::completed_transcript(&selected.id, publication)?; let (text, pasted_entries) = prepare_meeting_delta(&transcript.entries, seen)?; paste(&text, PasteMode::Standalone, &|| true)?; if !same_meeting { cursor.seen.clear(); } cursor.meeting_id = Some(selected.id.clone()); cursor.publication = Some(transcript.publication); cursor.seen.extend(pasted_entries); Ok(text) } fn prepare_meeting_delta( entries: &[TranscriptEntry], seen: &HashSet, ) -> Result<(String, Vec)> { let new_entries = entries .iter() .filter(|entry| !seen.contains(*entry)) .cloned() .collect::>(); if new_entries.is_empty() { return Err(eyre!("no new completed meeting transcript is available")); } let text = meeting::coalesce_transcript(new_entries.iter().cloned()) .into_iter() .map(|entry| format!("{}: {}", entry.source.label(), entry.text)) .collect::>() .join("\n\n"); Ok((text, new_entries)) } pub(crate) fn default_model_path() -> Result { model_path(crate::transcription_models::definition( crate::transcription_models::TranscriptionModelId::default(), )) } impl Parakeet { #[cfg(test)] pub fn load() -> Result { let (_, selection) = crate::app_settings::transcription_selection(); Self::load_selection(&selection) } pub fn load_selection(selection: &TranscriptionSelection) -> Result { let definition = validate(selection)?; if !crate::transcription_models::is_installed(definition, &selection.language) { return Err(eyre!("{} is not installed", definition.name)); } let path = model_path(definition)?; let mut parakeet = Self::load_from(&path, true, Some(selection))?; let prewarm_started = Instant::now(); let mut silence = Vec::new(); pad_for_parakeet(&mut silence); parakeet .transcribe(&silence) .wrap_err("could not prewarm transcription model")?; tracing::info!( prewarm_ms = prewarm_started.elapsed().as_millis(), "prewarmed transcription model" ); let stream_run = parakeet.stream_run_options(); let architecture = match definition.runtime { crate::transcription_models::ModelRuntime::Gguf(artifact) => artifact.architecture, crate::transcription_models::ModelRuntime::AppleSpeech => "", }; parakeet.stream = parakeet.session.probe_stream(&stream_run, architecture); Ok(parakeet) } /// Streams decode like offline runs, without Whisper's prompt extension /// and at segment timestamps: only the text of a stream is used. fn stream_run_options(&self) -> RunOptions { RunOptions { family: None, timestamps: match self.options.timestamps { TimestampKind::Word | TimestampKind::Token => TimestampKind::Segment, timestamps => timestamps, }, ..self.options.clone() } } pub(crate) fn stream_options(&self) -> Option<&transcribe_cpp::StreamOptions> { self.stream.as_ref() } pub(crate) fn with_stream( &mut self, stream_options: &transcribe_cpp::StreamOptions, cancel: &transcribe_cpp::CancelToken, drive: impl FnOnce(&mut transcribe_cpp::Stream<'_>) -> R, ) -> transcribe_cpp::Result { let options = self.stream_run_options(); self.session .with_stream(&options, stream_options, cancel, drive) } pub(crate) fn load_for_benchmark(model_path: &std::path::Path) -> Result { Self::load_from(model_path, false, None) } fn load_from( model_path: &std::path::Path, diagnostics: bool, selection: Option<&TranscriptionSelection>, ) -> Result { if diagnostics { TRANSCRIBE_LOGGING.call_once(transcribe_cpp::init_logging); } else { transcribe_cpp::disable_logging(); } let model = Model::load_with( model_path, &ModelOptions { backend: Backend::Metal, device: None, }, ) .wrap_err_with(|| { format!( "could not load transcription model from {}", model_path.display() ) })?; let device = model.device()?; if device.kind != "metal" { return Err(eyre!( "transcription model selected {} ({}) instead of Metal", device.kind, device.name )); } let variant = model.variant(); let architecture = model.arch(); let name = format!("transcribe-cpp-{}-{variant}", device.kind); let definition = selection.map(validate).transpose()?; tracing::info!( backend = device.kind, device = device.description, variant, "loaded transcription model" ); let capabilities = model.capabilities(); let mut language = None; let mut family = None; if let (Some(selection), Some(definition)) = (selection, definition) { let runtime_language = crate::gguf_session::validate_gguf_artifact( model_path, definition, &architecture, &variant, selection, capabilities.supports_language_detect, )?; if let Some(runtime_language) = &runtime_language && !capabilities.languages.is_empty() && !capabilities .languages .iter() .any(|language| language == runtime_language) { return Err(eyre!( "{} does not advertise support for {}", definition.name, crate::transcription_models::language_name(&selection.language) )); } if definition.supports_recognition_hints { if !model.accepts_ext(ExtSlot::Run, TRANSCRIBE_EXT_KIND_WHISPER_RUN) { return Err(eyre!( "{} does not accept Whisper recognition hints", definition.name )); } family = Some(RunExtension::Whisper(WhisperRunOptions { initial_prompt: (!selection.recognition_hints.trim().is_empty()) .then(|| selection.recognition_hints.trim().to_string()), ..Default::default() })); } language = runtime_language; } let session = OfflineGgufSession::new(model)?; Ok(Self { session, options: RunOptions { timestamps: match capabilities.max_timestamp_kind { TimestampKind::Word | TimestampKind::Token => TimestampKind::Word, TimestampKind::Auto | TimestampKind::Segment => TimestampKind::Segment, TimestampKind::None => TimestampKind::None, }, language, family, ..Default::default() }, name, selection: selection.cloned(), max_audio_samples: crate::transcription_models::max_audio_chunk_samples( &architecture, capabilities.max_audio_ms, ), stream: None, }) } pub(crate) fn name(&self) -> &str { &self.name } pub(crate) fn matches_selection(&self, selection: &TranscriptionSelection) -> bool { self.selection.as_ref() == Some(selection) } pub(crate) fn model_id(&self) -> Option { self.selection.as_ref().map(|selection| selection.model) } /// Low-level GGUF entry point over prepared audio. `Transcriber` owns the /// whole-clip padding; chunks and control-trimmed reruns below only apply /// the minimum duration, never another model-specific trailing context. pub fn transcribe(&mut self, samples: &[f32]) -> Result { let Some(max_audio_samples) = self .max_audio_samples .filter(|&max_samples| samples.len() > max_samples) else { return self.transcribe_segments(samples).map(|result| result.text); }; let mut text = Vec::new(); for chunk in samples.chunks(max_audio_samples) { let mut chunk = chunk.to_vec(); pad_for_parakeet(&mut chunk); let result = self.transcribe_segments(&chunk)?; let result = result.text.trim(); if !result.is_empty() { text.push(result.to_string()); } } Ok(text.join(" ")) } pub fn transcribe_voice( &mut self, samples: &[f32], protocol: &DictationProtocol, ) -> Result { if self .max_audio_samples .is_some_and(|maximum| samples.len() > maximum) { return self.transcribe(samples); } let transcript = self.transcribe_segments(samples)?; let words = transcript .words .iter() .map(|word| word.text.clone()) .collect::>(); let Some(control_words) = protocol.control_suffix_word_count(&words) else { return Ok(transcript.text); }; let control = &transcript.words[transcript.words.len() - control_words]; let cut = (control.t0_ms.max(0) as usize * TRANSCRIPTION_SAMPLES_PER_MS).min(samples.len()); if cut == 0 || cut == samples.len() { return Ok(transcript.text); } let mut content = samples[..cut].to_vec(); pad_for_parakeet(&mut content); tracing::debug!( control_start_ms = control.t0_ms, "retranscribing voice dictation without control audio" ); self.transcribe(&content) } pub fn transcribe_segments(&mut self, samples: &[f32]) -> Result { self.session .run(samples, &self.options) .wrap_err("transcription failed") } } #[cfg(test)] mod tests { use std::path::PathBuf; use hound::WavReader; use super::*; use crate::dictation::resample_for_parakeet; use crate::meeting::MeetingSource; fn processing_job(target: TranscriptionTarget) -> ProcessorJob { ProcessorJob { job_id: DictationJobId(0), control: Arc::new(JobControl::default()), target, text: "raw instruction".into(), context: ContextSnapshot::default(), timings: JobTimings { total_started: Instant::now(), queue_ms: 0, audio_ms: 1000, prepare_ms: 0, inference_ms: 0, }, } } #[test] fn voice_action_processing_never_loads_modes_and_announces_stage_before_generation() { use crate::dictation_processor::{Processed, ProcessingObservation}; use std::cell::Cell; let stage_sent = Cell::new(false); let processed = process_job_text( &processing_job(TranscriptionTarget::VoiceAction), || panic!("Voice Action must not read ordinary mode settings"), || { assert!( stage_sent.get(), "stage must precede Voice Action settings/generation" ); Processed { text: String::new(), observation: Some(ProcessingObservation { profile: "Voice Action".into(), latency_ms: 0, fallback: Some("fixture generation failed".into()), }), transformations: Vec::new(), } }, || stage_sent.set(true), ); assert!(stage_sent.get()); assert!( processed.text.is_empty(), "failed actions must not fall back to the instruction" ); assert!(processed.transformations.is_empty()); assert_eq!( processed.observation.unwrap().fallback.as_deref(), Some("fixture generation failed") ); } #[test] fn ordinary_processing_snapshots_modes_before_stage_and_keeps_transformations() { use crate::dictation_processor::{Profile, Profiles}; use std::cell::Cell; for target in [TranscriptionTarget::Paste, TranscriptionTarget::Send] { for transformations in [vec![], vec!["fixture-transform".to_string()]] { let loaded = Cell::new(false); let stage_sent = Cell::new(false); let processed = process_job_text( &processing_job(target), || { assert!(!stage_sent.get()); loaded.set(true); Profiles::new( Profile::new("Global", "").transformations(transformations.clone()), ) }, || panic!("ordinary dictation must not read Voice Action settings"), || { assert!(loaded.get()); stage_sent.set(true); }, ); assert!(loaded.get()); assert_eq!(stage_sent.get(), !transformations.is_empty()); assert_eq!(processed.text, "raw instruction"); assert_eq!(processed.transformations, transformations); } } } #[test] #[ignore = "requires HEX_COHERE_MODEL and HEX_COHERE_FIXTURES synthetic audio"] fn cohere_long_form_keeps_all_sections_with_and_without_pauses() { let model_path = PathBuf::from(std::env::var_os("HEX_COHERE_MODEL").expect("model path")); let fixtures = PathBuf::from(std::env::var_os("HEX_COHERE_FIXTURES").expect("fixture path")); let selection = TranscriptionSelection { model: TranscriptionModelId::CohereTranscribe, language: "en".into(), recognition_hints: String::new(), }; let mut model = Parakeet::load_from(&model_path, false, Some(&selection)).unwrap(); eprintln!( "Cohere maximum chunk: {:?} samples", model.max_audio_samples ); for (name, repeats) in [ ("long-continuous", 1), ("long-pauses", 1), ("long-repeated", 3), ] { let mut reader = WavReader::open(fixtures.join(format!("{name}.wav"))).unwrap(); assert_eq!(reader.spec().sample_rate, 16_000); assert_eq!(reader.spec().channels, 1); let samples = reader .samples::() .map(|sample| f32::from(sample.unwrap()) / 32768.0) .collect::>(); assert!(samples.len() > 35 * 16_000); let text = model.transcribe(&samples).unwrap().to_lowercase(); for phrase in [ "small community garden", "bright green door", "purple submarine", ] { assert_eq!( text.matches(phrase).count(), repeats, "{name} lost {phrase:?}: {text}" ); } assert!( text.split_whitespace().count() >= 160 * repeats, "{name}: {text}" ); } } #[test] fn protocol_stripping_removes_control_phrases_without_a_protocol_fallback() { let protocol = DictationProtocol::default(); assert_eq!( strip_transcript_protocol( "Dictate start, use open code and alpha. Dictate stop.", Some(&protocol), ), "Use open code and alpha." ); assert_eq!( strip_transcript_protocol("Dictate start, alpha. Dictate stop.", None), "Dictate start, alpha. Dictate stop." ); let custom_protocol = DictationProtocol::try_new( vec!["begin note".into()], vec!["finish note".into()], vec!["send note".into()], vec!["discard note".into()], ) .unwrap(); assert_eq!( strip_transcript_protocol("Begin note, alpha. Finish note.", Some(&custom_protocol)), "Alpha." ); } #[test] fn meeting_delta_tracks_each_source_and_coalesces_turns() { let entry = |source, start_ms, end_ms, text: &str| TranscriptEntry { source, start_ms, end_ms, text: text.into(), }; let entries = [ entry(MeetingSource::Microphone, 0, 1_000, "First."), entry(MeetingSource::System, 500, 2_500, "Reply."), entry(MeetingSource::Microphone, 1_100, 2_000, "Second."), ]; let mut seen = HashSet::new(); let (text, pasted) = prepare_meeting_delta(&entries, &seen).unwrap(); assert_eq!(text, "You: First.\n\nThem: Reply.\n\nYou: Second."); seen.extend(pasted); assert!(prepare_meeting_delta(&entries, &seen).is_err()); let appended = [ entries[0].clone(), entries[1].clone(), entry(MeetingSource::System, 900, 1_050, "Late completion."), entries[2].clone(), entry(MeetingSource::Microphone, 2_600, 3_000, "Third."), entry(MeetingSource::System, 2_800, 3_200, "Follow-up."), ]; let (text, pasted) = prepare_meeting_delta(&appended, &seen).unwrap(); assert_eq!( text, "Them: Late completion.\n\nYou: Third.\n\nThem: Follow-up." ); seen.extend(pasted); assert!(prepare_meeting_delta(&appended, &seen).is_err()); } #[test] fn parallel_processing_results_are_released_in_submission_order() { let mut outputs = OrderedOutputs::default(); let job = |sequence| OutputJob::Paste { sequence, kind: PasteKind::LastTranscript, }; assert!(outputs.push(job(1)).is_empty()); assert_eq!( outputs .push(job(0)) .into_iter() .map(|job| job.sequence()) .collect::>(), [0, 1] ); assert_eq!( outputs .push(job(2)) .into_iter() .map(|job| job.sequence()) .collect::>(), [2] ); } #[test] fn ordered_waiting_outputs_remain_bounded_after_channel_drain() { let (output_sender, output_receiver) = mpsc::sync_channel(1); let (event_sender, events) = mpsc::channel(); let worker = DictationWorker { engine: None, inference_jobs: None, output_jobs: Some(output_sender), events, event_sender, state: Arc::new(Mutex::new(WorkerState { next_sequence: 1, jobs: BTreeMap::from([(DictationJobId(0), Arc::new(JobControl::default()))]), pending_pastes: 0, })), inference_worker: None, processor_workers: Vec::new(), output_worker: None, }; let mut ordered = OrderedOutputs::default(); for _ in 1..MAX_PENDING_OUTPUTS { worker.paste_last().unwrap(); assert!(ordered.push(output_receiver.try_recv().unwrap()).is_empty()); } assert_eq!(worker.paste_last(), Err("dictation queue is full")); assert_eq!(worker.paste_meeting(), Err("dictation queue is full")); assert_eq!(ordered.waiting.len(), MAX_PENDING_OUTPUTS - 1); assert_eq!( worker.state.lock().unwrap().next_sequence, MAX_PENDING_OUTPUTS as u64 ); worker.state.lock().unwrap().jobs.remove(&DictationJobId(0)); worker.paste_last().unwrap(); assert_eq!( output_receiver.try_recv().unwrap().sequence(), MAX_PENDING_OUTPUTS as u64 ); } #[test] fn cancellation_replaces_waiting_output_and_ignores_late_completion() { let mut outputs = OrderedOutputs::default(); let completed = |sequence| OutputJob::Completed { job_id: DictationJobId(sequence), control: Arc::new(JobControl::default()), target: TranscriptionTarget::Paste, result: Box::new(Err("late result".into())), }; assert!(outputs.push(completed(1)).is_empty()); assert!( outputs .push(OutputJob::Cancelled { job_id: DictationJobId(1), }) .is_empty() ); let ready = outputs.push(OutputJob::Paste { sequence: 0, kind: PasteKind::LastTranscript, }); assert_eq!(ready.len(), 2); assert!(matches!(ready[1], OutputJob::Cancelled { .. })); assert!(outputs.push(completed(1)).is_empty()); } #[test] fn a_job_can_only_be_cancelled_once() { let control = JobControl::default(); assert!(control.cancel()); assert!(!control.cancel()); assert!(control.is_cancelled()); } #[test] fn output_commit_and_cancellation_are_mutually_exclusive() { let committed = JobControl::default(); assert!(committed.begin_output()); assert!(!committed.cancel()); assert!(!committed.is_cancelled()); let cancelled = JobControl::default(); assert!(cancelled.cancel()); assert!(!cancelled.begin_output()); } #[test] fn output_stays_cancellable_through_preparation_but_not_after_mutation() { use std::cell::{Cell, RefCell}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; use crate::history::{HistoryRetention, HistoryStore}; use crate::paste::commit_prepared_paste; let completed = |text: &str| CompletedTranscript { text: text.into(), raw: text.into(), application: None, timings: JobTimings { total_started: Instant::now(), queue_ms: 0, audio_ms: 1_000, prepare_ms: 0, inference_ms: 0, }, processing: None, }; for target in [ TranscriptionTarget::Paste, TranscriptionTarget::Send, TranscriptionTarget::VoiceAction, ] { for cancel_during_preparation in [true, false] { let path = std::env::temp_dir().join(format!( "hex-output-cancellation-{}-{}.json", std::process::id(), SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_nanos() )); let history = History::new(HistoryStore::open( path.clone(), HistoryRetention::Forever, crate::history::now_ms(), )); record_history(&history, TranscriptionTarget::Paste, &completed("previous")); let saved_history = std::fs::read(&path).unwrap(); let mut last_transcript = Some("previous".into()); let clipboard = RefCell::new("original clipboard".to_string()); let pasted = RefCell::new(Vec::new()); let sent = Cell::new(false); let control = Arc::new(JobControl::default()); let (blocked, blocking) = mpsc::channel(); let (resume, resumed) = mpsc::channel(); let cancelling = control.clone(); let cancellation = thread::spawn(move || { blocking.recv_timeout(Duration::from_secs(5)).unwrap(); let accepted = cancelling.cancel(); resume.send(()).unwrap(); accepted }); let pause = || { blocked.send(()).unwrap(); resumed.recv_timeout(Duration::from_secs(5)).unwrap(); }; let event = finish_output( OutputJob::Completed { job_id: DictationJobId(0), control: control.clone(), target, result: Box::new(Ok(completed("new output"))), }, &mut |text, mode, commit| { assert!(matches!( (target, mode), (TranscriptionTarget::Paste, PasteMode::Continue) | (TranscriptionTarget::Send, PasteMode::Send) | (TranscriptionTarget::VoiceAction, PasteMode::Standalone) )); commit_prepared_paste( || { // Model a blocked restore lock or lazy clipboard provider. if cancel_during_preparation { pause(); } Ok(clipboard.borrow().clone()) }, commit, |_previous| { *clipboard.borrow_mut() = text.into(); if !cancel_during_preparation { pause(); } pasted.borrow_mut().push(text.to_string()); Ok(()) }, )?; if matches!(mode, PasteMode::Send) { sent.set(true); } Ok(()) }, &mut last_transcript, &mut MeetingPasteCursor::default(), Some(&history), ); assert_eq!(cancellation.join().unwrap(), cancel_during_preparation); assert_eq!(control.is_cancelled(), cancel_during_preparation); if cancel_during_preparation { assert!(matches!( event, WorkerEvent::Cancelled { job_id: DictationJobId(0) } )); assert_eq!(*clipboard.borrow(), "original clipboard"); assert!(pasted.borrow().is_empty()); assert!(!sent.get()); assert_eq!(last_transcript.as_deref(), Some("previous")); assert_eq!(history.search("").len(), 1); assert_eq!(std::fs::read(&path).unwrap(), saved_history); } else { assert!(matches!( event, WorkerEvent::Completed { result: Ok(ref text), .. } if text == "new output" )); assert_eq!(*clipboard.borrow(), "new output"); assert_eq!(*pasted.borrow(), ["new output"]); assert_eq!(sent.get(), matches!(target, TranscriptionTarget::Send)); let expected_last = if matches!(target, TranscriptionTarget::VoiceAction) { "previous" } else { "new output" }; assert_eq!(last_transcript.as_deref(), Some(expected_last)); let entries = history.search(""); assert_eq!(entries.len(), 2); assert_eq!(entries[0].final_text, "new output"); assert_eq!( entries[0].kind, match target { TranscriptionTarget::Paste => HistoryKind::Dictation, TranscriptionTarget::Send => HistoryKind::Send, TranscriptionTarget::VoiceAction => HistoryKind::VoiceAction, TranscriptionTarget::Service => unreachable!(), } ); } drop(history); std::fs::remove_file(path).unwrap(); } } } /// Replays recordings into the production engine as capture would (160 ms /// blocks, the last 200 ms held back as the release tail) and compares /// the stream's release latency and text with whole-clip transcription. #[test] #[ignore = "requires HEX_STREAM_MODEL (Unified English GGUF) and HEX_STREAM_WAVS (comma-separated)"] fn metal_stream_tracks_offline_transcription() { use crate::transcription::{Transcriber, WarmTranscriber}; use crate::transcription_engine::{Offline, Source, TranscriptionEngine}; let selection = TranscriptionSelection::default(); let mut model = Parakeet::load_from( &PathBuf::from(std::env::var("HEX_STREAM_MODEL").unwrap()), false, Some(&selection), ) .unwrap(); model.stream = model .session .probe_stream(&model.stream_run_options(), "parakeet"); assert!(model.stream.is_some(), "Metal must stream"); let engine = TranscriptionEngine::start(WarmTranscriber::serving(Transcriber::Gguf( Box::new(model), ))) .unwrap(); let speed = std::env::var("HEX_STREAM_SPEED") .ok() .and_then(|speed| speed.parse::().ok()) .unwrap_or(4.0); let offline = || -> Offline { let selection = selection.clone(); Box::new(move |model, samples| { model .activate(&selection) .and_then(|model| model.transcribe(samples)) .map_err(|error| error.to_string()) }) }; let words = |text: &str| { text.split_whitespace() .map(|word| { word.chars() .filter(|c| c.is_alphanumeric()) .flat_map(char::to_lowercase) .collect::() }) .filter(|word| !word.is_empty()) .collect::>() }; for path in std::env::var("HEX_STREAM_WAVS").unwrap().split(',') { let mut reader = WavReader::open(path).unwrap(); let rate = reader.spec().sample_rate; let channels = usize::from(reader.spec().channels); let raw = match reader.spec().sample_format { hound::SampleFormat::Float => reader .samples::() .map(Result::unwrap) .collect::>(), hound::SampleFormat::Int => reader .samples::() .map(|sample| f32::from(sample.unwrap()) / 32_768.0) .collect(), }; let mono = raw .chunks(channels) .map(|frame| frame.iter().sum::() / channels as f32) .collect::>(); let samples = resample_for_parakeet(&mono, rate); // A warm app has already run inputs of this size. let run = |samples: Vec| { engine .finish_with(None, samples, offline()) .unwrap() .wait() .unwrap() }; run(samples.clone()); let whole = run(samples.clone()); let mut stream = engine.begin().expect("the model streams"); let held = samples.len().saturating_sub(3_200); let feeding = Instant::now(); for (index, block) in samples[..held].chunks(2_560).enumerate() { let due = std::time::Duration::from_secs_f64(index as f64 * 0.16 / speed); thread::sleep(due.saturating_sub(feeding.elapsed())); stream.feed(block); } thread::sleep(std::time::Duration::from_secs_f64(0.2 / speed)); let streamed = engine .finish_with(Some(stream), samples.clone(), offline()) .unwrap() .wait() .unwrap(); let (reference, hypothesis) = (words(&whole.text), words(&streamed.text)); let mut previous = (0..=hypothesis.len()).collect::>(); for (row, expected) in reference.iter().enumerate() { let mut current = vec![row + 1; hypothesis.len() + 1]; for (column, actual) in hypothesis.iter().enumerate() { current[column + 1] = (previous[column + 1] + 1) .min(current[column] + 1) .min(previous[column] + usize::from(expected != actual)); } previous = current; } let differences = previous[hypothesis.len()]; println!( "{path}: audio_ms={} offline_ms={} release_ms={} ({}) word_differences={differences}/{}", samples.len() / 16, whole.finish_ms, streamed.finish_ms, streamed.source.label(), reference.len() ); println!(" offline {}", whole.text); println!(" stream {}", streamed.text); assert_eq!(streamed.source, Source::Stream); assert!(differences * 20 <= reference.len().max(20)); } } #[test] fn repeated_cancellation_walks_back_through_pending_jobs() { let jobs = [0, 1, 2] .into_iter() .map(|sequence| (DictationJobId(sequence), Arc::new(JobControl::default()))) .collect(); let state = WorkerState { next_sequence: 3, jobs, pending_pastes: 0, }; assert_eq!(state.cancel_latest(), Some(DictationJobId(2))); assert_eq!(state.cancel_latest(), Some(DictationJobId(1))); assert_eq!(state.cancel_latest(), Some(DictationJobId(0))); assert_eq!(state.cancel_latest(), None); } #[test] #[ignore = "requires VOICE_CONTROL_DICTATION_FIXTURE_DIR and the installed Parakeet model"] fn dictation_protocol_audio() { let directory = PathBuf::from(std::env::var("VOICE_CONTROL_DICTATION_FIXTURE_DIR").unwrap()); let protocol = DictationProtocol::try_new( vec!["say".into()], vec!["say paste".into(), "say stop".into()], vec!["say send".into()], vec!["say cancel".into(), "never mind".into()], ) .unwrap(); let mut model = Parakeet::load().unwrap(); for (name, expected) in [ ("period-stop", "I don't understand this."), ("question-stop", "Is this working?"), ("exclamation-send", "Ship it."), ("comma-stop", "Meet me at five."), ] { let mut reader = WavReader::open(directory.join(format!("{name}.wav"))).unwrap(); assert_eq!(reader.spec().sample_rate, 16_000); let samples = reader .samples::() .collect::, _>>() .unwrap(); let transcript = model.transcribe_segments(&samples).unwrap(); println!("{name}: {:?}", transcript.text); for word in &transcript.words { println!(" {}..{} {:?}", word.t0_ms, word.t1_ms, word.text); } assert_ne!(transcript.timestamp_kind, TimestampKind::None); assert!( protocol.control_suffix(&transcript.text).is_some(), "{name} did not contain a configured control: {:?}", transcript.text ); let clipped = model.transcribe_voice(&samples, &protocol).unwrap(); let prepared = strip_transcript_protocol(&clipped, Some(&protocol)); println!(" clipped: {:?} -> {:?}", clipped, prepared); assert_eq!(prepared, expected); } } #[test] #[ignore = "requires VOICE_CONTROL_STUTTER_FIXTURE and the installed Parakeet model"] fn tdt_decoder_does_not_repeat_single_letter_tokens() { let path = PathBuf::from(std::env::var("VOICE_CONTROL_STUTTER_FIXTURE").unwrap()); let mut reader = WavReader::open(path).unwrap(); let sample_rate = reader.spec().sample_rate; let samples = reader .samples::() .skip(sample_rate as usize * 20) .take(sample_rate as usize * 25) .collect::, _>>() .unwrap(); let samples = resample_for_parakeet(&samples, sample_rate); let text = Parakeet::load().unwrap().transcribe(&samples).unwrap(); assert!(text.contains("sweatshirt"), "unexpected transcript: {text}"); assert!( !text.contains("p p p p") && !text.contains("p-p-p-p"), "pathological token repetition: {text}" ); } } #[cfg(test)] #[path = "dictation_tail_tests.rs"] mod dictation_tail_tests; #[cfg(test)] #[path = "dictation_corpus_tests.rs"] mod dictation_corpus_tests; /// Loads a GGUF model file directly, for opt-in harnesses that replay /// recordings without the installed model catalog. #[cfg(test)] pub(crate) fn load_test_model(path: &std::path::Path) -> Result { Parakeet::load_from(path, false, Some(&TranscriptionSelection::default())) }