DEV Community

Cover image for Hotword Detection for MCUs
Dilek Karasoy for Picovoice

Posted on

Hotword Detection for MCUs

Day 22 is here! Today, we'll add hotword detection to MCUs.

First, sign up for Picovoice Console if you haven't. You'll train your custom hotword (similar to Alexa, or OK Google) with Porcupine and get your AccessKey for free.

1. Log in to Picovoice Console and Go to the Porcupine Page
1.1. Select target language (e.g. English, Japanese, Spanish, etc.)
1.2. Select Arm Cortex-M as the platform to optimize the model.
1.3. Then type in your hotword.
A good hotword should have a few linguistic properties, read them first if you're not sure.
1.4 Enter your board type and the UUID corresponding to the MCU.
1.5. Click the train button. Your model with be ready instantly.
1.6 Unzip the downloaded file and find pv_porcupine_params.h. Below is a snippet:

#include <stdint.h>
static const uint8_t KEYWORD_ARRAY[] __attribute__ ((aligned (16))) = {
        0xc6, 0xd8, ...
static const uint32_t KEYWORD_ARRAY_LENGTH = 1234;
#endif // PV_PPN_PARAMS_H
Enter fullscreen mode Exit fullscreen mode

Porcupine SDK
Porcupine SDK is on GitHub. Find libraries for supported MCUs on the Porcupine GitHub repository. Arduino libraries are available via a specialized package manager offered by Arduino.

2. Initialization
Link the appropriate library (.a) and the parameter file(s) downloaded from Picovoice Console into your project. Include the header file for Porcupine into your project and put it is on the path. Initialize the engine:

pv_porcupine_t *handle = NULL;
const pv_status_t status = pv_porcupine_init(
if (status != PV_STATUS_SUCCESS) {
    // Error handling logic
Enter fullscreen mode Exit fullscreen mode

ACCESS_KEY is the AccessKey from your Picovoice Console account.memory_buffer is a buffer on RAM used within the engine, num_keywords shows the number of keywords you want to spot (you can spot multiple), KEYWORD_MODELS are the models downloaded from Picovoice Console, and SENSITIVITIES is the array of engine's sensitivity for detecting each keyword.

3. Processing
Pass audio to Porcupine in frames (chunk). Porcupine processes 16 kHz and 16-bit sampled PCM. Each frame should have pv_porcupine_frame_length() samples.

const int16_t *audio_frame = pv_audio_rec_get_new_buffer();

int32_t keyword_index = -1;
const pv_status_t status = pv_porcupine_process(
if (status != PV_STATUS_SUCCESS) {
    // Error handling callback
if (keyword_index != -1) {
    // Detection callback
Enter fullscreen mode Exit fullscreen mode

4. Cleanup
Release resources upon completion:

Enter fullscreen mode Exit fullscreen mode

This article was originally published on

Top comments (0)