transcribe_cpp_types/langid.rs
1//! The plain data of the LANGID role: info, options and results. The
2//! `transcribe_cpp::LangIdSession` handle stays in transcribe-cpp.
3
4/// Static facts about a language ID model (`transcribe_cpp::Model::langid_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 LangIdInfo {
12 /// Input PCM rate (16000).
13 pub sample_rate: i32,
14 /// Label indices are `[0, n_labels)`.
15 pub n_labels: i32,
16 /// Shorter scored audio is `transcribe_cpp::Error::InputTooShort`.
17 pub min_audio_ms: i32,
18 /// Longer input is scored on its first `max_audio_ms` (30000).
19 pub max_audio_ms: i32,
20}
21
22/// Options for creating a language ID session.
23#[derive(Debug, Clone, Default, PartialEq, Eq)]
24#[cfg_attr(
25 feature = "serde",
26 derive(serde::Serialize, serde::Deserialize),
27 serde(default)
28)]
29pub struct LangIdSessionOptions {
30 /// CPU threads for ops that run on CPU; 0 = library default.
31 pub n_threads: i32,
32}
33
34/// Per-run language ID parameters.
35#[derive(Debug, Clone, Default, PartialEq, Eq)]
36#[cfg_attr(
37 feature = "serde",
38 derive(serde::Serialize, serde::Deserialize),
39 serde(default)
40)]
41pub struct LangIdOptions {
42 /// Restrict the decision to these codes or aliases. `None` means every
43 /// label; `Some` of an empty list is `transcribe_cpp::Error::InvalidArgument`. An
44 /// unknown code is `transcribe_cpp::Error::Unsupported`.
45 pub allowed: Option<Vec<String>>,
46}
47
48/// One ranked label. `code` is the model's own label (`"en"`, `"iw"`).
49#[derive(Debug, Clone, Default, PartialEq)]
50#[non_exhaustive]
51#[cfg_attr(
52 feature = "serde",
53 derive(serde::Serialize, serde::Deserialize),
54 serde(default)
55)]
56pub struct LangIdCandidate {
57 /// Label index.
58 pub index: i32,
59 pub code: String,
60 pub name: String,
61 /// Softmax renormalized over the allowed set.
62 pub p: f32,
63 pub logit: f32,
64}
65
66/// The result of one `transcribe_cpp::LangIdSession::run`.
67#[derive(Debug, Clone, Default, PartialEq)]
68#[non_exhaustive]
69#[cfg_attr(
70 feature = "serde",
71 derive(serde::Serialize, serde::Deserialize),
72 serde(default)
73)]
74pub struct LangIdResult {
75 /// Every allowed label (duplicates once; all labels when unrestricted),
76 /// ranked by `p`, descending; ties keep label order.
77 pub candidates: Vec<LangIdCandidate>,
78 /// Unrestricted probability inside the allowed set (1.0 when
79 /// unrestricted); a low value means the speech is probably outside it.
80 pub allowed_mass: f32,
81}
82
83impl LangIdResult {
84 /// The top candidate's code, if any.
85 pub fn code(&self) -> Option<&str> {
86 self.candidates.first().map(|c| c.code.as_str())
87 }
88}