aria-kit 0.7.0

Audio capture, transcription, synthesis, and voice-analysis contracts for local speech apps
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
//! The text-to-speech contract engine crates implement.

pub mod chunk;
// Playback needs an output device, which only a native target has.
#[cfg(all(feature = "playback", not(target_family = "wasm")))]
mod playback;
#[cfg(not(target_family = "wasm"))]
mod session;
#[cfg(all(feature = "playback", not(target_family = "wasm")))]
mod speak;
mod stream;

use std::path::PathBuf;

use crate::backend::{BackendError, ModelDownload, ModelError};
use crate::cancel::CancelToken;
pub use chunk::chunks;
#[cfg(all(feature = "playback", not(target_family = "wasm")))]
pub use playback::SpeakerOutput;
#[cfg(not(target_family = "wasm"))]
pub use session::{Session, Synthesis};
pub use stream::{SpeechStream, SpeechWriter, StreamClosed, TryRead, stream};

/// One import for code that speaks through an engine. Chunking, the raw
/// stream pair, and model validation stay outside it. [`Synthesis`] and
/// [`Session`] exist only off wasm.
pub mod prelude {
    #[cfg(not(target_family = "wasm"))]
    pub use super::{Session, Synthesis};
    pub use super::{SpeakingRate, SpeechText, SynthesisError, Voice};
    pub use crate::cancel::CancelToken;
}

const MAX_SPEECH_TEXT_BYTES: usize = 8 * 1024;
const MAX_VOICE_BYTES: usize = 64;

const MIN_SPEAKING_RATE: f32 = 0.25;
const MAX_SPEAKING_RATE: f32 = 4.0;

/// The contract every text-to-speech engine implements.
///
/// An implementation must:
/// - write PCM into `out` as it is produced, so playback starts before the
///   whole utterance is synthesized;
/// - poll `cancel` at its natural pause points and answer
///   [`SynthesisError::Cancelled`];
/// - treat a closed writer as the listener being gone and stop, answering
///   [`SynthesisError::Cancelled`];
/// - close the writer on every exit — [`SpeechWriter::finish`] on success,
///   [`SpeechWriter::fail`] with the cause on failure;
/// - wrap its own failures in [`BackendError::engine`] under
///   [`SynthesisError::Load`] or [`SynthesisError::Inference`];
/// - refuse a voice it does not have as [`SynthesisError::UnsupportedVoice`].
pub trait Synthesizer: Send + Sync + 'static {
    /// The rate of the PCM this engine writes, fixed by its model.
    fn sample_rate(&self) -> u32;

    /// Synthesizes speech into `out`, chunk by chunk. `voice` of `None` means
    /// the engine's configured default.
    fn synthesize_with(
        &self,
        text: &SpeechText,
        voice: Option<&Voice>,
        rate: SpeakingRate,
        out: SpeechWriter,
        cancel: &CancelToken,
    ) -> Result<(), SynthesisError>;

    /// Whether this engine delivers sound itself rather than writing PCM: a
    /// platform voice program synthesizes and plays in one stroke.
    /// `Synthesis::speak` skips its own playback for such an engine. A
    /// wrapper around another synthesizer must forward this, or `start`
    /// hands back an empty session instead of refusing.
    fn plays_audio_itself(&self) -> bool {
        false
    }
}

/// How an engine is constructed from its own configuration. `NAME` names the
/// engine in logs and errors; `Config` carries whatever it needs to load.
pub trait Engine: Synthesizer + Sized {
    const NAME: &'static str;

    /// The files to download for this engine, all into one folder. Empty
    /// when nothing is downloaded for it. An engine that reads a second
    /// folder lists that folder's files in a const of its own.
    const MODEL: &'static [ModelDownload];

    type Config;

    /// Resolves what a load would read, without reading it or starting a
    /// child.
    fn check_from(config: &Self::Config) -> Result<PathBuf, SynthesisError>;

    fn load_from(config: Self::Config) -> Result<Self, SynthesisError>;
}

/// A shared engine synthesizes like the engine itself.
impl<T: Synthesizer + ?Sized> Synthesizer for std::sync::Arc<T> {
    fn sample_rate(&self) -> u32 {
        (**self).sample_rate()
    }

    fn synthesize_with(
        &self,
        text: &SpeechText,
        voice: Option<&Voice>,
        rate: SpeakingRate,
        out: SpeechWriter,
        cancel: &CancelToken,
    ) -> Result<(), SynthesisError> {
        (**self).synthesize_with(text, voice, rate, out, cancel)
    }

    fn plays_audio_itself(&self) -> bool {
        (**self).plays_audio_itself()
    }
}

impl<T: Synthesizer + ?Sized> Synthesizer for Box<T> {
    fn sample_rate(&self) -> u32 {
        (**self).sample_rate()
    }

    fn synthesize_with(
        &self,
        text: &SpeechText,
        voice: Option<&Voice>,
        rate: SpeakingRate,
        out: SpeechWriter,
        cancel: &CancelToken,
    ) -> Result<(), SynthesisError> {
        (**self).synthesize_with(text, voice, rate, out, cancel)
    }

    fn plays_audio_itself(&self) -> bool {
        (**self).plays_audio_itself()
    }
}

/// [`crate::backend::validate_model_file`], failing as a [`SynthesisError`].
#[cfg(not(target_family = "wasm"))]
pub fn validate_model_file(
    path: PathBuf,
    backend: &'static str,
) -> Result<PathBuf, SynthesisError> {
    crate::backend::validate_model_file(path)
        .map_err(|source| SynthesisError::Model { backend, source })
}

/// Text to speak. Personal content: it must never land in logs, filenames, or
/// command lines.
#[derive(Clone, PartialEq, Eq)]
pub struct SpeechText(String);

impl std::fmt::Debug for SpeechText {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "SpeechText({} bytes)", self.0.len())
    }
}

impl SpeechText {
    pub fn new(text: impl Into<String>) -> Result<Self, SpeechTextError> {
        let text = text.into();
        let text = text.trim();
        if text.is_empty() {
            return Err(SpeechTextError::Empty);
        }
        if text.len() > MAX_SPEECH_TEXT_BYTES {
            return Err(SpeechTextError::TooLong {
                actual_bytes: text.len(),
                maximum_bytes: MAX_SPEECH_TEXT_BYTES,
            });
        }
        Ok(Self(text.to_owned()))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SpeechTextError {
    #[error("speech text must not be empty")]
    Empty,

    #[error("speech text contains {actual_bytes} bytes; maximum is {maximum_bytes} bytes")]
    TooLong {
        actual_bytes: usize,
        maximum_bytes: usize,
    },
}

/// An engine voice name. Which names exist is each engine's own affair; this
/// only rejects what no engine could mean.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Voice(String);

impl Voice {
    pub fn new(name: impl Into<String>) -> Result<Self, VoiceError> {
        let name = name.into();
        let name = name.trim();
        if name.is_empty() {
            return Err(VoiceError::Empty);
        }
        if name.len() > MAX_VOICE_BYTES {
            return Err(VoiceError::TooLong {
                actual_bytes: name.len(),
                maximum_bytes: MAX_VOICE_BYTES,
            });
        }
        if name.chars().any(char::is_control) {
            return Err(VoiceError::Malformed(name.to_owned()));
        }
        Ok(Self(name.to_owned()))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[derive(Debug, thiserror::Error)]
pub enum VoiceError {
    #[error("voice name must not be empty")]
    Empty,

    #[error("voice name contains {actual_bytes} bytes; maximum is {maximum_bytes} bytes")]
    TooLong {
        actual_bytes: usize,
        maximum_bytes: usize,
    },

    #[error("voice name contains control characters: {0:?}")]
    Malformed(String),
}

/// A speaking-rate multiplier: 1.0 is the engine's natural pace.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpeakingRate(f32);

impl SpeakingRate {
    pub fn new(rate: f32) -> Result<Self, SpeakingRateError> {
        if !rate.is_finite() || !(MIN_SPEAKING_RATE..=MAX_SPEAKING_RATE).contains(&rate) {
            return Err(SpeakingRateError::OutOfRange {
                actual: rate,
                minimum: MIN_SPEAKING_RATE,
                maximum: MAX_SPEAKING_RATE,
            });
        }
        Ok(Self(rate))
    }

    pub fn get(self) -> f32 {
        self.0
    }

    pub const fn standard() -> Self {
        Self(1.0)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SpeakingRateError {
    #[error("speaking rate must be between {minimum} and {maximum}; got {actual}")]
    OutOfRange {
        actual: f32,
        minimum: f32,
        maximum: f32,
    },
}

#[derive(Debug, thiserror::Error)]
pub enum SynthesisError {
    #[error("the {backend} model cannot be used")]
    Model {
        backend: &'static str,
        #[source]
        source: ModelError,
    },

    #[error("failed to load the {backend} model {model}")]
    Load {
        backend: &'static str,
        model: String,
        #[source]
        source: BackendError,
    },

    #[error("{backend} inference failed")]
    Inference {
        backend: &'static str,
        #[source]
        source: BackendError,
    },

    #[error("the {backend} speech program {program} cannot be used")]
    Program {
        backend: &'static str,
        program: String,
        #[source]
        source: BackendError,
    },

    #[error("{backend} has no voice named {voice:?}")]
    UnsupportedVoice {
        backend: &'static str,
        voice: String,
    },

    #[error("the {backend} backend is not available on {}", std::env::consts::OS)]
    UnsupportedBackend { backend: &'static str },

    #[error("the {backend} synthesis worker stopped unexpectedly")]
    WorkerPanicked { backend: &'static str },

    #[error("the {backend} backend plays audio directly and cannot provide PCM")]
    PcmUnavailable { backend: &'static str },

    #[error("the PCM read buffer must not be empty")]
    EmptyReadBuffer,

    #[error("synthesis was cancelled")]
    Cancelled,

    #[error("the synthesizer stopped before the speech ended")]
    SourceGone,

    #[error("the {backend} synthesizer ran past its time ceiling")]
    TimedOut { backend: &'static str },

    #[error("the speech already failed and its cause was reported")]
    AlreadyFailed,

    #[cfg(not(target_family = "wasm"))]
    #[error("speech playback failed")]
    Playback {
        #[source]
        source: PlaybackError,
    },
}

#[cfg(not(target_family = "wasm"))]
#[derive(Debug, thiserror::Error)]
pub enum PlaybackError {
    #[error("no audio output device is available")]
    NoOutputDevice,

    #[error("the output device offers no usable configuration")]
    NoUsableConfig(#[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("the output device wants {0} samples, which this build cannot feed")]
    UnsupportedFormat(String),

    #[error("the output stream could not be started")]
    Stream(#[source] Box<dyn std::error::Error + Send + Sync>),

    #[error("the output device stopped mid-utterance")]
    DeviceLost,

    #[error("playback was cancelled")]
    Cancelled,
}

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

    #[test]
    fn speech_text_is_trimmed_and_bounded() {
        let text = SpeechText::new("  hello  ").expect("plain text is accepted");
        assert_eq!(text.as_str(), "hello");

        assert!(matches!(
            SpeechText::new("   "),
            Err(SpeechTextError::Empty)
        ));
        assert!(matches!(
            SpeechText::new("x".repeat(MAX_SPEECH_TEXT_BYTES + 1)),
            Err(SpeechTextError::TooLong { .. })
        ));
    }

    #[test]
    fn voice_names_reject_what_no_engine_could_mean() {
        assert_eq!(
            Voice::new("af_heart")
                .expect("a plain name is accepted")
                .as_str(),
            "af_heart"
        );

        assert!(matches!(Voice::new(""), Err(VoiceError::Empty)));
        assert!(matches!(
            Voice::new("x".repeat(MAX_VOICE_BYTES + 1)),
            Err(VoiceError::TooLong { .. })
        ));
        assert!(matches!(
            Voice::new("bad\nname"),
            Err(VoiceError::Malformed(_))
        ));
    }

    #[test]
    fn speaking_rate_is_bounded_and_finite() {
        assert_eq!(SpeakingRate::standard().get(), 1.0);
        assert!(SpeakingRate::new(2.0).is_ok());

        assert!(SpeakingRate::new(0.0).is_err());
        assert!(SpeakingRate::new(5.0).is_err());
        assert!(SpeakingRate::new(f32::NAN).is_err());
    }
}