Hotword Detection for MCUs
Tags: 100daysofcode, challenge, ai, machinelearning
Published: 2023-01-31 17:18:25

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

First, sign up for [Picovoice Console](https://console.picovoice.ai/signup) if you haven't. You'll train your custom hotword (similar to Alexa, or OK Google) with [Porcupine](https://picovoice.ai/platform/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](https://picovoice.ai/docs/tips/choosing-a-wake-word/), 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:

```
#ifndef PV_PPN_PARAMS_H
#define PV_PPN_PARAMS_H
#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
```
**Porcupine SDK**
Porcupine SDK is on GitHub. Find libraries for supported MCUs on the [Porcupine GitHub repository](https://github.com/Picovoice/porcupine/tree/master/lib/mcu). _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](https://github.com/Picovoice/porcupine/blob/master/include/pv_porcupine_mcu.h) 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(
        ACCESS_KEY,
        MEMORY_BUFFER_SIZE,
        memory_buffer,
        num_keywords,
        &KEYWORD_MODEL_SIZES,
        &KEYWORD_MODELS,
        &SENSITIVITIES,
        &handle);
if (status != PV_STATUS_SUCCESS) {
    // Error handling logic
}
```
`ACCESS_KEY` is the `AccessKey` from your [Picovoice Console](https://console.picovoice.ai/signup) 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(
        handle,
        audio_frame,
        &keyword_index);
if (status != PV_STATUS_SUCCESS) {
    // Error handling callback
}
if (keyword_index != -1) {
    // Detection callback
}
```
**4. Cleanup**
Release resources upon completion:
```
pv_porcupine_delete(handle);
```
This article was originally published on [picovoice.ai](https://picovoice.ai/blog/keyword-spotting-on-microcontrollers/)