coqui_stt/
token_metadata.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::borrow::{Borrow, Cow};
use std::ffi::CStr;
use std::fmt::{Debug, Display, Formatter};

/// Stores text of an individual token, along with its timing information.
#[repr(transparent)]
pub struct TokenMetadata {
    ptr: coqui_stt_sys::TokenMetadata,
}

unsafe impl Send for TokenMetadata {}
unsafe impl Sync for TokenMetadata {}

impl TokenMetadata {
    /// The text corresponding to this token
    #[inline]
    #[must_use]
    pub fn text(&self) -> Cow<str> {
        // SAFETY: self.ptr.text will always point to valid metadata
        let cstr = unsafe { CStr::from_ptr(self.ptr.text) };
        cstr.to_string_lossy()
    }

    /// Position of the token in units of 20ms
    #[inline]
    #[must_use]
    pub const fn timestep(&self) -> u32 {
        self.ptr.timestep
    }

    /// Position of the token in seconds
    #[inline]
    #[must_use]
    pub const fn start_time(&self) -> f32 {
        self.ptr.start_time
    }

    /// Convert this into an [`OwnedTokenMetadata`](OwnedTokenMetadata) struct.
    ///
    /// This is relatively cheap compared to its parent `to_owned` functions,
    /// as it needs to clone just a `String` and two numbers.
    #[inline]
    #[must_use]
    pub fn to_owned(&self) -> OwnedTokenMetadata {
        let coqui_stt_sys::TokenMetadata {
            timestep,
            start_time,
            ..
        } = self.ptr;

        let text = self.text().to_string();

        OwnedTokenMetadata {
            text,
            timestep,
            start_time,
        }
    }
}

impl Debug for TokenMetadata {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TokenMetadata")
            .field("text", &self.text())
            .field("timestep", &self.timestep())
            .field("start_time", &self.start_time())
            .finish()
    }
}

impl Display for TokenMetadata {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.text().borrow())
    }
}

/// An owned variant of [`TokenMetadata`](TokenMetadata).
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct OwnedTokenMetadata {
    /// The text corresponding to this token
    pub text: String,
    /// Position of the token in units of 20ms
    pub timestep: u32,
    /// Position of the token in seconds
    pub start_time: f32,
}

impl Display for OwnedTokenMetadata {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.text)
    }
}