Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ksyeo1010 committed Nov 23, 2023
1 parent db5ec4e commit 529174a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 10 deletions.
84 changes: 79 additions & 5 deletions demo/c/cheetah_demo_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ static void print_dl_error(const char *message) {
#endif
}

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 *access_key = NULL;
const char *model_path = NULL;
Expand Down Expand Up @@ -172,19 +178,53 @@ int picovoice_main(int argc, char **argv) {
exit(1);
}

void (*pv_free_func)(void *) = load_symbol(dl_handle, "pv_free");
if (!pv_free_func) {
print_dl_error("failed to load `pv_free`");
pv_status_t (*pv_cheetah_transcript_delete_func)(char *) =
load_symbol(dl_handle, "pv_cheetah_transcript_delete");
if (!pv_cheetah_transcript_delete_func) {
print_dl_error("failed to load `pv_cheetah_transcript_delete`");
exit(1);
}

pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(dl_handle, "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(dl_handle, "pv_free_error_stack");
if (!pv_free_error_stack_func) {
print_dl_error("failed to load 'pv_free_error_stack_func'");
exit(1);
}

struct timeval before;
gettimeofday(&before, NULL);

char **message_stack = NULL;
int32_t message_stack_depth = 0;
pv_status_t error_status = PV_STATUS_RUNTIME_ERROR;

pv_cheetah_t *cheetah = NULL;
pv_status_t status = pv_cheetah_init_func(access_key, model_path, 0.f, enable_automatic_punctuation, &cheetah);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "failed to init 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}

Expand Down Expand Up @@ -238,6 +278,23 @@ int picovoice_main(int argc, char **argv) {
status = pv_cheetah_process_func(cheetah, pcm, &partial_transcript, &_);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "`pv_cheetah_process` 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}

Expand All @@ -249,7 +306,7 @@ int picovoice_main(int argc, char **argv) {

fprintf(stdout, "%s", partial_transcript);
fflush(stdout);
pv_free_func(partial_transcript);
pv_cheetah_transcript_delete_func(partial_transcript);
}

gettimeofday(&before, NULL);
Expand All @@ -258,6 +315,23 @@ int picovoice_main(int argc, char **argv) {
status = pv_cheetah_flush_func(cheetah, &final_transcript);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "`pv_cheetah_flush` 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}

Expand All @@ -266,7 +340,7 @@ int picovoice_main(int argc, char **argv) {
proc_sec += ((double) (after.tv_sec - before.tv_sec)) + (((double) (after.tv_usec - before.tv_usec)) * 1e-6);

fprintf(stdout, "%s\n", final_transcript);
pv_free_func(final_transcript);
pv_cheetah_transcript_delete_func(final_transcript);

drwav_uninit(&f);
}
Expand Down
84 changes: 79 additions & 5 deletions demo/c/cheetah_demo_mic.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ static void print_dl_error(const char *message) {
#endif
}

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]);
}
}

static void show_audio_devices(void) {
char **devices = NULL;
int32_t count = 0;
Expand Down Expand Up @@ -217,16 +223,50 @@ int picovoice_main(int argc, char *argv[]) {
exit(1);
}

void (*pv_free_func)(void *) = load_symbol(dl_handle, "pv_free");
if (!pv_free_func) {
print_dl_error("failed to load `pv_free`");
pv_status_t (*pv_cheetah_transcript_delete_func)(char *) =
load_symbol(dl_handle, "pv_cheetah_transcript_delete");
if (!pv_cheetah_transcript_delete_func) {
print_dl_error("failed to load `pv_cheetah_transcript_delete`");
exit(1);
}

pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = load_symbol(dl_handle, "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(dl_handle, "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_cheetah_t *cheetah = NULL;
pv_status_t status = pv_cheetah_init_func(access_key, model_path, endpoint_duration_sec, enable_automatic_punctuation, &cheetah);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "failed to init 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}

Expand Down Expand Up @@ -268,20 +308,54 @@ int picovoice_main(int argc, char *argv[]) {
status = pv_cheetah_process_func(cheetah, pcm, &partial_transcript, &is_endpoint);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "`pv_cheetah_process` 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}
fprintf(stdout, "%s", partial_transcript);
fflush(stdout);
pv_free_func(partial_transcript);
pv_cheetah_transcript_delete_func(partial_transcript);
if (is_endpoint) {
char *final_transcript = NULL;
status = pv_cheetah_flush_func(cheetah, &final_transcript);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "`pv_cheetah_flush` 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 Cheetah 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);
} else {
fprintf(stderr, ".\n");
}

pv_free_error_stack_func(message_stack);
exit(1);
}
fprintf(stdout, "%s\n", final_transcript);
pv_free_func(final_transcript);
pv_cheetah_transcript_delete_func(final_transcript);
}
}
fprintf(stdout, "\n");
Expand Down

0 comments on commit 529174a

Please sign in to comment.