Skip to content

Commit

Permalink
v2.0 c (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
laves authored Oct 31, 2023
1 parent 708d4bb commit 1376887
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 89 deletions.
155 changes: 116 additions & 39 deletions demo/c/octopus_index_demo.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#if defined(_WIN32) || defined(_WIN64)

Expand Down Expand Up @@ -75,13 +74,19 @@ static struct option long_options[] = {
{"index_path", required_argument, NULL, 'i'},
};

void print_usage(const char *program_name) {
static void print_usage(const char *program_name) {
fprintf(
stderr,
"usage : %s -l LIBRARY_PATH -m MODEL_PATH -a ACCESS_KEY -w AUDIO_PATH -i INDEX_PATH\n",
program_name);
}

static 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 *model_path = NULL;
Expand All @@ -108,91 +113,163 @@ int picovoice_main(int argc, char *argv[]) {
index_path = optarg;
break;
default:
exit(1);
exit(EXIT_FAILURE);
}
}

if (!library_path || !model_path || !audio_path || !access_key || !index_path) {
print_usage(argv[0]);
exit(1);
exit(EXIT_FAILURE);
}

void *dl = pv_open_dl(library_path);
if (!dl) {
fprintf(stderr, "failed to open library.\n");
exit(1);
fprintf(stderr, "Failed to open library.\n");
exit(EXIT_FAILURE);
}

const char *(*pv_status_to_string_func)(pv_status_t) = pv_load_sym(dl, "pv_status_to_string");
if (!pv_status_to_string_func) {
print_dl_error("failed to load 'pv_status_to_string'");
exit(1);
print_dl_error("Failed to load 'pv_status_to_string'");
exit(EXIT_FAILURE);
}

pv_status_t (*pv_octopus_init_func)(const char *, const char *, pv_octopus_t **) = pv_load_sym(dl, "pv_octopus_init");
pv_status_t
(*pv_octopus_init_func)(const char *, const char *, pv_octopus_t **) = pv_load_sym(dl, "pv_octopus_init");
if (!pv_octopus_init_func) {
print_dl_error("failed to load 'pv_octopus_init()'.");
exit(1);
print_dl_error("Failed to load 'pv_octopus_init'.");
exit(EXIT_FAILURE);
}

void (*pv_octopus_delete_func)(pv_octopus_t *) = pv_load_sym(dl, "pv_octopus_delete");
if (!pv_octopus_delete_func) {
print_dl_error("failed to load 'pv_octopus_delete()'");
exit(1);
print_dl_error("Failed to load 'pv_octopus_delete'");
exit(EXIT_FAILURE);
}

pv_status_t (*pv_octopus_index_file_func)(pv_octopus_t *, const char *, void **, int32_t *) = NULL;
pv_status_t (*pv_octopus_index_file_size_func)(pv_octopus_t *, const char *, int32_t *) = NULL;
pv_octopus_index_file_size_func = pv_load_sym(dl, "pv_octopus_index_file_size");
if (!pv_octopus_index_file_size_func) {
print_dl_error("Failed to load 'pv_octopus_index_file_size'");
exit(EXIT_FAILURE);
}

pv_status_t (*pv_octopus_index_file_func)(pv_octopus_t *, const char *, void *) = NULL;
pv_octopus_index_file_func = pv_load_sym(dl, "pv_octopus_index_file");
if (!pv_octopus_index_file_func) {
print_dl_error("failed to load 'pv_octopus_index_file()'");
exit(1);
print_dl_error("Failed to load 'pv_octopus_index_file'");
exit(EXIT_FAILURE);
}

const char (*pv_octopus_version_func)(const pv_octopus_t *) = pv_load_sym(dl, "pv_octopus_version");
if (!pv_octopus_version_func) {
print_dl_error("failed to load 'pv_octopus_version()'");
exit(1);
print_dl_error("Failed to load 'pv_octopus_version'");
exit(EXIT_FAILURE);
}


pv_status_t (*pv_get_error_stack_func)(char ***, int32_t *) = pv_load_sym(dl, "pv_get_error_stack");
if (!pv_get_error_stack_func) {
print_dl_error("Failed to load 'pv_get_error_stack'");
exit(EXIT_FAILURE);
}

void (*pv_free_error_stack_func)(char **) = pv_load_sym(dl, "pv_free_error_stack");
if (!pv_free_error_stack_func) {
print_dl_error("Failed to load 'pv_free_error_stack'");
exit(EXIT_FAILURE);
}

pv_octopus_t *o = NULL;
pv_status_t status = pv_octopus_init_func(access_key, model_path, &o);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "failed to init with '%s'.\n", pv_status_to_string_func(status));
exit(1);
}
fprintf(stderr, "Failed to init with '%s'", pv_status_to_string_func(status));

char **message_stack = NULL;
int32_t message_stack_depth = 0;
pv_status_t error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth);
if (error_status != PV_STATUS_SUCCESS) {
fprintf(
stderr,
".\nUnable to get Octopus error state with '%s'.\n",
pv_status_to_string_func(error_status));
exit(EXIT_FAILURE);
}

double total_cpu_time_usec = 0;
if (message_stack_depth > 0) {
fprintf(stderr, ":\n");
print_error_message(message_stack, message_stack_depth);
pv_free_error_stack_func(message_stack);
}

exit(EXIT_FAILURE);
}

void *indices = NULL;
int32_t num_indices_byte = 0;
status = pv_octopus_index_file_size_func(o, audio_path, &num_indices_byte);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "Failed to get index size with '%s'", pv_status_to_string_func(status));

char **message_stack = NULL;
int32_t message_stack_depth = 0;
pv_status_t error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth);
if (error_status != PV_STATUS_SUCCESS) {
fprintf(
stderr,
".\nUnable to get Octopus error state with '%s'.\n",
pv_status_to_string_func(error_status));
exit(EXIT_FAILURE);
}

struct timeval before;
gettimeofday(&before, NULL);
if (message_stack_depth > 0) {
fprintf(stderr, ":\n");
print_error_message(message_stack, message_stack_depth);
pv_free_error_stack_func(message_stack);
}
exit(EXIT_FAILURE);
}

status = pv_octopus_index_file_func(o, audio_path, &indices, &num_indices_byte);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "failed to index with '%s'.\n", pv_status_to_string_func(status));
exit(1);
void *indices = calloc(num_indices_byte, sizeof(char));
if (!indices) {
fprintf(stderr, "Failed to allocate '%d' bytes of memory for Octopus indices.\n", num_indices_byte);
exit(EXIT_FAILURE);
}

struct timeval after;
gettimeofday(&after, NULL);
status = pv_octopus_index_file_func(o, audio_path, indices);
if (status != PV_STATUS_SUCCESS) {
fprintf(stderr, "Failed to index file with '%s'", pv_status_to_string_func(status));

char **message_stack = NULL;
int32_t message_stack_depth = 0;
pv_status_t error_status = pv_get_error_stack_func(&message_stack, &message_stack_depth);
if (error_status != PV_STATUS_SUCCESS) {
fprintf(
stderr,
".\nUnable to get Octopus error state with '%s'.\n",
pv_status_to_string_func(error_status));
exit(EXIT_FAILURE);
}

total_cpu_time_usec +=
(double) (after.tv_sec - before.tv_sec) * 1e6 + (double) (after.tv_usec - before.tv_usec);
if (message_stack_depth > 0) {
fprintf(stderr, ":\n");
print_error_message(message_stack, message_stack_depth);
pv_free_error_stack_func(message_stack);
}
exit(EXIT_FAILURE);
}

pv_octopus_delete_func(o);
pv_close_dl(dl);

FILE *f = fopen(index_path, "wb");
if (!f) {
fprintf(stderr, "failed to create index file at '%s'.\n", index_path);
exit(1);
fprintf(stderr, "Failed to create index file at '%s'.\n", index_path);
exit(EXIT_FAILURE);
}

if (fwrite(indices, 1, num_indices_byte, f) != (size_t) num_indices_byte) {
fprintf(stderr, "failed to write into index file at '%s'.\n", index_path);
exit(1);
fprintf(stderr, "Failed to write into index file at '%s'.\n", index_path);
exit(EXIT_FAILURE);
}

fclose(f);
Expand All @@ -211,7 +288,7 @@ int main(int argc, char *argv[]) {
LPWSTR *wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (wargv == NULL) {
fprintf(stderr, "CommandLineToArgvW failed\n");
exit(1);
exit(EXIT_FAILURE);
}

char *utf8_argv[argc];
Expand All @@ -221,7 +298,7 @@ int main(int argc, char *argv[]) {
int arg_chars_num = WideCharToMultiByte(CP_UTF8, UTF8_COMPOSITION_FLAG, wargv[i], NULL_TERMINATED, NULL, 0, NULL, NULL);
utf8_argv[i] = (char *) malloc(arg_chars_num * sizeof(char));
if (!utf8_argv[i]) {
fprintf(stderr, "failed to to allocate memory for converting args");
fprintf(stderr, "Failed to to allocate memory for converting args");
}
WideCharToMultiByte(CP_UTF8, UTF8_COMPOSITION_FLAG, wargv[i], NULL_TERMINATED, utf8_argv[i], arg_chars_num, NULL, NULL);
}
Expand Down
Loading

0 comments on commit 1376887

Please sign in to comment.