Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0 c #174

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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