talk-rs 0.7.1

Voice dictation for Linux -- record, transcribe, and paste
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
//! Mistral / Voxtral remote text-to-speech backend.
//!
//! The synthesis sibling of [`crate::transcription::mistral`].  Where
//! that module POSTs audio to `/v1/audio/transcriptions` and parses
//! text out, this one POSTs text to `/v1/audio/speech` and parses
//! audio out.
//!
//! # Wire contract (verified)
//!
//! ```text
//! POST {base}/v1/audio/speech
//! Authorization: Bearer <api_key>
//! Content-Type: application/json
//! { "model": "voxtral-mini-tts-latest",
//!   "input": "<text>",
//!   "voice_id": "<uuid>",
//!   "response_format": "wav" }
//!
//! 200 OK
//! { "audio_data": "<base64 WAV>" }
//! ```
//!
//! The returned WAV is 24 kHz mono 16-bit PCM.  We parse the WAV
//! header *properly* (locate the `data` chunk rather than assuming a
//! fixed 44-byte header) and extract the PCM samples into a
//! [`SynthesisResult`].

use base64::Engine;
use serde::Deserialize;
use std::time::Duration;

use crate::config::MistralConfig;
use crate::error::TalkError;

use super::{OneShotSynthesizer, SynthesisRequest, SynthesisResult};
use async_trait::async_trait;

/// Default API base URL for the Mistral API.
pub(crate) const API_BASE: &str = "https://api.mistral.ai";

/// Connect timeout for the speech request.  Mirrors the transport
/// layer's connect-timeout defence so a dead host fails fast.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Overall request timeout.  TTS of a short utterance is quick; a
/// generous cap keeps an unattended `speak` from hanging forever.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(120);

/// JSON body sent to `/v1/audio/speech`.
#[derive(Debug, serde::Serialize)]
struct SpeechRequestBody<'a> {
    model: &'a str,
    input: &'a str,
    voice_id: &'a str,
    response_format: &'a str,
}

/// JSON response from `/v1/audio/speech`.
#[derive(Debug, Deserialize)]
struct SpeechResponseBody {
    /// Base64-encoded WAV audio.
    audio_data: String,
}

/// Remote Mistral / Voxtral one-shot synthesizer.
pub struct MistralOneShotSynthesizer {
    config: MistralConfig,
    /// Resolved `POST` endpoint (`{base}/v1/audio/speech`).
    endpoint: String,
}

impl MistralOneShotSynthesizer {
    /// Build a synthesizer from a parsed [`MistralConfig`].
    ///
    /// The endpoint is derived from `config.url` (if set) by appending
    /// `/v1/audio/speech`; otherwise the default base URL is used.
    pub fn new(config: MistralConfig) -> Result<Self, TalkError> {
        let base = config.url.as_deref().unwrap_or(API_BASE);
        let endpoint = format!("{}/v1/audio/speech", base.trim_end_matches('/'));
        Ok(Self { config, endpoint })
    }

    /// Build a synthesizer with an explicit endpoint (for testing).
    #[cfg(test)]
    pub fn with_endpoint(config: MistralConfig, endpoint: String) -> Result<Self, TalkError> {
        Ok(Self { config, endpoint })
    }

    /// Resolve the voice id to use: `--voice` > config `tts_voice` >
    /// error.
    fn resolve_voice(&self, req_voice: &Option<String>) -> Result<String, TalkError> {
        if let Some(v) = req_voice {
            return Ok(v.clone());
        }
        if let Some(v) = &self.config.tts_voice {
            return Ok(v.clone());
        }
        Err(TalkError::Config(
            "no voice selected for Mistral TTS: pass --voice <id> or set \
             providers.mistral.tts_voice. List available voices with \
             `GET /v1/audio/voices?voice_type=preset`."
                .to_string(),
        ))
    }
}

#[async_trait]
impl OneShotSynthesizer for MistralOneShotSynthesizer {
    /// Cheap pre-flight: only checks that an API key is present.  Does
    /// NOT call the API (per the plan — no network in `validate`).
    async fn validate(&self) -> Result<(), TalkError> {
        if self.config.api_key.is_empty() {
            return Err(TalkError::Config(
                "providers.mistral.api_key is required for Mistral TTS".to_string(),
            ));
        }
        Ok(())
    }

    async fn synthesize(&self, req: SynthesisRequest) -> Result<SynthesisResult, TalkError> {
        let voice = self.resolve_voice(&req.voice)?;

        let body = SpeechRequestBody {
            model: &self.config.tts_model,
            input: &req.text,
            voice_id: &voice,
            response_format: "wav",
        };

        let client = reqwest::Client::builder()
            .connect_timeout(CONNECT_TIMEOUT)
            .timeout(REQUEST_TIMEOUT)
            .build()
            .map_err(|e| TalkError::Transcription(format!("failed to build HTTP client: {}", e)))?;

        let response = client
            .post(&self.endpoint)
            .header("Authorization", format!("Bearer {}", self.config.api_key))
            .json(&body)
            .send()
            .await
            .map_err(|e| TalkError::Transcription(format!("Mistral TTS request failed: {}", e)))?;

        let status = response.status();
        let bytes = response.bytes().await.map_err(|e| {
            TalkError::Transcription(format!("Mistral TTS read body failed: {}", e))
        })?;

        if !status.is_success() {
            let body = String::from_utf8_lossy(&bytes);
            return Err(TalkError::Transcription(format!(
                "Mistral TTS API error ({}): {}",
                status.as_u16(),
                body
            )));
        }

        let parsed: SpeechResponseBody = serde_json::from_slice(&bytes).map_err(|e| {
            TalkError::Transcription(format!("failed to parse Mistral TTS response: {}", e))
        })?;

        let wav_bytes = base64::engine::general_purpose::STANDARD
            .decode(parsed.audio_data.trim())
            .map_err(|e| {
                TalkError::Transcription(format!(
                    "failed to base64-decode Mistral TTS audio: {}",
                    e
                ))
            })?;

        let (pcm, sample_rate) = parse_wav_pcm_i16(&wav_bytes)?;
        Ok(SynthesisResult { pcm, sample_rate })
    }
}

/// Parse a 16-bit PCM WAV byte stream into `(samples, sample_rate)`.
///
/// Robustly locates the `fmt ` and `data` chunks by walking the RIFF
/// chunk list — it does NOT assume the canonical 44-byte header, since
/// some encoders insert extra chunks (`LIST`, `fact`, …) before
/// `data`.  Returns downmixed-to-mono `i16` samples: multi-channel WAV
/// is averaged to mono so the result meets the codebase's mono PCM
/// convention.
///
/// # Errors
///
/// Returns a [`TalkError::Transcription`] on a truncated header, a
/// non-`WAVE` RIFF, a missing `fmt `/`data` chunk, or an unsupported
/// (non-16-bit-PCM) format.
pub(crate) fn parse_wav_pcm_i16(bytes: &[u8]) -> Result<(Vec<i16>, u32), TalkError> {
    let err = |m: &str| TalkError::Transcription(format!("invalid WAV: {}", m));

    if bytes.len() < 12 || &bytes[0..4] != b"RIFF" || &bytes[8..12] != b"WAVE" {
        return Err(err("missing RIFF/WAVE header"));
    }

    let read_u16 = |b: &[u8], o: usize| u16::from_le_bytes([b[o], b[o + 1]]);
    let read_u32 = |b: &[u8], o: usize| u32::from_le_bytes([b[o], b[o + 1], b[o + 2], b[o + 3]]);

    let mut pos = 12usize;
    let mut channels: u16 = 0;
    let mut sample_rate: u32 = 0;
    let mut bits_per_sample: u16 = 0;
    let mut fmt_seen = false;
    let mut data: Option<&[u8]> = None;

    // Walk the chunk list: each chunk is `<4-byte id><4-byte size><size bytes>`,
    // padded to an even boundary.
    while pos + 8 <= bytes.len() {
        let chunk_id = &bytes[pos..pos + 4];
        let chunk_size = read_u32(bytes, pos + 4) as usize;
        let body_start = pos + 8;
        let body_end = body_start.saturating_add(chunk_size);
        if body_end > bytes.len() {
            // Truncated final chunk — clamp so we can still surface a
            // `data` chunk that ran to EOF (some encoders under-report).
            if chunk_id == b"data" {
                data = Some(&bytes[body_start..bytes.len()]);
            }
            break;
        }

        if chunk_id == b"fmt " {
            if chunk_size < 16 {
                return Err(err("fmt chunk too small"));
            }
            let audio_format = read_u16(bytes, body_start);
            channels = read_u16(bytes, body_start + 2);
            sample_rate = read_u32(bytes, body_start + 4);
            bits_per_sample = read_u16(bytes, body_start + 14);
            // 1 = PCM; 0xFFFE = WAVE_FORMAT_EXTENSIBLE (still PCM for our use).
            if audio_format != 1 && audio_format != 0xFFFE {
                return Err(err(&format!(
                    "unsupported WAV audio format {} (only 16-bit PCM supported)",
                    audio_format
                )));
            }
            fmt_seen = true;
        } else if chunk_id == b"data" {
            data = Some(&bytes[body_start..body_end]);
        }

        // Advance, honouring the RIFF even-padding rule.
        pos = body_end + (chunk_size & 1);
    }

    if !fmt_seen {
        return Err(err("missing fmt chunk"));
    }
    if bits_per_sample != 16 {
        return Err(err(&format!(
            "unsupported bits_per_sample {} (only 16-bit PCM supported)",
            bits_per_sample
        )));
    }
    if channels == 0 {
        return Err(err("fmt chunk reports zero channels"));
    }
    if sample_rate == 0 {
        return Err(err("fmt chunk reports zero sample rate"));
    }
    let data = data.ok_or_else(|| err("missing data chunk"))?;

    // Interpret little-endian i16 samples.
    let mut interleaved: Vec<i16> = Vec::with_capacity(data.len() / 2);
    for frame in data.chunks_exact(2) {
        interleaved.push(i16::from_le_bytes([frame[0], frame[1]]));
    }

    // Downmix to mono when needed by averaging channels per frame.
    let pcm = if channels == 1 {
        interleaved
    } else {
        let ch = channels as usize;
        interleaved
            .chunks_exact(ch)
            .map(|frame| {
                let sum: i32 = frame.iter().map(|&s| s as i32).sum();
                (sum / ch as i32) as i16
            })
            .collect()
    };

    Ok((pcm, sample_rate))
}

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

    /// Build a minimal canonical 44-byte-header mono 16-bit WAV around
    /// `samples` at `sample_rate`.
    fn make_wav(samples: &[i16], sample_rate: u32, channels: u16) -> Vec<u8> {
        let bits = 16u16;
        let byte_rate = sample_rate * channels as u32 * (bits as u32 / 8);
        let block_align = channels * (bits / 8);
        let data_size = (samples.len() * 2) as u32;

        let mut v = Vec::new();
        v.extend_from_slice(b"RIFF");
        v.extend_from_slice(&(36 + data_size).to_le_bytes());
        v.extend_from_slice(b"WAVE");
        v.extend_from_slice(b"fmt ");
        v.extend_from_slice(&16u32.to_le_bytes());
        v.extend_from_slice(&1u16.to_le_bytes()); // PCM
        v.extend_from_slice(&channels.to_le_bytes());
        v.extend_from_slice(&sample_rate.to_le_bytes());
        v.extend_from_slice(&byte_rate.to_le_bytes());
        v.extend_from_slice(&block_align.to_le_bytes());
        v.extend_from_slice(&bits.to_le_bytes());
        v.extend_from_slice(b"data");
        v.extend_from_slice(&data_size.to_le_bytes());
        for &s in samples {
            v.extend_from_slice(&s.to_le_bytes());
        }
        v
    }

    #[test]
    fn parse_canonical_mono_wav() {
        let samples = vec![0i16, 100, -100, 32767, -32768];
        let wav = make_wav(&samples, 24_000, 1);
        let (pcm, rate) = parse_wav_pcm_i16(&wav).expect("parse");
        assert_eq!(rate, 24_000);
        assert_eq!(pcm, samples);
    }

    #[test]
    fn parse_wav_with_extra_chunk_before_data() {
        // Insert a LIST chunk between fmt and data — the parser must
        // skip it and still find data (proving we don't assume a
        // fixed 44-byte header).
        let samples = vec![1i16, 2, 3, 4];
        let mut wav = make_wav(&samples, 24_000, 1);
        // Rebuild with an injected LIST chunk after the fmt chunk (the
        // fmt chunk ends at offset 36 in the canonical layout).
        let data_pos = wav
            .windows(4)
            .position(|w| w == b"data")
            .expect("data present");
        let list_chunk: &[u8] = b"LIST\x04\x00\x00\x00INFO";
        wav.splice(data_pos..data_pos, list_chunk.iter().copied());
        // RIFF size in header is now stale but the parser walks chunks,
        // not the RIFF size, so it still works.
        let (pcm, rate) = parse_wav_pcm_i16(&wav).expect("parse with extra chunk");
        assert_eq!(rate, 24_000);
        assert_eq!(pcm, samples);
    }

    #[test]
    fn parse_stereo_wav_downmixes_to_mono() {
        // Two channels: L=100, R=200 → mono avg 150.
        let interleaved = vec![100i16, 200, -100, -200];
        let wav = make_wav(&interleaved, 24_000, 2);
        let (pcm, _rate) = parse_wav_pcm_i16(&wav).expect("parse stereo");
        assert_eq!(pcm, vec![150i16, -150]);
    }

    #[test]
    fn parse_rejects_non_wav() {
        let err = parse_wav_pcm_i16(b"not a wav at all").expect_err("must reject");
        assert!(err.to_string().contains("RIFF/WAVE"));
    }

    #[test]
    fn parse_rejects_missing_data_chunk() {
        // fmt-only WAV, no data chunk.
        let mut v = Vec::new();
        v.extend_from_slice(b"RIFF");
        v.extend_from_slice(&28u32.to_le_bytes());
        v.extend_from_slice(b"WAVE");
        v.extend_from_slice(b"fmt ");
        v.extend_from_slice(&16u32.to_le_bytes());
        v.extend_from_slice(&1u16.to_le_bytes());
        v.extend_from_slice(&1u16.to_le_bytes());
        v.extend_from_slice(&24_000u32.to_le_bytes());
        v.extend_from_slice(&48_000u32.to_le_bytes());
        v.extend_from_slice(&2u16.to_le_bytes());
        v.extend_from_slice(&16u16.to_le_bytes());
        let err = parse_wav_pcm_i16(&v).expect_err("must reject");
        assert!(err.to_string().contains("data chunk"));
    }

    #[test]
    fn endpoint_derivation() {
        let cfg = MistralConfig {
            api_key: "k".to_string(),
            url: None,
            model: "voxtral-mini-2507".to_string(),
            context_bias: None,
            tts_model: "voxtral-mini-tts-latest".to_string(),
            tts_voice: None,
            tts_voices: None,
        };
        let s = MistralOneShotSynthesizer::new(cfg).unwrap();
        assert_eq!(s.endpoint, "https://api.mistral.ai/v1/audio/speech");
    }

    #[test]
    fn endpoint_derivation_custom_url_trailing_slash() {
        let cfg = MistralConfig {
            api_key: "k".to_string(),
            url: Some("https://custom.example.com/".to_string()),
            model: "voxtral-mini-2507".to_string(),
            context_bias: None,
            tts_model: "voxtral-mini-tts-latest".to_string(),
            tts_voice: None,
            tts_voices: None,
        };
        let s = MistralOneShotSynthesizer::new(cfg).unwrap();
        assert_eq!(s.endpoint, "https://custom.example.com/v1/audio/speech");
    }

    #[test]
    fn resolve_voice_prefers_request_over_config() {
        let cfg = MistralConfig {
            api_key: "k".to_string(),
            url: None,
            model: "m".to_string(),
            context_bias: None,
            tts_model: "voxtral-mini-tts-latest".to_string(),
            tts_voice: Some("config-voice".to_string()),
            tts_voices: None,
        };
        let s = MistralOneShotSynthesizer::new(cfg).unwrap();
        assert_eq!(
            s.resolve_voice(&Some("cli-voice".to_string())).unwrap(),
            "cli-voice"
        );
        assert_eq!(s.resolve_voice(&None).unwrap(), "config-voice");
    }

    #[test]
    fn resolve_voice_errors_when_none_available() {
        let cfg = MistralConfig {
            api_key: "k".to_string(),
            url: None,
            model: "m".to_string(),
            context_bias: None,
            tts_model: "voxtral-mini-tts-latest".to_string(),
            tts_voice: None,
            tts_voices: None,
        };
        let s = MistralOneShotSynthesizer::new(cfg).unwrap();
        let err = s.resolve_voice(&None).expect_err("must error");
        assert!(err.to_string().contains("no voice selected"));
    }
}