Bear-03 / vosk-rs

Rust bindings to the Vosk API Speech Recognition library
MIT License
98 stars 10 forks source link

Vosk

Latest release Documentation MIT Build Status

Safe FFI bindings around the Vosk API Speech Recognition Toolkit.

Usage

// Simplified version of examples/read_wav.rs

// Normally you would not want to hardcode the audio samples
let samples = vec![100, -2, 700, 30, 4, 5];
let model_path = "/path/to/model";

let model = Model::new(model_path).unwrap();
let mut recognizer = Recognizer::new(&model, 16000.0).unwrap();

recognizer.set_max_alternatives(10);
recognizer.set_words(true);
recognizer.set_partial_words(true);

for sample in samples.chunks(100) {
    recognizer.accept_waveform(sample);
    println!("{:#?}", recognizer.partial_result());
}

println!("{:#?}", recognizer.final_result().multiple().unwrap());

Setup

Compilation

The Vosk-API libraries have to be discoverable by the rust linker. Download the zip file containing the dynamic libraries for your platform here. For iOS development you have to use static libraries. Get the static libraries from the vosk-api team.

Using dynamic libraries

Do either of the following:

Although the approaches are equivalent, using a build script is more convenient because it does not require the developer to remember a terminal command or change anything outside the project scope.

Using static libraries (macOS-only, targeting iOS)

Troubleshooting

In real-world scenarios, one will use Rust to cross compile a library (e.g. Android and iOS). Therefore, we need both cdylib as well as the staticlib as crate-type. If you compile as usual with cargo build (e.g.: cargo build --target aarch64-apple-ios --release) it will not work, because cargo tries to build the dylib as well. Fortunately, since rust 1.64. there is a new option for rustc in the stable channel. Because of this, the following will work: cargo rustc --crate-type staticlib --lib --target aarch64-apple-ios --release

Execution

Executables compiled with a dynamic lib must have access to the vosk library at runtime. Executables compiled with a statically compiled library do not.

Using dynamic libraries

Do either of the following:

Using static libraries (iOS-only)