Skip to main content

transcribe_cpp_types/
result.rs

1//! Owned result types.
2//!
3//! transcribe-cpp fully materializes every result into these types — all text
4//! is copied at the FFI boundary (`CStr -> String`), so no borrowed session
5//! pointer ever escapes to user code (the "copy at the FFI boundary" rule).
6//! Holding a [`Transcript`] across the next `run`/`stream` call is therefore
7//! always safe.
8
9use crate::types::TimestampKind;
10
11/// A fully-materialized transcription result.
12#[derive(Debug, Clone, Default, PartialEq)]
13#[cfg_attr(
14    feature = "serde",
15    derive(serde::Serialize, serde::Deserialize),
16    serde(default)
17)]
18pub struct Transcript {
19    /// The full transcript text.
20    pub text: String,
21    /// The model's decoded output before family post-processing (diarization
22    /// markers, timestamp/special tokens, tag filtering, whitespace trims).
23    /// Equal to `text` modulo whitespace for families that emit clean text.
24    pub raw_text: String,
25    /// The model-detected language (ISO code), if it predicted one. `None`
26    /// when a language hint was given, the model lacks LID, or none applies.
27    pub language: Option<String>,
28    /// The finest timestamp granularity actually populated below.
29    pub timestamp_kind: TimestampKind,
30    /// Transcript segments. Speaker-attributed models may return untimed rows
31    /// even when timestamps are disabled.
32    pub segments: Vec<Segment>,
33    /// Diarized speaker turns (empty unless diarization was enabled and found).
34    pub speaker_segments: Vec<SpeakerSegment>,
35    /// Word rows (empty unless word-or-finer timestamps were produced).
36    pub words: Vec<Word>,
37    /// Token rows (empty unless token timestamps were produced).
38    pub tokens: Vec<Token>,
39    /// Stage timings for the run.
40    pub timings: Timings,
41}
42
43/// One segment row. Times are milliseconds relative to the original audio.
44#[derive(Debug, Clone, Default, PartialEq)]
45#[cfg_attr(
46    feature = "serde",
47    derive(serde::Serialize, serde::Deserialize),
48    serde(default)
49)]
50pub struct Segment {
51    pub t0_ms: i64,
52    pub t1_ms: i64,
53    pub first_word: i32,
54    pub n_words: i32,
55    pub first_token: i32,
56    pub n_tokens: i32,
57    pub text: String,
58    /// 1-based speaker id; zero means no attribution.
59    pub speaker_id: i32,
60}
61
62/// One diarized speaker turn. Zero times mean the model attributed text but
63/// did not provide turn timing; `p` is NaN when unavailable.
64#[derive(Debug, Clone, Default, PartialEq)]
65#[cfg_attr(
66    feature = "serde",
67    derive(serde::Serialize, serde::Deserialize),
68    serde(default)
69)]
70pub struct SpeakerSegment {
71    pub t0_ms: i64,
72    pub t1_ms: i64,
73    pub speaker_id: i32,
74    #[cfg_attr(
75        feature = "serde",
76        serde(default = "nan", deserialize_with = "nan_from_null")
77    )]
78    pub p: f32,
79}
80
81/// One word row.
82#[derive(Debug, Clone, Default, PartialEq)]
83#[cfg_attr(
84    feature = "serde",
85    derive(serde::Serialize, serde::Deserialize),
86    serde(default)
87)]
88pub struct Word {
89    pub t0_ms: i64,
90    pub t1_ms: i64,
91    pub seg_index: i32,
92    pub first_token: i32,
93    pub n_tokens: i32,
94    pub text: String,
95}
96
97/// One token row. `p` is a per-token confidence hint (NaN when the family
98/// produces none); its exact meaning is family-specific.
99#[derive(Debug, Clone, Default, PartialEq)]
100#[cfg_attr(
101    feature = "serde",
102    derive(serde::Serialize, serde::Deserialize),
103    serde(default)
104)]
105pub struct Token {
106    pub id: i32,
107    #[cfg_attr(
108        feature = "serde",
109        serde(default = "nan", deserialize_with = "nan_from_null")
110    )]
111    pub p: f32,
112    pub t0_ms: i64,
113    pub t1_ms: i64,
114    pub seg_index: i32,
115    pub word_index: i32,
116    pub text: String,
117}
118
119/// Per-run stage timings, in milliseconds. Zero means "unknown / not measured".
120#[derive(Debug, Clone, Copy, Default, PartialEq)]
121#[cfg_attr(
122    feature = "serde",
123    derive(serde::Serialize, serde::Deserialize),
124    serde(default)
125)]
126pub struct Timings {
127    pub load_ms: f32,
128    pub mel_ms: f32,
129    pub encode_ms: f32,
130    pub decode_ms: f32,
131}
132
133/// A missing confidence means "none reported" (NaN), not 0.0.
134#[cfg(feature = "serde")]
135fn nan() -> f32 {
136    f32::NAN
137}
138
139/// JSON writes NaN as `null`; read it back as NaN. Binary formats carry the
140/// raw `f32`.
141#[cfg(feature = "serde")]
142fn nan_from_null<'de, D: serde::Deserializer<'de>>(d: D) -> Result<f32, D::Error> {
143    use serde::Deserialize;
144    if d.is_human_readable() {
145        Ok(Option::<f32>::deserialize(d)?.unwrap_or(f32::NAN))
146    } else {
147        f32::deserialize(d)
148    }
149}