psoc062s2/main.c
Go to the documentation of this file.
1 /*
2  Copyright 2021 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5  file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8  an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  specific language governing permissions and limitations under the License.
10 */
11 
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 #include "cy_rgb_led.h"
17 #include "cybsp.h"
18 
19 #include "pv_audio_rec.h"
20 #include "pv_keywords.h"
21 #include "pv_porcupine.h"
22 #include "pv_psoc6.h"
23 
24 #define MEMORY_BUFFER_SIZE (70 * 1024)
25 
26 static const char* ACCESS_KEY = ... // AccessKey string obtained from Picovoice Console (https://picovoice.ai/console/)
27 
28 static int8_t memory_buffer[MEMORY_BUFFER_SIZE] __attribute__((aligned(16)));
29 
30 #ifdef __PV_LANGUAGE_ENGLISH__
31 
32 static const int32_t NUM_KEYWORDS = 4;
33 static const int32_t KEYWORD_MODEL_SIZES[] = {
34  sizeof(DEFAULT_KEYWORD_ARRAY),
35  sizeof(PICOVOICE_KEYWORD_ARRAY),
36  sizeof(BUMBLEBEE_KEYWORD_ARRAY),
37  sizeof(ALEXA_KEYWORD_ARRAY)
38 };
39 static const void *KEYWORD_MODELS[] = {
40  DEFAULT_KEYWORD_ARRAY,
41  PICOVOICE_KEYWORD_ARRAY,
42  BUMBLEBEE_KEYWORD_ARRAY,
43  ALEXA_KEYWORD_ARRAY
44 };
45 static const float SENSITIVITIES[] = {
46  0.75f,
47  0.75f,
48  0.75f,
49  0.75f
50 };
51 
52 static const char *KEYWORDS_NAME[] = {
53  "Porcupine",
54  "Picovoice",
55  "Bumblebee",
56  "Alexa"
57 };
58 
59 #else
60 
61 static const int32_t NUM_KEYWORDS = 1;
62 static const int32_t KEYWORD_MODEL_SIZES[] = { sizeof(DEFAULT_KEYWORD_ARRAY) };
63 static const void *KEYWORD_MODELS[] = { DEFAULT_KEYWORD_ARRAY };
64 static const float SENSITIVITIES[] = { 0.75f };
65 static const char *KEYWORDS_NAME[] = { "" };
66 
67 #endif
68 
69 static void error_handler(void) {
70  cy_rgb_led_on(CY_RGB_LED_COLOR_RED, CY_RGB_LED_MAX_BRIGHTNESS);
71  while(true);
72 }
73 
74 int main(void) {
75 
76  pv_status_t status = pv_board_init();
77  if (status != PV_STATUS_SUCCESS) {
78  error_handler();
79  }
80 
81  status = pv_message_init();
82  if (status != PV_STATUS_SUCCESS) {
83  error_handler();
84  }
85 
86  const uint8_t *board_uuid = pv_get_uuid();
87  printf("UUID: ");
88  for (uint32_t i = 0; i < pv_get_uuid_size(); i++) {
89  printf(" %.2x", board_uuid[i]);
90  }
91  printf("\r\n");
92 
93  status = pv_audio_rec_init();
94  if (status != PV_STATUS_SUCCESS) {
95  printf("Audio init failed with '%s'\r\n", pv_status_to_string(status));
96  error_handler();
97  }
98 
99  status = pv_audio_rec_start();
100  if (status != PV_STATUS_SUCCESS) {
101  printf("Recording audio failed with '%s'\r\n", pv_status_to_string(status));
102  error_handler();
103  }
104 
105  pv_porcupine_t *handle = NULL;
106 
107  status = pv_porcupine_init(
108  ACCESS_KEY,
110  memory_buffer,
111  NUM_KEYWORDS,
115  &handle);
116 
117  if (status != PV_STATUS_SUCCESS) {
118  printf("Porcupine init failed with '%s'\r\n", pv_status_to_string(status));
119  error_handler();
120  }
121 
122  while (true) {
123  const int16_t *buffer = pv_audio_rec_get_new_buffer();
124  if (buffer) {
125  int32_t keyword_index = -1;
126  const pv_status_t status = pv_porcupine_process(handle, buffer, &keyword_index);
127  if (status != PV_STATUS_SUCCESS) {
128  printf("Porcupine process failed with '%s'\r\n", pv_status_to_string(status));
129  error_handler();
130  }
131  if (keyword_index >= 0) {
132  printf("[wake word] %s\r\n", KEYWORDS_NAME[keyword_index]);
133 
134  switch (keyword_index) {
135  case 0:
136  cy_rgb_led_on(CY_RGB_LED_COLOR_GREEN, CY_RGB_LED_MAX_BRIGHTNESS);
137  break;
138  case 1:
139  cy_rgb_led_on(CY_RGB_LED_COLOR_CYAN, CY_RGB_LED_MAX_BRIGHTNESS);
140  break;
141  case 2:
142  cy_rgb_led_on(CY_RGB_LED_COLOR_RED, CY_RGB_LED_MAX_BRIGHTNESS);
143  break;
144  case 3:
145  cy_rgb_led_on(CY_RGB_LED_COLOR_BLUE, CY_RGB_LED_MAX_BRIGHTNESS);
146  break;
147  default:
148  break;
149  }
150  Cy_SysLib_Delay(500);
151  cy_rgb_led_off();
152  }
153  }
154 
155  }
156  pv_board_deinit();
158  pv_porcupine_delete(handle);
159 }
pv_psoc6.h
pv_audio_rec_start
pv_status_t pv_audio_rec_start(void)
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:186
pv_porcupine_init
PV_API pv_status_t pv_porcupine_init(const char *access_key, const char *model_path, int32_t num_keywords, const char *const *keyword_paths, const float *sensitivities, pv_porcupine_t **object)
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
KEYWORD_MODELS
static const void * KEYWORD_MODELS[]
Definition: psoc062s2/main.c:63
pv_board_deinit
void pv_board_deinit(void)
Definition: pv_imxrt1050.c:74
pv_get_uuid
const uint8_t * pv_get_uuid(void)
Definition: pv_imxrt1050.c:31
pv_porcupine_process
PV_API pv_status_t pv_porcupine_process(pv_porcupine_t *object, const int16_t *pcm, int32_t *keyword_index)
NUM_KEYWORDS
static const int32_t NUM_KEYWORDS
Definition: psoc062s2/main.c:61
pv_audio_rec_get_new_buffer
const int16_t * pv_audio_rec_get_new_buffer(void)
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:202
SENSITIVITIES
static const float SENSITIVITIES[]
Definition: psoc062s2/main.c:64
PV_STATUS_SUCCESS
@ PV_STATUS_SUCCESS
Definition: porcupine/include/picovoice.h:34
pv_status_t
pv_status_t
Definition: porcupine/include/picovoice.h:33
pv_board_init
pv_status_t pv_board_init(void)
Definition: pv_imxrt1050.c:39
pv_porcupine_delete
PV_API void pv_porcupine_delete(pv_porcupine_t *object)
error_handler
static void error_handler(void)
Definition: psoc062s2/main.c:69
pv_get_uuid_size
const uint32_t pv_get_uuid_size(void)
Definition: pv_imxrt1050.c:35
main
int main(void)
Definition: psoc062s2/main.c:74
KEYWORDS_NAME
static const char * KEYWORDS_NAME[]
Definition: psoc062s2/main.c:65
ACCESS_KEY
static const char * ACCESS_KEY
Definition: psoc062s2/main.c:26
MEMORY_BUFFER_SIZE
#define MEMORY_BUFFER_SIZE
Definition: psoc062s2/main.c:24
pv_keywords.h
pv_porcupine_t
struct pv_porcupine pv_porcupine_t
Definition: include/pv_porcupine.h:33
pv_message_init
pv_status_t pv_message_init(void)
Definition: pv_imxrt1050.c:67
__attribute__
__attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
Reverse byte order (16 bit)
Definition: imxrt1050/imxrt1050-evkb/CMSIS/cmsis_armcc.h:492
pv_audio_rec_deinit
void pv_audio_rec_deinit(void)
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:216
KEYWORD_MODEL_SIZES
static const int32_t KEYWORD_MODEL_SIZES[]
Definition: psoc062s2/main.c:62
pv_status_to_string
const PV_API char * pv_status_to_string(pv_status_t status)
pv_audio_rec_init
pv_status_t pv_audio_rec_init(void)
Definition: imxrt1050/imxrt1050-evkb/source/pv_audio_rec.c:125


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56