📄 read_binhmm.c
字号:
/** * @file read_binhmm.c * * <JA> * @brief バイナリ妨及の %HMM 年盗ファイルを粕み哈む * * Julius は迫极のバイナリ妨及の %HMM 年盗ファイルをサポ〖トしていますˉ * HTKのアスキ〖妨及の %HMM 年盗ファイルからバイナリ妨及への恃垂は· * 身掳のツ〖ル mkbinhmm で乖ないますˉこのバイナリ妨及は·HTK の * バイナリ妨及とは润高垂ですので庙罢して布さいˉ * </JA> * * <EN> * @brief Read a binary %HMM definition file * * Julius supports a binary format of %HMM definition file. * The tool "mkbinhmm" can convert the ascii format HTK %HMM definition * file to this format. Please note that this binary format is * not compatible with the HTK binary format. * </EN> * * @author Akinobu LEE * @date Wed Feb 16 05:23:59 2005 * * $Revision: 1.5 $ * *//* * Copyright (c) 2003-2005 Shikano Lab., Nara Institute of Science and Technology * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology * All rights reserved */#include <sent/stddefs.h>#include <sent/htk_param.h>#include <sent/htk_hmm.h>#undef DMES /* define to enable debug message */static boolean gzfile; ///< TRUE when opened by fopen_readfile#define rdn(A,B,C,D) if (rdnfunc(A,B,C,D) == FALSE) return FALSE#define rdn_str(A,B,C) if ((C = rdn_strfunc(A,B)) == NULL) return FALSE/** * Binary read function with byte swaping (assume file is BIG ENDIAN) * * @param fp [in] file pointer * @param buf [out] read data * @param unitbyte [in] size of a unit in bytes * @param unitnum [in] number of unit to be read */static booleanrdnfunc(FILE *fp, void *buf, size_t unitbyte, int unitnum){ size_t tmp; if (unitnum == 0) return TRUE; if (gzfile) { tmp = myfread(buf, unitbyte, unitnum, fp); } else { tmp = fread(buf, unitbyte, unitnum, fp); } if (tmp < (size_t)unitnum) { jlog("Error: read_binhmm: failed to read %d bytes\n", unitbyte * unitnum); return FALSE; }#ifndef WORDS_BIGENDIAN if (unitbyte != 1) { swap_bytes(buf, unitbyte, unitnum); }#endif return TRUE;}static char buf[MAXLINELEN]; ///< Local work are for text handlingstatic char nostr = '\0';/** * Read a string till NULL. * * @param fp [in] file pointer * @param hmm [out] pointer to %HMM definition data to store the values. * * @return pointer to a newly allocated buffer that contains the read string. */static char *rdn_strfunc(FILE *fp, HTK_HMM_INFO *hmm){ int c; int len; char *p; len = 0; while ((c = gzfile ? myfgetc(fp) : fgetc(fp)) != -1) { if (len >= MAXLINELEN) { jlog("Error: read_binhmm: string len exceeded %d bytes\n", MAXLINELEN); jlog("Error: read_binhmm: please check the value of MAXLINELEN\n"); return NULL; } buf[len++] = c; if (c == '\0') break; } if (len == 0) return NULL; if (len == 1) { p = &nostr; } else { p = (char *)mybmalloc2(len, &(hmm->mroot)); strcpy(p, buf); } return(p);}static char *binhmm_header = BINHMM_HEADER; ///< Header stringstatic char *binhmm_header_v2 = BINHMM_HEADER_V2; ///< Header string for V2/** * Read acoustic analysis configration parameters from header of binary HMM. * * @param fp [in] file pointer * @param para [out] acoustic analysis configration parameters */static booleanrd_para(FILE *fp, Value *para){ short version; float dummy; /* read version */ rdn(fp, &version, sizeof(short), 1); if (version > VALUE_VERSION) { jlog("Error: read_binhmm: unknown embedded parameter format version: %d\n", version); return FALSE; } jlog("Stat: rd_para: found embedded acoutic parameter (ver.%d)\n", version); /* read parameters */ rdn(fp, &(para->smp_period), sizeof(long), 1); rdn(fp, &(para->smp_freq), sizeof(long), 1); rdn(fp, &(para->framesize), sizeof(int), 1); rdn(fp, &(para->frameshift), sizeof(int), 1); rdn(fp, &(para->preEmph), sizeof(float), 1); rdn(fp, &(para->lifter), sizeof(int), 1); rdn(fp, &(para->fbank_num), sizeof(int), 1); rdn(fp, &(para->delWin), sizeof(int), 1); rdn(fp, &(para->accWin), sizeof(int), 1); rdn(fp, &(para->silFloor), sizeof(float), 1); rdn(fp, &(para->escale), sizeof(float), 1); rdn(fp, &(para->hipass), sizeof(int), 1); rdn(fp, &(para->lopass), sizeof(int), 1); rdn(fp, &(para->enormal), sizeof(int), 1); rdn(fp, &(para->raw_e), sizeof(int), 1); if (version == 1) { /* version 1 has ss related parameters, but version 2 and later not */ /* skip ss related parameters (ss_alpha and ss_floor) */ rdn(fp, &dummy, sizeof(float), 1); rdn(fp, &dummy, sizeof(float), 1); } rdn(fp, &(para->zmeanframe), sizeof(int), 1); if (version >= 3) { rdn(fp, &(para->usepower), sizeof(int), 1); } return(TRUE);}/** * Read header string of binary HMM file. * * @param fp [in] file pointer * @param hmm [out] pointer to %HMM definition data to store the values. * @param para [out] store embedded acoustic parameters if any (V2) * @param mpdf_macro_ret [out] will be set to TRUE if the file contains mixture pdf macro defined by "~p" * * @return TRUE if a correct header was read, FALSE if header string does not * match the current version. */static booleanrd_header(FILE *fp, HTK_HMM_INFO *hmm, Value *para, boolean *mpdf_macro_ret){ char *p, *q; boolean emp, inv; rdn_str(fp, hmm, p); if (strmatch(p, binhmm_header)) { /* version 1 */ hmm->variance_inversed = FALSE; } else if (strmatch(p, binhmm_header_v2)) { /* version 2 */ emp = inv = FALSE; rdn_str(fp, hmm, q); if (*q != '\0') { while(*q == '_') { q++; switch (*q) { case BINHMM_HEADER_V2_EMBEDPARA: /* read in embedded acoutic condition parameters */ emp = TRUE; jlog("Stat: binhmm-header: analysis parameter embedded\n"); break; case BINHMM_HEADER_V2_VARINV: inv = TRUE; jlog("Stat: binhmm-header: variance inversed\n"); break; case BINHMM_HEADER_V2_MPDFMACRO: *mpdf_macro_ret = TRUE; jlog("Stat: binhmm-header: mixture PDF macro used\n"); break; default: jlog("Error: unknown format qualifier in header: \"%c\"\n", *q); return FALSE; } q++; } } if (emp) { para->loaded = 1; if (rd_para(fp, para) == FALSE) { jlog("Error: read_binhmm: failed to read embeded parameter\n"); return FALSE; } jlog("Stat: read_binhmm: has acoutic analysis configurations in its header\n"); } if (inv) { hmm->variance_inversed = TRUE; jlog("Stat: read_binhmm: has inversed variances\n"); } else { hmm->variance_inversed = FALSE; } } else { /* failed to read header */ return FALSE; } return TRUE;}/** * Read %HMM option specifications. * * @param fp [in] file pointer * @param opt [out] pointer to the %HMM option structure to hold the read * values. */static booleanrd_opt(FILE *fp, HTK_HMM_Options *opt){ rdn(fp, &(opt->stream_info.num), sizeof(short), 1); rdn(fp, opt->stream_info.vsize, sizeof(short), MAXSTREAMNUM); rdn(fp, &(opt->vec_size), sizeof(short), 1); rdn(fp, &(opt->cov_type), sizeof(short), 1); rdn(fp, &(opt->dur_type), sizeof(short), 1); rdn(fp, &(opt->param_type), sizeof(short), 1); return(TRUE);}/** * Read %HMM type of mixture tying. * * @param fp [in] file pointer * @param hmm [out] pointer to %HMM definition data to store the values. */static booleanrd_type(FILE *fp, HTK_HMM_INFO *hmm){ rdn(fp, &(hmm->is_tied_mixture), sizeof(boolean), 1); rdn(fp, &(hmm->maxmixturenum), sizeof(int), 1); return TRUE;}/* read transition data */static HTK_HMM_Trans **tr_index; ///< Map transition matrix id to its pointerstatic unsigned int tr_num; ///< Length of above/** * @brief Read a sequence of transition matrix data for @a tr_num. * * The transition matrixes are stored into @a hmm, and their pointers * are also stored in @a tr_index for later data mapping operation * from upper structure (state etc.). * * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read transitions. */static booleanrd_trans(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Trans *t; unsigned int idx; int i; PROB *atmp; char *p; rdn(fp, &tr_num, sizeof(unsigned int), 1); tr_index = (HTK_HMM_Trans **)mymalloc(sizeof(HTK_HMM_Trans *) * tr_num); hmm->trstart = NULL; hmm->tr_root = NULL; for (idx = 0; idx < tr_num; idx++) { t = (HTK_HMM_Trans *)mybmalloc2(sizeof(HTK_HMM_Trans), &(hmm->mroot)); rdn_str(fp, hmm, p); t->name = (*p == '\0') ? NULL : p; rdn(fp, &(t->statenum), sizeof(short), 1); t->a = (PROB **)mybmalloc2(sizeof(PROB *) * t->statenum, &(hmm->mroot)); atmp = (PROB *)mybmalloc2(sizeof(PROB) * t->statenum * t->statenum, &(hmm->mroot)); for (i=0;i<t->statenum;i++) { t->a[i] = &(atmp[i*t->statenum]); rdn(fp, t->a[i], sizeof(PROB), t->statenum); } trans_add(hmm, t); tr_index[idx] = t; }#ifdef DMES jlog("Stat: read_binhmm: %d transition maxtix read\n", tr_num);#endif return TRUE;}static HTK_HMM_Var **vr_index; ///< Map variance id to its pointerstatic unsigned int vr_num; ///< Length of above/** * @brief Read a sequence of variance vector for @a vr_num. * * The variance vectors are stored into @a hmm, and their pointers * are also stored in @a vr_index for later data mapping operation * from upper structure (density etc.). * * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read variance. */static booleanrd_var(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Var *v; unsigned int idx; char *p; rdn(fp, &vr_num, sizeof(unsigned int), 1); vr_index = (HTK_HMM_Var **)mymalloc(sizeof(HTK_HMM_Var *) * vr_num); hmm->vrstart = NULL; hmm->vr_root = NULL; for (idx = 0; idx < vr_num; idx++) { v = (HTK_HMM_Var *)mybmalloc2(sizeof(HTK_HMM_Var), &(hmm->mroot)); rdn_str(fp, hmm, p); v->name = (*p == '\0') ? NULL : p; rdn(fp, &(v->len), sizeof(short), 1); v->vec = (VECT *)mybmalloc2(sizeof(VECT) * v->len, &(hmm->mroot)); rdn(fp, v->vec, sizeof(VECT), v->len); vr_index[idx] = v; var_add(hmm, v); }#ifdef DMES jlog("Stat: read_binhmm: %d variance read\n", vr_num);#endif return TRUE;}/* read density data */static HTK_HMM_Dens **dens_index; ///< Map density id to its pointerstatic unsigned int dens_num; ///< Length of above/** * @brief Read a sequence of mixture densities for @a dens_num. * * The mixture densities are stored into @a hmm, and their references * to lower structure (variance etc.) are recovered from the id-to-pointer * index. Their pointers are also stored in @a dens_index for * later data mapping operation from upper structure (state etc.). * * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read densities. */static booleanrd_dens(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Dens *d; unsigned int idx; unsigned int vid; char *p; rdn(fp, &dens_num, sizeof(unsigned int), 1); hmm->totalmixnum = dens_num; dens_index = (HTK_HMM_Dens **)mymalloc(sizeof(HTK_HMM_Dens *) * dens_num); hmm->dnstart = NULL; hmm->dn_root = NULL; for (idx = 0; idx < dens_num; idx++) { d = (HTK_HMM_Dens *)mybmalloc2(sizeof(HTK_HMM_Dens), &(hmm->mroot)); rdn_str(fp, hmm, p); d->name = (*p == '\0') ? NULL : p; rdn(fp, &(d->meanlen), sizeof(short), 1); d->mean = (VECT *)mybmalloc2(sizeof(VECT) * d->meanlen, &(hmm->mroot)); rdn(fp, d->mean, sizeof(VECT), d->meanlen); rdn(fp, &vid, sizeof(unsigned int), 1); d->var = vr_index[vid]; rdn(fp, &(d->gconst), sizeof(LOGPROB), 1); dens_index[idx] = d; dens_add(hmm, d); }#ifdef DMES jlog("Stat: read_binhmm: %d gaussian densities read\n", dens_num);#endif return TRUE;}/* read stream weight data */static HTK_HMM_StreamWeight **streamweight_index; ///< Map stream weights id to its pointerstatic unsigned int streamweight_num; ///< Length of above/** * @brief Read a sequence of stream weights for @a streamweight_num. * * The stream weights are stored into @a hmm, and their references * to lower structure (variance etc.) are recovered from the id-to-pointer * index. Their pointers are also stored in @a dens_index for * later data mapping operation from upper structure (state etc.). * * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read stream weights. */static booleanrd_streamweight(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_StreamWeight *sw; unsigned int idx; char *p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -