Picovoice SDK for Rust

Picovoice

Made in Vancouver, Canada by Picovoice

Picovoice is an end-to-end platform for building voice products on your terms. It enables creating voice experiences similar to Alexa and Google. But it entirely runs 100% on-device. Picovoice is

Compatibility

Installation

First you will need Rust and Cargo installed on your system.

To add the picovoice library into your app, add picovoice to your app's Cargo.toml manifest: toml [dependencies] picovoice = "${version}"

Usage

To create an instance of the engine with default parameters, use the PicovoiceBuilder function. You must provide a Porcupine keyword file, a wake word detection callback function, a Rhino context file and a inference callback function. You must then make a call to init().

```rust use picovoice::{rhino::RhinoInference, PicovoiceBuilder};

let wakewordcallback = || { // let user know wake word detected }; let inferencecallback = |inference: RhinoInference| { if inference.isunderstood { let intent = inference.intent.unwrap(); let slots = inference.slots; // add code to take action based on inferred intent and slot values } else { // add code to handle unsupported commands } };

let mut picovoice = PicovoiceBuilder::new( keywordpath, wakewordcallback, contextpath, inference_callback, ).init().expect("Failed to create picovoice"); ```

Upon detection of wake word defined by keyword_path it starts inferring user's intent from the follow-on voice command within the context defined by the file located at context_path. keyword_path is the absolute path to Porcupine wake word engine keyword file (with .ppn suffix). contextPath is the absolute path to Rhino Speech-to-Intent engine context file (with .rhn suffix). wake_word_callback is invoked upon the detection of wake phrase and inference_callback is invoked upon completion of follow-on voice command inference.

When instantiated, valid sample rate can be obtained via sample_rate(). Expected number of audio samples per frame is frame_length(). The engine accepts 16-bit linearly-encoded PCM and operates on single-channel audio.

```rust fn nextaudioframe() -> Vec { // get audio frame }

loop { picovoice.process(&nextaudioframe()).expect("Picovoice failed to process audio"); } ```

The sensitivity of the Porcupine (wake word) and Rhino (inference) engines can be tuned using the porcupine_sensitivity() and rhino_sensitivity() methods respectively. It is a floating point number within [0, 1]. A higher sensitivity value results in fewer misses at the cost of (potentially) increasing the erroneous inference rate.

rust let mut picovoice = PicovoiceBuilder::new( keyword_path, wake_word_callback, context_path, inference_callback, ) .porcupine_sensitivity(0.4f32) .rhino_sensitivity(0.77f32) .init().expect("Failed to create picovoice");

Non-standard model and library paths (For example, when using a non-english model) for both engines can be tuned in a similar manner.

rust let mut picovoice = PicovoiceBuilder::new( keyword_path, wake_word_callback, context_path, inference_callback, ) .porcupine_sensitivity(0.4f32) .rhino_sensitivity(0.77f32) .porcupine_model_path("path/to/model/params.pv") .rhino_model_path("path/to/model/params.pv") .porcupine_library_path("path/to/library.so") .rhino_library_path("path/to/library.so") .init().expect("Failed to create picovoice");

Non-English Models

In order to detect wake words and run inference in other languages you need to use the corresponding model file. The model files for all supported languages are available here and here.

Demos

Check out the Picovoice Rust demos here