speechcore 0.1.1

Reusable Rust speech-to-text runtime with audio capture, VAD, backend selection, model provisioning, and transcript streaming.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
use crate::backend::BackendConfig;
#[cfg(feature = "vad-silero")]
use crate::silero_audio_processor::VadConfig as SileroVadConfig;
#[cfg(feature = "backend-ctranslate2")]
use ct2rs::WhisperOptions;
use serde::{Deserialize, Serialize};

/// Audio sample rate in Hz - hardcoded to 16000 (required by Silero VAD)
pub const SAMPLE_RATE: usize = 16000;

/// Audio processor configuration parameters for general audio processing
/// This is separate from the VAD-specific settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AudioProcessorConfig {
    /// The global buffer size used throughout the application
    /// This is the fundamental audio processing block size in samples
    /// Also used for visualization sample count
    pub buffer_size: usize,
}

impl Default for AudioProcessorConfig {
    fn default() -> Self {
        Self { buffer_size: 1024 }
    }
}

/// Configuration for general core settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct GeneralConfig {
    /// Main model to use for transcription
    pub model: String,
    /// Language for transcription
    pub language: String,
    /// Transcription mode: "realtime" or "manual"
    pub transcription_mode: String,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            model: "small.en".to_string(),
            language: "en".to_string(),
            transcription_mode: "manual".to_string(),
        }
    }
}

/// Configuration for real-time transcription mode
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RealtimeModeConfig {
    /// Maximum audio buffer duration in seconds for VAD history
    pub max_buffer_duration_sec: f32,

    /// Maximum number of speech segments to keep in buffer
    pub max_segment_count: usize,
}

impl Default for RealtimeModeConfig {
    fn default() -> Self {
        Self {
            max_buffer_duration_sec: 30.0,
            max_segment_count: 20,
        }
    }
}

/// Configuration for manual transcription mode
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ManualModeConfig {
    /// Maximum recording duration in seconds (default: 120)
    /// Buffer size is calculated as: max_recording_duration_secs * sample_rate
    pub max_recording_duration_secs: u32,

    /// Whether to clear previous transcript when starting new session
    pub clear_on_new_session: bool,

    /// Duration of each chunk in seconds (default: 29.0)
    /// Note: 29s avoids edge case where duration == chunk_size hits token limits
    pub chunk_duration_seconds: f32,

    /// Whether to enable chunk overlap for manual mode transcription (default: true)
    /// When enabled, uses small overlap between chunks to catch boundary words
    /// Overlap amount is controlled by chunk_overlap_seconds
    pub enable_chunk_overlap: bool,

    /// Overlap duration in seconds between chunks (default: 0.5)
    /// Only used when enable_chunk_overlap is true
    /// Recommended range: 0.1 to 1.0 seconds (avoid 2+ seconds due to hallucination)
    pub chunk_overlap_seconds: f32,

    /// EXPERIMENTAL: Disable chunking for manual mode transcription (default: false)
    /// When enabled, processes entire recording as single segment (no chunk limit)
    /// Note: May consume more memory for very long recordings
    /// Note: many transcription models are trained on short chunks, so very long audio may have issues
    pub disable_chunking: bool,
}

impl Default for ManualModeConfig {
    fn default() -> Self {
        Self {
            max_recording_duration_secs: 120,
            clear_on_new_session: true,
            chunk_duration_seconds: 29.0, // 29s avoids edge case at exactly 30s boundary
            enable_chunk_overlap: true,   // Enable overlap by default
            chunk_overlap_seconds: 2.0,   // 2.0 second overlap (matches packaged config)
            disable_chunking: false,      // Chunking enabled by default
        }
    }
}

/// Configuration for debugging and development
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DebugConfig {
    /// Whether to log statistics
    pub log_stats_enabled: bool,
    /// Whether to save manual mode audio to WAV files for debugging
    pub save_manual_audio_debug: bool,
    /// Directory to save debug recordings (default: "recordings")
    pub recording_dir: String,
}

impl Default for DebugConfig {
    fn default() -> Self {
        Self {
            log_stats_enabled: false,
            save_manual_audio_debug: false,
            recording_dir: "recordings".to_string(),
        }
    }
}

/// Configuration for transcription post-processing
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PostProcessConfig {
    /// Enable post-processing of transcriptions
    pub enabled: bool,
    /// Remove leading dashes from transcriptions
    pub remove_leading_dashes: bool,
    /// Remove trailing dashes from transcriptions
    pub remove_trailing_dashes: bool,
    /// Normalize whitespace (collapse multiple spaces, remove leading/trailing)
    pub normalize_whitespace: bool,
}

impl Default for PostProcessConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            remove_leading_dashes: true,
            remove_trailing_dashes: true,
            normalize_whitespace: true,
        }
    }
}

/// VAD sensitivity presets for different acoustic environments
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum VadSensitivity {
    /// Less sensitive - reduces false positives in noisy environments
    Low,
    /// Balanced - good for most environments (default)
    #[default]
    Medium,
    /// More sensitive - catches quiet speech, may trigger on background noise
    High,
}

impl VadSensitivity {
    /// Get the speech detection threshold for this sensitivity level
    pub fn threshold(&self) -> f32 {
        match self {
            VadSensitivity::Low => 0.15,
            VadSensitivity::Medium => 0.10,
            VadSensitivity::High => 0.05,
        }
    }

    /// Get the speech end threshold (hysteresis) for this sensitivity level
    pub fn speech_end_threshold(&self) -> f32 {
        match self {
            VadSensitivity::Low => 0.12,
            VadSensitivity::Medium => 0.08,
            VadSensitivity::High => 0.03,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct SpeechConfig {
    /// General core configuration
    pub general_config: GeneralConfig,

    /// Backend configuration (includes backend selection)
    pub backend_config: BackendConfig,

    /// Audio processing configuration
    pub audio_processor_config: AudioProcessorConfig,

    /// Real-time transcription mode configuration
    pub realtime_mode_config: RealtimeModeConfig,

    /// Manual transcription mode configuration
    pub manual_mode_config: ManualModeConfig,

    /// Voice Activity Detection configuration
    pub vad_config: VadConfigSerde,

    /// Common transcription options shared across all backends
    pub common_transcription_options: CommonTranscriptionOptions,

    /// CTranslate2-specific options
    pub ctranslate2_options: CT2Options,

    /// Whisper.cpp-specific options
    pub whisper_cpp_options: WhisperCppOptions,

    /// Moonshine-specific options
    pub moonshine_options: MoonshineOptions,

    /// Parakeet TDT-specific options
    pub parakeet_options: ParakeetOptions,

    /// Nemotron 3.5 ASR-specific options
    pub nemotron_options: NemotronOptions,

    /// Debug and development configuration
    pub debug_config: DebugConfig,

    /// Transcription post-processing configuration
    pub post_process_config: PostProcessConfig,

    /// Deprecated legacy field - use backend_config instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compute_type: Option<String>,

    /// Deprecated legacy field - use backend_config instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub device: Option<String>,
}

/// Common transcription options shared across all backends
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CommonTranscriptionOptions {
    /// Beam search width (1 = greedy/fastest, higher = more accurate but slower)
    pub beam_size: usize,
    /// Beam search patience factor
    pub patience: f32,
}

impl Default for CommonTranscriptionOptions {
    fn default() -> Self {
        Self {
            beam_size: 5,
            patience: 1.0,
        }
    }
}

/// CTranslate2-specific transcription options
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CT2Options {
    /// Penalty for repeated tokens
    pub repetition_penalty: f32,
}

impl Default for CT2Options {
    fn default() -> Self {
        Self {
            repetition_penalty: 1.25,
        }
    }
}

/// Whisper.cpp internal thresholds - hardcoded to whisper.cpp defaults
pub const WHISPER_ENTROPY_THOLD: f32 = 2.4;
pub const WHISPER_LOGPROB_THOLD: f32 = -1.0;
pub const WHISPER_NO_SPEECH_THOLD: f32 = 0.6;

/// Whisper.cpp-specific transcription options
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct WhisperCppOptions {
    pub temperature: f32,
    pub suppress_blank: bool,
    pub no_context: bool,
    pub max_tokens: i32,
    /// Initial prompt to condition the model (used internally for chunk continuity)
    #[serde(skip)]
    pub initial_prompt: Option<String>,
}

impl Default for WhisperCppOptions {
    fn default() -> Self {
        Self {
            temperature: 0.2,     // Gentle sampling bump to match packaged config
            suppress_blank: true, // Skip blank segments
            no_context: true,     // Disable context to prevent double transcriptions
            max_tokens: 0,        // No limit
            initial_prompt: None, // Set dynamically for chunk continuity
        }
    }
}

/// Moonshine-specific options
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct MoonshineOptions {
    /// Whether to use cached decoder (prefill + decode steps) for faster inference
    pub enable_cache: bool,
}

/// Parakeet TDT-specific options
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct ParakeetOptions {}

/// Nemotron 3.5 ASR-specific options
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct NemotronOptions {
    /// Target language locale for the lang-ID prompt (e.g. "en-US", "de-DE",
    /// or "auto" for built-in language detection). Empty falls back to en-US.
    pub language: String,
}

impl Default for NemotronOptions {
    fn default() -> Self {
        Self {
            language: "en-US".to_string(),
        }
    }
}

/// Configuration for Voice Activity Detection
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct VadConfigSerde {
    /// VAD sensitivity preset for different acoustic environments
    /// Low: Reduces false positives in noisy environments
    /// Medium: Balanced for most environments (default)
    /// High: Catches quiet speech, may trigger on background noise
    pub sensitivity: VadSensitivity,
    /// Number of frames before confirming speech
    pub hangbefore_frames: usize,
    /// Number of frames after speech before ending segment
    pub hangover_frames: usize,
    /// Number of non-speech frames to tolerate in PossibleSpeech before giving up
    pub silence_tolerance_frames: usize,
    /// Exponential moving average smoothing factor (0.0-1.0)
    pub speech_prob_smoothing: f32,
}

impl Default for VadConfigSerde {
    fn default() -> Self {
        Self {
            sensitivity: VadSensitivity::default(), // Medium sensitivity (threshold: 0.10, speech_end: 0.08)
            hangbefore_frames: 5,                   // 50ms - capture more lead-in audio
            hangover_frames: 30,                    // 300ms - keep more trailing audio
            silence_tolerance_frames: 8,            // 80ms - tolerate more pauses
            speech_prob_smoothing: 0.3,             // EMA smoothing factor (production standard)
        }
    }
}

/// Silero v5 is a stateful recurrent model and MUST be fed contiguous,
/// non-overlapping 512-sample frames; the legacy 10ms (160-sample) hop re-fed
/// overlapping audio and corrupted its recurrent state. So each VAD frame is now
/// 32ms. The configured hang/tolerance frame-counts are expressed in legacy 10ms
/// units, so rescale them to 32ms frames to preserve their intended durations.
#[cfg(feature = "vad-silero")]
fn rescale_vad_frames(frames: usize, frame_size: usize) -> usize {
    const LEGACY_HOP: usize = 160;
    if frames == 0 {
        0
    } else {
        ((frames * LEGACY_HOP + frame_size / 2) / frame_size).max(1)
    }
}

#[cfg(feature = "vad-silero")]
impl SileroVadConfig {
    pub fn from_config(
        vad_config: &VadConfigSerde,
        realtime_config: &RealtimeModeConfig,
        _buffer_size: usize,
        sample_rate: usize,
    ) -> Self {
        let frame_size = 512;
        Self {
            threshold: vad_config.sensitivity.threshold(),
            frame_size,
            sample_rate,
            hangbefore_frames: rescale_vad_frames(vad_config.hangbefore_frames, frame_size),
            hangover_frames: rescale_vad_frames(vad_config.hangover_frames, frame_size),
            hop_samples: frame_size, // non-overlapping: feed Silero contiguous 512-sample frames
            max_buffer_duration: (realtime_config.max_buffer_duration_sec * sample_rate as f32)
                as usize,
            max_segment_count: realtime_config.max_segment_count,
            silence_tolerance_frames: rescale_vad_frames(
                vad_config.silence_tolerance_frames,
                frame_size,
            ),
            speech_end_threshold: vad_config.sensitivity.speech_end_threshold(),
            speech_prob_smoothing: vad_config.speech_prob_smoothing,
        }
    }
}

#[cfg(feature = "vad-silero")]
impl From<(VadConfigSerde, RealtimeModeConfig, usize, usize)> for SileroVadConfig {
    fn from(
        (config, realtime_config, _buffer_size, sample_rate): (
            VadConfigSerde,
            RealtimeModeConfig,
            usize,
            usize,
        ),
    ) -> Self {
        let frame_size = 512;
        Self {
            threshold: config.sensitivity.threshold(),
            frame_size,
            sample_rate,
            hangbefore_frames: rescale_vad_frames(config.hangbefore_frames, frame_size),
            hangover_frames: rescale_vad_frames(config.hangover_frames, frame_size),
            hop_samples: frame_size, // non-overlapping: feed Silero contiguous 512-sample frames
            max_buffer_duration: (realtime_config.max_buffer_duration_sec * sample_rate as f32)
                as usize,
            max_segment_count: realtime_config.max_segment_count,
            silence_tolerance_frames: rescale_vad_frames(
                config.silence_tolerance_frames,
                frame_size,
            ),
            speech_end_threshold: config.sensitivity.speech_end_threshold(),
            speech_prob_smoothing: config.speech_prob_smoothing,
        }
    }
}

impl SpeechConfig {
    /// Migrate legacy compute_type/device fields to new backend_config
    pub fn migrate_legacy_config(&mut self) {
        if let (Some(compute_type), Some(device)) = (&self.compute_type, &self.device) {
            let is_default_config = self.backend_config.threads == num_cpus::get().min(4)
                && !self.backend_config.gpu_enabled;

            if is_default_config {
                tracing::info!(
                    "Migrating legacy config fields (compute_type={}, device={}) to backend_config",
                    compute_type,
                    device
                );

                #[cfg(feature = "backend-ctranslate2")]
                {
                    self.backend_config = crate::backend::ctranslate2::migrate_legacy_config(
                        compute_type,
                        device,
                        None,
                    );
                }
                #[cfg(not(feature = "backend-ctranslate2"))]
                {
                    tracing::warn!(
                        "Legacy CTranslate2 config fields are present, but `backend-ctranslate2` is disabled"
                    );
                }
                self.compute_type = None;
                self.device = None;
            }
        }

        // Ensure whisper.cpp does not reuse context across sessions (prevents duplicate transcriptions)
        if !self.whisper_cpp_options.no_context {
            tracing::info!(
                "Enabling whisper_cpp_options.no_context to prevent cross-session duplication"
            );
            self.whisper_cpp_options.no_context = true;
        }

        // Bring legacy configs up to current default temperature if they were using the old default
        if (self.whisper_cpp_options.temperature - 0.0).abs() < f32::EPSILON {
            self.whisper_cpp_options.temperature = 0.2;
        }
    }
}

#[cfg(feature = "backend-ctranslate2")]
impl CT2Options {
    /// Convert to ct2rs::WhisperOptions, combining with common options
    pub fn to_whisper_options(
        &self,
        common_options: &CommonTranscriptionOptions,
    ) -> WhisperOptions {
        WhisperOptions {
            beam_size: common_options.beam_size,
            patience: common_options.patience,
            repetition_penalty: self.repetition_penalty,
            ..Default::default()
        }
    }
}

#[cfg(feature = "backend-ctranslate2")]
pub fn migrate_legacy_ctranslate2_config(
    compute_type: &str,
    device: &str,
    threads: Option<usize>,
) -> crate::backend::BackendConfig {
    crate::backend::ctranslate2::migrate_legacy_config(compute_type, device, threads)
}