/* * stream the members in a temp file to the file referenced by 'fd'. */ void arstream(int fd, Arfile *ap) { Armember *bp; int i; char buf[8192]; if (ap->paged) { /* copy from disk */ seek(ap->fd, 0, 0); for (;;) { i = read(ap->fd, buf, sizeof(buf)); if (i < 0) rderr(); if (i == 0) break; if (write(fd, buf, i) != i) wrerr(); } close(ap->fd); ap->paged = 0; } /* dump the in-core buffers */ for (bp = ap->head; bp; bp = bp->next) { if (!arwrite(fd, bp)) wrerr(); } }
/** * Get option name string from its type code. * * @param confdata [in] option description data * @param type [in] type code to search * * @return name string if found, or NULL if not found. */ static char * get_opttype_str(OptionStr *confdata, short type) { int i; for (i = 0; confdata[i].name != NULL; i++) { if (confdata[i].type == type) { return(confdata[i].name); } } rderr("unknown typecode in header!"); return(NULL); }
/** * Skip a regression tree data or its macro reference. * * @param name [in] macro name * @param fp [in] file pointer * @param hmm [in] %HMM definition data */ void def_regtree_macro(char *name, FILE *fp, HTK_HMM_INFO *hmm) { if (currentis("~r")) { /* macro reference */ /* ignore silently */ } else if (currentis("REGTREE")) { /* definition */ /* do not define actually, just read forward till next macro */ regtree_read(fp); } else { rderr("no regtree data\n"); } return; }
void arread(Biobuf *b, Armember *bp, int n) /* read an image into a member buffer */ { int i; bp->member = armalloc(n); i = Bread(b, bp->member, n); if (i < 0) { free(bp->member); bp->member = 0; rderr(); } }
/** * Read in global options. * * @param fp [in] file pointer * @param op [out] pointer to store the global options */ static void read_global_opt(FILE *fp, HTK_HMM_Options *op) { int i; short tmptype; int num; for (;;) { if (rdhmmdef_token == NULL) break; if (currentis("HMMSETID")) { /* <HMMSETID> */ read_token(fp); NoTokErr("missing HMMSETID argument"); } else if (currentis("STREAMINFO")) { /* <STREAMINFO> */ read_token(fp); NoTokErr("missing STREAMINFO num"); op->stream_info.num = atoi(rdhmmdef_token); /*DM("%d STREAMs:", op->stream_info.num);*/ if (op->stream_info.num > MAXSTREAMNUM) { jlog("Error: rdhmmdef_options: stream num exceeded %d\n", MAXSTREAMNUM); rderr(NULL); } for (i=0;i<op->stream_info.num;i++) { read_token(fp); NoTokErr("missing STREAMINFO vector size"); op->stream_info.vsize[i] = atoi(rdhmmdef_token); /*DM(" %d",op->stream_info.vsize[i]);*/ } /*DM("\n");*/ } else if (currentis("VECSIZE")) { /* <VECSIZE> */ read_token(fp); NoTokErr("missing VECSIZE value"); op->vec_size = atoi(rdhmmdef_token); /*DM("vector size: %d\n", op->vec_size);*/ } else if (currentis("MSDINFO")) { /* <MSDINFO> by HTS */ /* Julius can auto-detect MSD-HMM, so just skip this */ read_token(fp); NoTokErr("missing MSDINFO num"); num = atoi(rdhmmdef_token); for (i=0;i<num;i++) { read_token(fp); } } else { /* covariance matrix type */ for (i=0;optcov[i].name!=NULL;i++) { if (currentis(optcov[i].name)) { op->cov_type = optcov[i].type; /*DM("covariance matrix type: %s\n", optcov[i].desc);*/ goto optloop; } } /* duration type */ for (i=0;optdur[i].name!=NULL;i++) { if (currentis(optdur[i].name)) { op->dur_type = optdur[i].type; /*DM("duration type: %s\n", optdur[i].desc);*/ goto optloop; } } /* parameter type */ tmptype = param_str2code(rdhmmdef_token); if (tmptype != F_ERR_INVALID) { /* conv success */ op->param_type = tmptype; /*DM("param type: %s", param_code2str(buf, op->param_type, FALSE));*/ goto optloop; } else { /* nothing of above --- not option */ if(rdhmmdef_token[0] != '~') { jlog("Error: rdhmmdef_options: unknown option in header: %s\n", rdhmmdef_token); rderr("unknown option in header"); } return; } } optloop: read_token(fp); } }
/** * @brief Main top routine to read in HTK %HMM definition file. * * A HTK %HMM definition file will be read from @a fp. After reading, * the parameter type is checked and calculate some statistics. * * @param fp [in] file pointer * @param hmm [out] pointer to a %HMM definition structure to store data. * * @return TRUE on success, FALSE on failure. */ boolean rdhmmdef(FILE *fp, HTK_HMM_INFO *hmm) { char macrosw; char *name; /* variances in htkdefs are not inversed yet */ hmm->variance_inversed = FALSE; /* read the first token */ /* read new 1 line */ line = 1; if (getl(buf, MAXBUFLEN, fp) == NULL) { rdhmmdef_token = NULL; } else { rdhmmdef_token = mystrtok_quote(buf, HMMDEF_DELM); } /* the toplevel loop */ while (rdhmmdef_token != NULL) {/* break on EOF */ if (rdhmmdef_token[0] != '~') { /* toplevel commands are always macro */ return FALSE; } macrosw = rdhmmdef_token[1]; read_token(fp); /* read next token after the "~.." */ switch(macrosw) { case 'o': /* global option */ if (set_global_opt(fp,hmm) == FALSE) { return FALSE; } break; case 't': /* transition macro */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_trans_macro(name, fp, hmm); break; case 's': /* state macro */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_state_macro(name, fp, hmm); break; case 'm': /* density (mixture) macro */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_dens_macro(name, fp, hmm); break; case 'h': /* HMM define */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_HMM(name, fp, hmm); break; case 'v': /* Variance macro */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_var_macro(name, fp, hmm); break; case 'w': /* Stream weight macro */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_streamweight_macro(name, fp, hmm); break; case 'r': /* Regression class macro (ignore) */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_regtree_macro(name, fp, hmm); break; case 'p': /* Mixture pdf macro (extension of HTS) */ name = mybstrdup2(rdhmmdef_token, &(hmm->mroot)); if (strlen(name) >= MAX_HMMNAME_LEN) rderr("Macro name too long"); read_token(fp); def_mpdf_macro(name, fp, hmm); break; } } /* convert transition prob to log scale */ conv_log_arc(hmm); jlog("Stat: rdhmmdef: ascii format HMM definition\n"); /* check limitation */ if (check_all_hmm_limit(hmm)) { jlog("Stat: rdhmmdef: limit check passed\n"); } else { jlog("Error: rdhmmdef: cannot handle this HMM due to system limitation\n"); return FALSE; } /* 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: rdhmmdef: this HMM requires multipath handling at decoding\n"); } else { jlog("Stat: rdhmmdef: this HMM does not need multipath handling\n"); } /* inverse all variance values for faster computation */ if (! hmm->variance_inversed) { htk_hmm_inverse_variances(hmm); hmm->variance_inversed = TRUE; } /* check HMM parameter option type */ if (!check_hmm_options(hmm)) { jlog("Error: rdhmmdef: hmm options check failed\n"); return FALSE; } /* add ID number for all HTK_HMM_State if not assigned */ { HTK_HMM_State *stmp; int n; boolean has_sid; /* caclculate total num and check if has sid */ has_sid = FALSE; n = 0; for (stmp = hmm->ststart; stmp; stmp = stmp->next) { n++; if (n >= MAX_STATE_NUM) { jlog("Error: rdhmmdef: too much states in a model > %d\n", MAX_STATE_NUM); return FALSE; } if (stmp->id != -1) { has_sid = TRUE; } } hmm->totalstatenum = n; if (has_sid) { jlog("Stat: rdhmmdef: <SID> found in the definition\n"); /* check if each state is assigned a valid sid */ if (htk_hmm_check_sid(hmm) == FALSE) { jlog("Error: rdhmmdef: error in SID\n"); return FALSE; } } else { /* assign internal sid (will not be saved) */ jlog("Stat: rdhmmdef: no <SID> embedded\n"); jlog("Stat: rdhmmdef: assign SID by the order of appearance\n"); n = hmm->totalstatenum; for (stmp = hmm->ststart; stmp; stmp = stmp->next) { stmp->id = --n; } } } /* calculate the maximum number of mixture */ { HTK_HMM_State *stmp; int max, s, mix; max = 0; for (stmp = hmm->ststart; stmp; stmp = stmp->next) { for(s=0;s<stmp->nstream;s++) { mix = stmp->pdf[s]->mix_num; if (max < mix) max = mix; } } hmm->maxmixturenum = max; } /* compute total number of HMM models and maximum length */ { HTK_HMM_Data *dtmp; int n, maxlen; n = 0; maxlen = 0; for (dtmp = hmm->start; dtmp; dtmp = dtmp->next) { if (maxlen < dtmp->state_num) maxlen = dtmp->state_num; n++; } hmm->maxstatenum = maxlen; hmm->totalhmmnum = n; } /* compute total number of Gaussians */ { HTK_HMM_Dens *dtmp; int n = 0; for (dtmp = hmm->dnstart; dtmp; dtmp = dtmp->next) { n++; } hmm->totalmixnum = n; } /* check of HMM name length exceed the maximum */ { HTK_HMM_Dens *dtmp; int n = 0; for (dtmp = hmm->dnstart; dtmp; dtmp = dtmp->next) { n++; } hmm->totalmixnum = n; } /* 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; } /* assign ID number for all HTK_HMM_Trans */ { HTK_HMM_Trans *ttmp; int n = 0; for (ttmp = hmm->trstart; ttmp; ttmp = ttmp->next) { ttmp->id = n++; } hmm->totaltransnum = n; } #ifdef ENABLE_MSD /* check if MSD-HMM */ htk_hmm_check_msd(hmm); #endif return(TRUE); /* success */ }