📄 read_binhmm.c
字号:
/** * @file read_binhmm.c * @author Akinobu LEE * @date Wed Feb 16 05:23:59 2005 * * <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> * * $Revision: 1.8 $ * *//* * Copyright (c) 2003-2005 Shikano Lab., Nara Institute of Science and Technology * Copyright (c) 2005-2006 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/** * 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 voidrdn(FILE *fp, void *buf, size_t unitbyte, int unitnum){ size_t tmp; if (gzfile) { tmp = myfread(buf, unitbyte, unitnum, fp); } else { tmp = fread(buf, unitbyte, unitnum, fp); } if (tmp < (size_t)unitnum) { perror("ngram_read_bin"); j_error("read failed\n"); }#ifndef WORDS_BIGENDIAN if (unitbyte != 1) { swap_bytes(buf, unitbyte, unitnum); }#endif}static char buf[MAXLINELEN]; ///< Local work are for text handling/** * 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_str(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) j_error("Error: string len exceeded %d bytes\n", len); buf[len++] = c; if (c == '\0') break; } if (len == 1) { p = NULL; } 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 voidrd_para(FILE *fp, Value *para){ short version; rdn(fp, &version, sizeof(short), 1); switch(version) { case 1: 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); rdn(fp, &(para->ss_alpha), sizeof(float), 1); rdn(fp, &(para->ss_floor), sizeof(float), 1); rdn(fp, &(para->zmeanframe), sizeof(int), 1); break; default: j_error("Error: read_binhmm: unknown embedded parameter format version: %d\n", version); }}/** * 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) * * @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){ char *p, *q; boolean emp, inv; p = rdn_str(fp, hmm); if (strmatch(p, binhmm_header)) { /* version 1 */ hmm->variance_inversed = FALSE; } else if (strmatch(p, binhmm_header_v2)) { /* version 2 */ emp = inv = FALSE; q = rdn_str(fp, hmm); if (q != NULL) { while(*q == '_') { q++; switch (*q) { case BINHMM_HEADER_V2_EMBEDPARA: /* read in embedded acoutic condition parameters */ emp = TRUE; break; case BINHMM_HEADER_V2_VARINV: inv = TRUE; break; } q++; } } if (emp) { para->loaded = 1; rd_para(fp, para); j_printerr("(acoutic analysis conf embedded)..."); } if (inv) { hmm->variance_inversed = TRUE; j_printerr("(varinv)..."); } 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 voidrd_opt(FILE *fp, HTK_HMM_Options *opt){ rdn(fp, &(opt->stream_info.num), sizeof(short), 1); rdn(fp, opt->stream_info.vsize, sizeof(short), 50); 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);}/** * Read %HMM type of mixture tying. * * @param fp [in] file pointer * @param hmm [out] pointer to %HMM definition data to store the values. */static voidrd_type(FILE *fp, HTK_HMM_INFO *hmm){ rdn(fp, &(hmm->is_tied_mixture), sizeof(boolean), 1); rdn(fp, &(hmm->maxmixturenum), sizeof(int), 1);}/* 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 voidrd_trans(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Trans *t; unsigned int idx; int i; PROB *atmp; 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)); t->name = rdn_str(fp, hmm); 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 j_printf("%d transition maxtix read\n", tr_num);#endif}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 voidrd_var(FILE *fp, HTK_HMM_INFO *hmm){ HTK_HMM_Var *v; unsigned int idx; rdn(fp, &vr_num, sizeof(unsigned int), 1); vr_index = (HTK_HMM_Var **)mymalloc(sizeof(HTK_HMM_Var *) * vr_num); hmm->vrstart = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -