18 #if defined(_WIN32) || defined(_WIN64)
28 #include "pv_porcupine.h"
29 #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 {
"keyword_path", required_argument,
NULL,
'k'},
94 {
"sensitivity", required_argument,
NULL,
't'},
95 {
"access_key", required_argument,
NULL,
'a'},
96 {
"audio_device_index", required_argument,
NULL,
'd'}
100 fprintf(stderr,
"Usage : %s -l LIBRARY_PATH -m MODEL_PATH -k KEYWORD_PATH -t SENSTIVITY -a ACCESS_KEY -d AUDIO_DEVICE_INDEX\n"
101 " %s [-s, --show_audio_devices]\n", program_name, program_name);
110 char **devices =
NULL;
119 fprintf(stdout,
"Printing devices...\n");
120 for (int32_t i = 0; i <
count; i++) {
121 fprintf(stdout,
"index: %d, name: %s\n", i, devices[i]);
130 const char *library_path =
NULL;
131 const char *model_path =
NULL;
132 const char *keyword_path =
NULL;
133 float sensitivity = 0.5f;
134 const char *access_key =
NULL;
135 int32_t device_index = -1;
144 library_path = optarg;
150 keyword_path = optarg;
153 sensitivity = strtof(optarg,
NULL);
159 device_index = (int32_t) strtol(optarg,
NULL, 10);
166 if (!library_path || !model_path || !keyword_path || !access_key) {
171 void *porcupine_library =
open_dl(library_path);
172 if (!porcupine_library) {
173 fprintf(stderr,
"failed to open library.\n");
177 const char *(*pv_status_to_string_func)(
pv_status_t) =
load_symbol(porcupine_library,
"pv_status_to_string");
178 if (!pv_status_to_string_func) {
183 int32_t (*pv_sample_rate_func)() =
load_symbol(porcupine_library,
"pv_sample_rate");
184 if (!pv_sample_rate_func) {
189 pv_status_t (*pv_porcupine_init_func)(
const char *,
const char *, int32_t,
const char *
const *,
const float *,
pv_porcupine_t **)
190 =
load_symbol(porcupine_library,
"pv_porcupine_init");
191 if (!pv_porcupine_init_func) {
197 if (!pv_porcupine_delete_func) {
203 =
load_symbol(porcupine_library,
"pv_porcupine_process");
204 if (!pv_porcupine_process_func) {
209 int32_t (*pv_porcupine_frame_length_func)() =
load_symbol(porcupine_library,
"pv_porcupine_frame_length");
210 if (!pv_porcupine_frame_length_func) {
215 const char *(*pv_porcupine_version_func)() =
load_symbol(porcupine_library,
"pv_porcupine_version");
216 if (!pv_porcupine_version_func) {
222 pv_status_t porcupine_status = pv_porcupine_init_func(access_key, model_path, 1, &keyword_path, &sensitivity, &porcupine);
224 fprintf(stderr,
"'pv_porcupine_init' failed with '%s'\n", pv_status_to_string_func(porcupine_status));
228 fprintf(stdout,
"V%s\n\n", pv_porcupine_version_func());
230 const int32_t frame_length = pv_porcupine_frame_length_func();
239 fprintf(stdout,
"Selected device: %s.\n", selected_device);
241 fprintf(stdout,
"Start recording...\n");
248 int16_t *pcm = malloc(frame_length *
sizeof(int16_t));
250 fprintf(stderr,
"Failed to allocate pcm memory.\n");
261 int32_t keyword_index = -1;
262 porcupine_status = pv_porcupine_process_func(porcupine, pcm, &keyword_index);
264 fprintf(stderr,
"'pv_porcupine_process' failed with '%s'\n", pv_status_to_string_func(porcupine_status));
268 if (keyword_index != -1) {
269 fprintf(stdout,
"keyword detected\n");
273 fprintf(stdout,
"\n");
283 pv_porcupine_delete_func(porcupine);