use std::sync::Arc;
#[cfg(feature = "live")]
use bunsen::kits::speech::{
asr::driver::TranscriptEvent,
asr::parakeet::ParakeetStreamContext,
asr::qwen3_asr::Qwen3AsrStreamContext,
asr::voxtral::driver::VoxtralStreamContext,
asr::whisper::driver::WhisperStreamContext,
};
use bunsen::{
data::{
formats::LoadPolicy,
resources::ResourceCache,
},
errors::BunsenResult,
kits::speech::{
asr::{
driver::TranscriptSegment,
parakeet::ParakeetStreamDriver,
qwen3_asr::Qwen3AsrStreamDriver,
voxtral::driver::VoxtralStreamDriver,
whisper::driver::WhisperStreamDriver,
},
diarization::SpeakerActivityModel,
},
};
use burn::tensor::Device;
use crate::asr::{
AsrConstructWrapper,
Emission,
Transcript,
parakeet,
qwen3_asr,
stream,
voxtral,
whisper,
};
pub enum Driver {
Parakeet(ParakeetStreamDriver),
Qwen3Asr(Qwen3AsrStreamDriver),
Voxtral(VoxtralStreamDriver),
Whisper(Box<WhisperStreamDriver>),
}
impl Driver {
pub fn build(
loaded: AsrConstructWrapper,
emission: Emission,
vad: Option<&str>,
speakers: Option<Arc<dyn SpeakerActivityModel>>,
cache: &ResourceCache,
device: &Device,
policy: LoadPolicy,
) -> BunsenResult<Self> {
Ok(match loaded {
AsrConstructWrapper::Parakeet(bundle) => Driver::Parakeet(stream::with_speakers(
stream::with_vad(
parakeet::driver(bundle, emission)?,
vad,
cache,
device,
policy,
)?,
speakers,
)?),
AsrConstructWrapper::Qwen3Asr(bundle) => Driver::Qwen3Asr(stream::with_speakers(
stream::with_vad(
qwen3_asr::driver(bundle, emission)?,
vad,
cache,
device,
policy,
)?,
speakers,
)?),
AsrConstructWrapper::Voxtral(bundle) => Driver::Voxtral(stream::with_speakers(
stream::with_vad(
voxtral::driver(bundle, emission)?,
vad,
cache,
device,
policy,
)?,
speakers,
)?),
AsrConstructWrapper::Whisper(bundle) => {
Driver::Whisper(Box::new(stream::with_speakers(
stream::with_vad(
whisper::driver(bundle, emission, device)?,
vad,
cache,
device,
policy,
)?,
speakers,
)?))
}
})
}
pub fn sample_rate(&self) -> usize {
match self {
Driver::Parakeet(d) => d.sample_rate(),
Driver::Qwen3Asr(d) => d.sample_rate(),
Driver::Voxtral(d) => d.sample_rate(),
Driver::Whisper(d) => d.sample_rate(),
}
}
pub fn push_samples(
&self,
ms: usize,
) -> usize {
match self {
Driver::Parakeet(d) => stream::push_samples(d, ms),
Driver::Qwen3Asr(d) => stream::push_samples(d, ms),
Driver::Voxtral(d) => stream::push_samples(d, ms),
Driver::Whisper(d) => stream::push_samples(d, ms),
}
}
pub fn transcribe(
&self,
samples: &[f32],
chunk: usize,
) -> BunsenResult<(Transcript, Vec<TranscriptSegment>)> {
match self {
Driver::Parakeet(d) => parakeet::transcribe(d, samples, chunk),
Driver::Qwen3Asr(d) => qwen3_asr::transcribe(d, samples, chunk),
Driver::Voxtral(d) => voxtral::transcribe(d, samples, chunk),
Driver::Whisper(d) => whisper::transcribe(d, samples, chunk),
}
}
#[cfg(feature = "live")]
pub fn open(&self) -> BunsenResult<Stream> {
Ok(match self {
Driver::Parakeet(d) => Stream::Parakeet(Box::new(stream::open(d)?)),
Driver::Qwen3Asr(d) => Stream::Qwen3Asr(Box::new(stream::open(d)?)),
Driver::Voxtral(d) => Stream::Voxtral(Box::new(stream::open(d)?)),
Driver::Whisper(d) => Stream::Whisper(Box::new(stream::open(d)?)),
})
}
}
#[cfg(feature = "live")]
pub enum Stream {
Parakeet(Box<ParakeetStreamContext>),
Qwen3Asr(Box<Qwen3AsrStreamContext>),
Voxtral(Box<VoxtralStreamContext>),
Whisper(Box<WhisperStreamContext>),
}
#[cfg(feature = "live")]
macro_rules! each {
($stream:expr, $ctx:ident => $body:expr) => {
match $stream {
Stream::Parakeet($ctx) => $body,
Stream::Qwen3Asr($ctx) => $body,
Stream::Voxtral($ctx) => $body,
Stream::Whisper($ctx) => $body,
}
};
}
#[cfg(feature = "live")]
impl Stream {
pub fn write_read(
&mut self,
samples: &[f32],
) -> BunsenResult<Vec<TranscriptEvent>> {
each!(self, ctx => ctx.write_read(samples))
}
pub fn anchor_write_read(
&mut self,
time: f64,
samples: &[f32],
) -> BunsenResult<Vec<TranscriptEvent>> {
each!(self, ctx => ctx.anchor_write_read(time, samples))
}
pub fn end_read(&mut self) -> BunsenResult<Vec<TranscriptEvent>> {
each!(self, ctx => ctx.end_read())
}
pub fn samples_seen(&self) -> usize {
each!(self, ctx => ctx.samples_seen())
}
pub fn language(&self) -> Option<&str> {
each!(self, ctx => ctx.language())
}
}