rustPicovoice Platform — Rust API

  • End-to-End Voice Platform
  • Offline Voice Recognition
  • Local Speech Recognition
  • Speech-to-Intent
  • Domain-Specific NLU
  • Wake Word Detection
  • Raspberry Pi
  • BeagleBone
  • NVIDIA Jetson
  • Linux
  • macOS
  • Windows
  • Rust

This document outlines how to integrate Picovoice platform within an application using its Rust API.

Requirements

  • Rust 1.54+
  • Cargo

Compatibility

  • Linux (x86_64)
  • macOS (x86_64)
  • Windows (x86_64)
  • Raspberry Pi (all variants)
  • NVIDIA Jetson (Nano)
  • BeagleBone.

Installation

You can install the latest version of Porcupine into your Rust crate by adding picovoice into your Cargo.toml:

[dependencies]
picovoice = "*"

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():

use picovoice::{rhino::RhinoInference, PicovoiceBuilder};
let wake_word_callback = || {
// let user know wake word detected
};
let inference_callback = |inference: RhinoInference| {
if inference.is_understood {
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(
keyword_path,
wake_word_callback,
context_path,
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). context_path 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.

fn next_audio_frame() -> Vec<i16> {
// get audio frame
}
loop {
picovoice.process(&next_audio_frame()).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:

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.

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");

Custom Wake Word & Context

You can create custom Porcupine wake word and Rhino context models using Picovoice Console

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.


Issue with this doc? Please let us know.