coqui_stt/
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
use crate::{CandidateTranscript, OwnedCandidateTranscript};

/// An array of [`CandidateTranscript`](CandidateTranscript) objects computed by the model.
#[repr(transparent)]
pub struct Metadata(*mut coqui_stt_sys::Metadata);

unsafe impl Send for Metadata {}
unsafe impl Sync for Metadata {}

impl Drop for Metadata {
    #[inline]
    fn drop(&mut self) {
        // SAFETY: this function is called with a pointer to self
        // at the end of this object's lifetime
        unsafe { coqui_stt_sys::STT_FreeMetadata(self.0) }
    }
}

impl Metadata {
    pub(crate) fn new(ptr: *mut coqui_stt_sys::Metadata) -> Self {
        if ptr.is_null() {
            unreachable!("attempted to construct Metadata with a null pointer");
        }
        Self(ptr)
    }

    /// Return an array of possible transcriptions.
    #[inline]
    #[must_use]
    pub fn transcripts(&self) -> &[CandidateTranscript] {
        // SAFETY: this object will never be constructed with a null pointer
        let data = unsafe { (*self.0).transcripts.cast() };
        let len = self.num_transcripts() as usize;

        // SAFETY: the inner objects will always be of type TokenMetadata,
        // and the length will always be proper
        unsafe { std::slice::from_raw_parts(data, len) }
    }

    /// Size of the transcripts array
    #[inline]
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn num_transcripts(&self) -> u32 {
        // SAFETY: this object will never be constructed with a null pointer
        unsafe { (*self.0).num_transcripts }
    }

    /// Convert this into an [`OwnedMetadata`](OwnedMetadata) struct.
    ///
    /// **Warning**: this can be an extremely expensive operation depending on
    /// how many transcriptions were returned, as well as the average length.
    #[inline]
    #[must_use]
    pub fn to_owned(&self) -> OwnedMetadata {
        OwnedMetadata(
            self.transcripts()
                .iter()
                .map(CandidateTranscript::to_owned)
                .collect(),
        )
    }
}

/// An owned variant of [`Metadata`](Metadata).
pub struct OwnedMetadata(Vec<OwnedCandidateTranscript>);

impl OwnedMetadata {
    /// Return an array of possible transcriptions.
    #[inline]
    #[must_use]
    pub fn transcripts(&self) -> &[OwnedCandidateTranscript] {
        self.0.as_slice()
    }

    /// Size of the transcripts array
    #[inline]
    #[must_use]
    pub fn num_transcripts(&self) -> u32 {
        self.0.len() as u32
    }

    /// Return the inner
    /// `Vec<`[`OwnedCandidateTranscript`](OwnedCandidateTranscript)`>`
    /// this data owns.
    #[inline]
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn into_inner(self) -> Vec<OwnedCandidateTranscript> {
        self.0
    }
}