Skip to content

Commit

Permalink
update c demos
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Oct 24, 2023
1 parent caf42d7 commit 9403ceb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
52 changes: 51 additions & 1 deletion demo/c/cobra_demo_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions demo/c/cobra_demo_mic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 9403ceb

Please sign in to comment.