18 #if defined(_WIN32) || defined(_WIN64)
28 #include "pv_recorder.h"
33 static void *
open_dl(
const char *dl_path) {
35 #if defined(_WIN32) || defined(_WIN64)
37 return LoadLibrary(dl_path);
41 return dlopen(dl_path, RTLD_NOW);
49 #if defined(_WIN32) || defined(_WIN64)
51 return GetProcAddress((HMODULE) handle, symbol);
55 return dlsym(handle, symbol);
63 #if defined(_WIN32) || defined(_WIN64)
65 FreeLibrary((HMODULE) handle);
77 #if defined(_WIN32) || defined(_WIN64)
79 fprintf(stderr,
"%s with code '%lu'.\n", message, GetLastError());
83 fprintf(stderr,
"%s with '%s'.\n", message, dlerror());
90 {
"show_audio_devices", no_argument,
NULL,
's'},
91 {
"library_path", required_argument,
NULL,
'l'},
92 {
"model_path", required_argument,
NULL,
'm'},
93 {
"context_path", required_argument,
NULL,
'c'},
94 {
"sensitivity", required_argument,
NULL,
't'},
95 {
"require_endpoint", required_argument,
NULL,
'e'},
96 {
"access_key", required_argument,
NULL,
'a'},
97 {
"audio_device_index", required_argument,
NULL,
'd'}
102 "Usage : %s -l LIBRARY_PATH -m MODEL_PATH -c CONTEXT_PATH -t SENSTIVITY -a ACCESS_KEY -d AUDIO_DEVICE_INDEX [-e, --require_endpoint (true,false)]\n"
103 " %s [-s, --show_audio_devices]\n", program_name, program_name);
112 char **devices =
NULL;
121 fprintf(stdout,
"Printing devices...\n");
122 for (int32_t i = 0; i <
count; i++) {
123 fprintf(stdout,
"index: %d, name: %s\n", i, devices[i]);
132 const char *library_path =
NULL;
133 const char *model_path =
NULL;
134 const char *context_path =
NULL;
135 float sensitivity = 0.5f;
136 bool require_endpoint =
false;
137 const char *access_key =
NULL;
138 int32_t device_index = -1;
147 library_path = optarg;
153 context_path = optarg;
156 sensitivity = strtof(optarg,
NULL);
159 require_endpoint = (strcmp(optarg,
"false") != 0);
165 device_index = (int32_t) strtol(optarg,
NULL, 10);
172 if (!library_path || !model_path || !context_path || !access_key) {
177 void *rhino_library =
open_dl(library_path);
178 if (!rhino_library) {
179 fprintf(stderr,
"failed to open library.\n");
184 if (!pv_status_to_string_func) {
189 int32_t (*pv_sample_rate_func)() =
load_symbol(rhino_library,
"pv_sample_rate");
190 if (!pv_sample_rate_func) {
203 if (!pv_rhino_init_func) {
209 if (!pv_rhino_delete_func) {
216 if (!pv_rhino_process_func) {
222 load_symbol(rhino_library,
"pv_rhino_is_understood");
223 if (!pv_rhino_is_understood_func) {
229 (*pv_rhino_get_intent_func)(
const pv_rhino_t *,
const char **, int32_t *,
const char ***,
const char ***) =
231 if (!pv_rhino_get_intent_func) {
236 pv_status_t (*pv_rhino_free_slots_and_values_func)(
const pv_rhino_t *,
const char **,
const char **) =
237 load_symbol(rhino_library,
"pv_rhino_free_slots_and_values");
238 if (!pv_rhino_free_slots_and_values_func) {
239 print_dl_error(
"failed to load 'pv_rhino_free_slots_and_values'");
245 if (!pv_rhino_reset_func) {
251 load_symbol(rhino_library,
"pv_rhino_context_info");
252 if (!pv_rhino_context_info_func) {
257 int32_t (*pv_rhino_frame_length_func)() =
load_symbol(rhino_library,
"pv_rhino_frame_length");
258 if (!pv_rhino_frame_length_func) {
263 const char *(*pv_rhino_version_func)() =
load_symbol(rhino_library,
"pv_rhino_version");
264 if (!pv_rhino_version_func) {
278 fprintf(stderr,
"'pv_rhino_init' failed with '%s'\n", pv_status_to_string_func(status));
282 fprintf(stdout,
"Picovoice Rhino Speech-to-Intent (%s)\n\n", pv_rhino_version_func());
284 const int32_t frame_length = pv_rhino_frame_length_func();
292 const char *context_info =
NULL;
293 status = pv_rhino_context_info_func(rhino, &context_info);
295 fprintf(stderr,
"'pv_rhino_context_info' failed with '%s'\n", pv_status_to_string_func(status));
298 fprintf(stdout,
"%s\n\n", context_info);
301 fprintf(stdout,
"Selected device: %s.\n", selected_device);
302 fprintf(stdout,
"Listening...\n\n");
310 int16_t *pcm = malloc(frame_length *
sizeof(int16_t));
312 fprintf(stderr,
"Failed to allocate pcm memory.\n");
324 bool is_finalized =
false;
325 status = pv_rhino_process_func(
331 fprintf(stderr,
"'pv_rhino_process' failed with '%s'\n", pv_status_to_string_func(status));
336 bool is_understood =
false;
337 status = pv_rhino_is_understood_func(rhino, &is_understood);
339 fprintf(stderr,
"'pv_rhino_is_understood' failed with '%s'\n",
340 pv_status_to_string_func(status));
344 const char *intent =
NULL;
345 int32_t num_slots = 0;
346 const char **slots =
NULL;
347 const char **values =
NULL;
350 status = pv_rhino_get_intent_func(
357 fprintf(stderr,
"'pv_rhino_get_intent' failed with '%s'\n",
358 pv_status_to_string_func(status));
363 fprintf(stdout,
"{\n");
364 fprintf(stdout,
" 'is_understood' : '%s',\n", is_understood ?
"true" :
"false");
366 fprintf(stdout,
" 'intent' : '%s',\n", intent);
368 fprintf(stdout,
" 'slots' : {\n");
369 for (int32_t i = 0; i < num_slots; i++) {
370 fprintf(stdout,
" '%s' : '%s',\n", slots[i], values[i]);
372 fprintf(stdout,
" }\n");
375 fprintf(stdout,
"}\n");
379 status = pv_rhino_free_slots_and_values_func(rhino, slots, values);
381 fprintf(stderr,
"'pv_rhino_free_slots_and_values' failed with '%s'\n",
382 pv_status_to_string_func(status));
387 status = pv_rhino_reset_func(rhino);
389 fprintf(stderr,
"'pv_rhino_reset' failed with '%s'\n",
390 pv_status_to_string_func(status));
395 fprintf(stdout,
"\n");
405 pv_rhino_delete_func(rhino);