use std::pin::Pin;
use async_trait::async_trait;
use futures::Stream;
use crate::error::{AudioError, AudioResult};
use crate::frame::AudioFrame;
use crate::metering::AudioBilling;
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct SttOptions {
pub language: Option<String>,
pub diarize: bool,
pub word_timestamps: bool,
pub smart_format: bool,
pub model_hint: Option<String>,
pub keywords: Vec<String>,
pub language_bias: Vec<String>,
}
impl SttOptions {
#[must_use]
pub fn with_language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
#[must_use]
pub fn with_diarize(mut self, diarize: bool) -> Self {
self.diarize = diarize;
self
}
#[must_use]
pub fn with_word_timestamps(mut self, word_timestamps: bool) -> Self {
self.word_timestamps = word_timestamps;
self
}
#[must_use]
pub fn with_smart_format(mut self, smart_format: bool) -> Self {
self.smart_format = smart_format;
self
}
#[must_use]
pub fn with_model_hint(mut self, model: impl Into<String>) -> Self {
self.model_hint = Some(model.into());
self
}
#[must_use]
pub fn with_keywords(mut self, keywords: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.keywords = keywords.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn with_language_bias(
mut self,
languages: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
self.language_bias = languages.into_iter().map(Into::into).collect();
self
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TranscriptEvent {
Started {
session_id: Option<String>,
},
SpeechStarted {
turn_id: Option<u32>,
offset_ms: Option<u64>,
},
Partial {
turn_id: Option<u32>,
text: String,
},
Final {
turn_id: Option<u32>,
transcript: Transcript,
},
SpeechEnded {
turn_id: Option<u32>,
offset_ms: Option<u64>,
},
Speaker {
label: String,
offset_ms: Option<u64>,
},
Progress {
processed_ms: u64,
},
Usage {
audio_ms: u64,
},
}
pub type TranscriptStream = Pin<Box<dyn Stream<Item = AudioResult<TranscriptEvent>> + Send>>;
#[derive(Debug, Clone, Default)]
pub struct Transcript {
pub text: String,
pub words: Vec<Word>,
pub speakers: Vec<Speaker>,
pub confidence: f32,
pub language_detected: Option<String>,
}
#[derive(Debug, Clone)]
pub struct Word {
pub text: String,
pub start_ms: u32,
pub end_ms: u32,
pub confidence: f32,
pub speaker: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct Speaker {
pub id: u32,
pub label: Option<String>,
}
#[async_trait]
pub trait SttProvider: Send + Sync {
async fn transcribe(&self, audio: &AudioFrame, opts: &SttOptions) -> AudioResult<Transcript>;
async fn transcribe_stream(
&self,
audio: Pin<Box<dyn Stream<Item = AudioFrame> + Send>>,
opts: &SttOptions,
) -> AudioResult<TranscriptStream> {
let _ = (audio, opts);
Err(AudioError::Stt {
provider: "stt".into(),
message: "this provider does not support streaming transcription; use transcribe"
.into(),
})
}
fn billing(&self) -> Option<AudioBilling> {
None
}
}