Skip to main content

clawft_plugin/voice/
tts.rs

1//! Text-to-speech via sherpa-rs streaming synthesizer.
2
3/// TTS synthesis result.
4#[derive(Debug, Clone)]
5pub struct TtsResult {
6    /// Audio samples (f32, mono, at configured sample rate).
7    pub samples: Vec<f32>,
8    /// Sample rate of the output audio.
9    pub sample_rate: u32,
10}
11
12/// Abort handle for cancelling TTS playback.
13#[derive(Clone)]
14pub struct TtsAbortHandle {
15    cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
16}
17
18impl TtsAbortHandle {
19    pub fn new() -> Self {
20        Self {
21            cancelled: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
22        }
23    }
24
25    pub fn cancel(&self) {
26        self.cancelled.store(true, std::sync::atomic::Ordering::SeqCst);
27    }
28
29    pub fn is_cancelled(&self) -> bool {
30        self.cancelled.load(std::sync::atomic::Ordering::SeqCst)
31    }
32}
33
34impl Default for TtsAbortHandle {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40/// Streaming text-to-speech engine.
41///
42/// Currently a stub -- real sherpa-rs integration after VP.
43pub struct TextToSpeech {
44    model_path: std::path::PathBuf,
45    voice: String,
46    speed: f32,
47}
48
49impl TextToSpeech {
50    pub fn new(model_path: std::path::PathBuf, voice: String, speed: f32) -> Self {
51        Self { model_path, voice, speed }
52    }
53
54    /// Synthesize text to audio samples.
55    pub fn synthesize(&self, _text: &str) -> Result<TtsResult, String> {
56        // Stub: real sherpa-rs synthesis goes here
57        Ok(TtsResult {
58            samples: vec![],
59            sample_rate: 16000,
60        })
61    }
62
63    /// Synthesize with an abort handle for interruption support.
64    pub fn synthesize_with_abort(
65        &self,
66        _text: &str,
67        _abort: &TtsAbortHandle,
68    ) -> Result<TtsResult, String> {
69        // Stub
70        Ok(TtsResult {
71            samples: vec![],
72            sample_rate: 16000,
73        })
74    }
75
76    pub fn model_path(&self) -> &std::path::Path {
77        &self.model_path
78    }
79
80    pub fn voice(&self) -> &str {
81        &self.voice
82    }
83
84    pub fn speed(&self) -> f32 {
85        self.speed
86    }
87}