Skip to content

Your first listener

Listener is the batteries-included API. It opens a microphone, converts its audio to the format the model expects, owns a Detector, and suppresses immediate repeats.

use micro_wakeword::Listener;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut listener = Listener::from_config("models/wake-word.json")?;
println!(
"Listening for {}",
listener.detector().config().wake_word
);
while let Some(detection) = listener.next_detection()? {
println!(
"Detected {} ({:.1}%)",
detection.wake_word,
detection.probability * 100.0
);
}
Ok(())
}
Terminal window
cargo run --release

Speak the model’s wake phrase. A detection contains:

  • wake_word: the label from your configuration;
  • probability: the smoothed model score from 0.0 to 1.0.
  1. Listener::from_config(...) parses and validates JSON, resolves its model path, loads TensorFlow Lite, opens the current default input, and starts the audio stream.
  2. next_detection() waits for microphone audio and processes it until a detection occurs or the stream ends.
  3. The listener applies its default one-second cooldown after a detection.
  4. The while let loop continues until next_detection() returns Ok(None). Microphone failures are returned as errors rather than silently hidden.
use std::time::Duration;
use micro_wakeword::Listener;
# fn run() -> micro_wakeword::Result<()> {
let mut listener = Listener::config_builder("models/wake-word.json")?
.device("USB Mic")
.cooldown(Duration::from_secs_f64(0.5))
.build()?;
# Ok(())

The device can be its numeric index, its exact name, or an unambiguous case-insensitive part of its name.