whisrs 0.1.19

Linux-first voice-to-text dictation tool with Groq, OpenAI, and local Whisper backends
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
use base64::Engine;
use tracing::warn;

use super::wire::{LemonadeSessionUpdate, OpenAiSessionUpdate};

/// OpenAI Realtime API rejects transcription prompts longer than this.
pub const PROMPT_MAX_CHARS: usize = 1024;

/// Supported OpenAI-compatible realtime protocol profiles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenAiRealtimeProfile {
    OpenAi,
    Lemonade,
}

/// Transcript delta semantics for a profile.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeltaMode {
    AppendOnly,
    ReplaceableInterim,
}

/// Turn detection mode used for a realtime session.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TurnDetectionMode {
    ServerVad,
    ManualCommit,
}

impl OpenAiRealtimeProfile {
    pub fn parse(value: &str) -> anyhow::Result<Self> {
        match value {
            "openai" => Ok(Self::OpenAi),
            "lemonade" => Ok(Self::Lemonade),
            other => anyhow::bail!(
                "unsupported OpenAI-compatible realtime profile '{other}' (supported: openai, lemonade)"
            ),
        }
    }

    pub fn input_sample_rate(self) -> u32 {
        match self {
            Self::OpenAi => 24_000,
            Self::Lemonade => 16_000,
        }
    }

    pub fn delta_mode(self) -> DeltaMode {
        match self {
            Self::OpenAi => DeltaMode::AppendOnly,
            Self::Lemonade => DeltaMode::ReplaceableInterim,
        }
    }

    pub fn session_update(
        self,
        model: &str,
        language: &str,
        prompt: Option<&str>,
        turn_detection: TurnDetectionMode,
    ) -> anyhow::Result<serde_json::Value> {
        let value = match self {
            Self::OpenAi => serde_json::to_value(OpenAiSessionUpdate::new(
                model,
                language,
                prompt,
                turn_detection,
            ))?,
            Self::Lemonade => {
                serde_json::to_value(LemonadeSessionUpdate::new(model, turn_detection))?
            }
        };
        Ok(value)
    }

    // Decision: keep this `true` for every profile, including the OpenAI
    // append-only / server-VAD profile. Both currently supported profiles need
    // an explicit commit at end-of-audio to flush any trailing speech, even
    // when server VAD is enabled. The risk that server VAD already finalized
    // the last turn before recording stopped (so the commit has nothing to
    // flush and the post-commit completion never arrives) is handled in the
    // engine: once input has closed and at least one transcript was already
    // typed, a missing post-commit completion finalizes gracefully (after a
    // short grace period) instead of erroring or stalling for the full timeout.
    // That keeps the commit-aware lifecycle the design wants without discarding
    // correct transcripts. The turn-detection parameter stays in the signature
    // because a future provider may need different EOS behavior for server-VAD
    // vs manual-commit sessions.
    pub fn should_send_commit_on_eos(self, _turn_detection: TurnDetectionMode) -> bool {
        true
    }
}

impl TurnDetectionMode {
    pub fn parse(value: &str) -> anyhow::Result<Self> {
        match value {
            "server-vad" => Ok(Self::ServerVad),
            "manual-commit" => Ok(Self::ManualCommit),
            other => anyhow::bail!(
                "unsupported turn detection '{other}' (supported: server-vad, manual-commit)"
            ),
        }
    }
}

pub fn openai_turn_detection_mode_for_model(model: &str) -> TurnDetectionMode {
    if model.eq_ignore_ascii_case("gpt-realtime-whisper") {
        TurnDetectionMode::ManualCommit
    } else {
        TurnDetectionMode::ServerVad
    }
}

/// Trim, drop empties, and truncate at the API's 1024-char limit on a char
/// boundary. Truncation is logged so users notice their prompt was clipped.
pub fn clamp_prompt(prompt: Option<&str>) -> Option<String> {
    let trimmed = prompt.map(str::trim).filter(|s| !s.is_empty())?;
    let char_count = trimmed.chars().count();
    if char_count > PROMPT_MAX_CHARS {
        warn!(
            "openai-realtime: transcription prompt is {char_count} chars; \
             truncating to API limit of {PROMPT_MAX_CHARS}"
        );
        Some(trimmed.chars().take(PROMPT_MAX_CHARS).collect())
    } else {
        Some(trimmed.to_string())
    }
}

/// Resample 16kHz i16 samples to 24kHz i16 samples using linear interpolation.
pub fn resample_16k_to_24k(samples: &[i16]) -> Vec<i16> {
    if samples.is_empty() {
        return Vec::new();
    }

    let ratio = 24_000.0 / 16_000.0;
    let output_len = (samples.len() as f64 * ratio).ceil() as usize;
    let mut output = Vec::with_capacity(output_len);

    for i in 0..output_len {
        let src_pos = i as f64 / ratio;
        let src_idx = src_pos as usize;
        let frac = src_pos - src_idx as f64;

        let sample = if src_idx + 1 < samples.len() {
            let a = samples[src_idx] as f64;
            let b = samples[src_idx + 1] as f64;
            (a + frac * (b - a)) as i16
        } else if src_idx < samples.len() {
            samples[src_idx]
        } else {
            0
        };

        output.push(sample);
    }

    output
}

/// Encode i16 PCM samples to base64.
pub fn encode_pcm_base64(samples: &[i16]) -> String {
    let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
    base64::engine::general_purpose::STANDARD.encode(&bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_profile() {
        assert_eq!(
            OpenAiRealtimeProfile::parse("openai").unwrap(),
            OpenAiRealtimeProfile::OpenAi
        );
        assert_eq!(
            OpenAiRealtimeProfile::parse("lemonade").unwrap(),
            OpenAiRealtimeProfile::Lemonade
        );
        assert!(OpenAiRealtimeProfile::parse("unknown").is_err());
    }

    #[test]
    fn parse_turn_detection_mode() {
        assert_eq!(
            TurnDetectionMode::parse("server-vad").unwrap(),
            TurnDetectionMode::ServerVad
        );
        assert_eq!(
            TurnDetectionMode::parse("manual-commit").unwrap(),
            TurnDetectionMode::ManualCommit
        );
        assert!(TurnDetectionMode::parse("unknown").is_err());
    }

    #[test]
    fn profile_sample_rates_and_delta_modes() {
        assert_eq!(OpenAiRealtimeProfile::OpenAi.input_sample_rate(), 24_000);
        assert_eq!(OpenAiRealtimeProfile::Lemonade.input_sample_rate(), 16_000);
        assert_eq!(
            OpenAiRealtimeProfile::OpenAi.delta_mode(),
            DeltaMode::AppendOnly
        );
        assert_eq!(
            OpenAiRealtimeProfile::Lemonade.delta_mode(),
            DeltaMode::ReplaceableInterim
        );
    }

    #[test]
    fn openai_session_update_serialization() {
        let json = OpenAiRealtimeProfile::OpenAi
            .session_update(
                "gpt-4o-mini-transcribe",
                "en",
                None,
                TurnDetectionMode::ServerVad,
            )
            .unwrap();

        assert_eq!(json["type"], "session.update");
        assert_eq!(json["session"]["type"], "transcription");
        assert_eq!(
            json["session"]["audio"]["input"]["format"]["type"],
            "audio/pcm"
        );
        assert_eq!(json["session"]["audio"]["input"]["format"]["rate"], 24000);
        assert_eq!(
            json["session"]["audio"]["input"]["transcription"]["model"],
            "gpt-4o-mini-transcribe"
        );
        assert_eq!(
            json["session"]["audio"]["input"]["transcription"]["language"],
            "en"
        );
        let turn = &json["session"]["audio"]["input"]["turn_detection"];
        assert_eq!(turn["type"], "server_vad");
        assert_eq!(turn["threshold"], 0.5);
        assert_eq!(turn["prefix_padding_ms"], 300);
        assert_eq!(turn["silence_duration_ms"], 500);
    }

    #[test]
    fn lemonade_session_update_serialization() {
        let json = OpenAiRealtimeProfile::Lemonade
            .session_update(
                "Whisper-Tiny",
                "auto",
                Some("ignored"),
                TurnDetectionMode::ServerVad,
            )
            .unwrap();

        assert_eq!(json["type"], "session.update");
        assert_eq!(json["session"]["model"], "Whisper-Tiny");
        assert_eq!(json["session"]["turn_detection"]["type"], "server_vad");
        assert!(json["session"].get("language").is_none());
        assert!(json["session"].get("prompt").is_none());
    }

    #[test]
    fn lemonade_manual_commit_omits_turn_detection() {
        let json = OpenAiRealtimeProfile::Lemonade
            .session_update(
                "Whisper-Tiny",
                "en",
                Some("ignored"),
                TurnDetectionMode::ManualCommit,
            )
            .unwrap();

        assert!(json["session"]["turn_detection"].is_null());
        assert!(json["session"].get("language").is_none());
        assert!(json["session"].get("prompt").is_none());
    }

    #[test]
    fn manual_commit_session_update_omits_prompt_and_turn_detection() {
        let json = OpenAiRealtimeProfile::OpenAi
            .session_update(
                "gpt-realtime-whisper",
                "en",
                Some("domain prompt is unsupported here"),
                TurnDetectionMode::ManualCommit,
            )
            .unwrap();
        assert!(json["session"]["audio"]["input"]["turn_detection"].is_null());
        assert!(json["session"]["audio"]["input"]["transcription"]
            .get("prompt")
            .is_none());
    }

    #[test]
    fn session_update_auto_language_omitted() {
        let json = OpenAiRealtimeProfile::OpenAi
            .session_update(
                "gpt-4o-transcribe",
                "auto",
                None,
                TurnDetectionMode::ServerVad,
            )
            .unwrap();

        assert!(json["session"]["audio"]["input"]["transcription"]
            .get("language")
            .is_none());
    }

    #[test]
    fn session_update_with_prompt_includes_field() {
        let json = OpenAiRealtimeProfile::OpenAi
            .session_update(
                "gpt-4o-transcribe",
                "en",
                Some("Yocto, Hyprland, NixOS"),
                TurnDetectionMode::ServerVad,
            )
            .unwrap();
        assert_eq!(
            json["session"]["audio"]["input"]["transcription"]["prompt"],
            "Yocto, Hyprland, NixOS"
        );
    }

    #[test]
    fn session_update_blank_prompt_omits_field() {
        let json = OpenAiRealtimeProfile::OpenAi
            .session_update(
                "gpt-4o-transcribe",
                "en",
                Some("   \t\n  "),
                TurnDetectionMode::ServerVad,
            )
            .unwrap();
        assert!(json["session"]["audio"]["input"]["transcription"]
            .get("prompt")
            .is_none());
    }

    #[test]
    fn clamp_prompt_truncates_at_limit() {
        let long = "a".repeat(PROMPT_MAX_CHARS + 500);
        let clamped = clamp_prompt(Some(&long)).unwrap();
        assert_eq!(clamped.chars().count(), PROMPT_MAX_CHARS);
    }

    #[test]
    fn clamp_prompt_handles_multibyte_at_boundary() {
        let long: String = "".repeat(PROMPT_MAX_CHARS + 50);
        let clamped = clamp_prompt(Some(&long)).unwrap();
        assert_eq!(clamped.chars().count(), PROMPT_MAX_CHARS);
        assert!(clamped.is_char_boundary(0));
    }

    #[test]
    fn openai_turn_detection_uses_manual_commit_for_realtime_whisper() {
        assert_eq!(
            openai_turn_detection_mode_for_model("gpt-realtime-whisper"),
            TurnDetectionMode::ManualCommit
        );
        assert_eq!(
            openai_turn_detection_mode_for_model("gpt-4o-mini-transcribe"),
            TurnDetectionMode::ServerVad
        );
    }

    #[test]
    fn resample_empty() {
        let result = resample_16k_to_24k(&[]);
        assert!(result.is_empty());
    }

    #[test]
    fn resample_ratio() {
        let input: Vec<i16> = vec![100; 16_000];
        let output = resample_16k_to_24k(&input);
        assert!(
            (output.len() as i64 - 24_000).abs() <= 2,
            "expected ~24000, got {}",
            output.len()
        );
    }

    #[test]
    fn lemonade_uses_16k_audio_directly() {
        let input: Vec<i16> = vec![7; 160];
        assert_eq!(OpenAiRealtimeProfile::Lemonade.input_sample_rate(), 16_000);
        assert_eq!(input.len(), 160);
    }

    #[test]
    fn encode_pcm_base64_roundtrip() {
        let samples: Vec<i16> = vec![1, 2, 3, -1];
        let encoded = encode_pcm_base64(&samples);

        let decoded_bytes = base64::engine::general_purpose::STANDARD
            .decode(&encoded)
            .unwrap();
        let decoded_samples: Vec<i16> = decoded_bytes
            .chunks_exact(2)
            .map(|c| i16::from_le_bytes([c[0], c[1]]))
            .collect();
        assert_eq!(decoded_samples, samples);
    }
}