#speech-recognition #speech #api-bindings #stt

vosk

Safe wrapper around the Vosk API Speech Recognition Toolkit

4 releases (2 breaking)

0.3.1 Oct 27, 2024
0.3.0 Oct 27, 2024
0.2.0 Oct 1, 2022
0.1.0 Jun 29, 2022

#104 in Audio

Download history 90/week @ 2024-08-30 117/week @ 2024-09-06 108/week @ 2024-09-13 130/week @ 2024-09-20 171/week @ 2024-09-27 130/week @ 2024-10-04 97/week @ 2024-10-11 107/week @ 2024-10-18 374/week @ 2024-10-25 150/week @ 2024-11-01 59/week @ 2024-11-08 84/week @ 2024-11-15 93/week @ 2024-11-22 103/week @ 2024-11-29 146/week @ 2024-12-06 198/week @ 2024-12-13

550 downloads per month
Used in marek_vosk_speech_recogni…

MIT license

52KB
672 lines

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:

  • Recommended: Create a build script and provide cargo with the path to the libraries
  • Use the RUSTFLAGS environment variable to provide the path to the variables like so: RUSTFLAGS=-L/path/to/the/libraries with cargo:rustc-link-search or cargo:rustc-link-lib.
  • Make the vosk library accessible system or user-wide:
    • Windows: Move the libraries to a directory in your PATH environment variable.
    • Linux: Move them to /usr/local/lib, /usr/lib or set the LIBRARY_PATH environment variable to the directory containing the libraries.

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)

  • Extract the correct non-fat file (also called thin file) from the static fat file (libvosk.a) for each architecture you would like to support.
  • Mark your crate type as staticlib.
  • Create a build script and provide cargo with the path to the libraries with cargo:rustc-link-search= and cargo:rustc-link-lib=static=.
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:

  • Recommended: Copy the libraries to the root of the executable (target/<cargo profile name> by default). It is recommended that you use a tool such as cargo-make to automate moving the libraries from another, more practical, directory to the destination during build.
  • Make the vosk library accessible system or user-wide:
    • Windows: Move the libraries to a directory in your PATH environment variable.
    • Linux: Move them to /usr/local/lib, /usr/lib or set the LD_LIBRARY_PATH environment variable to the directory containing the libraries. Note: LD_LIBRARY_PATH is not the same as LIBRARY_PATH mentioned in the compilation step.

Using static libraries (iOS-only)

  • Add the compiled .a library (or libraries if you would like to support more than one architecture) to your iOS project
  • Set Enable Bitcode to no for your target
  • Add the Accelerate Framework from the iOS SDK to your project
  • Depending on your library and use case, you have to write some C -> Objective-C -> Swift glue code.

Dependencies

~0.7–1.6MB
~34K SLoC