mirror of
https://github.com/espressif/esp-sr.git
synced 2025-09-15 15:28:44 +08:00
Unify code format
This commit is contained in:
parent
51bc9f7d23
commit
556b945dde
@ -33,7 +33,7 @@ esp_err_t esp_mn_commands_free(void)
|
||||
|
||||
int esp_mn_commands_num(void)
|
||||
{
|
||||
esp_mn_node_t * t = esp_mn_root;
|
||||
esp_mn_node_t *t = esp_mn_root;
|
||||
int i = 0;
|
||||
while (t->next) {
|
||||
t = t->next;
|
||||
@ -46,7 +46,7 @@ esp_err_t esp_mn_commands_clear(void)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(NULL != esp_mn_root, ESP_ERR_INVALID_STATE, TAG, "The mn commands is not initialized");
|
||||
|
||||
esp_mn_node_t * t = esp_mn_root->next;
|
||||
esp_mn_node_t *t = esp_mn_root->next;
|
||||
while (t) {
|
||||
esp_mn_node_t *cur_node = t;
|
||||
t = t->next;
|
||||
@ -65,7 +65,9 @@ esp_err_t esp_mn_commands_add(int command_id, char *phoneme_string)
|
||||
ESP_RETURN_ON_FALSE(ESP_MN_MAX_PHRASE_NUM >= last_node_elem_num, ESP_ERR_INVALID_STATE, TAG, "The number of speech commands phrase must less than 200");
|
||||
|
||||
esp_mn_phrase_t *phrase = esp_mn_phrase_alloc(command_id, phoneme_string);
|
||||
if (phrase == NULL) return ESP_ERR_INVALID_STATE;
|
||||
if (phrase == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_mn_node_t *new_node = esp_mn_node_alloc(phrase);
|
||||
while (temp->next != NULL) {
|
||||
@ -96,14 +98,16 @@ esp_err_t esp_mn_commands_modify(char *old_phoneme_string, char *new_phoneme_str
|
||||
// replace old phrase with new phrase
|
||||
if (flag) {
|
||||
esp_mn_phrase_t *phrase = esp_mn_phrase_alloc(command_id, new_phoneme_string);
|
||||
if (phrase == NULL) return ESP_ERR_INVALID_STATE;
|
||||
if (phrase == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_mn_phrase_free(temp->phrase);
|
||||
temp->phrase = phrase;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "No such speech command: \"%s\"", old_phoneme_string);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -111,7 +115,7 @@ esp_err_t esp_mn_commands_remove(char *phoneme_string)
|
||||
{
|
||||
esp_mn_node_t *temp = esp_mn_root;
|
||||
ESP_RETURN_ON_FALSE(NULL != esp_mn_root, ESP_ERR_INVALID_STATE, TAG, "The mn commands is not initialized");
|
||||
|
||||
|
||||
// search phoneme_string to get node point
|
||||
bool flag = false;
|
||||
while (temp->next) {
|
||||
@ -138,22 +142,24 @@ esp_err_t esp_mn_commands_remove(char *phoneme_string)
|
||||
esp_mn_phrase_t *esp_mn_commands_get_from_index(int index)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(NULL != esp_mn_root, NULL, TAG, "The mn commands is not initialized");
|
||||
|
||||
// phrase index also is phrase id, which is the depth from this phrase node to root node
|
||||
|
||||
// phrase index also is phrase id, which is the depth from this phrase node to root node
|
||||
esp_mn_node_t *temp = esp_mn_root;
|
||||
for (int i=-1; i<index; i++) {
|
||||
if (temp->next == NULL) return NULL;
|
||||
for (int i = -1; i < index; i++) {
|
||||
if (temp->next == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
|
||||
return temp->phrase;
|
||||
}
|
||||
|
||||
esp_mn_phrase_t *esp_mn_commands_get_from_string(const char *phoneme_string)
|
||||
{
|
||||
ESP_RETURN_ON_FALSE(NULL != esp_mn_root, NULL, TAG, "The mn commands is not initialized");
|
||||
|
||||
// phrase index also is phrase id, which is the depth from this phrase node to root node
|
||||
|
||||
// phrase index also is phrase id, which is the depth from this phrase node to root node
|
||||
esp_mn_node_t *temp = esp_mn_root;
|
||||
while (temp->next) {
|
||||
if (strcmp(phoneme_string, temp->next->phrase->phoneme_string) == 0) {
|
||||
@ -161,7 +167,7 @@ esp_mn_phrase_t *esp_mn_commands_get_from_string(const char *phoneme_string)
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -170,8 +176,9 @@ esp_mn_error_t *esp_mn_commands_update(const esp_mn_iface_t *multinet, model_ifa
|
||||
ESP_RETURN_ON_FALSE(NULL != esp_mn_root, NULL, TAG, "The mn commands is not initialize");
|
||||
esp_mn_error_t *error = multinet->set_speech_commands(model_data, esp_mn_root);
|
||||
|
||||
if (error->num == 0)
|
||||
if (error->num == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
@ -179,7 +186,7 @@ esp_mn_error_t *esp_mn_commands_update(const esp_mn_iface_t *multinet, model_ifa
|
||||
void esp_mn_commands_print(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "---------------------SPEECH COMMANDS---------------------");
|
||||
esp_mn_node_t* temp = esp_mn_root;
|
||||
esp_mn_node_t *temp = esp_mn_root;
|
||||
int phrase_id = 0;
|
||||
while (temp->next) {
|
||||
temp = temp->next;
|
||||
@ -198,11 +205,11 @@ void *_esp_mn_calloc_(int n, int size)
|
||||
#endif
|
||||
}
|
||||
|
||||
esp_mn_phrase_t *esp_mn_phrase_alloc(int command_id, char *phoneme_string)
|
||||
esp_mn_phrase_t *esp_mn_phrase_alloc(int command_id, char *phoneme_string)
|
||||
{
|
||||
|
||||
int phoneme_string_len = strlen(phoneme_string);
|
||||
if (phoneme_string_len > ESP_MN_MAX_PHRASE_LEN || phoneme_string_len<1) {
|
||||
if (phoneme_string_len > ESP_MN_MAX_PHRASE_LEN || phoneme_string_len < 1) {
|
||||
ESP_LOGE(TAG, "The Length of \"%s\" > ESP_MN_MAX_PHRASE_LEN", phoneme_string);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -5,16 +5,14 @@
|
||||
#include "esp_partition.h"
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
int num; // the number of files
|
||||
char **files; // the model files, like wn9_index, wn9_data
|
||||
char **data; // the pointer of file data
|
||||
int *sizes; // the size of different file
|
||||
} srmodel_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
char **model_name; // the name of models, like "wn9_hilexin"(wakenet9, hilexin), "mn5_en"(multinet5, english)
|
||||
esp_partition_t *partition; // partition label used to save the files of model
|
||||
esp_partition_mmap_handle_t mmap_handle; // mmap_handle if using esp_partition_mmap else NULL; // support esp-idf v5.0
|
||||
@ -29,14 +27,14 @@ typedef struct
|
||||
* @brief Return all avaliable models in flash.
|
||||
*
|
||||
* @param partition_label The spiffs label defined in your partition file used to save models.
|
||||
*
|
||||
*
|
||||
* @return all avaliable models,save as srmodel_list_t.
|
||||
*/
|
||||
srmodel_list_t* esp_srmodel_init(const char* partition_label);
|
||||
srmodel_list_t *esp_srmodel_init(const char *partition_label);
|
||||
|
||||
/**
|
||||
* @brief Free srmodel_list_t and unregister SPIFFS filesystem if open SPIFFS filesystem.
|
||||
*
|
||||
*
|
||||
* @param models The srmodel_list_t point allocated by esp_srmodel_init function.
|
||||
*
|
||||
* @return all avaliable models in spiffs,save as srmodel_list_t.
|
||||
@ -45,12 +43,12 @@ void esp_srmodel_deinit(srmodel_list_t *models);
|
||||
|
||||
/**
|
||||
* @brief Return the first model name containing the specified keywords
|
||||
* If keyword is NULL, we will ignore the keyword.
|
||||
*
|
||||
* If keyword is NULL, we will ignore the keyword.
|
||||
*
|
||||
* @param models The srmodel_list_t point allocated by esp_srmodel_init function.
|
||||
* @param keyword1 The specified keyword1 , like ESP_WN_PREDIX(the prefix of wakenet),
|
||||
* @param keyword1 The specified keyword1 , like ESP_WN_PREDIX(the prefix of wakenet),
|
||||
* ESP_MN_PREFIX(the prefix of multinet),
|
||||
*
|
||||
*
|
||||
* @param keyword2 The specified keyword2, like ESP_MN_ENGLISH(the english multinet)
|
||||
* ESP_MN_CHINESE(the chinese multinet)
|
||||
* "alexa" (the "alexa" wakenet)
|
||||
@ -61,7 +59,7 @@ char *esp_srmodel_filter(srmodel_list_t *models, const char *keyword1, const cha
|
||||
|
||||
/**
|
||||
* @brief Check whether the specified model name exists or not.
|
||||
*
|
||||
*
|
||||
* @param models The srmodel_list_t point allocated by esp_srmodel_init function.
|
||||
* @param model_name The specified model name
|
||||
* @return return index in models if model name exists otherwise return -1
|
||||
@ -72,14 +70,14 @@ int esp_srmodel_exists(srmodel_list_t *models, char *model_name);
|
||||
* @brief Initialize and mount SPIFFS filesystem, return all avaliable models in spiffs.
|
||||
*
|
||||
* @param part The spiffs partition.
|
||||
*
|
||||
*
|
||||
* @return all avaliable models in spiffs,save as srmodel_list_t.
|
||||
*/
|
||||
srmodel_list_t* srmodel_spiffs_init(const esp_partition_t *part);
|
||||
srmodel_list_t *srmodel_spiffs_init(const esp_partition_t *part);
|
||||
|
||||
/**
|
||||
* @brief unregister SPIFFS filesystem and free srmodel_list_t.
|
||||
*
|
||||
*
|
||||
* @param models The srmodel_list_t point allocated by srmodel_spiffs_init function.
|
||||
*
|
||||
* @return all avaliable models in spiffs,save as srmodel_list_t.
|
||||
@ -95,8 +93,8 @@ void srmodel_spiffs_deinit(srmodel_list_t *models);
|
||||
char *get_model_base_path(void);
|
||||
|
||||
/**
|
||||
* @brief Return static srmodel pointer.
|
||||
* static srmodel pointer will be set after esp_srmodel_init
|
||||
* @brief Return static srmodel pointer.
|
||||
* static srmodel pointer will be set after esp_srmodel_init
|
||||
*
|
||||
* @return the pointer of srmodel_list_t
|
||||
*/
|
||||
@ -109,10 +107,10 @@ srmodel_list_t *get_static_srmodels(void);
|
||||
* @brief Return model_coeff_getter_t pointer base on model_name
|
||||
*
|
||||
* @warning Just support ESP32 to load old wakenet
|
||||
*
|
||||
*
|
||||
* @param model_name The model name
|
||||
*
|
||||
* @return model_coeff_getter_t pointer or NULL
|
||||
*/
|
||||
model_coeff_getter_t* srmodel_get_model_coeff(char *model_name);
|
||||
model_coeff_getter_t *srmodel_get_model_coeff(char *model_name);
|
||||
#endif
|
||||
207
src/model_path.c
207
src/model_path.c
@ -22,9 +22,9 @@ void set_model_base_path(const char *base_path)
|
||||
SRMODE_BASE_PATH = (char *)base_path;
|
||||
}
|
||||
|
||||
static srmodel_list_t* srmodel_list_alloc(void)
|
||||
static srmodel_list_t *srmodel_list_alloc(void)
|
||||
{
|
||||
srmodel_list_t *models = (srmodel_list_t*) malloc(sizeof(srmodel_list_t));
|
||||
srmodel_list_t *models = (srmodel_list_t *) malloc(sizeof(srmodel_list_t));
|
||||
models->model_data = NULL;
|
||||
models->model_name = NULL;
|
||||
models->num = 0;
|
||||
@ -36,10 +36,11 @@ static srmodel_list_t* srmodel_list_alloc(void)
|
||||
#ifdef ESP_PLATFORM
|
||||
srmodel_list_t *read_models_form_spiffs(esp_vfs_spiffs_conf_t *conf)
|
||||
{
|
||||
if (static_srmodels == NULL)
|
||||
if (static_srmodels == NULL) {
|
||||
static_srmodels = srmodel_list_alloc();
|
||||
else
|
||||
} else {
|
||||
return static_srmodels;
|
||||
}
|
||||
|
||||
srmodel_list_t *models = static_srmodels;
|
||||
|
||||
@ -49,20 +50,21 @@ srmodel_list_t *read_models_form_spiffs(esp_vfs_spiffs_conf_t *conf)
|
||||
int model_num = 0;
|
||||
int idx = 0;
|
||||
|
||||
if (dir != NULL)
|
||||
{
|
||||
if (dir != NULL) {
|
||||
// get the number of models
|
||||
while ((ret = readdir(dir)) != NULL)
|
||||
{ // NULL if reach the end of directory
|
||||
while ((ret = readdir(dir)) != NULL) {
|
||||
// NULL if reach the end of directory
|
||||
|
||||
if (ret->d_type == DT_DIR) // continue if d_type is not file
|
||||
if (ret->d_type == DT_DIR) { // continue if d_type is not file
|
||||
continue;
|
||||
}
|
||||
|
||||
int len = strlen(ret->d_name);
|
||||
char *suffix = ret->d_name + len - 12;
|
||||
|
||||
if (strcmp(suffix, "_MODEL_INFO_") == 0)
|
||||
if (strcmp(suffix, "_MODEL_INFO_") == 0) {
|
||||
model_num ++;
|
||||
}
|
||||
}
|
||||
|
||||
// allocate model names
|
||||
@ -70,26 +72,27 @@ srmodel_list_t *read_models_form_spiffs(esp_vfs_spiffs_conf_t *conf)
|
||||
return models;
|
||||
} else {
|
||||
models->num = model_num;
|
||||
models->model_name = malloc(models->num*sizeof(char*));
|
||||
for (int i=0; i<models->num; i++)
|
||||
models->model_name[i] = (char*) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
models->model_name = malloc(models->num * sizeof(char *));
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
models->model_name[i] = (char *) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
}
|
||||
}
|
||||
|
||||
// read & save model names
|
||||
closedir(dir);
|
||||
dir = opendir(conf->base_path);
|
||||
while ((ret = readdir(dir)) != NULL)
|
||||
{ // NULL if reach the end of directory
|
||||
while ((ret = readdir(dir)) != NULL) {
|
||||
// NULL if reach the end of directory
|
||||
|
||||
if (ret->d_type == DT_DIR) // continue if d_type is not file
|
||||
if (ret->d_type == DT_DIR) { // continue if d_type is not file
|
||||
continue;
|
||||
}
|
||||
|
||||
int len = strlen(ret->d_name);
|
||||
char *suffix = ret->d_name + len - 12;
|
||||
|
||||
if (strcmp(suffix, "_MODEL_INFO_") == 0)
|
||||
{
|
||||
memcpy(models->model_name[idx], ret->d_name, (len-13)*sizeof(char));
|
||||
if (strcmp(suffix, "_MODEL_INFO_") == 0) {
|
||||
memcpy(models->model_name[idx], ret->d_name, (len - 13)*sizeof(char));
|
||||
// models->model_name[idx][len-13] = '\0';
|
||||
idx ++;
|
||||
}
|
||||
@ -102,7 +105,7 @@ srmodel_list_t *read_models_form_spiffs(esp_vfs_spiffs_conf_t *conf)
|
||||
}
|
||||
|
||||
|
||||
srmodel_list_t* srmodel_spiffs_init(const esp_partition_t *part)
|
||||
srmodel_list_t *srmodel_spiffs_init(const esp_partition_t *part)
|
||||
{
|
||||
ESP_LOGI(TAG, "\nInitializing models from SPIFFS, partition label: %s\n", part->label);
|
||||
|
||||
@ -155,8 +158,8 @@ void srmodel_spiffs_deinit(srmodel_list_t *models)
|
||||
}
|
||||
|
||||
if (models != NULL) {
|
||||
if (models->num>0) {
|
||||
for (int i=0; i<models->num; i++) {
|
||||
if (models->num > 0) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
free(models->model_name[i]);
|
||||
}
|
||||
free(models->model_name);
|
||||
@ -169,16 +172,17 @@ void srmodel_spiffs_deinit(srmodel_list_t *models)
|
||||
|
||||
srmodel_list_t *srmodel_config_init()
|
||||
{
|
||||
if (static_srmodels == NULL)
|
||||
if (static_srmodels == NULL) {
|
||||
static_srmodels = srmodel_list_alloc();
|
||||
else
|
||||
} else {
|
||||
return static_srmodels;
|
||||
}
|
||||
|
||||
srmodel_list_t *models = static_srmodels;
|
||||
models->num = 2;
|
||||
models->model_name = malloc(models->num*sizeof(char*));
|
||||
for (int i=0; i<models->num; i++) {
|
||||
models->model_name[i] = (char*) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
models->model_name = malloc(models->num * sizeof(char *));
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
models->model_name[i] = (char *) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
}
|
||||
|
||||
// If wakenet is selected in menuconfig, load the wakenet model
|
||||
@ -194,10 +198,10 @@ srmodel_list_t *srmodel_config_init()
|
||||
models->num --;
|
||||
free(models->model_name[models->num]);
|
||||
} else {
|
||||
strcpy(models->model_name[models->num-1], MULTINET_MODEL_NAME);
|
||||
strcpy(models->model_name[models->num - 1], MULTINET_MODEL_NAME);
|
||||
}
|
||||
|
||||
// could not find any avaliable models, return NULL
|
||||
// could not find any avaliable models, return NULL
|
||||
if (models->num == 0) {
|
||||
free(models->model_name);
|
||||
free(models);
|
||||
@ -210,8 +214,8 @@ srmodel_list_t *srmodel_config_init()
|
||||
void srmodel_config_deinit(srmodel_list_t *models)
|
||||
{
|
||||
if (models != NULL) {
|
||||
if (models->num>0) {
|
||||
for (int i=0; i<models->num; i++) {
|
||||
if (models->num > 0) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
free(models->model_name[i]);
|
||||
}
|
||||
free(models->model_name);
|
||||
@ -221,58 +225,60 @@ void srmodel_config_deinit(srmodel_list_t *models)
|
||||
models = NULL;
|
||||
}
|
||||
|
||||
model_coeff_getter_t* srmodel_get_model_coeff(char *model_name)
|
||||
model_coeff_getter_t *srmodel_get_model_coeff(char *model_name)
|
||||
{
|
||||
model_coeff_getter_t *gettercb = (model_coeff_getter_t *)&WAKENET_COEFF;
|
||||
return gettercb;
|
||||
}
|
||||
|
||||
static uint32_t read_int32(char *data) {
|
||||
uint32_t value = 0;
|
||||
value |= data[0] << 0;
|
||||
value |= data[1] << 8;
|
||||
value |= data[2] << 16;
|
||||
value |= data[3] << 24;
|
||||
return value;
|
||||
static uint32_t read_int32(char *data)
|
||||
{
|
||||
uint32_t value = 0;
|
||||
value |= data[0] << 0;
|
||||
value |= data[1] << 8;
|
||||
value |= data[2] << 16;
|
||||
value |= data[3] << 24;
|
||||
return value;
|
||||
}
|
||||
|
||||
srmodel_list_t *srmodel_mmap_init(const esp_partition_t *partition)
|
||||
{
|
||||
if (static_srmodels == NULL)
|
||||
if (static_srmodels == NULL) {
|
||||
static_srmodels = srmodel_list_alloc();
|
||||
else
|
||||
} else {
|
||||
return static_srmodels;
|
||||
|
||||
}
|
||||
|
||||
srmodel_list_t *models = static_srmodels;
|
||||
const void *root;
|
||||
ESP_ERROR_CHECK(esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &root, &models->mmap_handle));
|
||||
|
||||
models->partition = (esp_partition_t *)partition;
|
||||
char *start = (char *)root;
|
||||
char *data = (char *)root;
|
||||
char *data = (char *)root;
|
||||
int str_len = SRMODEL_STRING_LENGTH;
|
||||
int int_len = 4;
|
||||
//read model number
|
||||
models->num = read_int32(data);
|
||||
data += int_len;
|
||||
models->model_data = (srmodel_data_t **)malloc(sizeof(srmodel_data_t*) * models->num);
|
||||
models->model_name = (char **)malloc(sizeof(char*) * models->num);
|
||||
models->model_data = (srmodel_data_t **)malloc(sizeof(srmodel_data_t *) * models->num);
|
||||
models->model_name = (char **)malloc(sizeof(char *) * models->num);
|
||||
|
||||
for (int i=0; i<models->num; i++) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
srmodel_data_t *model_data = (srmodel_data_t *) malloc(sizeof(srmodel_data_t));
|
||||
// read model name
|
||||
models->model_name[i] = (char*)malloc((strlen(data)+1)*sizeof(char));
|
||||
models->model_name[i] = (char *)malloc((strlen(data) + 1) * sizeof(char));
|
||||
strcpy(models->model_name[i], data);
|
||||
data += str_len;
|
||||
//read model number
|
||||
int file_num = read_int32(data);
|
||||
model_data->num = file_num;
|
||||
data += int_len;
|
||||
model_data->files = (char **) malloc(sizeof(char*)*file_num);
|
||||
model_data->data = (char **) malloc(sizeof(void*)*file_num);
|
||||
model_data->sizes = (int *) malloc(sizeof(int)*file_num);
|
||||
model_data->files = (char **) malloc(sizeof(char *)*file_num);
|
||||
model_data->data = (char **) malloc(sizeof(void *)*file_num);
|
||||
model_data->sizes = (int *) malloc(sizeof(int) * file_num);
|
||||
|
||||
for (int j=0; j<file_num; j++) {
|
||||
for (int j = 0; j < file_num; j++) {
|
||||
//read file name
|
||||
// model_data->files[j] = (char*)malloc(str_len*sizeof(char));
|
||||
// memcpy(model_data->files[j], data, str_len);
|
||||
@ -301,8 +307,8 @@ void srmodel_mmap_deinit(srmodel_list_t *models)
|
||||
esp_partition_munmap(models->mmap_handle); // support esp-idf v5.0
|
||||
// spi_flash_munmap(models->mmap_handle);
|
||||
|
||||
if (models->num>0) {
|
||||
for (int i=0; i<models->num; i++) {
|
||||
if (models->num > 0) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
free(models->model_data[i]->files);
|
||||
free(models->model_data[i]->data);
|
||||
free(models->model_data[i]->sizes);
|
||||
@ -330,33 +336,35 @@ srmodel_list_t *get_static_srmodels(void)
|
||||
return static_srmodels;
|
||||
}
|
||||
|
||||
static char* join_path(const char* dirname, const char *filename)
|
||||
static char *join_path(const char *dirname, const char *filename)
|
||||
{
|
||||
if (dirname == NULL || filename == NULL)
|
||||
if (dirname == NULL || filename == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int dirname_len = strlen(dirname);
|
||||
int filename_len = strlen(filename);
|
||||
int len = filename_len + dirname_len + 2;
|
||||
int len = filename_len + dirname_len + 2;
|
||||
char *path = calloc(len, sizeof(char));
|
||||
memcpy(path, dirname, dirname_len);
|
||||
if (dirname[dirname_len-1] == '/') {
|
||||
memcpy(path+dirname_len, filename, filename_len);
|
||||
if (dirname[dirname_len - 1] == '/') {
|
||||
memcpy(path + dirname_len, filename, filename_len);
|
||||
} else {
|
||||
path[dirname_len] = '/';
|
||||
memcpy(path+dirname_len+1, filename, filename_len);
|
||||
memcpy(path + dirname_len + 1, filename, filename_len);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// read srmodel from sdcard
|
||||
srmodel_list_t* srmodel_sdcard_init(const char *base_path)
|
||||
srmodel_list_t *srmodel_sdcard_init(const char *base_path)
|
||||
{
|
||||
printf("Initializing models from path: %s\n", base_path);
|
||||
if (static_srmodels == NULL)
|
||||
if (static_srmodels == NULL) {
|
||||
static_srmodels = srmodel_list_alloc();
|
||||
else
|
||||
} else {
|
||||
return static_srmodels;
|
||||
|
||||
}
|
||||
|
||||
srmodel_list_t *models = static_srmodels;
|
||||
set_model_base_path(base_path);
|
||||
|
||||
@ -365,13 +373,12 @@ srmodel_list_t* srmodel_sdcard_init(const char *base_path)
|
||||
dir = opendir(base_path);
|
||||
int model_num = 0;
|
||||
int idx = 0;
|
||||
FILE* fp;
|
||||
FILE *fp;
|
||||
|
||||
if (dir != NULL)
|
||||
{
|
||||
if (dir != NULL) {
|
||||
// get the number of models
|
||||
while ((ret = readdir(dir)) != NULL)
|
||||
{ // NULL if reach the end of directory
|
||||
while ((ret = readdir(dir)) != NULL) {
|
||||
// NULL if reach the end of directory
|
||||
|
||||
if (ret->d_type == DT_DIR) { // if d_type is directory
|
||||
char *sub_path = join_path(base_path, ret->d_name);
|
||||
@ -394,16 +401,17 @@ srmodel_list_t* srmodel_sdcard_init(const char *base_path)
|
||||
} else {
|
||||
models->num = model_num;
|
||||
models->partition = NULL;
|
||||
models->model_name = malloc(models->num*sizeof(char*));
|
||||
for (int i=0; i<models->num; i++)
|
||||
models->model_name[i] = (char*) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
models->model_name = malloc(models->num * sizeof(char *));
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
models->model_name[i] = (char *) calloc(MODEL_NAME_MAX_LENGTH, sizeof(char));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// read & save model names
|
||||
|
||||
// read & save model names
|
||||
dir = opendir(base_path);
|
||||
while ((ret = readdir(dir)) != NULL)
|
||||
{ // NULL if reach the end of directory
|
||||
while ((ret = readdir(dir)) != NULL) {
|
||||
// NULL if reach the end of directory
|
||||
|
||||
if (ret->d_type == DT_DIR) { // if d_type is directory
|
||||
char *sub_path = join_path(base_path, ret->d_name);
|
||||
@ -428,8 +436,8 @@ srmodel_list_t* srmodel_sdcard_init(const char *base_path)
|
||||
void srmodel_sdcard_deinit(srmodel_list_t *models)
|
||||
{
|
||||
if (models != NULL) {
|
||||
if (models->num>0) {
|
||||
for (int i=0; i<models->num; i++) {
|
||||
if (models->num > 0) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
free(models->model_name[i]);
|
||||
}
|
||||
}
|
||||
@ -440,31 +448,31 @@ void srmodel_sdcard_deinit(srmodel_list_t *models)
|
||||
|
||||
|
||||
|
||||
srmodel_list_t* esp_srmodel_init(const char* partition_label)
|
||||
srmodel_list_t *esp_srmodel_init(const char *partition_label)
|
||||
{
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
return srmodel_config_init();
|
||||
#else
|
||||
const esp_partition_t* partition = NULL;
|
||||
// find spiffs partition
|
||||
const esp_partition_t *partition = NULL;
|
||||
// find spiffs partition
|
||||
partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partition_label
|
||||
);
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partition_label
|
||||
);
|
||||
|
||||
if (partition) {
|
||||
return srmodel_mmap_init(partition);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Can not find %s in partition table", partition_label);
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
#else
|
||||
return srmodel_sdcard_init(partition_label);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void esp_srmodel_deinit(srmodel_list_t *models)
|
||||
@ -485,30 +493,39 @@ void esp_srmodel_deinit(srmodel_list_t *models)
|
||||
// repackage strstr function to support needle==NULL
|
||||
static char *esp_strstr(const char *haystack, const char *needle)
|
||||
{
|
||||
if (needle == NULL) return (char *)haystack;
|
||||
else return (char *)strstr(haystack, needle);
|
||||
if (needle == NULL) {
|
||||
return (char *)haystack;
|
||||
} else {
|
||||
return (char *)strstr(haystack, needle);
|
||||
}
|
||||
}
|
||||
|
||||
char *esp_srmodel_filter(srmodel_list_t *models, const char *keyword1, const char *keyword2)
|
||||
char *esp_srmodel_filter(srmodel_list_t *models, const char *keyword1, const char *keyword2)
|
||||
{
|
||||
if (models == NULL) return NULL;
|
||||
|
||||
if (models == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// return the first model name including specific keyword
|
||||
for (int i=0; i<models->num; i++) {
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
|
||||
if (esp_strstr(models->model_name[i], keyword1) != NULL) {
|
||||
if (esp_strstr(models->model_name[i], keyword2) != NULL)
|
||||
if (esp_strstr(models->model_name[i], keyword2) != NULL) {
|
||||
return models->model_name[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int esp_srmodel_exists(srmodel_list_t *models, char *model_name) {
|
||||
if (models == NULL) return -1;
|
||||
|
||||
for (int i=0; i<models->num; i++) {
|
||||
int esp_srmodel_exists(srmodel_list_t *models, char *model_name)
|
||||
{
|
||||
if (models == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < models->num; i++) {
|
||||
if (strcmp(models->model_name[i], model_name) == 0) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@ -34,31 +34,29 @@ TEST_CASE("multinet create/destroy API & memory leak", "[mn]")
|
||||
printf("load multinet!\n");
|
||||
model_iface_data_t *model_data = multinet->create(model_name, 6000);
|
||||
gettimeofday(&tv_end, NULL);
|
||||
int tv_ms=(tv_end.tv_sec-tv_start.tv_sec)*1000+(tv_end.tv_usec-tv_start.tv_usec)/1000;
|
||||
int tv_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
|
||||
printf("create latency:%d ms\n", tv_ms);
|
||||
|
||||
// test model memory concumption
|
||||
int create_size = start_size - heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
int create_internal_size = start_internal_size - heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
printf("Internal RAM: %d, PSRAM:%d\n", create_internal_size, create_size-create_internal_size);
|
||||
printf("Internal RAM: %d, PSRAM:%d\n", create_internal_size, create_size - create_internal_size);
|
||||
multinet->destroy(model_data);
|
||||
esp_srmodel_deinit(models);
|
||||
|
||||
// test memory leak
|
||||
int first_end_size = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
int first_end_internal_size = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
int last_end_size = first_end_size;
|
||||
int last_end_internal_size = first_end_internal_size;
|
||||
int mem_leak = start_size - last_end_size;
|
||||
printf("create&destroy times:%d, memory leak:%d\n", 1, mem_leak);
|
||||
|
||||
for (int i=0; i<5; i++) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
printf("init partition ...\n");
|
||||
models = esp_srmodel_init("model");
|
||||
model_name = esp_srmodel_filter(models, ESP_MN_PREFIX, NULL);
|
||||
multinet = esp_mn_handle_from_name(model_name);
|
||||
|
||||
int time_out = 3000+i*2000;
|
||||
int time_out = 3000 + i * 2000;
|
||||
printf("create ..., time out = %d\n", time_out);
|
||||
model_data = multinet->create(model_name, time_out);
|
||||
|
||||
@ -67,15 +65,14 @@ TEST_CASE("multinet create/destroy API & memory leak", "[mn]")
|
||||
esp_srmodel_deinit(models);
|
||||
|
||||
last_end_size = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
last_end_internal_size = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
mem_leak = start_size - last_end_size;
|
||||
printf("create&destroy times:%d, memory leak:%d\n", i+2, mem_leak);
|
||||
printf("create&destroy times:%d, memory leak:%d\n", i + 2, mem_leak);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(true, (mem_leak) < 1000 && last_end_size == first_end_size);
|
||||
}
|
||||
|
||||
TEST_CASE("multinet detect API & cpu loading", "[mn]")
|
||||
TEST_CASE("multinet detect/get_results API & cpu loading", "[mn]")
|
||||
{
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
srmodel_list_t *models = esp_srmodel_init("model");
|
||||
@ -96,20 +93,25 @@ TEST_CASE("multinet detect API & cpu loading", "[mn]")
|
||||
memset(buffer, 0, audio_chunksize);
|
||||
}
|
||||
esp_mn_state_t mn_state = multinet->detect(model_data, buffer);
|
||||
if (mn_state == ESP_MN_STATE_DETECTED) {
|
||||
esp_mn_results_t *mn_result = multinet->get_results(model_data);
|
||||
printf("Command id:%d\n", mn_result->command_id[0]);
|
||||
}
|
||||
|
||||
chunks++;
|
||||
if (chunks == 64)
|
||||
if (chunks == 64) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
gettimeofday(&tv_end, NULL);
|
||||
int tv_ms=(tv_end.tv_sec-tv_start.tv_sec)*1000+(tv_end.tv_usec-tv_start.tv_usec)/1000;
|
||||
int tv_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
|
||||
chunks -= 7;
|
||||
int run_ms = (chunks)*audio_chunksize/sizeof(int16_t)*1000/frequency;
|
||||
float cpu_loading = tv_ms*100.0/run_ms;
|
||||
printf("Done! Took %d ms to parse %d ms worth of samples in %d iterations. CPU loading(single core):%.1f%%\n",
|
||||
int run_ms = (chunks) * audio_chunksize / sizeof(int16_t) * 1000 / frequency;
|
||||
float cpu_loading = tv_ms * 100.0 / run_ms;
|
||||
printf("Done! Took %d ms to parse %d ms worth of samples in %d iterations. CPU loading(single core):%.1f%%\n",
|
||||
tv_ms, run_ms, chunks, cpu_loading);
|
||||
|
||||
|
||||
multinet->destroy(model_data);
|
||||
esp_srmodel_deinit(models);
|
||||
TEST_ASSERT_EQUAL(true, (cpu_loading<75 && tv_ms>0));
|
||||
TEST_ASSERT_EQUAL(true, (cpu_loading < 75 && tv_ms > 0));
|
||||
}
|
||||
@ -32,13 +32,13 @@ TEST_CASE("wakenet create/destroy API & memory leak", "[wn]")
|
||||
gettimeofday(&tv_start, NULL);
|
||||
model_iface_data_t *model_data = wakenet->create(model_name, DET_MODE_3CH_95);
|
||||
gettimeofday(&tv_end, NULL);
|
||||
int tv_ms=(tv_end.tv_sec-tv_start.tv_sec)*1000+(tv_end.tv_usec-tv_start.tv_usec)/1000;
|
||||
int tv_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
|
||||
printf("create latency:%d ms\n", tv_ms);
|
||||
|
||||
// test model memory concumption
|
||||
int create_size = start_size - heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
int create_internal_size = start_internal_size - heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||
printf("Internal RAM: %d, PSRAM:%d\n", create_internal_size, create_size-create_internal_size);
|
||||
printf("Internal RAM: %d, PSRAM:%d\n", create_internal_size, create_size - create_internal_size);
|
||||
wakenet->destroy(model_data);
|
||||
esp_srmodel_deinit(models);
|
||||
|
||||
@ -48,7 +48,7 @@ TEST_CASE("wakenet create/destroy API & memory leak", "[wn]")
|
||||
int mem_leak = start_size - last_end_size;
|
||||
printf("create&destroy times:%d, memory leak:%d\n", 1, mem_leak);
|
||||
|
||||
for (int i=0; i<6; i++) {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("init partition ...\n");
|
||||
models = esp_srmodel_init("model");
|
||||
model_name = esp_srmodel_filter(models, ESP_WN_PREFIX, NULL);
|
||||
@ -71,7 +71,7 @@ TEST_CASE("wakenet create/destroy API & memory leak", "[wn]")
|
||||
|
||||
last_end_size = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
mem_leak = start_size - last_end_size;
|
||||
printf("create&destroy times:%d, memory leak:%d\n", i+2, mem_leak);
|
||||
printf("create&destroy times:%d, memory leak:%d\n", i + 2, mem_leak);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(true, (mem_leak) < 1000 && last_end_size == first_end_size);
|
||||
@ -99,21 +99,23 @@ TEST_CASE("wakenet detect API & cpu loading", "[wn]")
|
||||
memset(buffer, 0, audio_chunksize);
|
||||
}
|
||||
int res = wakenet->detect(model_data, buffer);
|
||||
if (res > 0)
|
||||
if (res > 0) {
|
||||
detected = 1;
|
||||
}
|
||||
|
||||
chunks++;
|
||||
if (detected == 1)
|
||||
if (detected == 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
gettimeofday(&tv_end, NULL);
|
||||
int tv_ms=(tv_end.tv_sec-tv_start.tv_sec)*1000+(tv_end.tv_usec-tv_start.tv_usec)/1000;
|
||||
int run_ms = (chunks)*audio_chunksize/sizeof(int16_t)*1000/frequency;
|
||||
float cpu_loading = tv_ms*100.0/run_ms;
|
||||
printf("Done! Took %d ms to parse %d ms worth of samples in %d iterations. CPU loading(single core):%.1f%%\n",
|
||||
int tv_ms = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
|
||||
int run_ms = (chunks) * audio_chunksize / sizeof(int16_t) * 1000 / frequency;
|
||||
float cpu_loading = tv_ms * 100.0 / run_ms;
|
||||
printf("Done! Took %d ms to parse %d ms worth of samples in %d iterations. CPU loading(single core):%.1f%%\n",
|
||||
tv_ms, run_ms, chunks, cpu_loading);
|
||||
|
||||
|
||||
wakenet->destroy(model_data);
|
||||
esp_srmodel_deinit(models);
|
||||
TEST_ASSERT_EQUAL(true, (cpu_loading<75 && detected==1));
|
||||
TEST_ASSERT_EQUAL(true, (cpu_loading < 75 && detected == 1));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user