transcribe_cpp/
streaming.rs1use transcribe_cpp_sys as sys;
8
9use crate::family::{StreamExtRaw, StreamExtension};
10use crate::result::owned_str;
11use crate::types::CommitPolicy;
12
13#[derive(Debug, Clone, PartialEq, Eq, Default)]
15#[cfg_attr(
16 feature = "serde",
17 derive(serde::Serialize, serde::Deserialize),
18 serde(default)
19)]
20pub struct StreamOptions {
21 pub commit_policy: CommitPolicy,
23 pub stable_prefix_agreement_n: u32,
26 pub family: Option<StreamExtension>,
28}
29
30#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
32#[cfg_attr(
33 feature = "serde",
34 derive(serde::Serialize, serde::Deserialize),
35 serde(default)
36)]
37pub struct StreamUpdate {
38 pub result_changed: bool,
40 pub is_final: bool,
42 pub revision: i32,
44 pub input_received_ms: i64,
46 pub audio_committed_ms: i64,
48 pub buffered_ms: i64,
50 pub committed_changed: bool,
52 pub tentative_changed: bool,
54}
55
56impl StreamUpdate {
57 pub(crate) fn from_raw(raw: &sys::transcribe_stream_update) -> Self {
58 StreamUpdate {
59 result_changed: raw.result_changed,
60 is_final: raw.is_final,
61 revision: raw.revision,
62 input_received_ms: raw.input_received_ms,
63 audio_committed_ms: raw.audio_committed_ms,
64 buffered_ms: raw.buffered_ms,
65 committed_changed: raw.committed_changed,
66 tentative_changed: raw.tentative_changed,
67 }
68 }
69}
70
71#[derive(Debug, Clone, Default, PartialEq, Eq)]
73#[cfg_attr(
74 feature = "serde",
75 derive(serde::Serialize, serde::Deserialize),
76 serde(default)
77)]
78pub struct StreamText {
79 pub full: String,
81 pub committed: String,
83 pub tentative: String,
85}
86
87impl StreamText {
88 pub fn display(&self) -> String {
90 format!("{}{}", self.committed, self.tentative)
91 }
92
93 pub(crate) fn from_raw(raw: &sys::transcribe_stream_text) -> Self {
94 StreamText {
95 full: owned_str(raw.full_text),
96 committed: owned_str(raw.committed_text),
97 tentative: owned_str(raw.tentative_text),
98 }
99 }
100}
101
102pub(crate) fn build_stream_params(
105 o: &StreamOptions,
106) -> (sys::transcribe_stream_params, Option<StreamExtRaw>) {
107 let mut params: sys::transcribe_stream_params = unsafe { std::mem::zeroed() };
108 unsafe { sys::transcribe_stream_params_init(&mut params) };
109 params.commit_policy = o.commit_policy.to_raw();
110 params.stable_prefix_agreement_n = o.stable_prefix_agreement_n;
111
112 let family = o.family.as_ref().map(StreamExtension::materialize);
113 params.family = family.as_ref().map_or(std::ptr::null(), |f| f.ext_ptr());
114 (params, family)
115}