/* * Initialize phones (ci and triphones) and state->senone mappings from .mdef file. */ mdef_t *mdef_init (char *mdeffile) { FILE *fp; int32 n_ci, n_tri, n_map, n; char tag[1024], buf[1024]; int32 n_read; s3senid_t *senmap; s3pid_t p; int32 s, ci, cd; E_INFO("Reading model definition: %s\n", mdeffile); assert (! mdef); mdef = (mdef_t *) ckd_calloc (1, sizeof(mdef_t)); if ((fp = fopen(mdeffile, "r")) == NULL) E_FATAL("fopen(%s,r) failed\n", mdeffile); n_read = 0; if (noncomment_line(buf, sizeof(buf), &n_read, fp) < 0) E_FATAL("Empty file: %s\n", mdeffile); if (strncmp(buf, MODEL_DEF_VERSION, strlen(MODEL_DEF_VERSION)) != 0) E_FATAL("Line %d: Version error. Expecing %s, but read %s\n", n_read, MODEL_DEF_VERSION, buf); /* Read #base phones, #triphones, #senone mappings defined in header */ n_ci = -1; n_tri = -1; n_map = -1; mdef->n_ci_sen = -1; mdef->n_sen = -1; mdef->n_tmat = -1; do { if (noncomment_line(buf, sizeof(buf), &n_read, fp) < 0) E_FATAL("Incomplete header\n"); if ((sscanf(buf, "%d %s", &n, tag) != 2) || (n < 0)) E_FATAL("Line %d: Error in reading header\n", n_read); if (strcmp(tag, "n_base") == 0) n_ci = n; else if (strcmp(tag, "n_tri") == 0) n_tri = n; else if (strcmp(tag, "n_state_map") == 0) n_map = n; else if (strcmp(tag, "n_tied_ci_state") == 0) mdef->n_ci_sen = n; else if (strcmp(tag, "n_tied_state") == 0) mdef->n_sen = n; else if (strcmp(tag, "n_tied_tmat") == 0) mdef->n_tmat = n; else E_FATAL("Line %d: Unknown tag: %s\n", n_read, tag); } while ((n_ci < 0) || (n_tri < 0) || (n_map < 0) || (mdef->n_ci_sen < 0) || (mdef->n_sen < 0) || (mdef->n_tmat < 0)); if ((n_ci == 0) || (mdef->n_ci_sen == 0) || (mdef->n_tmat == 0) || (mdef->n_ci_sen > mdef->n_sen)) E_FATAL("Incorrect information in header\n"); mdef->n_emit_state = (n_map / (n_ci+n_tri)) - 1; if ((mdef->n_emit_state+1) * (n_ci+n_tri) != n_map) E_FATAL("Header error: n_state_map not a multiple of n_ci*n_tri\n"); /* Initialize ciphone info */ max_ciphone = n_ci; mdef->ciphone_ht = hash_new ("ciphone", n_ci); mdef->ciphone = (ciphone_t *) ckd_calloc (n_ci, sizeof(ciphone_t)); mdef->n_ciphone = 0; /* Initialize phones info (ciphones + triphones) */ max_phone = n_ci + n_tri; mdef->phone = (phone_t *) ckd_calloc (max_phone, sizeof(phone_t)); mdef->n_phone = 0; /* Allocate space for state->senone map for each phone */ senmap = (s3senid_t *) ckd_calloc (max_phone * mdef->n_emit_state, sizeof(s3senid_t)); for (p = 0; p < max_phone; p++) mdef->phone[p].state = senmap + (p * mdef->n_emit_state); /* Allocate initial space for <ci,lc,rc,wpos> -> pid mapping */ mdef->wpos_ci_lclist = (ph_lc_t ***) ckd_calloc_2d (N_WORD_POSN, max_ciphone, sizeof(ph_lc_t *)); /* * Read base phones and triphones. They'll simply be assigned a running sequence * number as their "phone-id". If the phone-id < n_ci, it's a ciphone. */ /* Read base phones */ for (p = 0; p < n_ci; p++) { if (noncomment_line(buf, sizeof(buf), &n_read, fp) < 0) E_FATAL("Premature EOF(%s)\n", mdeffile); parse_base_line (mdef, buf, n_read, p); } mdef->sil = mdef_ciphone_id (mdef, SILENCE_CIPHONE); /* Read triphones, if any */ for (; p < max_phone; p++) { if (noncomment_line(buf, sizeof(buf), &n_read, fp) < 0) E_FATAL("Premature EOF(%s)\n", mdeffile); parse_tri_line (mdef, buf, n_read, p); } if (noncomment_line(buf, sizeof(buf), &n_read, fp) >= 0) E_ERROR("Line %d: File continues beyond expected size\n", n_read); /* Build CD senones to CI senones map */ mdef->cd2cisen = (s3senid_t *) ckd_calloc (mdef->n_sen, sizeof(s3senid_t)); for (s = 0; s < mdef->n_ci_sen; s++) /* CI senones */ mdef->cd2cisen[s] = (s3senid_t) s; for (p = n_ci; p < max_phone; p++) { /* CD senones */ for (s = 0; s < mdef->n_emit_state; s++) { cd = mdef->phone[p].state[s]; ci = mdef->phone[mdef->phone[p].ci].state[s]; mdef->cd2cisen[cd] = (s3senid_t) ci; } } E_INFO("%d CI-phones, %d CD-phones, %d emitting states/phone, %d sen, %d CI-sen\n", mdef->n_ciphone, mdef->n_phone - mdef->n_ciphone, mdef->n_emit_state, mdef->n_sen, mdef->n_ci_sen); fclose (fp); return mdef; }
/* * Initialize phones (ci and triphones) and state->senone mappings from .mdef file. */ mdef_t * mdef_init(char *mdeffile, int32 breport) { FILE *fp; int32 n_ci, n_tri, n_map, n; __BIGSTACKVARIABLE__ char tag[1024], buf[1024]; uint16 **senmap; int p; int32 s, ci, cd; mdef_t *m; if (!mdeffile) E_FATAL("No mdef-file\n"); if (breport) E_INFO("Reading model definition: %s\n", mdeffile); m = (mdef_t *) ckd_calloc(1, sizeof(mdef_t)); /* freed in mdef_free */ if ((fp = fopen(mdeffile, "r")) == NULL) E_FATAL_SYSTEM("Failed to open mdef file '%s' for reading", mdeffile); if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Empty file: %s\n", mdeffile); if (strncmp(buf, "BMDF", 4) == 0 || strncmp(buf, "FDMB", 4) == 0) { E_INFO ("Found byte-order mark %.4s, assuming this is a binary mdef file\n", buf); fclose(fp); ckd_free(m); return NULL; } if (strncmp(buf, MODEL_DEF_VERSION, strlen(MODEL_DEF_VERSION)) != 0) E_FATAL("Version error: Expecing %s, but read %s\n", MODEL_DEF_VERSION, buf); /* Read #base phones, #triphones, #senone mappings defined in header */ n_ci = -1; n_tri = -1; n_map = -1; m->n_ci_sen = -1; m->n_sen = -1; m->n_tmat = -1; do { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Incomplete header\n"); if ((sscanf(buf, "%d %s", &n, tag) != 2) || (n < 0)) E_FATAL("Error in header: %s\n", buf); if (strcmp(tag, "n_base") == 0) n_ci = n; else if (strcmp(tag, "n_tri") == 0) n_tri = n; else if (strcmp(tag, "n_state_map") == 0) n_map = n; else if (strcmp(tag, "n_tied_ci_state") == 0) m->n_ci_sen = n; else if (strcmp(tag, "n_tied_state") == 0) m->n_sen = n; else if (strcmp(tag, "n_tied_tmat") == 0) m->n_tmat = n; else E_FATAL("Unknown header line: %s\n", buf); } while ((n_ci < 0) || (n_tri < 0) || (n_map < 0) || (m->n_ci_sen < 0) || (m->n_sen < 0) || (m->n_tmat < 0)); if ((n_ci == 0) || (m->n_ci_sen == 0) || (m->n_tmat == 0) || (m->n_ci_sen > m->n_sen)) E_FATAL("%s: Error in header\n", mdeffile); /* Check typesize limits */ if (n_ci >= MAX_INT16) E_FATAL("%s: #CI phones (%d) exceeds limit (%d)\n", mdeffile, n_ci, MAX_INT16); if (n_ci + n_tri >= MAX_INT32) /* Comparison is always false... */ E_FATAL("%s: #Phones (%d) exceeds limit (%d)\n", mdeffile, n_ci + n_tri, MAX_INT32); if (m->n_sen >= MAX_INT16) E_FATAL("%s: #senones (%d) exceeds limit (%d)\n", mdeffile, m->n_sen, MAX_INT16); if (m->n_tmat >= MAX_INT32) /* Comparison is always false... */ E_FATAL("%s: #tmats (%d) exceeds limit (%d)\n", mdeffile, m->n_tmat, MAX_INT32); m->n_emit_state = (n_map / (n_ci + n_tri)) - 1; if ((m->n_emit_state + 1) * (n_ci + n_tri) != n_map) E_FATAL ("Header error: n_state_map not a multiple of n_ci*n_tri\n"); /* Initialize ciphone info */ m->n_ciphone = n_ci; m->ciphone_ht = hash_table_new(n_ci, HASH_CASE_YES); /* With case-insensitive string names *//* freed in mdef_free */ m->ciphone = (ciphone_t *) ckd_calloc(n_ci, sizeof(ciphone_t)); /* freed in mdef_free */ /* Initialize phones info (ciphones + triphones) */ m->n_phone = n_ci + n_tri; m->phone = (phone_t *) ckd_calloc(m->n_phone, sizeof(phone_t)); /* freed in mdef_free */ /* Allocate space for state->senone map for each phone */ senmap = ckd_calloc_2d(m->n_phone, m->n_emit_state, sizeof(**senmap)); /* freed in mdef_free */ m->sseq = senmap; /* TEMPORARY; until it is compressed into just the unique ones */ /* Allocate initial space for <ci,lc,rc,wpos> -> pid mapping */ m->wpos_ci_lclist = (ph_lc_t ***) ckd_calloc_2d(N_WORD_POSN, m->n_ciphone, sizeof(ph_lc_t *)); /* freed in mdef_free */ /* * Read base phones and triphones. They'll simply be assigned a running sequence * number as their "phone-id". If the phone-id < n_ci, it's a ciphone. */ /* Read base phones */ for (p = 0; p < n_ci; p++) { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Premature EOF reading CIphone %d\n", p); parse_base_line(m, buf, p); } m->sil = mdef_ciphone_id(m, S3_SILENCE_CIPHONE); /* Read triphones, if any */ for (; p < m->n_phone; p++) { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Premature EOF reading phone %d\n", p); parse_tri_line(m, buf, p); } if (noncomment_line(buf, sizeof(buf), fp) >= 0) E_ERROR("Non-empty file beyond expected #phones (%d)\n", m->n_phone); /* Build CD senones to CI senones map */ if (m->n_ciphone * m->n_emit_state != m->n_ci_sen) E_FATAL ("#CI-senones(%d) != #CI-phone(%d) x #emitting-states(%d)\n", m->n_ci_sen, m->n_ciphone, m->n_emit_state); m->cd2cisen = (int16 *) ckd_calloc(m->n_sen, sizeof(*m->cd2cisen)); /* freed in mdef_free */ m->sen2cimap = (int16 *) ckd_calloc(m->n_sen, sizeof(*m->sen2cimap)); /* freed in mdef_free */ for (s = 0; s < m->n_sen; s++) m->sen2cimap[s] = -1; for (s = 0; s < m->n_ci_sen; s++) { /* CI senones */ m->cd2cisen[s] = s; m->sen2cimap[s] = s / m->n_emit_state; } for (p = n_ci; p < m->n_phone; p++) { /* CD senones */ for (s = 0; s < m->n_emit_state; s++) { cd = m->sseq[p][s]; ci = m->sseq[m->phone[p].ci][s]; m->cd2cisen[cd] = ci; m->sen2cimap[cd] = m->phone[p].ci; } } sseq_compress(m); fclose(fp); return m; }
/* * Initialize phones (ci and triphones) and state->senone mappings from .mdef file. */ mdef_t * mdef_init(const char *mdeffile, int32 breport) { FILE *fp; int32 n_ci, n_tri, n_map, n; char tag[1024], buf[1024]; s3senid_t **senmap; /* s3senid_t *tempsenmap; */ s3pid_t p; int32 s, ci, cd; mdef_t *m; if (!mdeffile) E_FATAL("No mdef-file\n"); if (breport) E_INFO("Reading model definition: %s\n", mdeffile); m = (mdef_t *) ckd_calloc(1, sizeof(mdef_t)); /* freed in mdef_free */ if ((fp = fopen(mdeffile, "r")) == NULL) E_FATAL_SYSTEM("fopen(%s,r) failed\n", mdeffile); if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Empty file: %s\n", mdeffile); if (strncmp(buf, MODEL_DEF_VERSION, strlen(MODEL_DEF_VERSION)) != 0) E_FATAL("Version error: Expecing %s, but read %s\n", MODEL_DEF_VERSION, buf); /* Read #base phones, #triphones, #senone mappings defined in header */ n_ci = -1; n_tri = -1; n_map = -1; m->n_ci_sen = -1; m->n_sen = -1; m->n_tmat = -1; do { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Incomplete header\n"); if ((sscanf(buf, "%d %s", &n, tag) != 2) || (n < 0)) E_FATAL("Error in header: %s\n", buf); if (strcmp(tag, "n_base") == 0) n_ci = n; else if (strcmp(tag, "n_tri") == 0) n_tri = n; else if (strcmp(tag, "n_state_map") == 0) n_map = n; else if (strcmp(tag, "n_tied_ci_state") == 0) m->n_ci_sen = n; else if (strcmp(tag, "n_tied_state") == 0) m->n_sen = n; else if (strcmp(tag, "n_tied_tmat") == 0) m->n_tmat = n; else E_FATAL("Unknown header line: %s\n", buf); } while ((n_ci < 0) || (n_tri < 0) || (n_map < 0) || (m->n_ci_sen < 0) || (m->n_sen < 0) || (m->n_tmat < 0)); if ((n_ci == 0) || (m->n_ci_sen == 0) || (m->n_tmat == 0) || (m->n_ci_sen > m->n_sen)) E_FATAL("%s: Error in header\n", mdeffile); /* Check typesize limits */ if (n_ci >= MAX_S3CIPID) E_FATAL("%s: #CI phones (%d) exceeds limit (%d)\n", mdeffile, n_ci, MAX_S3CIPID); if (n_ci + n_tri >= MAX_S3PID) E_FATAL("%s: #Phones (%d) exceeds limit (%d)\n", mdeffile, n_ci + n_tri, MAX_S3PID); if (m->n_sen >= MAX_S3SENID) E_FATAL("%s: #senones (%d) exceeds limit (%d)\n", mdeffile, m->n_sen, MAX_S3SENID); if (m->n_tmat >= MAX_S3TMATID) E_FATAL("%s: #tmats (%d) exceeds limit (%d)\n", mdeffile, m->n_tmat, MAX_S3TMATID); m->n_emit_state = (n_map / (n_ci + n_tri)) - 1; if ((m->n_emit_state + 1) * (n_ci + n_tri) != n_map) E_FATAL ("Header error: n_state_map not a multiple of n_ci*n_tri\n"); /* Initialize ciphone info */ m->n_ciphone = n_ci; m->ciphone_ht = hash_table_new(n_ci, HASH_CASE_YES); /* With case-insensitive string names *//* freed in mdef_free */ m->ciphone = (ciphone_t *) ckd_calloc(n_ci, sizeof(ciphone_t)); /* freed in mdef_free */ /* Initialize phones info (ciphones + triphones) */ m->n_phone = n_ci + n_tri; m->phone = (phone_t *) ckd_calloc(m->n_phone, sizeof(phone_t)); /* freed in mdef_free */ /* Allocate space for state->senone map for each phone */ senmap = (s3senid_t **) ckd_calloc_2d(m->n_phone, m->n_emit_state, sizeof(s3senid_t)); /* freed in mdef_free */ m->sseq = senmap; /* TEMPORARY; until it is compressed into just the unique ones */ /**CODE DUPLICATION!*****************************************************************************************************/ /* Flat decoder-specific */ /* Allocate space for state->senone map for each phone */ /* ARCHAN 20040820, this sacrifice readability and may cause pointer problems in future. However, this is a less evil than duplication of code. This is trick point all the state mapping to the global mapping and avoid duplicated memory. */ /* S3 xwdpid_compress will compress the below list phone list. */ /* ARCHAN, this part should not be used when one of the recognizer is used. */ m->st2senmap = (s3senid_t *) ckd_calloc(m->n_phone * m->n_emit_state, sizeof(s3senid_t)); for (p = 0; p < m->n_phone; p++) m->phone[p].state = m->st2senmap + (p * m->n_emit_state); /******************************************************************************************************/ /* Allocate initial space for <ci,lc,rc,wpos> -> pid mapping */ m->wpos_ci_lclist = (ph_lc_t ***) ckd_calloc_2d(N_WORD_POSN, m->n_ciphone, sizeof(ph_lc_t *)); /* freed in mdef_free */ /* * Read base phones and triphones. They'll simply be assigned a running sequence * number as their "phone-id". If the phone-id < n_ci, it's a ciphone. */ /* Read base phones */ for (p = 0; p < n_ci; p++) { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Premature EOF reading CIphone %d\n", p); parse_base_line(m, buf, p); } m->sil = mdef_ciphone_id(m, S3_SILENCE_CIPHONE); /* Read triphones, if any */ for (; p < m->n_phone; p++) { if (noncomment_line(buf, sizeof(buf), fp) < 0) E_FATAL("Premature EOF reading phone %d\n", p); parse_tri_line(m, buf, p); } if (noncomment_line(buf, sizeof(buf), fp) >= 0) E_ERROR("Non-empty file beyond expected #phones (%d)\n", m->n_phone); /* Build CD senones to CI senones map */ if (m->n_ciphone * m->n_emit_state != m->n_ci_sen) E_FATAL ("#CI-senones(%d) != #CI-phone(%d) x #emitting-states(%d)\n", m->n_ci_sen, m->n_ciphone, m->n_emit_state); m->cd2cisen = (s3senid_t *) ckd_calloc(m->n_sen, sizeof(s3senid_t)); /* freed in mdef_free */ m->sen2cimap = (s3cipid_t *) ckd_calloc(m->n_sen, sizeof(s3cipid_t)); /* freed in mdef_free */ for (s = 0; s < m->n_sen; s++) m->sen2cimap[s] = BAD_S3CIPID; for (s = 0; s < m->n_ci_sen; s++) { /* CI senones */ m->cd2cisen[s] = (s3senid_t) s; m->sen2cimap[s] = s / m->n_emit_state; } for (p = n_ci; p < m->n_phone; p++) { /* CD senones */ for (s = 0; s < m->n_emit_state; s++) { cd = m->sseq[p][s]; ci = m->sseq[(int) m->phone[p].ci][s]; m->cd2cisen[cd] = (s3senid_t) ci; m->sen2cimap[cd] = m->phone[p].ci; } } sseq_compress(m); fclose(fp); return m; }