Skip to main content

transcribe_cpp/
lib.rs

1//! Safe, idiomatic Rust bindings for
2//! [transcribe.cpp](https://github.com/handy-computer/transcribe.cpp), a C/C++
3//! speech-to-text library built on ggml.
4//!
5//! The raw FFI lives in the [`transcribe-cpp-sys`](transcribe_cpp_sys) crate
6//! (re-exported as [`sys`]); this crate wraps it with owned result types, an
7//! error enum, RAII handles, and a thread-safety model that matches the C
8//! contract.
9//!
10//! # Quickstart
11//!
12//! ```no_run
13//! use transcribe_cpp::{Model, RunOptions};
14//!
15//! let mut session = Model::load("model.gguf")?.session()?;
16//! let pcm: Vec<f32> = vec![/* 16 kHz mono samples in [-1, 1] */];
17//! let result = session.run(&pcm, &RunOptions::default())?;
18//! println!("{}", result.text);
19//! # Ok::<(), transcribe_cpp::Error>(())
20//! ```
21//!
22//! # Threading
23//!
24//! - [`Model`] is `Send + Sync` and cheap to clone (`Arc`-backed). The native
25//!   model is freed only after the last [`Model`] handle and every [`Session`]
26//!   derived from it are dropped — in any order.
27//! - [`Session`] is `Send` but not `Sync`; its mutating calls take `&mut self`,
28//!   so the type system enforces one-call-at-a-time.
29//! - **0.x concurrency:** the C library permits at most one in-flight compute
30//!   across all sessions of a model, [`DiarizeSession`]s, [`LangIdSession`]s,
31//!   [`VadSession`]s and [`TurnSession`]s included. This crate enforces that
32//!   with a per-model mutex (held only for the native compute call), so
33//!   concurrent calls from many sessions queue rather than race. An active ASR
34//!   stream holds the model's stream lease; other runs, including VAD and TURN
35//!   `run` / `stream_feed` / `stream_flush`, return [`Error::Busy`] meanwhile.
36//!   VAD and TURN streams do not take the lease. For real parallelism, use one
37//!   [`Model`] per worker today.
38//!
39//! # ABI verification
40//!
41//! The per-field struct-layout check that the ctypes binding performs is
42//! **waived** here: bindgen takes every struct's layout from a real compiler at
43//! generation time, so the generated FFI cannot disagree with the headers it
44//! was built against. The load-time base-version lock (see [`Model::load`]) is
45//! retained.
46
47#![doc(html_root_url = "https://docs.rs/transcribe-cpp")]
48#![warn(missing_debug_implementations)]
49
50pub use transcribe_cpp_sys as sys;
51
52mod backend;
53mod cancel;
54mod diarize;
55mod error;
56mod family;
57mod langid;
58mod logging;
59mod model;
60mod result;
61mod session;
62mod streaming;
63mod turn;
64mod types;
65mod vad;
66mod version;
67
68pub use backend::{
69    allowed_backends, backend_available, device_count, devices, init_backends,
70    init_backends_default, init_backends_with, BackendMask, Device, DeviceType,
71};
72pub use cancel::CancelToken;
73pub use diarize::{DiarizeInfo, DiarizeOptions, DiarizeSession, DiarizeSessionOptions};
74pub use error::{Error, ErrorKind, ErrorReport, Result};
75pub use family::{
76    DiarizeExtension, MoonshineStreamingOptions, ParakeetBufferedStreamOptions,
77    ParakeetStreamOptions, RunExtension, SortformerDiarizeOptions, SortformerPreset,
78    StreamExtension, VoxtralRealtimeStreamOptions, WhisperRunOptions,
79};
80pub use langid::{
81    LangIdCandidate, LangIdInfo, LangIdOptions, LangIdResult, LangIdSession, LangIdSessionOptions,
82};
83pub use logging::{disable_logging, init_logging};
84pub use model::{Capabilities, Model, ModelOptions, SessionLimits, SessionOptions};
85pub use result::{Segment, SpeakerSegment, Timings, Token, Transcript, Word};
86pub use session::{RunOptions, Session, Stream};
87pub use streaming::{StreamOptions, StreamText, StreamUpdate};
88pub use turn::{TurnInfo, TurnOptions, TurnResult, TurnSession, TurnSessionOptions};
89pub use types::{
90    AbiStruct, Backend, CommitPolicy, Diarize, ExtSlot, Feature, Itn, KvType, Pnc, Role, Roles,
91    StreamState, Task, TimestampKind,
92};
93pub use vad::{VadInfo, VadOptions, VadResult, VadSegment, VadSession, VadSessionOptions};
94pub use version::{
95    abi_struct_align, abi_struct_size, compiled_version, header_hash, version, version_commit,
96};
97
98/// Convenience: load a model, transcribe one PCM buffer, and return the result.
99///
100/// For repeated transcription, hold a [`Session`] instead of paying model load
101/// on every call.
102pub fn transcribe(
103    model_path: impl AsRef<std::path::Path>,
104    pcm: &[f32],
105    options: &RunOptions,
106) -> Result<Transcript> {
107    let mut session = Model::load(model_path)?.session()?;
108    session.run(pcm, options)
109}