Skip to main content

transcribe_cpp_types/
vad.rs

1//! The plain data of the VAD role: info, options and results. The
2//! `transcribe_cpp::VadSession` handle stays in transcribe-cpp.
3
4/// Static facts about a VAD model (`transcribe_cpp::Model::vad_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, Eq)]
9#[non_exhaustive]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct VadInfo {
12    /// Input PCM rate (16000).
13    pub sample_rate: i32,
14    /// Samples per probability (512 = 32 ms).
15    pub frame_samples: i32,
16}
17
18/// Options for creating a VAD session.
19#[derive(Debug, Clone, Default, PartialEq, Eq)]
20#[cfg_attr(
21    feature = "serde",
22    derive(serde::Serialize, serde::Deserialize),
23    serde(default)
24)]
25pub struct VadSessionOptions {
26    /// CPU threads for ops that run on CPU; 0 = library default.
27    pub n_threads: i32,
28}
29
30/// Probabilities -> speech segments (`transcribe_cpp::VadSession::run` only). Fields and
31/// defaults are those of Silero's `get_speech_timestamps`.
32#[derive(Debug, Clone, PartialEq)]
33#[cfg_attr(
34    feature = "serde",
35    derive(serde::Serialize, serde::Deserialize),
36    serde(default)
37)]
38pub struct VadOptions {
39    /// A frame with `p >= threshold` is speech (default 0.5). `[0, 1]`.
40    pub threshold: f64,
41    /// Inside speech, a frame with `p < neg_threshold` is silence. Negative
42    /// (the default, -1) means `max(threshold - 0.15, 0.01)`; otherwise
43    /// `[0, threshold]`.
44    pub neg_threshold: f64,
45    /// Segments of at most this length are dropped (default 250), except
46    /// those split by `max_speech_ms`. Durations must be nonnegative.
47    pub min_speech_ms: i32,
48    /// Silence this long ends a segment (default 100).
49    pub min_silence_ms: i32,
50    /// Padding on both sides of each segment (default 30).
51    pub speech_pad_ms: i32,
52    /// Segments longer than this are split; 0 (the default) = no limit.
53    pub max_speech_ms: i32,
54}
55
56impl Default for VadOptions {
57    fn default() -> Self {
58        VadOptions {
59            threshold: 0.5,
60            neg_threshold: -1.0,
61            min_speech_ms: 250,
62            min_silence_ms: 100,
63            speech_pad_ms: 30,
64            max_speech_ms: 0,
65        }
66    }
67}
68
69/// One speech segment, in samples of the run's input: `[start, end)`.
70#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
71#[non_exhaustive]
72#[cfg_attr(
73    feature = "serde",
74    derive(serde::Serialize, serde::Deserialize),
75    serde(default)
76)]
77pub struct VadSegment {
78    /// First sample of speech.
79    pub start_sample: i64,
80    /// First sample after speech.
81    pub end_sample: i64,
82}
83
84/// The result of one `transcribe_cpp::VadSession::run`, `transcribe_cpp::VadSession::stream_feed` or
85/// `transcribe_cpp::VadSession::stream_flush`.
86#[derive(Debug, Clone, Default, PartialEq)]
87#[non_exhaustive]
88#[cfg_attr(
89    feature = "serde",
90    derive(serde::Serialize, serde::Deserialize),
91    serde(default)
92)]
93pub struct VadResult {
94    /// Speech probability per frame produced by the call.
95    pub probs: Vec<f32>,
96    /// Stream position of `probs[0]`, in frames: 0 after a run; for a feed
97    /// or flush, the frames the stream had scored before the call.
98    pub first_frame: i64,
99    /// Speech segments, in time order; run only (empty after a feed or flush).
100    pub segments: Vec<VadSegment>,
101}