00001
00027
00028
00029
00030
00031
00032
00033 #include <sent/stddefs.h>
00034 #include <sent/htk_param.h>
00035 #include <sent/htk_hmm.h>
00036
00037 #undef DMES
00038
00047 static void
00048 rdn(FILE *fp, void *buf, size_t unitbyte, int unitnum)
00049 {
00050 size_t tmp;
00051 if ((tmp =
00052 myfread(buf, unitbyte, unitnum, fp)
00053 ) < (size_t)unitnum) {
00054 perror("ngram_read_bin");
00055 j_error("read failed\n");
00056 }
00057 #ifndef WORDS_BIGENDIAN
00058 if (unitbyte != 1) {
00059 swap_bytes(buf, unitbyte, unitnum);
00060 }
00061 #endif
00062 }
00063
00064 static char buf[MAXLINELEN];
00065
00072 static char *
00073 rdn_str(FILE *fp)
00074 {
00075 int c;
00076 int len;
00077 char *p;
00078
00079 len = 0;
00080 while ((c = myfgetc(fp)) != -1) {
00081 if (len >= MAXLINELEN) j_error("Error: string len exceeded %d bytes\n", len);
00082 buf[len++] = c;
00083 if (c == '\0') break;
00084 }
00085 if (len == 1) {
00086 p = NULL;
00087 } else {
00088 p = (char *)mybmalloc(len);
00089 strcpy(p, buf);
00090 }
00091 return(p);
00092 }
00093
00094 static char *binhmm_header = BINHMM_HEADER;
00095
00103 static boolean
00104 rd_header(FILE *fp)
00105 {
00106 char *p;
00107
00108 p = rdn_str(fp);
00109 if (strmatch(p, binhmm_header)) {
00110 return TRUE;
00111 } else {
00112 return FALSE;
00113 }
00114 }
00115
00116
00117
00125 static void
00126 rd_opt(FILE *fp, HTK_HMM_Options *opt)
00127 {
00128 rdn(fp, &(opt->stream_info.num), sizeof(short), 1);
00129 rdn(fp, opt->stream_info.vsize, sizeof(short), 50);
00130 rdn(fp, &(opt->vec_size), sizeof(short), 1);
00131 rdn(fp, &(opt->cov_type), sizeof(short), 1);
00132 rdn(fp, &(opt->dur_type), sizeof(short), 1);
00133 rdn(fp, &(opt->param_type), sizeof(short), 1);
00134 }
00135
00142 static void
00143 rd_type(FILE *fp, HTK_HMM_INFO *hmm)
00144 {
00145 rdn(fp, &(hmm->is_tied_mixture), sizeof(boolean), 1);
00146 rdn(fp, &(hmm->maxmixturenum), sizeof(int), 1);
00147 }
00148
00149
00150
00151 static HTK_HMM_Trans **tr_index;
00152 static unsigned int tr_num;
00153
00164 static void
00165 rd_trans(FILE *fp, HTK_HMM_INFO *hmm)
00166 {
00167 HTK_HMM_Trans *t;
00168 unsigned int idx;
00169 int i;
00170 PROB *atmp;
00171
00172 rdn(fp, &tr_num, sizeof(unsigned int), 1);
00173 tr_index = (HTK_HMM_Trans **)mymalloc(sizeof(HTK_HMM_Trans *) * tr_num);
00174
00175 hmm->trstart = NULL;
00176 hmm->tr_root = NULL;
00177 for (idx = 0; idx < tr_num; idx++) {
00178 t = (HTK_HMM_Trans *)mybmalloc(sizeof(HTK_HMM_Trans));
00179 t->name = rdn_str(fp);
00180 rdn(fp, &(t->statenum), sizeof(short), 1);
00181 t->a = (PROB **)mybmalloc(sizeof(PROB *) * t->statenum);
00182 atmp = (PROB *)mybmalloc(sizeof(PROB) * t->statenum * t->statenum);
00183 for (i=0;i<t->statenum;i++) {
00184 t->a[i] = &(atmp[i*t->statenum]);
00185 rdn(fp, t->a[i], sizeof(PROB), t->statenum);
00186 }
00187 trans_add(hmm, t);
00188 tr_index[idx] = t;
00189 }
00190
00191 #ifdef DMES
00192 j_printf("%d transition maxtix read\n", tr_num);
00193 #endif
00194 }
00195
00196
00197 static HTK_HMM_Var **vr_index;
00198 static unsigned int vr_num;
00199
00210 static void
00211 rd_var(FILE *fp, HTK_HMM_INFO *hmm)
00212 {
00213 HTK_HMM_Var *v;
00214 unsigned int idx;
00215
00216 rdn(fp, &vr_num, sizeof(unsigned int), 1);
00217 vr_index = (HTK_HMM_Var **)mymalloc(sizeof(HTK_HMM_Var *) * vr_num);
00218
00219 hmm->vrstart = NULL;
00220 hmm->vr_root = NULL;
00221 for (idx = 0; idx < vr_num; idx++) {
00222 v = (HTK_HMM_Var *)mybmalloc(sizeof(HTK_HMM_Var));
00223 v->name = rdn_str(fp);
00224 rdn(fp, &(v->len), sizeof(short), 1);
00225 v->vec = (VECT *)mybmalloc(sizeof(VECT) * v->len);
00226 rdn(fp, v->vec, sizeof(VECT), v->len);
00227 vr_index[idx] = v;
00228 var_add(hmm, v);
00229 }
00230 #ifdef DMES
00231 j_printf("%d variance read\n", vr_num);
00232 #endif
00233 }
00234
00235
00236
00237 static HTK_HMM_Dens **dens_index;
00238 static unsigned int dens_num;
00239
00251 static void
00252 rd_dens(FILE *fp, HTK_HMM_INFO *hmm)
00253 {
00254 HTK_HMM_Dens *d;
00255 unsigned int idx;
00256 unsigned int vid;
00257
00258 rdn(fp, &dens_num, sizeof(unsigned int), 1);
00259 hmm->totalmixnum = dens_num;
00260 dens_index = (HTK_HMM_Dens **)mymalloc(sizeof(HTK_HMM_Dens *) * dens_num);
00261
00262 hmm->dnstart = NULL;
00263 hmm->dn_root = NULL;
00264 for (idx = 0; idx < dens_num; idx++) {
00265 d = (HTK_HMM_Dens *)mybmalloc(sizeof(HTK_HMM_Dens));
00266 d->name = rdn_str(fp);
00267 rdn(fp, &(d->meanlen), sizeof(short), 1);
00268 d->mean = (VECT *)mybmalloc(sizeof(VECT) * d->meanlen);
00269 rdn(fp, d->mean, sizeof(VECT), d->meanlen);
00270 rdn(fp, &vid, sizeof(unsigned int), 1);
00271 d->var = vr_index[vid];
00272 rdn(fp, &(d->gconst), sizeof(LOGPROB), 1);
00273 dens_index[idx] = d;
00274 dens_add(hmm, d);
00275 }
00276 #ifdef DMES
00277 j_printf("%d gaussian densities read\n", dens_num);
00278 #endif
00279 }
00280
00281
00282
00283 static GCODEBOOK **tm_index;
00284 static unsigned int tm_num;
00285
00297 static void
00298 rd_tmix(FILE *fp, HTK_HMM_INFO *hmm)
00299 {
00300 GCODEBOOK *tm;
00301 unsigned int idx;
00302 unsigned int did;
00303 int i;
00304
00305 rdn(fp, &tm_num, sizeof(unsigned int), 1);
00306 hmm->codebooknum = tm_num;
00307 tm_index = (GCODEBOOK **)mymalloc(sizeof(GCODEBOOK *) * tm_num);
00308 hmm->maxcodebooksize = 0;
00309
00310 hmm->codebook_root = NULL;
00311 for (idx = 0; idx < tm_num; idx++) {
00312 tm = (GCODEBOOK *)mybmalloc(sizeof(GCODEBOOK));
00313 tm->name = rdn_str(fp);
00314 rdn(fp, &(tm->num), sizeof(int), 1);
00315 if (hmm->maxcodebooksize < tm->num) hmm->maxcodebooksize = tm->num;
00316 tm->d = (HTK_HMM_Dens **)mybmalloc(sizeof(HTK_HMM_Dens *) * tm->num);
00317 for(i=0;i<tm->num;i++) {
00318 rdn(fp, &did, sizeof(unsigned int), 1);
00319 if (did >= dens_num) {
00320 tm->d[i] = NULL;
00321 } else {
00322 tm->d[i] = dens_index[did];
00323 }
00324 }
00325 tm->id = idx;
00326 tm_index[idx] = tm;
00327 codebook_add(hmm, tm);
00328 }
00329 #ifdef DMES
00330 j_printf("%d tied-mixture codebooks read\n", tm_num);
00331 #endif
00332 }
00333
00334
00335 static HTK_HMM_State **st_index;
00336 static unsigned int st_num;
00337
00350 static void
00351 rd_state(FILE *fp, HTK_HMM_INFO *hmm)
00352 {
00353 HTK_HMM_State *s;
00354 unsigned int idx;
00355 unsigned int did;
00356 int i;
00357
00358 rdn(fp, &st_num, sizeof(unsigned int), 1);
00359 hmm->totalstatenum = st_num;
00360 st_index = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * st_num);
00361
00362 hmm->ststart = NULL;
00363 hmm->st_root = NULL;
00364 for (idx = 0; idx < st_num; idx++) {
00365 s = (HTK_HMM_State *)mybmalloc(sizeof(HTK_HMM_State));
00366 s->name = rdn_str(fp);
00367 rdn(fp, &(s->mix_num), sizeof(short), 1);
00368 if (s->mix_num == -1) {
00369
00370 rdn(fp, &did, sizeof(unsigned int), 1);
00371 s->b = (HTK_HMM_Dens **)tm_index[did];
00372 s->mix_num = (tm_index[did])->num;
00373 } else {
00374
00375 s->b = (HTK_HMM_Dens **)mybmalloc(sizeof(HTK_HMM_Dens *) * s->mix_num);
00376 for (i=0;i<s->mix_num;i++) {
00377 rdn(fp, &did, sizeof(unsigned int), 1);
00378 if (did >= dens_num) {
00379 s->b[i] = NULL;
00380 } else {
00381 s->b[i] = dens_index[did];
00382 }
00383 }
00384 }
00385 s->bweight = (PROB *)mybmalloc(sizeof(PROB) * s->mix_num);
00386 rdn(fp, s->bweight, sizeof(PROB), s->mix_num);
00387 s->id = idx;
00388 st_index[idx] = s;
00389 state_add(hmm, s);
00390 }
00391 #ifdef DMES
00392 j_printf("%d states read\n", st_num);
00393 #endif
00394 }
00395
00407 static void
00408 rd_data(FILE *fp, HTK_HMM_INFO *hmm)
00409 {
00410 HTK_HMM_Data *d;
00411 unsigned int md_num;
00412 unsigned int sid, tid;
00413 unsigned int idx;
00414 int i;
00415
00416 rdn(fp, &(md_num), sizeof(unsigned int), 1);
00417 hmm->totalhmmnum = md_num;
00418
00419 hmm->start = NULL;
00420 hmm->physical_root = NULL;
00421 for (idx = 0; idx < md_num; idx++) {
00422 d = (HTK_HMM_Data *)mybmalloc(sizeof(HTK_HMM_Data));
00423 d->name = rdn_str(fp);
00424 rdn(fp, &(d->state_num), sizeof(short), 1);
00425 d->s = (HTK_HMM_State **)mybmalloc(sizeof(HTK_HMM_State *) * d->state_num);
00426 for (i=0;i<d->state_num;i++) {
00427 rdn(fp, &sid, sizeof(unsigned int), 1);
00428 if (sid > hmm->totalstatenum) {
00429 d->s[i] = NULL;
00430 } else {
00431 d->s[i] = st_index[sid];
00432 }
00433 }
00434 rdn(fp, &tid, sizeof(unsigned int), 1);
00435 d->tr = tr_index[tid];
00436 htk_hmmdata_add(hmm, d);
00437 }
00438 #ifdef DMES
00439 j_printf("%d HMM model definition read\n", md_num);
00440 #endif
00441 }
00442
00443
00444
00453 boolean
00454 read_binhmm(FILE *fp, HTK_HMM_INFO *hmm)
00455 {
00456
00457 if (rd_header(fp) == FALSE) {
00458 return FALSE;
00459 }
00460
00461 j_printerr("(binary)...");
00462
00463
00464 rd_opt(fp, &(hmm->opt));
00465
00466
00467 rd_type(fp, hmm);
00468
00469
00470 rd_trans(fp, hmm);
00471
00472
00473 rd_var(fp, hmm);
00474
00475
00476 rd_dens(fp, hmm);
00477
00478
00479 if (hmm->is_tied_mixture) {
00480 rd_tmix(fp, hmm);
00481 }
00482
00483
00484 rd_state(fp, hmm);
00485
00486
00487 rd_data(fp, hmm);
00488
00489
00490 free(tr_index);
00491 free(vr_index);
00492 free(dens_index);
00493 if (hmm->is_tied_mixture) free(tm_index);
00494 free(st_index);
00495
00496 j_printerr("finished\n");
00497
00498 return (TRUE);
00499 }