bunsen-tool 0.43.0

bunsen's models on the command line: speech to text (Parakeet, Qwen3-ASR, Voxtral Realtime, Whisper) and who spoke when (Sortformer), text to speech (ZONOS3, Qwen3-TTS), chat (Qwen3, Qwen3.5, Gemma, nanochat) and typed decisions (Laya). The chat driver is also a library, `bunsen_tool::chat`.
Documentation
//! A family's stream driver, and one stream through it.
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,
};

/// A family's stream driver; Whisper's, much the largest, boxed.
pub enum Driver {
    Parakeet(ParakeetStreamDriver),
    Qwen3Asr(Qwen3AsrStreamDriver),
    Voxtral(VoxtralStreamDriver),
    Whisper(Box<WhisperStreamDriver>),
}

impl Driver {
    /// The driver for `loaded` under its policy and `emission`, with
    /// the VAD `vad` names attached ([`VadArgs::slot`](stream::VadArgs::slot)
    /// gives one when the emission finds speech regions), and `speakers` when
    /// there is a diarizer.
    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,
                )?))
            }
        })
    }

    /// The model's sample rate, in Hz.
    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(),
        }
    }

    /// Samples per push of `ms` milliseconds ([`stream::push_samples`]).
    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),
        }
    }

    /// Transcribes one recording, `chunk` samples a push; the transcript,
    /// and the committed segments it was made of.
    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),
        }
    }

    /// Opens a stream ([`stream::open`]): its clock at the model's rate from
    /// 0, its windows scaled as its engine's reference scales them.
    #[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)?)),
        })
    }
}

/// One stream through a family's driver, boxed: a context holds its
/// feature ring and its engine's state.
#[cfg(feature = "live")]
pub enum Stream {
    Parakeet(Box<ParakeetStreamContext>),
    Qwen3Asr(Box<Qwen3AsrStreamContext>),
    Voxtral(Box<VoxtralStreamContext>),
    Whisper(Box<WhisperStreamContext>),
}

/// `$body` on the stream's context, whichever family's.
#[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 {
    /// Pushes samples at the model's rate; what the driver emitted.
    pub fn write_read(
        &mut self,
        samples: &[f32],
    ) -> BunsenResult<Vec<TranscriptEvent>> {
        each!(self, ctx => ctx.write_read(samples))
    }

    /// Anchors the clock, the first of `samples` captured at media time
    /// `time` in seconds, then pushes them.
    pub fn anchor_write_read(
        &mut self,
        time: f64,
        samples: &[f32],
    ) -> BunsenResult<Vec<TranscriptEvent>> {
        each!(self, ctx => ctx.anchor_write_read(time, samples))
    }

    /// Ends the stream; what was left past the seek pointer.
    pub fn end_read(&mut self) -> BunsenResult<Vec<TranscriptEvent>> {
        each!(self, ctx => ctx.end_read())
    }

    /// Samples pushed so far.
    pub fn samples_seen(&self) -> usize {
        each!(self, ctx => ctx.samples_seen())
    }

    /// The language, once the model has named one; Parakeet and Voxtral
    /// name none.
    pub fn language(&self) -> Option<&str> {
        each!(self, ctx => ctx.language())
    }
}