use std::ffi::CStr;
use std::os::raw::c_char;
use transcribe_cpp_sys as sys;
use crate::types::TimestampKind;
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Transcript {
pub text: String,
pub raw_text: String,
pub language: Option<String>,
pub timestamp_kind: TimestampKind,
pub segments: Vec<Segment>,
pub speaker_segments: Vec<SpeakerSegment>,
pub words: Vec<Word>,
pub tokens: Vec<Token>,
pub timings: Timings,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Segment {
pub t0_ms: i64,
pub t1_ms: i64,
pub first_word: i32,
pub n_words: i32,
pub first_token: i32,
pub n_tokens: i32,
pub text: String,
pub speaker_id: i32,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SpeakerSegment {
pub t0_ms: i64,
pub t1_ms: i64,
pub speaker_id: i32,
pub p: f32,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Word {
pub t0_ms: i64,
pub t1_ms: i64,
pub seg_index: i32,
pub first_token: i32,
pub n_tokens: i32,
pub text: String,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Token {
pub id: i32,
pub p: f32,
pub t0_ms: i64,
pub t1_ms: i64,
pub seg_index: i32,
pub word_index: i32,
pub text: String,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Timings {
pub load_ms: f32,
pub mel_ms: f32,
pub encode_ms: f32,
pub decode_ms: f32,
}
pub(crate) fn owned_str(ptr: *const c_char) -> String {
if ptr.is_null() {
return String::new();
}
unsafe { CStr::from_ptr(ptr) }
.to_string_lossy()
.into_owned()
}
pub(crate) fn owned_opt_str(ptr: *const c_char) -> Option<String> {
if ptr.is_null() {
return None;
}
let s = unsafe { CStr::from_ptr(ptr) }
.to_string_lossy()
.into_owned();
if s.is_empty() {
None
} else {
Some(s)
}
}
impl Segment {
pub(crate) fn from_raw(raw: &sys::transcribe_segment) -> Self {
Segment {
t0_ms: raw.t0_ms,
t1_ms: raw.t1_ms,
first_word: raw.first_word,
n_words: raw.n_words,
first_token: raw.first_token,
n_tokens: raw.n_tokens,
text: owned_str(raw.text),
speaker_id: raw.speaker_id,
}
}
}
impl SpeakerSegment {
pub(crate) fn from_raw(raw: &sys::transcribe_speaker_segment) -> Self {
SpeakerSegment {
t0_ms: raw.t0_ms,
t1_ms: raw.t1_ms,
speaker_id: raw.speaker_id,
p: raw.p,
}
}
}
impl Word {
pub(crate) fn from_raw(raw: &sys::transcribe_word) -> Self {
Word {
t0_ms: raw.t0_ms,
t1_ms: raw.t1_ms,
seg_index: raw.seg_index,
first_token: raw.first_token,
n_tokens: raw.n_tokens,
text: owned_str(raw.text),
}
}
}
impl Token {
pub(crate) fn from_raw(raw: &sys::transcribe_token) -> Self {
Token {
id: raw.id,
p: raw.p,
t0_ms: raw.t0_ms,
t1_ms: raw.t1_ms,
seg_index: raw.seg_index,
word_index: raw.word_index,
text: owned_str(raw.text),
}
}
}
impl Timings {
pub(crate) fn from_raw(raw: &sys::transcribe_timings) -> Self {
Timings {
load_ms: raw.load_ms,
mel_ms: raw.mel_ms,
encode_ms: raw.encode_ms,
decode_ms: raw.decode_ms,
}
}
}