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
//! One live synthesis run and the PCM it produces.

use std::sync::Arc;
use std::thread::{self, JoinHandle};

#[cfg(feature = "playback")]
use super::{PlaybackError, SpeakerOutput};
use crate::audio::Clip;
use crate::backend::BackendError;
use crate::cancel::CancelToken;

use super::{Engine, SpeakingRate, SpeechStream, SpeechText, SynthesisError, Voice, stream};

const CLIP_READ_SAMPLES: usize = 4_096;

/// A loaded synthesis engine ready to start utterances.
pub struct Synthesis<T> {
    pub(super) engine: Arc<T>,
}

impl<T> Synthesis<T> {
    pub fn new(engine: T) -> Self {
        Self {
            engine: Arc::new(engine),
        }
    }
}

/// One PCM-producing utterance.
///
/// The session owns its synthesis worker. Dropping it cancels and joins that
/// worker, so an abandoned reader cannot leave model inference running.
pub struct Session {
    stream: Option<SpeechStream>,
    worker: Option<JoinHandle<Result<(), SynthesisError>>>,
    stop: CancelToken,
    #[cfg_attr(
        not(feature = "playback"),
        expect(dead_code, reason = "only playback reads it")
    )]
    cancel: CancelToken,
    backend: &'static str,
}

impl<T: Engine> Synthesis<T> {
    /// Starts an utterance with the engine's default voice and natural pace.
    pub fn start(&self, text: &SpeechText) -> Result<Session, SynthesisError> {
        self.start_with(text, None, SpeakingRate::standard(), &CancelToken::never())
    }

    /// Starts an utterance and returns its PCM as it is produced.
    ///
    /// An engine that owns playback cannot provide a PCM session. Use
    /// `Synthesis::speak_with` for that engine instead.
    pub fn start_with(
        &self,
        text: &SpeechText,
        voice: Option<&Voice>,
        rate: SpeakingRate,
        cancel: &CancelToken,
    ) -> Result<Session, SynthesisError> {
        if cancel.is_cancelled() {
            return Err(SynthesisError::Cancelled);
        }
        if self.engine.plays_audio_itself() {
            return Err(SynthesisError::PcmUnavailable { backend: T::NAME });
        }

        let (writer, speech) = stream(self.engine.sample_rate());
        let stop = CancelToken::new();
        let requested = cancel.clone();
        let stopped = stop.clone();
        let synthesis_cancel =
            CancelToken::from_fn(move || requested.is_cancelled() || stopped.is_cancelled());

        let engine = Arc::clone(&self.engine);
        let text = text.clone();
        let voice = voice.cloned();
        let worker_cancel = synthesis_cancel.clone();
        let worker = thread::Builder::new()
            .name(format!("{}-synthesis", T::NAME))
            .spawn(move || {
                engine.synthesize_with(&text, voice.as_ref(), rate, writer, &worker_cancel)
            })
            .map_err(|source| SynthesisError::Inference {
                backend: T::NAME,
                source: BackendError::engine(source),
            })?;

        Ok(Session {
            stream: Some(speech),
            worker: Some(worker),
            stop,
            cancel: synthesis_cancel,
            backend: T::NAME,
        })
    }
}

impl Session {
    pub fn sample_rate(&self) -> u32 {
        self.stream
            .as_ref()
            .expect("the stream exists until the session is consumed")
            .sample_rate()
    }

    /// Copies the next PCM samples into `out`, waiting for samples or the
    /// terminal synthesis result. Zero means the utterance completed.
    pub fn read(&mut self, out: &mut [f32]) -> Result<usize, SynthesisError> {
        // Checked before the stream so a bad buffer never joins the worker.
        if out.is_empty() {
            return Err(SynthesisError::EmptyReadBuffer);
        }
        let result = self
            .stream
            .as_mut()
            .expect("the stream exists until the session is consumed")
            .read(out);
        if matches!(result, Ok(0) | Err(_))
            && let Err(error) = self.join()
        {
            return Err(error);
        }
        result
    }

    /// Reads the rest of the utterance as one clip at the engine's rate.
    pub fn into_clip(mut self) -> Result<Clip, SynthesisError> {
        let sample_rate = self.sample_rate();
        let mut samples = Vec::new();
        let mut block = [0.0; CLIP_READ_SAMPLES];
        loop {
            let read = self.read(&mut block)?;
            if read == 0 {
                break;
            }
            samples.extend_from_slice(&block[..read]);
        }
        Clip::new(sample_rate, samples).map_err(|source| SynthesisError::Inference {
            backend: self.backend,
            source: BackendError::engine(source),
        })
    }

    /// Plays this utterance and reports when its first source sample reaches
    /// the output callback.
    #[cfg(feature = "playback")]
    pub fn play_on(
        mut self,
        output: &SpeakerOutput,
        started: impl FnOnce(),
    ) -> Result<(), SynthesisError> {
        let speech = self
            .stream
            .take()
            .expect("the stream exists until the session is consumed");
        let played = output.play_with(speech, &self.cancel, started);
        let synthesized = self.join();

        if let Err(error) = synthesized
            && !matches!(error, SynthesisError::Cancelled)
        {
            return Err(error);
        }
        match played {
            Ok(()) => Ok(()),
            Err(PlaybackError::Cancelled) => Err(SynthesisError::Cancelled),
            Err(source) => Err(SynthesisError::Playback { source }),
        }
    }

    fn join(&mut self) -> Result<(), SynthesisError> {
        let Some(worker) = self.worker.take() else {
            return Ok(());
        };
        worker.join().unwrap_or(Err(SynthesisError::WorkerPanicked {
            backend: self.backend,
        }))
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        self.stop.cancel();
        drop(self.stream.take());
        let _finished = self.join();
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicBool, Ordering};

    use super::*;
    use crate::synthesis::{SpeechWriter, Synthesizer};

    enum Behavior {
        Samples,
        WaitForCancel(Arc<AtomicBool>),
        UnsupportedVoice,
        Panic,
        Direct,
    }

    struct Fake {
        behavior: Behavior,
    }

    impl Fake {
        fn with(behavior: Behavior) -> Self {
            Self { behavior }
        }
    }

    impl Engine for Fake {
        const NAME: &'static str = "fake";
        const MODEL: &'static [crate::backend::ModelDownload] = &[];

        type Config = Behavior;

        fn check_from(_config: &Self::Config) -> Result<PathBuf, SynthesisError> {
            Ok(PathBuf::from("fake"))
        }

        fn load_from(config: Self::Config) -> Result<Self, SynthesisError> {
            Ok(Self::with(config))
        }
    }

    impl Synthesizer for Fake {
        fn sample_rate(&self) -> u32 {
            24_000
        }

        fn plays_audio_itself(&self) -> bool {
            matches!(self.behavior, Behavior::Direct)
        }

        fn synthesize_with(
            &self,
            _text: &SpeechText,
            _voice: Option<&Voice>,
            _rate: SpeakingRate,
            mut out: SpeechWriter,
            cancel: &CancelToken,
        ) -> Result<(), SynthesisError> {
            match &self.behavior {
                Behavior::Samples => {
                    out.write(&[0.1, 0.2, 0.3])
                        .expect("the session is listening");
                    out.finish();
                    Ok(())
                }
                Behavior::WaitForCancel(stopped) => {
                    while !cancel.is_cancelled() {
                        std::thread::yield_now();
                    }
                    stopped.store(true, Ordering::Release);
                    out.fail(SynthesisError::Cancelled);
                    Err(SynthesisError::Cancelled)
                }
                Behavior::UnsupportedVoice => {
                    out.fail(SynthesisError::UnsupportedVoice {
                        backend: Self::NAME,
                        voice: "unheard".to_owned(),
                    });
                    Err(SynthesisError::UnsupportedVoice {
                        backend: Self::NAME,
                        voice: "original".to_owned(),
                    })
                }
                Behavior::Panic => panic!("scripted worker panic"),
                Behavior::Direct => {
                    out.finish();
                    Ok(())
                }
            }
        }
    }

    fn text() -> SpeechText {
        SpeechText::new("hello").expect("test text is valid")
    }

    #[test]
    fn a_session_streams_pcm_and_its_sample_rate() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Samples));
        let mut session = synthesis.start(&text()).expect("the session starts");
        assert_eq!(session.sample_rate(), 24_000);

        let mut heard = Vec::new();
        let mut samples = [0.0; 2];
        loop {
            let copied = session.read(&mut samples).expect("synthesis succeeds");
            if copied == 0 {
                break;
            }
            heard.extend_from_slice(&samples[..copied]);
        }

        assert_eq!(heard, [0.1, 0.2, 0.3]);
    }

    #[test]
    fn a_session_reads_to_one_clip_at_the_engine_rate() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Samples));
        let session = synthesis.start(&text()).expect("the session starts");

        let clip = session.into_clip().expect("synthesis succeeds");

        assert_eq!(clip.sample_rate(), 24_000);
        assert_eq!(clip.samples(), [0.1, 0.2, 0.3]);
    }

    #[test]
    fn dropping_a_session_cancels_and_joins_its_worker() {
        let stopped = Arc::new(AtomicBool::new(false));
        let synthesis = Synthesis::new(Fake::with(Behavior::WaitForCancel(Arc::clone(&stopped))));
        let session = synthesis.start(&text()).expect("the session starts");

        drop(session);

        assert!(stopped.load(Ordering::Acquire));
    }

    #[test]
    fn a_terminal_read_returns_the_worker_cause() {
        let synthesis = Synthesis::new(Fake::with(Behavior::UnsupportedVoice));
        let mut session = synthesis.start(&text()).expect("the session starts");

        let error = session
            .read(&mut [0.0; 1])
            .expect_err("the engine rejects the voice");

        assert!(matches!(
            error,
            SynthesisError::UnsupportedVoice { voice, .. } if voice == "original"
        ));
    }

    #[test]
    fn a_panicked_worker_has_a_typed_terminal_error() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Panic));
        let mut session = synthesis.start(&text()).expect("the session starts");

        let error = session.read(&mut [0.0; 1]).expect_err("the worker panics");

        assert!(matches!(
            error,
            SynthesisError::WorkerPanicked { backend: "fake" }
        ));
    }

    #[test]
    fn a_direct_playback_engine_refuses_a_pcm_session() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Direct));

        let error = synthesis
            .start(&text())
            .err()
            .expect("a direct engine has no PCM");

        assert!(matches!(
            error,
            SynthesisError::PcmUnavailable { backend: "fake" }
        ));
    }

    #[test]
    fn a_boxed_or_shared_engine_still_plays_audio_itself() {
        let boxed: Box<dyn Synthesizer> = Box::new(Fake::with(Behavior::Direct));
        let shared = Arc::new(Fake::with(Behavior::Direct));

        assert!(Synthesizer::plays_audio_itself(&boxed));
        assert!(Synthesizer::plays_audio_itself(&shared));
    }

    #[cfg(feature = "playback")]
    #[test]
    fn a_direct_engine_speaks_without_a_pcm_session() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Direct));

        synthesis
            .speak_with(
                &text(),
                None,
                SpeakingRate::standard(),
                &CancelToken::never(),
            )
            .expect("the engine plays the speech itself");
    }

    #[test]
    fn an_empty_read_buffer_is_rejected() {
        let synthesis = Synthesis::new(Fake::with(Behavior::Samples));
        let mut session = synthesis.start(&text()).expect("the session starts");

        assert!(matches!(
            session.read(&mut []),
            Err(SynthesisError::EmptyReadBuffer)
        ));
        assert_eq!(
            session
                .read(&mut [0.0; 4])
                .expect("the session can still read"),
            3
        );
    }
}