use core::fmt::Debug;
use burn::Tensor;
use num_traits::{
Float,
ToPrimitive,
};
use crate::{
errors::{
BunsenError,
BunsenResult,
ConstraintError,
},
kits::speech::{
asr::driver::{
LevelPolicy,
LevelStatistic,
},
diarization::SpeakerActivity,
},
};
pub trait AsrEngine: Clone + Debug + Send + Sync {
type Features: StreamingFeatures;
type Level: LevelStatistic;
type StreamState: Clone + Debug + Send + Sync;
type Carry: Clone + Debug + Send + Sync;
fn sample_rate(&self) -> usize;
fn hop(&self) -> usize;
fn window_frames(&self) -> usize;
fn grid_frames(&self) -> usize;
fn end_frames_dropped(&self) -> usize {
0
}
fn level_policy(&self) -> LevelPolicy {
LevelPolicy::PerWindow
}
fn new_features(&self) -> BunsenResult<Self::Features>;
fn new_stream_state(&self) -> Self::StreamState;
fn decode(
&self,
requests: Vec<DecodeRequest<'_, Self>>,
) -> BunsenResult<Vec<WindowDecode<Self::Carry>>>;
fn commit(
&self,
state: &mut Self::StreamState,
transcript: &[i64],
carry: Self::Carry,
);
fn text(
&self,
tokens: &[i64],
) -> BunsenResult<Option<String>>;
fn language<'s>(
&self,
_state: &'s Self::StreamState,
) -> Option<&'s str> {
None
}
}
pub fn token_ids(
tokens: impl IntoIterator<Item = i64>,
model: &str,
) -> BunsenResult<Vec<usize>> {
tokens
.into_iter()
.map(|t| {
usize::try_from(t).map_err(|_| {
BunsenError::invalid_resource(alloc::format!("{t} is not a {model} token id"))
})
})
.collect()
}
pub(crate) fn window_frames_in(
owner: &'static str,
seconds: f64,
sample_rate: usize,
hop: usize,
grid: usize,
most: Option<usize>,
) -> BunsenResult<usize> {
let float = |n: usize| n.to_f64().unwrap_or(f64::NAN);
let frames = Float::floor(seconds * float(sample_rate))
.to_usize()
.map_or(0, |samples| samples / hop / grid * grid);
if frames == 0 || most.is_some_and(|most| frames > most) {
let least = float(grid * hop) / float(sample_rate);
let range = match most {
Some(most) => alloc::format!("[{least}, {}] s", float(most * hop) / float(sample_rate)),
None => alloc::format!(">= {least} s"),
};
return Err(ConstraintError::out_of_range(owner, "seconds", seconds, range).into());
}
Ok(frames)
}
pub trait StreamingFeatures: Clone + Debug + Send + Sync {
fn push(
&mut self,
samples: &[f32],
) -> BunsenResult<Option<Tensor<3>>>;
fn finish(&mut self) -> BunsenResult<Option<Tensor<3>>>;
}
#[derive(Debug)]
pub struct DecodeRequest<'a, E: AsrEngine> {
pub state: &'a E::StreamState,
pub transcript: &'a [i64],
pub start: usize,
pub window: Tensor<3>,
pub level: E::Level,
pub draft: bool,
pub speakers: Option<SpeakerActivity>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TimedTokens {
pub start: usize,
pub end: usize,
pub tokens: Vec<i64>,
}
#[derive(Clone, Debug)]
pub struct WindowDecode<C> {
pub tokens: Vec<i64>,
pub skip: bool,
pub segments: Vec<TimedTokens>,
pub advance: usize,
pub tail: Option<TimedTokens>,
pub carry: C,
}
impl<C> WindowDecode<C> {
pub fn whole(
frames: usize,
tokens: Vec<i64>,
carry: C,
) -> Self {
Self {
segments: vec![TimedTokens {
start: 0,
end: frames,
tokens: tokens.clone(),
}],
tokens,
skip: false,
advance: frames,
tail: None,
carry,
}
}
pub fn skipped(
frames: usize,
carry: C,
) -> Self {
Self {
tokens: Vec::new(),
skip: true,
segments: Vec::new(),
advance: frames,
tail: None,
carry,
}
}
}