8
8
#include "audio.h"
9
9
#include "led.h"
10
10
#include "log.h"
11
- #include "pv_access_key.h"
12
- #include "pv_picovoice.h"
13
- #include "pv_porcupine_params.h"
14
- #include "pv_rhino_params.h"
11
+ #include "picovoice.h"
12
+ #include "pv.h"
15
13
#include "stm32wbxx_hal.h"
16
14
#include "touch_mapper.h"
17
15
#include "touch_targets.h"
22
20
#include <stdlib.h>
23
21
#include <string.h>
24
22
25
- //! Picovoice specific defines
26
- #define MEMORY_BUFFER_SIZE (70 * 1024)
27
23
#define TIMEOUT_PERIOD_TICKS (8 * TX_TIMER_TICKS_PER_SECOND) // 8 seconds
28
24
29
- #ifndef PV_ACCESS_KEY
30
- #error "ACCESS_KEY must be defined in pv_access_key.h"
31
- #endif
32
- static const char * _access_key = PV_ACCESS_KEY ;
33
-
34
- //! Picovoice statics
35
- static int8_t _memory_buffer [MEMORY_BUFFER_SIZE ] __attribute__((aligned (16 )));
36
- static pv_picovoice_t * _handle = NULL ;
37
25
//! Temporary storage of speech samples
38
26
static int16_t _speech_buffer [512 ];
39
27
40
- //! Picovoice settings
41
- static const float PORCUPINE_SENSITIVITY = 0.9f ;
42
- static const float RHINO_SENSITIVITY = 0.9f ;
43
- static const float RHINO_ENDPOINT_DURATION_SEC = 1.0f ;
44
- static const bool RHINO_REQUIRE_ENDPOINT = true;
45
-
46
28
//! Azure RTOS settings
47
29
#define SPEECH_THREAD_STACK_SIZE (1024)
48
30
#define SPEECH_THREAD_PRIO (1)
@@ -65,7 +47,7 @@ static void SPEECH_Reset(void);
65
47
*
66
48
* Sets LED to listening state, resets touch mapper, and starts timeout timer.
67
49
*/
68
- static void wakeWordCallback (void )
50
+ void SPEECH_WakeWordCallback (void )
69
51
{
70
52
log_info ("[wake word]\n" );
71
53
LED_SetState (LED_STATE_LISTENING );
@@ -87,7 +69,7 @@ static void wakeWordCallback(void)
87
69
*
88
70
* @param target_str The beverage name string from speech recognition
89
71
* @return TARGET_T enum value or TARGET_COUNT if not found
90
- * @note Called by inferenceCallback when processing beverage orders
72
+ * @note Called by SPEECH_InferenceCallback when processing beverage orders
91
73
*/
92
74
static TARGET_T getTargetFromString (const char * target_str )
93
75
{
@@ -122,7 +104,7 @@ static TARGET_T getTargetFromString(const char *target_str)
122
104
* Processes beverage orders and cancel commands, maps them to touch targets.
123
105
* Updates LED state and forwards valid targets to touch mapper.
124
106
*/
125
- static void inferenceCallback (pv_inference_t * inference )
107
+ void SPEECH_InferenceCallback (pv_inference_t * inference )
126
108
{
127
109
static const char * beverage_slot = "beverage" ;
128
110
static uint8_t beverage_slot_len = 8 ; // Length of "beverage"
@@ -136,7 +118,6 @@ static void inferenceCallback(pv_inference_t *inference)
136
118
log_error ("Failed to deactivate SPEECH timeout timer: %d" , status );
137
119
}
138
120
139
- // LED_SetState(LED_3, 0);
140
121
if (inference -> is_understood )
141
122
{
142
123
log_info ("Command understood" );
@@ -178,73 +159,6 @@ static void inferenceCallback(pv_inference_t *inference)
178
159
pv_inference_delete (inference );
179
160
}
180
161
181
- /**
182
- * @brief Prints Picovoice error messages from error stack.
183
- *
184
- * @param message_stack Array of error message strings
185
- * @param message_stack_depth Number of messages in the stack
186
- */
187
- static void printErrorMessage (char * * message_stack , int32_t message_stack_depth )
188
- {
189
- for (int32_t i = 0 ; i < message_stack_depth ; i ++ )
190
- {
191
- log_error ("[%ld] %s" , i , message_stack [i ]);
192
- }
193
- }
194
-
195
- /**
196
- * @brief Initializes the Picovoice engine.
197
- *
198
- * @return EXIT_SUCCESS on success, EXIT_FAILURE on error
199
- */
200
- static uint8_t initPicovoice (void )
201
- {
202
- char * * message_stack = NULL ;
203
- int32_t message_stack_depth = 0 ;
204
- pv_status_t error_status ;
205
-
206
- pv_status_t status = pv_picovoice_init (_access_key , // access key
207
- MEMORY_BUFFER_SIZE , // memory size
208
- _memory_buffer , // memory buffer
209
- sizeof (KEYWORD_ARRAY ), // keyword model size
210
- KEYWORD_ARRAY , // keyword model
211
- PORCUPINE_SENSITIVITY , // wake word sensitivity
212
- wakeWordCallback , // wake word callback
213
- sizeof (CONTEXT_ARRAY ), // context model size
214
- CONTEXT_ARRAY , // context model
215
- RHINO_SENSITIVITY , // inference sensitivity
216
- RHINO_ENDPOINT_DURATION_SEC , // endpoint duration
217
- RHINO_REQUIRE_ENDPOINT , // require endpoint
218
- inferenceCallback , // inference callback
219
- & _handle // handle
220
- );
221
- if (status != PV_STATUS_SUCCESS )
222
- {
223
- log_fatal ("Picovoice init failed: %s" , pv_status_to_string (status ));
224
-
225
- error_status = pv_get_error_stack (& message_stack , & message_stack_depth );
226
- if (error_status != PV_STATUS_SUCCESS )
227
- {
228
- log_fatal ("Failed to get error stack: %s" , pv_status_to_string (error_status ));
229
- return EXIT_FAILURE ;
230
- }
231
-
232
- printErrorMessage (message_stack , message_stack_depth );
233
- pv_free_error_stack (message_stack );
234
- return EXIT_FAILURE ;
235
- }
236
-
237
- const char * rhino_context = NULL ;
238
- status = pv_picovoice_context_info (_handle , & rhino_context );
239
- if (status != PV_STATUS_SUCCESS )
240
- {
241
- log_error ("retrieving context info failed with '%s'" , pv_status_to_string (status ));
242
- return EXIT_FAILURE ;
243
- }
244
-
245
- return EXIT_SUCCESS ;
246
- }
247
-
248
162
/**
249
163
* @brief Initializes the speech processing module.
250
164
*
@@ -253,17 +167,16 @@ static uint8_t initPicovoice(void)
253
167
*/
254
168
uint8_t SPEECH_Init (void * memory_ptr )
255
169
{
256
-
257
170
// Initialize Picovoice
258
- uint8_t ret = initPicovoice ( );
259
- if (ret != EXIT_SUCCESS )
171
+ pv_status_t pv_status = PV_Init ( SPEECH_WakeWordCallback , SPEECH_InferenceCallback );
172
+ if (pv_status != PV_STATUS_SUCCESS )
260
173
{
261
174
log_fatal ("Failed to initialize Picovoice" );
262
175
return EXIT_FAILURE ;
263
176
}
264
177
265
178
// Setup AUDIO
266
- ret = AUDIO_Init (memory_ptr );
179
+ uint8_t ret = AUDIO_Init (memory_ptr );
267
180
if (ret != EXIT_SUCCESS )
268
181
{
269
182
log_fatal ("AUDIO_Init failed" );
@@ -347,6 +260,7 @@ static void SPEECH_Process(ULONG thread_input)
347
260
(void )thread_input ;
348
261
int16_t * buffer ;
349
262
uint8_t status ;
263
+ pv_status_t pv_status ;
350
264
351
265
log_info ("AUDIO starting..." );
352
266
AUDIO_Start ();
@@ -368,11 +282,10 @@ static void SPEECH_Process(ULONG thread_input)
368
282
#ifdef AUDIO_OVER_USART
369
283
// HAL_UART_Transmit(&huart1, (uint8_t *)speech_buffer, 512 * sizeof(int16_t), HAL_MAX_DELAY);
370
284
#else
371
-
372
- const pv_status_t status = pv_picovoice_process (_handle , _speech_buffer );
373
- if (status != PV_STATUS_SUCCESS )
285
+ pv_status = PV_Process (_speech_buffer );
286
+ if (pv_status != PV_STATUS_SUCCESS )
374
287
{
375
- log_error ("Picovoice process failed: %s" , pv_status_to_string ( status ) );
288
+ log_error ("Picovoice process failed" );
376
289
}
377
290
#endif
378
291
}
@@ -394,17 +307,9 @@ static void SPEECH_Reset(void)
394
307
AUDIO_Stop ();
395
308
396
309
// Delete Picovoice instance
397
- if (_handle == NULL )
398
- {
399
- log_error ("Handle == NULL, failed to delete Picovoice instance" );
400
- }
401
- else
402
- {
403
- pv_picovoice_delete (_handle );
404
- _handle = NULL ;
405
- }
310
+ PV_Delete ();
406
311
407
- if (initPicovoice ( ) != EXIT_SUCCESS )
312
+ if (PV_Init ( SPEECH_WakeWordCallback , SPEECH_InferenceCallback ) != PV_STATUS_SUCCESS )
408
313
{
409
314
log_fatal ("Failed to re-initialize Picovoice" );
410
315
return ;
0 commit comments