18 #if defined(_WIN32) || defined(_WIN64)
28 #define DR_WAV_IMPLEMENTATION
34 static void *
open_dl(
const char *dl_path) {
36 #if defined(_WIN32) || defined(_WIN64)
38 return LoadLibrary(dl_path);
42 return dlopen(dl_path, RTLD_NOW);
50 #if defined(_WIN32) || defined(_WIN64)
52 return GetProcAddress((HMODULE) handle, symbol);
56 return dlsym(handle, symbol);
64 #if defined(_WIN32) || defined(_WIN64)
66 FreeLibrary((HMODULE) handle);
78 #if defined(_WIN32) || defined(_WIN64)
80 fprintf(stderr,
"%s with code '%lu'.\n", message, GetLastError());
84 fprintf(stderr,
"%s with '%s'.\n", message, dlerror());
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 {
"wav_path", required_argument,
NULL,
'w'}
102 "Usage : %s -l LIBRARY_PATH -m MODEL_PATH -c CONTEXT_PATH -t SENSTIVITY -a ACCESS_KEY -w WAV_PATH [-e, --require_endpoint (true,false)]\n",
107 const char *library_path =
NULL;
108 const char *model_path =
NULL;
109 const char *context_path =
NULL;
110 const char *access_key =
NULL;
111 const char *wav_path =
NULL;
112 float sensitivity = 0.5f;
113 bool require_endpoint =
false;
119 library_path = optarg;
125 context_path = optarg;
128 sensitivity = strtof(optarg,
NULL);
131 require_endpoint = (strcmp(optarg,
"false") != 0);
144 if (!library_path || !model_path || !context_path || !access_key) {
149 void *rhino_library =
open_dl(library_path);
150 if (!rhino_library) {
151 fprintf(stderr,
"failed to open library.\n");
156 if (!pv_status_to_string_func) {
161 int32_t (*pv_sample_rate_func)() =
load_symbol(rhino_library,
"pv_sample_rate");
162 if (!pv_sample_rate_func) {
175 if (!pv_rhino_init_func) {
181 if (!pv_rhino_delete_func) {
188 if (!pv_rhino_process_func) {
194 load_symbol(rhino_library,
"pv_rhino_is_understood");
195 if (!pv_rhino_is_understood_func) {
201 (*pv_rhino_get_intent_func)(
const pv_rhino_t *,
const char **, int32_t *,
const char ***,
const char ***) =
203 if (!pv_rhino_get_intent_func) {
208 pv_status_t (*pv_rhino_free_slots_and_values_func)(
const pv_rhino_t *,
const char **,
const char **) =
209 load_symbol(rhino_library,
"pv_rhino_free_slots_and_values");
210 if (!pv_rhino_free_slots_and_values_func) {
211 print_dl_error(
"failed to load 'pv_rhino_free_slots_and_values'");
215 int32_t (*pv_rhino_frame_length_func)() =
load_symbol(rhino_library,
"pv_rhino_frame_length");
216 if (!pv_rhino_frame_length_func) {
221 const char *(*pv_rhino_version_func)() =
load_symbol(rhino_library,
"pv_rhino_version");
222 if (!pv_rhino_version_func) {
230 fprintf(stderr,
"failed to open wav file at '%s'.", wav_path);
234 if (
f.sampleRate != (uint32_t) pv_sample_rate_func()) {
235 fprintf(stderr,
"audio sample rate should be %d\n.", pv_sample_rate_func());
239 if (
f.bitsPerSample != 16) {
240 fprintf(stderr,
"audio format should be 16-bit\n.");
244 if (
f.channels != 1) {
245 fprintf(stderr,
"audio should be single-channel.\n");
249 int16_t *pcm = calloc(pv_rhino_frame_length_func(),
sizeof(int16_t));
251 fprintf(stderr,
"failed to allocate memory for audio frame.\n");
264 fprintf(stderr,
"'pv_rhino_init' failed with '%s'\n", pv_status_to_string_func(status));
268 fprintf(stdout,
"Picovoice Rhino Speech-to-Intent (%s) :\n\n", pv_rhino_version_func());
270 double total_cpu_time_usec = 0;
271 double total_processed_time_usec = 0;
272 int32_t frame_index = 0;
275 struct timeval before;
276 gettimeofday(&before,
NULL);
278 bool is_finalized =
false;
279 status = pv_rhino_process_func(rhino, pcm, &is_finalized);
281 fprintf(stderr,
"'pv_rhino_process' failed with '%s'\n", pv_status_to_string_func(status));
286 bool is_understood =
false;
287 status = pv_rhino_is_understood_func(rhino, &is_understood);
289 fprintf(stderr,
"'pv_rhino_is_understood'failed with '%s'\n", pv_status_to_string_func(status));
293 const char *intent =
NULL;
294 int32_t num_slots = 0;
295 const char **slots =
NULL;
296 const char **values =
NULL;
299 status = pv_rhino_get_intent_func(rhino, &intent, &num_slots, &slots, &values);
301 fprintf(stderr,
"'pv_rhino_get_intent' failed with '%s'\n", pv_status_to_string_func(status));
306 fprintf(stdout,
"{\n");
307 fprintf(stdout,
" 'is_understood' : '%s',\n", is_understood ?
"true" :
"false");
309 fprintf(stdout,
" 'intent' : '%s'\n", intent);
311 fprintf(stdout,
" 'slots' : {\n");
312 for (int32_t i = 0; i < num_slots; i++) {
313 fprintf(stdout,
" '%s' : '%s',\n", slots[i], values[i]);
315 fprintf(stdout,
" }\n");
318 fprintf(stdout,
"}\n\n");
321 status = pv_rhino_free_slots_and_values_func(rhino, slots, values);
323 fprintf(stderr,
"'pv_rhino_free_slots_and_values' failed with '%s'\n",
324 pv_status_to_string_func(status));
332 struct timeval after;
333 gettimeofday(&after,
NULL);
335 total_cpu_time_usec +=
336 (double) (after.tv_sec - before.tv_sec) * 1e6 + (double) (after.tv_usec - before.tv_usec);
337 total_processed_time_usec += (pv_rhino_frame_length_func() * 1e6) / pv_sample_rate_func();
341 const double real_time_factor = total_cpu_time_usec / total_processed_time_usec;
342 fprintf(stdout,
"real time factor : %.3f\n", real_time_factor);
346 pv_rhino_delete_func(rhino);