Skip to content

Commit

Permalink
v3.0 c (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 authored Oct 24, 2023
1 parent f4f1c41 commit dc56a67
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
50 changes: 49 additions & 1 deletion demo/c/picovoice_demo_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ void print_usage(const char *program_name) {
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;
Expand Down Expand Up @@ -261,6 +267,22 @@ int picovoice_main(int argc, char *argv[]) {
exit(1);
}

pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(picovoice_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(picovoice_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;

drwav f;

#if defined(_WIN32) || defined(_WIN64)
Expand Down Expand Up @@ -318,6 +340,19 @@ int picovoice_main(int argc, char *argv[]) {
&handle);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "'pv_picovoice_init' failed with '%s'\n", 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 Rhino 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);
exit(1);
}

Expand All @@ -333,7 +368,20 @@ int picovoice_main(int argc, char *argv[]) {

status = pv_picovoice_process_func(handle, pcm);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "'pv_picovoice_process' failed with '%s'\n", pv_status_to_string_func(status));
fprintf(stderr, "'pv_picovoice_process' failed 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 Rhino 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);
exit(1);
}

Expand Down
52 changes: 50 additions & 2 deletions demo/c/picovoice_demo_mic.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ static void inference_callback(pv_inference_t *inference) {
pv_inference_delete_func(inference);
}

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);
Expand Down Expand Up @@ -291,6 +297,22 @@ int picovoice_main(int argc, char *argv[]) {
exit(1);
}

pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(picovoice_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(picovoice_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;

fprintf(stdout, "%s\n", access_key);
fprintf(stdout, "%s\n", library_path);
fprintf(stdout, "%s\n", keyword_path);
Expand All @@ -312,7 +334,20 @@ int picovoice_main(int argc, char *argv[]) {
inference_callback,
&picovoice);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "'pv_picovoice_init' failed with '%s'\n", pv_status_to_string_func(status));
fprintf(stderr, "'pv_picovoice_init' failed 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 Rhino 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);
exit(1);
}

Expand Down Expand Up @@ -354,7 +389,20 @@ int picovoice_main(int argc, char *argv[]) {

status = pv_picovoice_process_func(picovoice, pcm);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "'pv_picovoice_process' failed with '%s'\n", pv_status_to_string_func(status));
fprintf(stderr, "'pv_picovoice_process' failed 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 Rhino 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);
exit(1);
}
}
Expand Down

0 comments on commit dc56a67

Please sign in to comment.