⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read_binhmm.c

📁 julius version 4.12.about sound recognition.
💻 C
📖 第 1 页 / 共 2 页
字号:
  rdn(fp, &streamweight_num, sizeof(unsigned int), 1);  streamweight_index = (HTK_HMM_StreamWeight **)mymalloc(sizeof(HTK_HMM_StreamWeight *) * streamweight_num);  hmm->swstart = NULL;  hmm->sw_root = NULL;  for (idx = 0; idx < streamweight_num; idx++) {    sw = (HTK_HMM_StreamWeight *)mybmalloc2(sizeof(HTK_HMM_StreamWeight), &(hmm->mroot));    rdn_str(fp, hmm, p);    sw->name = (*p == '\0') ? NULL : p;    rdn(fp, &(sw->len), sizeof(short), 1);    sw->weight = (VECT *)mybmalloc2(sizeof(VECT) * sw->len, &(hmm->mroot));    rdn(fp, sw->weight, sizeof(VECT), sw->len);    streamweight_index[idx] = sw;    sw_add(hmm, sw);  }#ifdef DMES  jlog("Stat: read_binhmm: %d stream weights read\n", streamweight_num);#endif  return TRUE;}/* read tmix data */static GCODEBOOK **tm_index;	///< Map codebook id to its pointerstatic unsigned int tm_num;	///< Length of above/**  * @brief  Read a sequence of mixture codebook for @a tm_num. * * The mixture codebook data are stored into @a hmm, and their references * to lower structure (mixtures etc.) are recovered from the id-to-pointer * index.  Their pointers are also stored in @a tm_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 codebooks. */static booleanrd_tmix(FILE *fp, HTK_HMM_INFO *hmm){  GCODEBOOK *tm;  unsigned int idx;  unsigned int did;  int i;  char *p;  rdn(fp, &tm_num, sizeof(unsigned int), 1);  hmm->codebooknum = tm_num;  tm_index = (GCODEBOOK **)mymalloc(sizeof(GCODEBOOK *) * tm_num);  hmm->maxcodebooksize = 0;  hmm->codebook_root = NULL;  for (idx = 0; idx < tm_num; idx++) {    tm = (GCODEBOOK *)mybmalloc2(sizeof(GCODEBOOK), &(hmm->mroot));    rdn_str(fp, hmm, p);    tm->name = (*p == '\0') ? NULL : p;    rdn(fp, &(tm->num), sizeof(int), 1);    if (hmm->maxcodebooksize < tm->num) hmm->maxcodebooksize = tm->num;    tm->d = (HTK_HMM_Dens **)mybmalloc2(sizeof(HTK_HMM_Dens *) * tm->num, &(hmm->mroot));    for(i=0;i<tm->num;i++) {      rdn(fp, &did, sizeof(unsigned int), 1);      if (did >= dens_num) {	tm->d[i] = NULL;      } else {	tm->d[i] = dens_index[did];      }    }    tm->id = idx;    tm_index[idx] = tm;    codebook_add(hmm, tm);  }#ifdef DMES  jlog("Stat: read_binhmm: %d tied-mixture codebooks read\n", tm_num);#endif    return TRUE;}/* read mpdf data */static HTK_HMM_PDF **mpdf_index; ///< Map mixture pdf id to its pointerstatic unsigned int mpdf_num;	///< Length of above/** * Read a mixture PDF. *  * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read codebooks. * @param m [out] pointer where to store the input mixture PDF. *  * @return TRUE on success, FALSE on error. *  */static booleanrd_pdf_sub(FILE *fp, HTK_HMM_INFO *hmm, HTK_HMM_PDF *m){  int i;  unsigned int did;  rdn(fp, &(m->mix_num), sizeof(short), 1);  if (m->mix_num == -1) {    /* tmix */    rdn(fp, &did, sizeof(unsigned int), 1);    m->b = (HTK_HMM_Dens **)tm_index[did];    m->mix_num = (tm_index[did])->num;    m->tmix = TRUE;  } else {    /* mixture */    m->b = (HTK_HMM_Dens **)mybmalloc2(sizeof(HTK_HMM_Dens *) * m->mix_num, &(hmm->mroot));    for (i=0;i<m->mix_num;i++) {      rdn(fp, &did, sizeof(unsigned int), 1);      if (did >= dens_num) {	m->b[i] = NULL;      } else {	m->b[i] = dens_index[did];      }    }    m->tmix = FALSE;  }  m->bweight = (PROB *)mybmalloc2(sizeof(PROB) * m->mix_num, &(hmm->mroot));  rdn(fp, m->bweight, sizeof(PROB), m->mix_num);  return TRUE;}/**  * @brief  Read a sequence of mixture pdf for @a mpdf_num. * * The mixture pdfs 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 mpdf_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 data. */static booleanrd_mpdf(FILE *fp, HTK_HMM_INFO *hmm){  HTK_HMM_PDF *m;  unsigned int idx;  char *p;  rdn(fp, &mpdf_num, sizeof(unsigned int), 1);  mpdf_index = (HTK_HMM_PDF **)mymalloc(sizeof(HTK_HMM_PDF *) * mpdf_num);  hmm->pdfstart = NULL;  hmm->pdf_root = NULL;  for (idx = 0; idx < mpdf_num; idx++) {    m = (HTK_HMM_PDF *)mybmalloc2(sizeof(HTK_HMM_PDF), &(hmm->mroot));    rdn_str(fp, hmm, p);    m->name = (*p == '\0') ? NULL : p;    rdn(fp, &(m->stream_id), sizeof(short), 1);    if (rd_pdf_sub(fp, hmm, m) == FALSE) return FALSE;    mpdf_index[idx] = m;    mpdf_add(hmm, m);  }#ifdef DMES  jlog("Stat: read_binhmm: %d mixture PDFs read\n", mpdf_num);#endif  return TRUE;}/* read state data */static HTK_HMM_State **st_index; ///< Map state id to its pointerstatic unsigned int st_num;	///< Length of above/**  * @brief  Read a sequence of state data for @a st_num. * * The state data are stored into @a hmm, and their references * to lower structure (mixture, codebook, etc.) are recovered * from the id-to-pointer index.  Their pointers are also stored * in @a st_index for later data mapping operation from * upper structure (models etc.). *  * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read states. * @param mpdf_macro [in] TRUE if mixture pdfs are already read separatedly, or FALSE if they are all defined in-line */static booleanrd_state(FILE *fp, HTK_HMM_INFO *hmm, boolean mpdf_macro){  HTK_HMM_State *s;  unsigned int idx;  unsigned int mid, swid;  int m;  char *buf;  rdn(fp, &st_num, sizeof(unsigned int), 1);  hmm->totalstatenum = st_num;  st_index = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * st_num);  hmm->ststart = NULL;  hmm->st_root = NULL;  for (idx = 0; idx < st_num; idx++) {    s = (HTK_HMM_State *)mybmalloc2(sizeof(HTK_HMM_State), &(hmm->mroot));    rdn_str(fp, hmm, buf);    s->name = (*buf == '\0') ? NULL : buf;    s->nstream = hmm->opt.stream_info.num;    s->pdf = (HTK_HMM_PDF **)mybmalloc2(sizeof(HTK_HMM_PDF *) * s->nstream, &(hmm->mroot));    if (mpdf_macro) {      /* mpdf are stored separatedly, so read index */      for(m=0;m<s->nstream;m++) {	rdn(fp, &mid, sizeof(unsigned int), 1);	if (mid >= mpdf_num) {	  s->pdf[m] = NULL;	} else {	  s->pdf[m] = mpdf_index[mid];	}      }    } else {      /* mpdf are stored sequencially, so read the content here */      for(m=0;m<s->nstream;m++) {	s->pdf[m] = (HTK_HMM_PDF *)mybmalloc2(sizeof(HTK_HMM_PDF), &(hmm->mroot));	s->pdf[m]->name = NULL;	if (rd_pdf_sub(fp, hmm, s->pdf[m]) == FALSE) return FALSE;	s->pdf[m]->stream_id = m;	mpdf_add(hmm, s->pdf[m]);      }    }    if (hmm->opt.stream_info.num > 1) {      /* read steam weight info */      rdn(fp, &swid, sizeof(unsigned int), 1);      if (swid >= streamweight_num) {	s->w = NULL;      } else {	s->w = streamweight_index[swid];      }    } else {      s->w = NULL;    }    s->id = idx;    st_index[idx] = s;    state_add(hmm, s);  }#ifdef DMES  jlog("Stat: read_binhmm: %d states read\n", st_num);#endif  return TRUE;}/**  * @brief  Read a sequence of %HMM models. * * The models are stored into @a hmm.  Their references * to lower structures (state, transition, etc.) are stored in schalar * ID, and are recovered from the previously built id-to-pointer index. * when reading the sub structures. *  * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read models. */static booleanrd_data(FILE *fp, HTK_HMM_INFO *hmm){  HTK_HMM_Data *d;  unsigned int md_num;  unsigned int sid, tid;  unsigned int idx;  int i;  char *p;  rdn(fp, &(md_num), sizeof(unsigned int), 1);  hmm->totalhmmnum = md_num;  hmm->start = NULL;  hmm->physical_root = NULL;  for (idx = 0; idx < md_num; idx++) {    d = (HTK_HMM_Data *)mybmalloc2(sizeof(HTK_HMM_Data), &(hmm->mroot));    rdn_str(fp, hmm, p);    d->name = (*p == '\0') ? NULL : p;    rdn(fp, &(d->state_num), sizeof(short), 1);    d->s = (HTK_HMM_State **)mybmalloc2(sizeof(HTK_HMM_State *) * d->state_num, &(hmm->mroot));    for (i=0;i<d->state_num;i++) {      rdn(fp, &sid, sizeof(unsigned int), 1);      if (sid > (unsigned int)hmm->totalstatenum) {	d->s[i] = NULL;      } else {	d->s[i] = st_index[sid];      }    }    rdn(fp, &tid, sizeof(unsigned int), 1);    d->tr = tr_index[tid];    htk_hmmdata_add(hmm, d);  }#ifdef DMES  jlog("Stat: read_binhmm: %d HMM model definition read\n", md_num);#endif  return TRUE;}/**  * Top function to read a binary %HMM file from @a fp. *  * @param fp [in] file pointer * @param hmm [out] %HMM definition structure to hold the read models. * @param gzfile_p [in] TRUE if the file pointer points to a gzip file * @param para [out] store acoustic parameters if embedded in binhmm (V2) *  * @return TRUE on success, FALSE on failure. */booleanread_binhmm(FILE *fp, HTK_HMM_INFO *hmm, boolean gzfile_p, Value *para){  boolean mpdf_macro = FALSE;  gzfile = gzfile_p;  /* read header */  if (rd_header(fp, hmm, para, &mpdf_macro) == FALSE) {    return FALSE;  }  jlog("Stat: read_binhmm: binary format HMM definition\n");    /* read option data */  if (rd_opt(fp, &(hmm->opt)) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM options\n");    return FALSE;  }  /* read type data */  if (rd_type(fp, hmm) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM type of mixture tying\n");    return FALSE;  }  /* read transition data */  if (rd_trans(fp, hmm) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM transition data\n");    return FALSE;  }  /* read variance data */  if (rd_var(fp, hmm) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM variance data\n");    return FALSE;  }  /* read density data */  if (rd_dens(fp, hmm) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM density data\n");    return FALSE;  }  /* read stream weight data */  if (hmm->opt.stream_info.num > 1) {    if (rd_streamweight(fp, hmm) == FALSE) {      jlog("Error: read_binhmm: failed to read stream weights data\n");      return FALSE;    }  }  /* read tmix data */  if (hmm->is_tied_mixture) {    if (rd_tmix(fp, hmm) == FALSE) {      jlog("Error: read_binhmm: failed to read HMM tied-mixture codebook data\n");      return FALSE;    }  }  /* read mixture pdf data */  if (mpdf_macro) {    if (rd_mpdf(fp, hmm) == FALSE) {      jlog("Error: read_binhmm: failed to read mixture PDF data\n");      return FALSE;    }  }  /* read state data */  if (rd_state(fp, hmm, mpdf_macro) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM state data\n");    return FALSE;  }  /* read model data */  if (rd_data(fp, hmm) == FALSE) {    jlog("Error: read_binhmm: failed to read HMM data\n");    return FALSE;  }  /* free pointer->index work area */  if (mpdf_macro) free(mpdf_index);  free(tr_index);  free(vr_index);  if (hmm->opt.stream_info.num > 1) free(streamweight_index);  free(dens_index);  if (hmm->is_tied_mixture) free(tm_index);  free(st_index);  /* count maximum state num (it is not stored in binhmm... */  {    HTK_HMM_Data *dtmp;    int maxlen = 0;    for (dtmp = hmm->start; dtmp; dtmp = dtmp->next) {      if (maxlen < dtmp->state_num) maxlen = dtmp->state_num;    }    hmm->maxstatenum = maxlen;  }  /* compute total number of mixture PDFs */  {    HTK_HMM_PDF *p;    int n = 0;    for (p = hmm->pdfstart; p; p = p->next) {      n++;    }    hmm->totalpdfnum = n;  }  /* determine whether this model needs multi-path handling */  hmm->need_multipath = htk_hmm_has_several_arc_on_edge(hmm);  if (hmm->need_multipath) {    jlog("Stat: read_binhmm: this HMM requires multipath handling at decoding\n");  } else {    jlog("Stat: read_binhmm: this HMM does not need multipath handling\n");  }    if (! hmm->variance_inversed) {    /* inverse all variance values for faster computation */    htk_hmm_inverse_variances(hmm);    hmm->variance_inversed = TRUE;  }#ifdef ENABLE_MSD  /* check if MSD-HMM */  htk_hmm_check_msd(hmm);#endif  return (TRUE);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -