Skip to main content

transcribe_cpp_types/
turn.rs

1//! The plain data of the TURN role: info, options and results. The
2//! `transcribe_cpp::TurnSession` handle stays in transcribe-cpp.
3
4/// Static facts about an end-of-turn model (`transcribe_cpp::Model::turn_info`).
5///
6/// `Default` is all zeros; outside this crate (it is `#[non_exhaustive]`),
7/// build one from `default()` and set the fields.
8#[derive(Debug, Clone, Copy, Default, PartialEq)]
9#[non_exhaustive]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct TurnInfo {
12    /// Input PCM rate (16000).
13    pub sample_rate: i32,
14    /// The longest audio `transcribe_cpp::TurnSession::run` looks at; longer audio is scored
15    /// on its last `max_audio_ms`. 0 = no limit.
16    pub max_audio_ms: i32,
17    /// Samples per frame for streaming; 0 = the model cannot stream (the
18    /// `stream_*` calls return `transcribe_cpp::Error::NotImplemented`).
19    pub frame_samples: i32,
20    /// Whether the model also returns a speech probability per frame
21    /// ([`TurnResult::speech_probs`]).
22    pub has_speech: bool,
23    /// The model's recommended cut-off: the default for
24    /// [`TurnOptions::threshold`].
25    pub threshold: f64,
26}
27
28/// Options for creating a turn session.
29#[derive(Debug, Clone, Default, PartialEq, Eq)]
30#[cfg_attr(
31    feature = "serde",
32    derive(serde::Serialize, serde::Deserialize),
33    serde(default)
34)]
35pub struct TurnSessionOptions {
36    /// CPU threads for ops that run on CPU; 0 = library default.
37    pub n_threads: i32,
38}
39
40/// Settings for `transcribe_cpp::TurnSession::run`.
41#[derive(Debug, Clone, PartialEq)]
42#[cfg_attr(
43    feature = "serde",
44    derive(serde::Serialize, serde::Deserialize),
45    serde(default)
46)]
47pub struct TurnOptions {
48    /// Probability at or above which the turn counts as ended
49    /// ([`TurnResult::ended`]). Negative (the default, -1) uses the model's
50    /// [`TurnInfo::threshold`]; otherwise `[0, 1]`.
51    pub threshold: f64,
52}
53
54impl Default for TurnOptions {
55    fn default() -> Self {
56        TurnOptions { threshold: -1.0 }
57    }
58}
59
60/// The result of one `transcribe_cpp::TurnSession::run`, `transcribe_cpp::TurnSession::stream_feed`,
61/// `transcribe_cpp::TurnSession::stream_feed_batch` or `transcribe_cpp::TurnSession::stream_flush`.
62#[derive(Debug, Clone, Default, PartialEq)]
63#[non_exhaustive]
64#[cfg_attr(
65    feature = "serde",
66    derive(serde::Serialize, serde::Deserialize),
67    serde(default)
68)]
69pub struct TurnResult {
70    /// After a run: the probability that the turn has ended at the end of the
71    /// clip. 0 after a feed or flush.
72    pub p: f32,
73    /// After a run: `p >= threshold`. False after a feed or flush.
74    pub ended: bool,
75    /// Per-frame turn probabilities from the call: `probs[i]` says whether the
76    /// turn has ended at the end of frame `first_frame + i`. Always empty for
77    /// models that cannot stream.
78    pub probs: Vec<f32>,
79    /// Stream position of `probs[0]`, in frames: 0 after a run; for a feed or
80    /// flush, the frames the stream had processed before the call.
81    pub first_frame: i64,
82    /// Per-frame speech probabilities from the call (empty unless
83    /// [`TurnInfo::has_speech`]). During a stream they can trail the turn
84    /// probabilities; after a run every frame has both.
85    pub speech_probs: Vec<f32>,
86    /// Stream position of `speech_probs[0]`, in frames.
87    pub speech_first_frame: i64,
88}