📄 write_binhmm.c
字号:
{ HTK_HMM_Dens *d; unsigned int idx; unsigned int vid; dens_num = hmm->totalmixnum; dens_index = (HTK_HMM_Dens **)mymalloc(sizeof(HTK_HMM_Dens *) * dens_num); idx = 0; for(d = hmm->dnstart; d; d = d->next) dens_index[idx++] = d; qsort(dens_index, dens_num, sizeof(HTK_HMM_Dens *), (int (*)(const void *, const void *))qsort_dens_index); wrt(fp, &dens_num, sizeof(unsigned int), 1); for (idx = 0; idx < dens_num; idx++) { d = dens_index[idx]; wrt_str(fp, d->name); wrt(fp, &(d->meanlen), sizeof(short), 1); wrt(fp, d->mean, sizeof(VECT), d->meanlen); vid = search_vid(d->var); /* for debug */ if (d->var != vr_index[vid]) j_error("index not match!!! dens\n"); wrt(fp, &vid, sizeof(unsigned int), 1); wrt(fp, &(d->gconst), sizeof(LOGPROB), 1); } j_printf("%d gaussian densities written\n", dens_num);}/** * Binary search function to convert density pointer to a scholar ID. * * @param d [in] pointer to a mixture density * * @return the corresponding scholar ID. */static unsigned intsearch_did(HTK_HMM_Dens *d){ unsigned int left = 0; unsigned int right = dens_num - 1; unsigned int mid; while (left < right) { mid = (left + right) / 2; if (dens_index[mid] < d) { left = mid + 1; } else { right = mid; } } return(left);}/* write tmix data */static GCODEBOOK **tm_index; ///< Sorted data pointers for mapping from pointer to idstatic unsigned int tm_num; ///< Length of abovestatic unsigned int tm_idx; ///< Current index/** * Traverse callback function to store pointers in @a tm_index. * * @param p [in] pointer to the codebook data */static voidtmix_list_callback(void *p){ GCODEBOOK *tm; tm = p; tm_index[tm_idx++] = tm;}/** * qsort callback function to sort density pointers by their * address for indexing. * * @param tm1 [in] data 1 * @param tm2 [in] data 2 * * @return value required for qsort. */static intqsort_tm_index(GCODEBOOK **tm1, GCODEBOOK **tm2){ if (*tm1 > *tm2) return 1; else if (*tm1 < *tm2) return -1; else return 0;}/** * @brief Write all codebook data. * * The pointers of all codebook densities are first gathered, * sorted by the address. Then the densities are written * by the sorted order. The pointers to the lower structure (mixture etc.) * in the data are written by the corresponding scholar id. * The pointer index of this data will be used later to convert any pointer * reference to a codebook into scholar id. * * @param fp [in] file pointer * @param hmm [in] writing %HMM definition data */static voidwt_tmix(FILE *fp, HTK_HMM_INFO *hmm){ GCODEBOOK *tm; unsigned int idx; unsigned int did; int i; tm_num = hmm->codebooknum; tm_index = (GCODEBOOK **)mymalloc(sizeof(GCODEBOOK *) * tm_num); tm_idx = 0; aptree_traverse_and_do(hmm->codebook_root, tmix_list_callback); qsort(tm_index, tm_num, sizeof(GCODEBOOK *), (int (*)(const void *, const void *))qsort_tm_index); wrt(fp, &tm_num, sizeof(unsigned int), 1); for (idx = 0; idx < tm_num; idx++) { tm = tm_index[idx]; wrt_str(fp, tm->name); wrt(fp, &(tm->num), sizeof(int), 1); for(i=0;i<tm->num;i++) { if (tm->d[i] == NULL) { did = dens_num; } else { did = search_did(tm->d[i]); /* for debug */ if (tm->d[i] != dens_index[did]) j_error("index not match!!! dens\n"); } wrt(fp, &did, sizeof(unsigned int), 1); } } j_printf("%d tied-mixture codebooks written\n", tm_num);}/** * Binary search function to convert codebook pointer to a scholar ID. * * @param tm [in] pointer to a codebook * * @return the corresponding scholar ID. */static unsigned intsearch_tmid(GCODEBOOK *tm){ unsigned int left = 0; unsigned int right = tm_num - 1; unsigned int mid; while (left < right) { mid = (left + right) / 2; if (tm_index[mid] < tm) { left = mid + 1; } else { right = mid; } } return(left);}/* write state data */static HTK_HMM_State **st_index; ///< Sorted data pointers for mapping from pointer to idstatic unsigned int st_num; ///< Length of above/** * qsort callback function to sort state pointers by their * address for indexing. * * @param s1 [in] data 1 * @param s2 [in] data 2 * * @return value required for qsort. */static intqsort_st_index(HTK_HMM_State **s1, HTK_HMM_State **s2){ if (*s1 > *s2) return 1; else if (*s1 < *s2) return -1; else return 0;}/** * @brief Write all state data. * * The pointers of all states are first gathered, * sorted by the address. Then the state informations are written * by the sorted order. The pointers to the lower structure (mixture etc.) * in the data are written in a corresponding scholar id. * The pointer index of this data will be used later to convert any pointer * reference to a state data into scholar id. * * @param fp [in] file pointer * @param hmm [in] writing %HMM definition data */static voidwt_state(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_State *s; unsigned int idx; unsigned int did; int i; short dummy; st_num = hmm->totalstatenum; st_index = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * st_num); idx = 0; for(s = hmm->ststart; s; s = s->next) st_index[idx++] = s; qsort(st_index, st_num, sizeof(HTK_HMM_State *), (int (*)(const void *, const void *))qsort_st_index); wrt(fp, &st_num, sizeof(unsigned int), 1); for (idx = 0; idx < st_num; idx++) { s = st_index[idx]; wrt_str(fp, s->name); if (hmm->is_tied_mixture) { /* try tmix */ did = search_tmid((GCODEBOOK *)(s->b)); if ((GCODEBOOK *)s->b == tm_index[did]) { /* tmix */ dummy = -1; wrt(fp, &dummy, sizeof(short), 1); wrt(fp, &did, sizeof(unsigned int), 1); } else { /* tmix failed -> normal mixture */ wrt(fp, &(s->mix_num), sizeof(short), 1); for (i=0;i<s->mix_num;i++) { if (s->b[i] == NULL) { did = dens_num; } else { did = search_did(s->b[i]); if (s->b[i] != dens_index[did]) { j_error("index not match!!!"); } } wrt(fp, &did, sizeof(unsigned int), 1); } } } else { /* not tied mixture */ wrt(fp, &(s->mix_num), sizeof(short), 1); for (i=0;i<s->mix_num;i++) { if (s->b[i] == NULL) { did = dens_num; } else { did = search_did(s->b[i]); if (s->b[i] != dens_index[did]) { j_error("index not match!!!"); } } wrt(fp, &did, sizeof(unsigned int), 1); } } wrt(fp, s->bweight, sizeof(PROB), s->mix_num); } j_printf("%d states written\n", st_num);}/** * Binary search function to convert state pointer to a scholar ID. * * @param s [in] pointer to a state * * @return the corresponding scholar ID. */static unsigned intsearch_stid(HTK_HMM_State *s){ unsigned int left = 0; unsigned int right = st_num - 1; unsigned int mid; while (left < right) { mid = (left + right) / 2; if (st_index[mid] < s) { left = mid + 1; } else { right = mid; } } return(left);}/** * @brief Write all model data. * * The data of all models are written. The order is not important * at this top level, since there are no reference to this data. * The pointers to the lower structure (states, transitions, etc.) * in the data are written by the corresponding scholar id. * * @param fp [in] file pointer * @param hmm [in] writing %HMM definition data */static voidwt_data(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Data *d; unsigned int md_num; unsigned int sid, tid; int i; md_num = hmm->totalhmmnum; wrt(fp, &(md_num), sizeof(unsigned int), 1); for(d = hmm->start; d; d = d->next) { wrt_str(fp, d->name); wrt(fp, &(d->state_num), sizeof(short), 1); for (i=0;i<d->state_num;i++) { if (d->s[i] != NULL) { sid = search_stid(d->s[i]); /* for debug */ if (d->s[i] != st_index[sid]) j_error("index not match!!! data state\n"); } else { sid = hmm->totalstatenum + 1; /* error value */ } wrt(fp, &sid, sizeof(unsigned int), 1); } tid = search_trid(d->tr); /* for debug */ if (d->tr != tr_index[tid]) j_error("index not match!!! data trans\n"); wrt(fp, &tid, sizeof(unsigned int), 1); } j_printf("%d HMM model definition written\n", md_num);}/** * Top function to write %HMM definition data to a binary file. * * @param fp [in] file pointer * @param hmm [in] %HMM definition structure to be written * @param para [in] acoustic analysis parameter, or NULL if not available * * @return TRUE on success, FALSE on failure. */booleanwrite_binhmm(FILE *fp, HTK_HMM_INFO *hmm, Value *para){ /* write header */ wt_header(fp, (para ? TRUE : FALSE), hmm->variance_inversed); if (para) { /* write acoustic analysis parameter info */ wt_para(fp, para); } /* write option data */ wt_opt(fp, &(hmm->opt)); /* write type data */ wt_type(fp, hmm); /* write transition data */ wt_trans(fp, hmm); /* write variance data */ wt_var(fp, hmm); /* write density data */ wt_dens(fp, hmm); /* write tmix data */ if (hmm->is_tied_mixture) { wt_tmix(fp, hmm); } /* write state data */ wt_state(fp, hmm); /* write model data */ wt_data(fp, hmm); /* free pointer->index work area */ free(tr_index); free(vr_index); free(dens_index); if (hmm->is_tied_mixture) free(tm_index); free(st_index); return (TRUE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -