I have just received my Nano 33 BLE Sense and am trying to get this project (my first in this device) working: Offline Voice AI on Arduino. Did you know it’s now possible to… | by Mohammadreza Rostam | Picovoice | Medium You can see the code here
#include <Picovoice_EN.h>
#include "params.h"
#define RED (22)
#define BLUE (24)
#define GREEN (23)
#define MEMORY_BUFFER_SIZE (70 * 1024)
static pv_picovoice_t *handle = NULL;
static int8_t memory_buffer[MEMORY_BUFFER_SIZE] __attribute__((aligned(16)));
static const float PORCUPINE_SENSITIVITY = 0.75f;
static const float RHINO_SENSITIVITY = 0.5f;
static void blink_led(String color, String speed, String iteration) {
int32_t led_pin = 0;
int32_t delay_time = 0;
int32_t iteration_int = iteration.toInt();
if (color == "red") {
led_pin = RED;
} else if (color == "blue") {
led_pin = BLUE;
} else if (color == "green") {
led_pin = GREEN;
}
if (speed == "slow" || speed == "slowly" || speed == "low") {
delay_time = 500;
} else if (speed == "normal") {
delay_time = 300;
} else if (speed == "fast" || speed == "quickly" || speed == "rapidly" || speed == "high") {
delay_time = 150;
}
for (int32_t i = 0; i < iteration_int; i++) {
digitalWrite(led_pin, LOW); // turn the LED on (LOW is the voltage level)
delay(delay_time);
digitalWrite(led_pin, HIGH); // turn the LED off (HIGH is the voltage level)
delay(delay_time);
}
}
static void change_state(String color, String state) {
int32_t led_pin = 0;
if (color == "red") {
led_pin = RED;
} else if (color == "blue") {
led_pin = BLUE;
} else if (color == "green") {
led_pin = GREEN;
}
if (state == "on") {
digitalWrite(led_pin, LOW);
} else {
digitalWrite(led_pin, HIGH);
}
}
static void party_led(void) {
int32_t delay_time = 100;
for (int32_t i = 0; i < 4; i++) {
digitalWrite(RED, LOW);
delay(delay_time);
digitalWrite(GREEN, LOW);
delay(delay_time);
digitalWrite(BLUE, LOW);
delay(delay_time);
digitalWrite(RED, HIGH);
delay(delay_time);
digitalWrite(GREEN, HIGH);
delay(delay_time);
digitalWrite(BLUE, HIGH);
delay(delay_time);
}
}
static void wake_word_callback(void) {
Serial.println("Wake word detected!");
}
static void inference_callback(pv_inference_t *inference) {
Serial.println("{");
Serial.print(" is_understood : ");
Serial.println(inference->is_understood ? "true" : "false");
if (inference->is_understood) {
Serial.print(" intent : ");
Serial.println(inference->intent);
if (inference->num_slots > 0) {
Serial.println(" slots : {");
for (int32_t i = 0; i < inference->num_slots; i++) {
Serial.print(" ");
Serial.print(inference->slots[i]);
Serial.print(" : ");
Serial.println(inference->values[i]);
}
Serial.println(" }");
}
}
Serial.println("}\n");
if (inference->is_understood) {
if (String(inference->intent) == "changeLightState") {
String state = "";
String color = "";
for (int32_t i = 0; i < inference->num_slots; i++) {
if (String(inference->slots[i]) == "state") {
state = String(inference->values[i]);
} else if (String(inference->slots[i]) == "color") {
color = String(inference->values[i]);
}
}
if (color == "") {
change_state("red", state);
change_state("green", state);
change_state("blue", state);
} else {
change_state(color, state);
}
} else if (String(inference->intent) == "blinkLight") {
String color = "";
String iteration = "5";
String speed = "normal";
for (int32_t i = 0; i < inference->num_slots; i++) {
if (String(inference->slots[i]) == "color") {
color = String(inference->values[i]);
} else if (String(inference->slots[i]) == "iteration") {
iteration = String(inference->values[i]);
} else if (String(inference->slots[i]) == "speed") {
speed = String(inference->values[i]);
}
}
blink_led(color, speed, iteration);
} else if (String(inference->intent) == "partyLight") {
party_led();
}
}
pv_inference_delete(inference);
}
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
pv_status_t status = pv_audio_rec_init();
if (status != PV_STATUS_SUCCESS) {
Serial.print("Audio init failed with ");
Serial.println(pv_status_to_string(status));
while (1);
}
status = pv_picovoice_init(
MEMORY_BUFFER_SIZE,
memory_buffer,
sizeof(KEYWORD_ARRAY),
KEYWORD_ARRAY,
PORCUPINE_SENSITIVITY,
wake_word_callback,
sizeof(CONTEXT_ARRAY),
CONTEXT_ARRAY,
RHINO_SENSITIVITY,
inference_callback,
&handle);
if (status != PV_STATUS_SUCCESS) {
Serial.print("Picovoice init failed with ");
Serial.println(pv_status_to_string(status));
while (1);
}
const char *rhino_context = NULL;
status = pv_picovoice_context_info(handle, &rhino_context);
if (status != PV_STATUS_SUCCESS) {
Serial.print("retrieving context info failed with");
Serial.println(pv_status_to_string(status));
while (1);
}
Serial.println("Wake word: Picovoice");
Serial.println(rhino_context);
}
void loop() {
const int16_t *buffer = pv_audio_rec_get_new_buffer();
if (buffer) {
const pv_status_t status = pv_picovoice_process(handle, buffer);
if (status != PV_STATUS_SUCCESS) {
Serial.print("Picovoice process failed with ");
Serial.println(pv_status_to_string(status));
while (1);
}
}
}
However I cannot get the .ino file to compile. I have included the project library and the Picovoice.EN library but the .ino fails to compile with the following error (on both my IDE and the CloudIDE).
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino: In function 'void setup()':
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino:10:32: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
#define MEMORY_BUFFER_SIZE (70 * 1024)
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino:170:13: note: in expansion of macro 'MEMORY_BUFFER_SIZE'
MEMORY_BUFFER_SIZE,
^~~~~~~~~~~~~~~~~~
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino:180:20: error: invalid conversion from 'int8_t* {aka signed char*}' to 'int32_t {aka long int}' [-fpermissive]
&handle);
^
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino:172:19: error: invalid conversion from 'unsigned int' to 'void*' [-fpermissive]
sizeof(KEYWORD_ARRAY),
^
/tmp/354268789/Control_LED_Picovoice/Control_LED_Picovoice.ino:180:20: error: invalid conversion from 'const uint8_t* {aka const unsigned char*}' to 'int32_t {aka long int}' [-fpermissive]
&handle);
^