Rust API reference · Kjarni 0.2.8

kjarni::transcriber

Speech-to-text transcription powered by Whisper.

Quick Start

use kjarni::transcriber::Transcriber;

let transcriber = Transcriber::builder("whisper-small")
    .language("en")
    .build()
    .await?;

let result = transcriber.transcribe_file("audio.wav")?;
println!("{}", result.text);

TranscribedToken

pub struct TranscribedToken

A single token emitted during streamed transcription.

Fields

  • pub text: String,: Decoded text for this token.
  • pub id: u32,: Token ID in the Whisper vocabulary.
  • pub is_special: bool,: true for timestamp tokens and other special tokens.

Transcriber

pub struct Transcriber

builder

pub fn builder(model: &str) -> TranscriberBuilder

Create a builder targeting the given model

model_name

pub fn model_name(&self) -> &str

Human-readable model name.

device

pub fn device(&self) -> Device

Device the model is running on.

transcribe_file

pub fn transcribe_file(&self, path: impl AsRef<Path>) -> TranscriberResult<TranscriptionResult>

Transcribe an audio file on disk.

transcribe_audio

pub fn transcribe_audio(&self, samples: &[f32], sample_rate: u32) -> TranscriberResult<TranscriptionResult>

Transcribe raw audio samples.

stream_file

pub async fn stream_file(&self, path: impl AsRef<Path>) -> TranscriberResult< std::pin::Pin<Box<dyn Stream<Item = TranscriberResult<TranscribedToken>> + Send>>, >

Stream decoded tokens from an audio file.

stream_audio

pub async fn stream_audio(&self, samples: Vec<f32>, sample_rate: u32) -> TranscriberResult< std::pin::Pin<Box<dyn Stream<Item = TranscriberResult<TranscribedToken>> + Send>>, >

Stream decoded tokens from raw audio samples

TranscriberBuilder

pub struct TranscriberBuilder

new

pub fn new(model: &str) -> Self

Create a new builder targeting the given model identifier.

cpu

pub fn cpu(mut self) -> Self

Use CPU for inference (default).

gpu

pub fn gpu(mut self) -> Self

Use GPU for inference.

with_context

pub fn with_context(mut self, ctx: Arc<WgpuContext>) -> Self

Provide a pre-existing GPU context (avoids creating a new one).

language

pub fn language(mut self, lang: &str) -> Self

Force a language

translate

pub fn translate(mut self) -> Self

Set the task to translate

transcribe

pub fn transcribe(mut self) -> Self

Set the task to transcribe (default).

timestamps

pub fn timestamps(mut self, enabled: bool) -> Self

Enable or disable timestamp segments.

max_tokens

pub fn max_tokens(mut self, n: usize) -> Self

Maximum tokens per 30-second chunk (default: 448).

quiet

pub fn quiet(mut self) -> Self

Suppress progress output to stderr.

on_progress

pub fn on_progress<F>(mut self, callback: F) -> Self where F: Fn(&super::types::TranscriptionProgress, Option<&str>) + Send + Sync + 'static,

Register a progress callback.

build

pub async fn build(self) -> TranscriberResult<Transcriber>

TranscriptionProgress

pub struct TranscriptionProgress

Progress update emitted during transcription.

Fields

  • pub stage: TranscriptionStage,: Current pipeline stage.
  • pub current: usize,: Current item (e.g. chunk index, 0-based).
  • pub total: Option<usize>,: Total items, if known.

new

pub fn new(stage: TranscriptionStage, current: usize, total: Option<usize>) -> Self

loading_audio

pub fn loading_audio() -> Self

encoding

pub fn encoding(current: usize, total: usize) -> Self

decoding

pub fn decoding(current: usize, total: usize) -> Self

stitching

pub fn stitching() -> Self

TranscriptionResult

pub struct TranscriptionResult

Fields

  • pub text: String,: Full stitched transcript text.
  • pub segments: Vec<TranscriptionSegment>,: Timed segments (populated when timestamps are enabled, otherwise one segment per chunk with estimated boundaries).
  • pub language: String,: Detected or forced language code (e.g. "en").
  • pub duration_secs: f32,: Total audio duration in seconds.

TranscriptionSegment

pub struct TranscriptionSegment

A timed segment of the transcription.

Fields

  • pub start: f32,: Start time in seconds.
  • pub end: f32,: End time in seconds.
  • pub text: String,: Transcribed text.

Task

pub enum Task

Whisper task type.

Variants

  • Transcribe,: Transcribe speech in the source language.
  • Translate,: Translate speech to English.

TranscriberError

pub enum TranscriberError

Errors returned by the transcription API.

Variants

  • ModelLoadFailed(anyhow::Error),: Failed to load the Whisper model.
  • InvalidAudioPath(PathBuf),: Audio file path does not exist or is not a file.
  • UnsupportedFormat(String),: Audio format is not supported.
  • AudioLoadFailed(anyhow::Error),: Failed to load or resample audio.
  • TranscriptionFailed(anyhow::Error),: Error during mel / encode / decode.
  • InvalidConfig(String),: Invalid configuration value.
  • GpuUnavailable,: GPU was requested but is unavailable.
  • Cancelled,: Operation cancelled via callback.

TranscriberPreset

pub enum TranscriberPreset

Pre-defined transcriber configurations.

Variants

  • Default,: Default: Whisper Small, transcribe, no timestamps, auto-detect language.
  • English,: English-only: Whisper Small, language forced to English.
  • Translate,: Translation: Whisper Small, translate any language to English.
  • LargeV3,: High quality: Whisper Large V3, transcribe, auto-detect language.

model_type

pub fn model_type(&self) -> ModelType

Model type for this preset.

language

pub fn language(&self) -> Option<&str>

Fixed language, or None for auto-detect.

task

pub fn task(&self) -> Task

Task for this preset.

timestamps

pub fn timestamps(&self) -> bool

Whether timestamps are enabled by default.

model_id

pub fn model_id(&self) -> &str

Model identifier string (for builder resolution).

TranscriberResult

pub type TranscriberResult<T> = Result<T, TranscriberError>

Convenience alias.

TranscriptionProgressCallback

pub type TranscriptionProgressCallback = Box<dyn Fn(&TranscriptionProgress, Option<&str>) + Send + Sync>

Callback type for progress reporting.

TranscriptionStage

pub enum TranscriptionStage

Stage of the transcription pipeline.

Variants

  • LoadingAudio,: Loading and resampling audio from disk.
  • Encoding,: Computing mel spectrogram + running the encoder for a chunk.
  • Decoding,: Running greedy decode for a chunk.
  • Stitching,: Stitching segments from all chunks.

Kjarni runs the same engine on every platform: