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