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
use crate::Result;
use crate::opts::Opts;
use crate::segment_encoder::SegmentEncoder;
/// Pluggable ASR backend used by [`crate::Scribble`].
///
/// A backend is responsible for turning mono `f32` samples at Scribble's target sample rate into `Segment`s written via a
/// [`SegmentEncoder`].
///
/// Backends may choose to implement streaming/incremental emission by returning a stream object
/// that implements [`BackendStream`].
pub trait Backend {
/// Streaming transcription state for this backend.
///
/// The lifetime typically ties together:
/// - the backend borrow (`&'a self`)
/// - the options borrow (`&'a Opts`)
/// - the encoder borrow (`&'a mut dyn SegmentEncoder`)
type Stream<'a>: BackendStream + 'a
where
Self: 'a;
/// Run a non-streaming transcription pass over a contiguous sample buffer.
///
/// Backends should not call `encoder.close()`; the caller is responsible for encoder lifecycle.
fn transcribe_full(
&self,
opts: &Opts,
encoder: &mut dyn SegmentEncoder,
samples: &[f32],
) -> Result<()>;
/// Create a streaming transcriber that accepts samples incrementally.
///
/// Backends should not call `encoder.close()`; the caller is responsible for encoder lifecycle.
fn create_stream<'a>(
&'a self,
opts: &'a Opts,
encoder: &'a mut dyn SegmentEncoder,
) -> Result<Self::Stream<'a>>;
}
/// Streaming transcription interface returned by [`Backend::create_stream`].
pub trait BackendStream {
/// Consume a new chunk of mono `f32` samples at Scribble's target sample rate.
///
/// Returning `Ok(false)` signals "stop early".
fn on_samples(&mut self, samples_16k_mono: &[f32]) -> Result<bool>;
/// Flush and emit any final segments.
fn finish(&mut self) -> Result<()>;
}