/* HTS_Stream_load_pdf_and_tree: load PDFs and trees */ static void HTS_Stream_load_pdf_and_tree(HTS_Stream * stream, FILE ** pdf_fp, FILE ** tree_fp, HTS_Boolean msd_flag, int interpolation_size) { int i; /* initialize */ stream->msd_flag = msd_flag; stream->interpolation_size = interpolation_size; stream->model = (HTS_Model *) HTS_calloc(interpolation_size, sizeof(HTS_Model)); /* load */ for (i = 0; i < stream->interpolation_size; i++) { if (!pdf_fp[i]) HTS_error(1, "HTS_Stream_load_pdf_and_tree: File for duration PDFs is not specified.\n"); if (!tree_fp[i]) HTS_error(1, "HTS_Stream_load_pdf_and_tree: File for duration trees is not specified.\n"); HTS_Model_initialize(&stream->model[i]); HTS_Model_load_tree(&stream->model[i], tree_fp[i]); HTS_Model_load_pdf(&stream->model[i], pdf_fp[i], stream->model[i].ntree, stream->msd_flag); } /* check */ for (i = 1; i < stream->interpolation_size; i++) if (stream->model[0].vector_length != stream->model[i].vector_length) HTS_error(1, "HTS_Stream_load_pdf_and_tree: Vector sizes of state output vectors are different in between given modelsets.\n"); /* set */ stream->vector_length = stream->model[0].vector_length; }
/* HTS_Model_clear: free pdfs and trees */ static void HTS_Model_clear(HTS_Model * model) { int i, j; HTS_Question *question, *next_question; HTS_Tree *tree, *next_tree; for (question = model->question; question; question = next_question) { next_question = question->next; HTS_Question_clear(question); HTS_free(question); } for (tree = model->tree; tree; tree = next_tree) { next_tree = tree->next; HTS_Tree_clear(tree); HTS_free(tree); } if (model->pdf) { for (i = 2; i <= model->ntree + 1; i++) { for (j = 1; j <= model->npdf[i]; j++) { HTS_free(model->pdf[i][j]); } model->pdf[i]++; HTS_free(model->pdf[i]); } model->pdf += 2; HTS_free(model->pdf); } if (model->npdf) { model->npdf += 2; HTS_free(model->npdf); } HTS_Model_initialize(model); }
/* HTS_Stream_load_pdf: load pdf */ static HTS_Boolean HTS_Stream_load_pdf(HTS_Stream * stream, HTS_File ** fp, int ntree, int interpolation_size) { int i; HTS_Boolean result = TRUE; /* initialize */ stream->interpolation_size = interpolation_size; stream->model = (HTS_Model *) HTS_calloc(interpolation_size, sizeof(HTS_Model)); /* load pdfs */ for (i = 0; i < stream->interpolation_size; i++) { HTS_Model_initialize(&stream->model[i]); if (HTS_Model_load_pdf(&stream->model[i], fp[i], ntree, &stream->msd_flag) == FALSE) result = FALSE; } if (result == FALSE) { HTS_Stream_clear(stream); return FALSE; } /* check */ for (i = 1; i < stream->interpolation_size; i++) { if (stream->model[0].vector_length != stream->model[1].vector_length) { HTS_error(1, "HTS_Stream_load_pdf: # of states are different in between given modelsets.\n"); HTS_Stream_clear(stream); return FALSE; } } /* set */ stream->vector_length = stream->model[0].vector_length; return TRUE; }
/* HTS_ModelSet_initialize: initialize model set */ void HTS_ModelSet_initialize(HTS_ModelSet * ms, int nstream) { HTS_Stream_initialize(&ms->duration); ms->stream = NULL; ms->gv = NULL; HTS_Model_initialize(&ms->gv_switch); ms->nstate = -1; ms->nstream = nstream; }
/* HTS_Stream_load_pdf_and_tree: load PDFs and trees */ static HTS_Boolean HTS_Stream_load_pdf_and_tree(HTS_Stream * stream, HTS_File ** pdf_fp, HTS_File ** tree_fp, int interpolation_size) { int i; HTS_Boolean result = TRUE; /* initialize */ stream->interpolation_size = interpolation_size; stream->model = (HTS_Model *) HTS_calloc(interpolation_size, sizeof(HTS_Model)); /* load */ for (i = 0; i < stream->interpolation_size; i++) { if (!pdf_fp[i]) { HTS_error(1, "HTS_Stream_load_pdf_and_tree: File for duration PDFs is not specified.\n"); HTS_Stream_clear(stream); return FALSE; } if (!tree_fp[i]) { HTS_error(1, "HTS_Stream_load_pdf_and_tree: File for duration trees is not specified.\n"); HTS_Stream_clear(stream); return FALSE; } HTS_Model_initialize(&stream->model[i]); result = HTS_Model_load_tree(&stream->model[i], tree_fp[i]); if (result == FALSE) { HTS_Stream_clear(stream); return FALSE; } result = HTS_Model_load_pdf(&stream->model[i], pdf_fp[i], stream->model[i].ntree, &stream->msd_flag); if (result == FALSE) { HTS_Stream_clear(stream); return FALSE; } } /* check */ for (i = 1; i < stream->interpolation_size; i++) if (stream->model[0].vector_length != stream->model[i].vector_length) { HTS_error(1, "HTS_Stream_load_pdf_and_tree: Vector sizes of state output vectors are different in between given modelsets.\n"); HTS_Stream_clear(stream); return FALSE; } /* set */ stream->vector_length = stream->model[0].vector_length; return TRUE; }
/* HTS_Stream_load_pdf: load pdf */ static void HTS_Stream_load_pdf(HTS_Stream * stream, FILE ** fp, int ntree, HTS_Boolean msd_flag, int interpolation_size) { int i; /* initialize */ stream->msd_flag = msd_flag; stream->interpolation_size = interpolation_size; stream->model = (HTS_Model *) HTS_calloc(interpolation_size, sizeof(HTS_Model)); /* load pdfs */ for (i = 0; i < stream->interpolation_size; i++) { HTS_Model_initialize(&stream->model[i]); HTS_Model_load_pdf(&stream->model[i], fp[i], ntree, stream->msd_flag); } /* check */ for (i = 1; i < stream->interpolation_size; i++) if (stream->model[0].vector_length != stream->model[1].vector_length) HTS_error(1, "HTS_Stream_load_pdf: # of states are different in between given modelsets.\n"); /* set */ stream->vector_length = stream->model[0].vector_length; }
/* HTS_Model_load_pdf: load pdfs */ static HTS_Boolean HTS_Model_load_pdf(HTS_Model * model, HTS_File * fp, int ntree, HTS_Boolean * msd_flag) { int i, j, k, l, m; float temp; int ssize; HTS_Boolean result = TRUE; /* check */ if (model == NULL || fp == NULL || ntree <= 0) { HTS_error(1, "HTS_Model_load_pdf: File for pdfs is not specified.\n"); return FALSE; } /* load pdf */ model->ntree = ntree; /* read MSD flag */ HTS_fread_big_endian(&i, sizeof(int), 1, fp); *msd_flag = i; if ((*msd_flag != FALSE) && (*msd_flag != TRUE)) { HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n"); HTS_Model_initialize(model); return FALSE; } /* read stream size */ HTS_fread_big_endian(&ssize, sizeof(int), 1, fp); if (ssize < 1) { HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n"); HTS_Model_initialize(model); return FALSE; } /* read vector size */ HTS_fread_big_endian(&model->vector_length, sizeof(int), 1, fp); if (model->vector_length <= 0) { HTS_error(1, "HTS_Model_load_pdf: # of HMM states %d should be positive.\n", model->vector_length); HTS_Model_initialize(model); return FALSE; } model->npdf = (int *) HTS_calloc(ntree, sizeof(int)); model->npdf -= 2; /* read the number of pdfs */ if (HTS_fread_big_endian(&model->npdf[2], sizeof(int), ntree, fp) != ntree) result = FALSE; for (i = 2; i <= ntree + 1; i++) if (model->npdf[i] <= 0) { HTS_error(1, "HTS_Model_load_pdf: # of pdfs at %d-th state should be positive.\n", i); result = FALSE; break; } if (result == FALSE) { model->npdf += 2; free(model->npdf); HTS_Model_initialize(model); return FALSE; } model->pdf = (double ***) HTS_calloc(ntree, sizeof(double **)); model->pdf -= 2; /* read means and variances */ if (*msd_flag) { /* for MSD */ for (j = 2; j <= ntree + 1; j++) { model->pdf[j] = (double **) HTS_calloc(model->npdf[j], sizeof(double *)); model->pdf[j]--; for (k = 1; k <= model->npdf[j]; k++) { model->pdf[j][k] = (double *) HTS_calloc(2 * model->vector_length + 1, sizeof(double)); for (l = 0; l < ssize; l++) { for (m = 0; m < model->vector_length / ssize; m++) { if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; model->pdf[j][k][l * model->vector_length / ssize + m] = (double) temp; if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; model->pdf[j][k][l * model->vector_length / ssize + m + model->vector_length] = (double) temp; } if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; if (l == 0) { if (temp < 0.0 || temp > 1.0) { HTS_error(1, "HTS_Model_load_pdf: MSD weight should be within 0.0 to 1.0.\n"); result = FALSE; } model->pdf[j][k][2 * model->vector_length] = (double) temp; } if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; } } } } else { /* for non MSD */ for (j = 2; j <= ntree + 1; j++) { model->pdf[j] = (double **) HTS_calloc(model->npdf[j], sizeof(double *)); model->pdf[j]--; for (k = 1; k <= model->npdf[j]; k++) { model->pdf[j][k] = (double *) HTS_calloc(2 * model->vector_length, sizeof(double)); for (l = 0; l < model->vector_length; l++) { if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; model->pdf[j][k][l] = (double) temp; if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1) result = FALSE; model->pdf[j][k][l + model->vector_length] = (double) temp; } } } } if (result == FALSE) { HTS_Model_clear(model); return FALSE; } return TRUE; }