diff --git a/demo/c/cobra_demo_file.c b/demo/c/cobra_demo_file.c index 2034356a..b03dec09 100644 --- a/demo/c/cobra_demo_file.c +++ b/demo/c/cobra_demo_file.c @@ -91,6 +91,12 @@ void print_usage(const char *program_name) { fprintf(stdout, "Usage: %s [-l LIBRARY_PATH -a ACCESS_KEY -w WAV_PATH]\n", program_name); } +void print_error_message(char **message_stack, int32_t message_stack_depth) { + for (int32_t i = 0; i < message_stack_depth; i++) { + fprintf(stderr, " [%d] %s\n", i, message_stack[i]); + } +} + int picovoice_main(int argc, char *argv[]) { const char *library_path = NULL; const char *access_key = NULL; @@ -173,6 +179,18 @@ int picovoice_main(int argc, char *argv[]) { exit(1); } + pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(cobra_library, "pv_get_error_stack"); + if (!pv_get_error_stack_func) { + print_dl_error("failed to load 'pv_get_error_stack_func'"); + exit(1); + } + + void (*pv_free_error_stack_func)(char **) = load_symbol(cobra_library, "pv_free_error_stack"); + if (!pv_free_error_stack_func) { + print_dl_error("failed to load 'pv_free_error_stack_func'"); + exit(1); + } + drwav f; if (!drwav_init_file(&f, wav_path, NULL)) { @@ -201,10 +219,28 @@ int picovoice_main(int argc, char *argv[]) { exit(1); } + char **message_stack = NULL; + int32_t message_stack_depth = 0; + pv_status_t error_status = PV_STATUS_RUNTIME_ERROR; + pv_cobra_t *cobra = NULL; pv_status_t status = pv_cobra_init_func(access_key, &cobra); if (status != PV_STATUS_SUCCESS) { fprintf(stderr, "failed to init with '%s'", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Cobra error state with '%s'.\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + pv_free_error_stack_func(message_stack); + } else { + fprintf(stderr, ".\n"); + } + exit(1); } @@ -221,7 +257,21 @@ int picovoice_main(int argc, char *argv[]) { float is_voiced = 0.f; status = pv_cobra_process_func(cobra, pcm, &is_voiced); if (status != PV_STATUS_SUCCESS) { - fprintf(stderr, "failed to init with '%s'", pv_status_to_string_func(status)); + fprintf(stderr, "failed to process with '%s'", pv_status_to_string_func(status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Cobra error state with '%s'.\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + pv_free_error_stack_func(message_stack); + } else { + fprintf(stderr, ".\n"); + } + exit(1); } fprintf(stdout, "%.2f ", is_voiced); diff --git a/demo/c/cobra_demo_mic.c b/demo/c/cobra_demo_mic.c index e5ddd769..68231bb0 100644 --- a/demo/c/cobra_demo_mic.c +++ b/demo/c/cobra_demo_mic.c @@ -133,6 +133,12 @@ static void print_analog(float is_voiced) { fflush(stdout); } +void print_error_message(char **message_stack, int32_t message_stack_depth) { + for (int32_t i = 0; i < message_stack_depth; i++) { + fprintf(stderr, " [%d] %s\n", i, message_stack[i]); + } +} + int picovoice_main(int argc, char *argv[]) { signal(SIGINT, interrupt_handler); @@ -220,10 +226,40 @@ int picovoice_main(int argc, char *argv[]) { exit(1); } + pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(cobra_library, "pv_get_error_stack"); + if (!pv_get_error_stack_func) { + print_dl_error("failed to load 'pv_get_error_stack_func'"); + exit(1); + } + + void (*pv_free_error_stack_func)(char **) = load_symbol(cobra_library, "pv_free_error_stack"); + if (!pv_free_error_stack_func) { + print_dl_error("failed to load 'pv_free_error_stack_func'"); + exit(1); + } + + char **message_stack = NULL; + int32_t message_stack_depth = 0; + pv_status_t error_status = PV_STATUS_RUNTIME_ERROR; + pv_cobra_t *cobra = NULL; pv_status_t cobra_status = pv_cobra_init_func(access_key, &cobra); if (cobra_status != PV_STATUS_SUCCESS) { fprintf(stderr, "failed to init with '%s'", pv_status_to_string_func(cobra_status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Cobra error state with '%s'.\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + pv_free_error_stack_func(message_stack); + } else { + fprintf(stderr, ".\n"); + } + exit(1); } @@ -265,6 +301,20 @@ int picovoice_main(int argc, char *argv[]) { cobra_status = pv_cobra_process_func(cobra, pcm, &is_voiced); if (cobra_status != PV_STATUS_SUCCESS) { fprintf(stderr, "'pv_cobra_process' failed with '%s'\n", pv_status_to_string_func(cobra_status)); + error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth); + if (error_status != PV_STATUS_SUCCESS) { + fprintf(stderr, ".\nUnable to get Cobra error state with '%s'.\n", pv_status_to_string_func(error_status)); + exit(1); + } + + if (message_stack_depth > 0) { + fprintf(stderr, ":\n"); + print_error_message(message_stack, message_stack_depth); + pv_free_error_stack_func(message_stack); + } else { + fprintf(stderr, ".\n"); + } + exit(1); }