void vector_gautbl_alloc (vector_gautbl_t *gautbl, int32 n_gau, int32 veclen) { gautbl->n_gau = n_gau; gautbl->veclen = veclen; gautbl->mean = (float32 **) ckd_calloc_2d (n_gau, veclen, sizeof(float32)); gautbl->var = (float32 **) ckd_calloc_2d (n_gau, veclen, sizeof(float32)); gautbl->lrd = (float32 *) ckd_calloc (n_gau, sizeof(float32)); gautbl->distfloor = logs3_to_log (S3_LOGPROB_ZERO); }
int32 read_reg_mat ( const char *regmatfn, uint32 **veclen, uint32 *n_class, uint32 *n_stream, float32 *****A, float32 ****B ) { uint32 i,j,k,m,nstream,nclass; FILE *fp; uint32 *vlen; float32 ****lA,***lB; if ((fp = fopen(regmatfn,"r")) == NULL) { E_INFO("Unable to open %s to read MLLR matrices\n",regmatfn); return S3_ERROR; } fscanf(fp,"%d",&nclass); fscanf(fp,"%d",&nstream); vlen = (uint32 *)ckd_calloc(nstream,sizeof(uint32)); lA = (float32 ****)ckd_calloc_2d (nclass,nstream,sizeof (float32 **)); lB = (float32 ***)ckd_calloc_2d (nclass,nstream,sizeof (float32 *)); for (m = 0; m < nclass; ++m) { for (i = 0; i < nstream; ++i) { fscanf(fp,"%d", &vlen[i]); lA[m][i] = (float32 **) ckd_calloc_2d(vlen[i],vlen[i],sizeof(float32)); lB[m][i] = (float32 *) ckd_calloc(vlen[i],sizeof(float32)); for (j = 0; j < vlen[i]; j++) { for (k = 0; k < vlen[i]; ++k) { fscanf(fp,"%f ",&lA[m][i][j][k]); } } for (j = 0; j < vlen[i]; j++) { fscanf(fp,"%f ",&lB[m][i][j]); } /* Identity transform for variances. */ for (j = 0; j < vlen[i]; j++) { float32 dummy; fscanf(fp,"%f ",&dummy); } } } *n_class = nclass; *n_stream = nstream; *veclen = vlen; *A = lA; *B = lB; fclose(fp); return S3_SUCCESS; }
void s3_endpointer_feed_frames(s3_endpointer_t *_ep, float32 **_frames, int _n_frames, int _eof) { float32 **fbuf; int *cbuf; int i, sz, leftover; assert(_ep != NULL); if (_ep->n_frames > _ep->offset) { leftover = _ep->n_frames - _ep->offset; sz = _n_frames + leftover; fbuf = (float32 **)ckd_calloc_2d(sz, CEP_LEN, sizeof(float32)); cbuf = (int *)ckd_calloc(sizeof(int), sz); for (i = 0; i < leftover; i++) memcpy(fbuf[i], _ep->frames[_ep->offset + i], FRAME_LEN); memcpy(cbuf, &_ep->classes[_ep->offset], leftover * sizeof(int)); for (i = leftover; i < sz; i++) memcpy(fbuf[i], _frames[i - leftover], FRAME_LEN); get_frame_classes(_ep, _frames, _n_frames, &cbuf[leftover]); ckd_free_2d((void **)_ep->frames); ckd_free(_ep->classes); _ep->frames = fbuf; _ep->classes = cbuf; _ep->n_frames = sz; _ep->offset = 0; } else { fbuf = (float32 **)ckd_calloc_2d(_n_frames, CEP_LEN, sizeof(float32)); cbuf = (int *)ckd_calloc(sizeof(int), _n_frames); for (i = 0; i < _n_frames; i++) memcpy(fbuf[i], _frames[i], FRAME_LEN); get_frame_classes(_ep, _frames, _n_frames, cbuf); ckd_free_2d((void **)_ep->frames); ckd_free(_ep->classes); _ep->frames = fbuf; _ep->classes = cbuf; _ep->n_frames = _n_frames; _ep->offset = 0; } if (_ep->state == STATE_BEGIN_STREAM && update_available(_ep)) init_frame_stats(_ep); _ep->eof = _eof; }
int32 solve(float32 **a, /*Input : an n*n matrix A */ float32 *b, /*Input : a n dimesion vector b */ float32 *out_x, /*Output : a n dimesion vector x */ int32 n) { char uplo; float32 **tmp_a; int32 info, nrhs; /* a is assumed to be symmetric, so we don't need to switch the * ordering of the data. But we do need to copy it since it is * overwritten by LAPACK. */ tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); memcpy(out_x, b, n*sizeof(float32)); uplo = 'L'; nrhs = 1; sposv_(&uplo, &n, &nrhs, tmp_a[0], &n, out_x, &n, &info); ckd_free_2d((void **)tmp_a); if (info != 0) return -1; else return info; }
/* Find inverse by solving AX=I. */ int32 invert(float32 ** ainv, float32 ** a, int32 n) { char uplo; float32 **tmp_a; int32 info, nrhs, i; /* a is assumed to be symmetric, so we don't need to switch the * ordering of the data. But we do need to copy it since it is * overwritten by LAPACK. */ tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); /* Construct an identity matrix. */ memset(ainv[0], 0, sizeof(float32) * n * n); for (i = 0; i < n; i++) ainv[i][i] = 1.0; uplo = 'L'; nrhs = n; sposv_(&uplo, &n, &nrhs, tmp_a[0], &n, ainv[0], &n, &info); ckd_free_2d((void **)tmp_a); if (info != 0) return -1; else return info; }
static int acmod_process_full_raw(acmod_t *acmod, int16 const **inout_raw, size_t *inout_n_samps) { int32 nfr, ntail; mfcc_t **cepptr; /* Write to logging file if any. */ if (acmod->rawfh) fwrite(*inout_raw, 2, *inout_n_samps, acmod->rawfh); /* Resize mfc_buf to fit. */ if (fe_process_frames(acmod->fe, NULL, inout_n_samps, NULL, &nfr) < 0) return -1; if (acmod->n_mfc_alloc < nfr + 1) { ckd_free_2d(acmod->mfc_buf); acmod->mfc_buf = ckd_calloc_2d(nfr + 1, fe_get_output_size(acmod->fe), sizeof(**acmod->mfc_buf)); acmod->n_mfc_alloc = nfr + 1; } acmod->n_mfc_frame = 0; acmod->mfc_outidx = 0; fe_start_utt(acmod->fe); if (fe_process_frames(acmod->fe, inout_raw, inout_n_samps, acmod->mfc_buf, &nfr) < 0) return -1; fe_end_utt(acmod->fe, acmod->mfc_buf[nfr], &ntail); nfr += ntail; cepptr = acmod->mfc_buf; nfr = acmod_process_full_cep(acmod, &cepptr, &nfr); acmod->n_mfc_frame = 0; return nfr; }
void ngram_model_set_map_words(ngram_model_t * base, const char **words, int32 n_words) { ngram_model_set_t *set = (ngram_model_set_t *) base; int32 i; /* Recreate the word mapping. */ if (base->writable) { for (i = 0; i < base->n_words; ++i) { ckd_free(base->word_str[i]); } } ckd_free(base->word_str); ckd_free_2d((void **) set->widmap); base->writable = TRUE; base->n_words = base->n_1g_alloc = n_words; base->word_str = ckd_calloc(n_words, sizeof(*base->word_str)); set->widmap = (int32 **) ckd_calloc_2d(n_words, set->n_models, sizeof(**set->widmap)); hash_table_empty(base->wid); for (i = 0; i < n_words; ++i) { int32 j; base->word_str[i] = ckd_salloc(words[i]); (void) hash_table_enter_int32(base->wid, base->word_str[i], i); for (j = 0; j < set->n_models; ++j) { set->widmap[i][j] = ngram_wid(set->lms[j], base->word_str[i]); } } }
/* Find determinant through LU decomposition. */ float64 determinant(float32 ** a, int32 n) { float32 **tmp_a; float64 det; char uplo; int32 info, i; /* a is assumed to be symmetric, so we don't need to switch the * ordering of the data. But we do need to copy it since it is * overwritten by LAPACK. */ tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); uplo = 'L'; spotrf_(&uplo, &n, tmp_a[0], &n, &info); det = tmp_a[0][0]; /* det = prod(diag(l))^2 */ for (i = 1; i < n; ++i) det *= tmp_a[i][i]; ckd_free_2d((void **)tmp_a); if (info > 0) return -1.0; /* Generic "not positive-definite" answer */ else return det * det; }
void vector_gautbl_alloc (vector_gautbl_t *gautbl, int32 n_gau, int32 veclen) { int32 tmp; gautbl->n_gau = n_gau; gautbl->veclen = veclen; #if 1 tmp =(veclen%4)? (veclen+4-(veclen%4)):veclen; gautbl->mean = (float32 **) ckd_calloc_2da (n_gau, tmp, sizeof(float32)); gautbl->var = (float32 **) ckd_calloc_2da (n_gau, tmp, sizeof(float32)); /*printf("n_gau %d tmp %d %p %p\n",n_gau,tmp, gautbl->mean, gautbl->var);*/ #else gautbl->mean = (float32 **) ckd_calloc_2d (n_gau, veclen, sizeof(float32)); gautbl->var = (float32 **) ckd_calloc_2d (n_gau, veclen, sizeof(float32)); #endif gautbl->lrd = (float32 *) ckd_calloc (n_gau, sizeof(float32)); gautbl->distfloor = logs3_to_log (S3_LOGPROB_ZERO); }
ngram_model_t * ngram_model_set_add(ngram_model_t * base, ngram_model_t * model, const char *name, float32 weight, int reuse_widmap) { ngram_model_set_t *set = (ngram_model_set_t *) base; float32 fprob; int32 scale, i; /* Add it to the array of lms. */ ++set->n_models; set->lms = ckd_realloc(set->lms, set->n_models * sizeof(*set->lms)); set->lms[set->n_models - 1] = model; set->names = ckd_realloc(set->names, set->n_models * sizeof(*set->names)); set->names[set->n_models - 1] = ckd_salloc(name); /* Expand the history mapping table if necessary. */ if (model->n > base->n) { base->n = model->n; set->maphist = ckd_realloc(set->maphist, (model->n - 1) * sizeof(*set->maphist)); } /* Renormalize the interpolation weights. */ fprob = weight * 1.0f / set->n_models; set->lweights = ckd_realloc(set->lweights, set->n_models * sizeof(*set->lweights)); set->lweights[set->n_models - 1] = logmath_log(base->lmath, fprob); /* Now normalize everything else to fit it in. This is * accomplished by simply scaling all the other probabilities * by (1-fprob). */ scale = logmath_log(base->lmath, 1.0 - fprob); for (i = 0; i < set->n_models - 1; ++i) set->lweights[i] += scale; /* Reuse the old word ID mapping if requested. */ if (reuse_widmap) { int32 **new_widmap; /* Tack another column onto the widmap array. */ new_widmap = (int32 **) ckd_calloc_2d(base->n_words, set->n_models, sizeof(**new_widmap)); for (i = 0; i < base->n_words; ++i) { /* Copy all the existing mappings. */ memcpy(new_widmap[i], set->widmap[i], (set->n_models - 1) * sizeof(**new_widmap)); /* Create the new mapping. */ new_widmap[i][set->n_models - 1] = ngram_wid(model, base->word_str[i]); } ckd_free_2d((void **) set->widmap); set->widmap = new_widmap; } else { build_widmap(base, base->lmath, base->n); } return model; }
static void build_widmap(ngram_model_t * base, logmath_t * lmath, int32 n) { ngram_model_set_t *set = (ngram_model_set_t *) base; ngram_model_t **models = set->lms; hash_table_t *vocab; glist_t hlist; gnode_t *gn; int32 i; /* Construct a merged vocabulary and a set of word-ID mappings. */ vocab = hash_table_new(models[0]->n_words, FALSE); /* Create the set of merged words. */ for (i = 0; i < set->n_models; ++i) { int32 j; for (j = 0; j < models[i]->n_words; ++j) { /* Ignore collisions. */ (void) hash_table_enter_int32(vocab, models[i]->word_str[j], j); } } /* Create the array of words, then sort it. */ if (hash_table_lookup(vocab, "<UNK>", NULL) != 0) (void) hash_table_enter_int32(vocab, "<UNK>", 0); /* Now we know the number of unigrams, initialize the base model. */ ngram_model_init(base, &ngram_model_set_funcs, lmath, n, hash_table_inuse(vocab)); base->writable = FALSE; /* We will reuse the pointers from the submodels. */ i = 0; hlist = hash_table_tolist(vocab, NULL); for (gn = hlist; gn; gn = gnode_next(gn)) { hash_entry_t *ent = gnode_ptr(gn); base->word_str[i++] = (char *) ent->key; } glist_free(hlist); qsort(base->word_str, base->n_words, sizeof(*base->word_str), my_compare); /* Now create the word ID mappings. */ if (set->widmap) ckd_free_2d((void **) set->widmap); set->widmap = (int32 **) ckd_calloc_2d(base->n_words, set->n_models, sizeof(**set->widmap)); for (i = 0; i < base->n_words; ++i) { int32 j; /* Also create the master wid mapping. */ (void) hash_table_enter_int32(base->wid, base->word_str[i], i); /* printf("%s: %d => ", base->word_str[i], i); */ for (j = 0; j < set->n_models; ++j) { set->widmap[i][j] = ngram_wid(models[j], base->word_str[i]); /* printf("%d ", set->widmap[i][j]); */ } /* printf("\n"); */ } hash_table_free(vocab); }
int fe_process_utt(fe_t * fe, int16 const * spch, size_t nsamps, mfcc_t *** cep_block, int32 * nframes) { mfcc_t **cep; int rv; /* Figure out how many frames we will need. */ fe_process_frames(fe, NULL, &nsamps, NULL, nframes, NULL); /* Create the output buffer (it has to exist, even if there are no output frames). */ if (*nframes) cep = (mfcc_t **)ckd_calloc_2d(*nframes, fe->feature_dimension, sizeof(**cep)); else cep = (mfcc_t **)ckd_calloc_2d(1, fe->feature_dimension, sizeof(**cep)); /* Now just call fe_process_frames() with the allocated buffer. */ rv = fe_process_frames(fe, &spch, &nsamps, cep, nframes, NULL); *cep_block = cep; return rv; }
static int phone_loop_search_reinit(ps_search_t *search, dict_t *dict, dict2pid_t *d2p) { phone_loop_search_t *pls = (phone_loop_search_t *)search; cmd_ln_t *config = ps_search_config(search); acmod_t *acmod = ps_search_acmod(search); int i; /* Free old dict2pid, dict, if necessary. */ ps_search_base_reinit(search, dict, d2p); /* Initialize HMM context. */ if (pls->hmmctx) hmm_context_free(pls->hmmctx); pls->hmmctx = hmm_context_init(bin_mdef_n_emit_state(acmod->mdef), acmod->tmat->tp, NULL, acmod->mdef->sseq); if (pls->hmmctx == NULL) return -1; /* Initialize penalty storage */ pls->n_phones = bin_mdef_n_ciphone(acmod->mdef); pls->window = cmd_ln_int32_r(config, "-pl_window"); if (pls->penalties) ckd_free(pls->penalties); pls->penalties = (int32 *)ckd_calloc(pls->n_phones, sizeof(*pls->penalties)); if (pls->pen_buf) ckd_free_2d(pls->pen_buf); pls->pen_buf = (int32 **)ckd_calloc_2d(pls->window, pls->n_phones, sizeof(**pls->pen_buf)); /* Initialize phone HMMs. */ if (pls->hmms) { for (i = 0; i < pls->n_phones; ++i) hmm_deinit((hmm_t *)&pls->hmms[i]); ckd_free(pls->hmms); } pls->hmms = (hmm_t *)ckd_calloc(pls->n_phones, sizeof(*pls->hmms)); for (i = 0; i < pls->n_phones; ++i) { hmm_init(pls->hmmctx, (hmm_t *)&pls->hmms[i], FALSE, bin_mdef_pid2ssid(acmod->mdef, i), bin_mdef_pid2tmatid(acmod->mdef, i)); } pls->penalty_weight = cmd_ln_float64_r(config, "-pl_weight"); pls->beam = logmath_log(acmod->lmath, cmd_ln_float64_r(config, "-pl_beam")) >> SENSCR_SHIFT; pls->pbeam = logmath_log(acmod->lmath, cmd_ln_float64_r(config, "-pl_pbeam")) >> SENSCR_SHIFT; pls->pip = logmath_log(acmod->lmath, cmd_ln_float32_r(config, "-pl_pip")) >> SENSCR_SHIFT; E_INFO("State beam %d Phone exit beam %d Insertion penalty %d\n", pls->beam, pls->pbeam, pls->pip); return 0; }
void parse_args_file(char *live_args) { static char **liveargs; static int32 nliveargs; int32 nargs, maxarglen; char *argline, *targ; FILE *fp; if ((fp = fopen(live_args,"r")) == NULL) E_FATAL("Unable to open arguments file %s for reading\n",live_args); argline = (char*) ckd_calloc(10000,sizeof(char)); /* Longest line allowed */ nargs = 1; maxarglen = 0; while (fgets(argline,10000,fp) != NULL){ if ((targ = strtok(argline," \t\n")) == NULL) continue; /* Empty line in argfile */ if (strlen(targ) > (unsigned int)maxarglen) maxarglen = strlen(targ); nargs++; while ((targ = strtok(NULL," \t\n")) != NULL){ if (strlen(targ) > (unsigned int)maxarglen) maxarglen = strlen(targ); nargs++; } } rewind(fp); nliveargs = nargs; liveargs = (char**) ckd_calloc_2d(nargs,maxarglen+1,sizeof(char)); nargs = 1; while (fgets(argline,10000,fp) != NULL){ if ((targ = strtok(argline," \t\n")) == NULL) continue; /* Empty line in argfile */ strcpy(liveargs[nargs++],targ); while ((targ = strtok(NULL," \t\n")) != NULL){ strcpy(liveargs[nargs++],targ); } } fclose(fp); assert(nargs == nliveargs); free(argline); cmd_ln_parse(arg, nliveargs, liveargs); return; }
static void sseq_compress(mdef_t * m) { hash_table_t *h; s3senid_t **sseq; int32 n_sseq; int32 p, j, k; glist_t g; gnode_t *gn; hash_entry_t *he; k = m->n_emit_state * sizeof(s3senid_t); h = hash_table_new(m->n_phone, HASH_CASE_YES); n_sseq = 0; /* Identify unique senone-sequence IDs. BUG: tmat-id not being considered!! */ for (p = 0; p < m->n_phone; p++) { /* Add senone sequence to hash table */ if ((j = (long) hash_table_enter_bkey(h, (char *) (m->sseq[p]), k, (void *)(long)n_sseq)) == n_sseq) n_sseq++; m->phone[p].ssid = j; } /* Generate compacted sseq table */ sseq = (s3senid_t **) ckd_calloc_2d(n_sseq, m->n_emit_state, sizeof(s3senid_t)); /* freed in mdef_free() */ g = hash_table_tolist(h, &j); assert(j == n_sseq); for (gn = g; gn; gn = gnode_next(gn)) { he = (hash_entry_t *) gnode_ptr(gn); j = (int32)(long)hash_entry_val(he); memcpy(sseq[j], hash_entry_key(he), k); } glist_free(g); /* Free the old, temporary senone sequence table, replace with compacted one */ ckd_free_2d((void **) m->sseq); m->sseq = sseq; m->n_sseq = n_sseq; hash_table_free(h); }
fsg_history_t *fsg_history_init (word_fsg_t *fsg) { fsg_history_t *h; h = (fsg_history_t *) ckd_calloc (1, sizeof(fsg_history_t)); h->fsg = fsg; h->entries = blkarray_list_init(); if (fsg) { h->frame_entries = (glist_t **) ckd_calloc_2d (word_fsg_n_state(fsg), phoneCiCount(), sizeof(glist_t)); } else { h->frame_entries = NULL; } return h; }
int main(int argc, char *argv[]) { float32 **a; a = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32)); memcpy(a[0], foo, sizeof(float32) * 3 * 3); /* Should see 5.22 */ printf("%.2f\n", determinant(a, 3)); /* Should see -1.0 */ memcpy(a[0], bar, sizeof(float32) * 3 * 3); printf("%.2f\n", determinant(a, 3)); ckd_free_2d((void **)a); return 0; }
void fsg_history_set_fsg (fsg_history_t *h, word_fsg_t *fsg) { if (blkarray_list_n_valid(h->entries) != 0) { E_WARN("Switching FSG while history not empty; history cleared\n"); blkarray_list_reset (h->entries); } if (h->frame_entries) ckd_free_2d ((void **) h->frame_entries); h->frame_entries = NULL; h->fsg = fsg; if (fsg) { h->frame_entries = (glist_t **) ckd_calloc_2d (word_fsg_n_state(fsg), phoneCiCount(), sizeof(glist_t)); } }
/* * Precompute variances/(covariance-matrix-determinants) to simplify Mahalanobis distance * calculation (see libmain/gauden.*). Also, calculate 1/(det) for the original codebooks, * based on the VQ vars. */ static void subvq_ivar_idet_precompute (subvq_t *vq, float64 floor) { int32 s, r, c; float32 **idet; E_INFO("Precomputing 1/det(covar), 1/2*var\n"); /* Temporary idet array for vars in subvq */ idet = (float32 **) ckd_calloc_2d (vq->n_sv, vq->vqsize, sizeof(float32)); for (s = 0; s < vq->n_sv; s++) { for (r = 0; r < vq->vqsize; r++) { vector_nz_floor (vq->var[s][r], vq->svsize[s], floor); idet[s][r] = (float32) vector_maha_precomp (vq->var[s][r], vq->svsize[s]); } } vq->idet = idet; }
/* In the old S3 files, all senones have the same "shape" (#codewords/senone/feat) */ static void build_mgau2sen (senone_t *s, int32 n_cw) { int32 i, j, m, f; s3senid_t *sen; mixw_t *fw; /* Create mgau2sen map from sen2mgau */ s->mgau2sen = (mgau2sen_t *) ckd_calloc (s->n_mgau, sizeof(mgau2sen_t)); s->mgau2sen_idx = (uint32 *) ckd_calloc (s->n_sen, sizeof(int32)); for (i = 0; i < s->n_sen; i++) { m = s->sen2mgau[i]; assert ((m < s->n_mgau) && (m >= 0)); (s->mgau2sen[m].n_sen)++; } sen = (s3senid_t *) ckd_calloc (s->n_sen, sizeof(s3senid_t)); for (m = 0; m < s->n_mgau; m++) { s->mgau2sen[m].sen = sen; sen += s->mgau2sen[m].n_sen; s->mgau2sen[m].n_sen = 0; } for (i = 0; i < s->n_sen; i++) { m = s->sen2mgau[i]; j = s->mgau2sen[m].n_sen; s->mgau2sen[m].sen[j] = i; s->mgau2sen_idx[i] = j; (s->mgau2sen[m].n_sen)++; } /* Allocate space for the weights */ for (m = 0; m < s->n_mgau; m++) { fw = (mixw_t *) ckd_calloc (s->n_feat, sizeof(mixw_t)); s->mgau2sen[m].feat_mixw = fw; for (f = 0; f < s->n_feat; f++) { fw[f].n_wt = n_cw; fw[f].prob = (senprob_t **) ckd_calloc_2d (s->mgau2sen[m].n_sen, n_cw, sizeof(senprob_t)); } } }
void fsg_history_set_fsg(fsg_history_t *h, fsg_model_t *fsg, dict_t *dict) { if (blkarray_list_n_valid(h->entries) != 0) { E_WARN("Switching FSG while history not empty; history cleared\n"); blkarray_list_reset(h->entries); } if (h->frame_entries) ckd_free_2d((void **) h->frame_entries); h->frame_entries = NULL; h->fsg = fsg; if (fsg && dict) { h->n_ciphone = bin_mdef_n_ciphone(dict->mdef); h->frame_entries = (glist_t **) ckd_calloc_2d(fsg_model_n_state(fsg), bin_mdef_n_ciphone(dict->mdef), sizeof(glist_t)); } }
fsg_history_t * fsg_history_init(fsg_model_t * fsg, dict_t *dict) { fsg_history_t *h; h = (fsg_history_t *) ckd_calloc(1, sizeof(fsg_history_t)); h->fsg = fsg; h->entries = blkarray_list_init(); if (fsg && dict) { h->n_ciphone = bin_mdef_n_ciphone(dict->mdef); h->frame_entries = (glist_t **) ckd_calloc_2d(fsg_model_n_state(fsg), bin_mdef_n_ciphone(dict->mdef), sizeof(**h->frame_entries)); } else { h->frame_entries = NULL; } return h; }
feat_t * feat_init(char const *type, cmn_type_t cmn, int32 varnorm, agc_type_t agc, int32 breport, int32 cepsize) { feat_t *fcb; if (cepsize == 0) cepsize = 13; if (breport) E_INFO ("Initializing feature stream to type: '%s', ceplen=%d, CMN='%s', VARNORM='%s', AGC='%s'\n", type, cepsize, cmn_type_str[cmn], varnorm ? "yes" : "no", agc_type_str[agc]); fcb = (feat_t *) ckd_calloc(1, sizeof(feat_t)); fcb->refcount = 1; fcb->name = (char *) ckd_salloc(type); if (strcmp(type, "s2_4x") == 0) { /* Sphinx-II format 4-stream feature (Hack!! hardwired constants below) */ if (cepsize != 13) { E_ERROR("s2_4x features require cepsize == 13\n"); ckd_free(fcb); return 0; } fcb->cepsize = 13; fcb->n_stream = 4; fcb->stream_len = (uint32 *) ckd_calloc(4, sizeof(uint32)); fcb->stream_len[0] = 12; fcb->stream_len[1] = 24; fcb->stream_len[2] = 3; fcb->stream_len[3] = 12; fcb->out_dim = 51; fcb->window_size = 4; fcb->compute_feat = feat_s2_4x_cep2feat; } else if ((strcmp(type, "s3_1x39") == 0) || (strcmp(type, "1s_12c_12d_3p_12dd") == 0)) { /* 1-stream cep/dcep/pow/ddcep (Hack!! hardwired constants below) */ if (cepsize != 13) { E_ERROR("s2_4x features require cepsize == 13\n"); ckd_free(fcb); return 0; } fcb->cepsize = 13; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = 39; fcb->out_dim = 39; fcb->window_size = 3; fcb->compute_feat = feat_s3_1x39_cep2feat; } else if (strncmp(type, "1s_c_d_dd", 9) == 0) { fcb->cepsize = cepsize; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = cepsize * 3; fcb->out_dim = cepsize * 3; fcb->window_size = FEAT_DCEP_WIN + 1; /* ddcep needs the extra 1 */ fcb->compute_feat = feat_1s_c_d_dd_cep2feat; } else if (strncmp(type, "1s_c_d_ld_dd", 12) == 0) { fcb->cepsize = cepsize; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = cepsize * 4; fcb->out_dim = cepsize * 4; fcb->window_size = FEAT_DCEP_WIN * 2; fcb->compute_feat = feat_1s_c_d_ld_dd_cep2feat; } else if (strncmp(type, "cep_dcep", 8) == 0 || strncmp(type, "1s_c_d", 6) == 0) { /* 1-stream cep/dcep */ fcb->cepsize = cepsize; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = feat_cepsize(fcb) * 2; fcb->out_dim = fcb->stream_len[0]; fcb->window_size = 2; fcb->compute_feat = feat_s3_cep_dcep; } else if (strncmp(type, "cep", 3) == 0 || strncmp(type, "1s_c", 4) == 0) { /* 1-stream cep */ fcb->cepsize = cepsize; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = feat_cepsize(fcb); fcb->out_dim = fcb->stream_len[0]; fcb->window_size = 0; fcb->compute_feat = feat_s3_cep; } else if (strncmp(type, "1s_3c", 5) == 0 || strncmp(type, "1s_4c", 5) == 0) { /* 1-stream cep with frames concatenated, so called cepwin features */ if (strncmp(type, "1s_3c", 5) == 0) fcb->window_size = 3; else fcb->window_size = 4; fcb->cepsize = cepsize; fcb->n_stream = 1; fcb->stream_len = (uint32 *) ckd_calloc(1, sizeof(uint32)); fcb->stream_len[0] = feat_cepsize(fcb) * (2 * fcb->window_size + 1); fcb->out_dim = fcb->stream_len[0]; fcb->compute_feat = feat_copy; } else { int32 i, l, k; char *strp; char *mtype = ckd_salloc(type); char *wd = ckd_salloc(type); /* * Generic definition: Format should be %d,%d,%d,...,%d (i.e., * comma separated list of feature stream widths; #items = * #streams). An optional window size (frames will be * concatenated) is also allowed, which can be specified with * a colon after the list of feature streams. */ l = strlen(mtype); k = 0; for (i = 1; i < l - 1; i++) { if (mtype[i] == ',') { mtype[i] = ' '; k++; } else if (mtype[i] == ':') { mtype[i] = '\0'; fcb->window_size = atoi(mtype + i + 1); break; } } k++; /* Presumably there are (#commas+1) streams */ fcb->n_stream = k; fcb->stream_len = (uint32 *) ckd_calloc(k, sizeof(uint32)); /* Scan individual feature stream lengths */ strp = mtype; i = 0; fcb->out_dim = 0; fcb->cepsize = 0; #ifndef POCKETSPHINX_NET while (sscanf(strp, "%s%n", wd, &l) == 1) #else while (net_sscanf_word(strp, wd, &l) == 1) #endif { strp += l; if ((i >= fcb->n_stream) || #ifndef POCKETSPHINX_NET (sscanf(wd, "%d", &(fcb->stream_len[i])) != 1) #else UInt32::TryParse(gcnew String(wd), fcb->stream_len[i]) #endif || (fcb->stream_len[i] <= 0)) E_FATAL("Bad feature type argument\n"); /* Input size before windowing */ fcb->cepsize += fcb->stream_len[i]; if (fcb->window_size > 0) fcb->stream_len[i] *= (fcb->window_size * 2 + 1); /* Output size after windowing */ fcb->out_dim += fcb->stream_len[i]; i++; } if (i != fcb->n_stream) E_FATAL("Bad feature type argument\n"); if (fcb->cepsize != cepsize) E_FATAL("Bad feature type argument\n"); /* Input is already the feature stream */ fcb->compute_feat = feat_copy; ckd_free(mtype); ckd_free(wd); } if (cmn != CMN_NONE) fcb->cmn_struct = cmn_init(feat_cepsize(fcb)); fcb->cmn = cmn; fcb->varnorm = varnorm; if (agc != AGC_NONE) { fcb->agc_struct = agc_init(); /* * No need to check if agc is set to EMAX; agc_emax_set() changes only emax related things * Moreover, if agc is not NONE and block mode is used, feat_agc() SILENTLY * switches to EMAX */ /* HACK: hardwired initial estimates based on use of CMN (from Sphinx2) */ agc_emax_set(fcb->agc_struct, (cmn != CMN_NONE) ? 5.0 : 10.0); } fcb->agc = agc; /* * Make sure this buffer is large enough to be used in feat_s2mfc2feat_block_utt() */ fcb->cepbuf = (mfcc_t **) ckd_calloc_2d((LIVEBUFBLOCKSIZE < feat_window_size(fcb) * 2) ? feat_window_size(fcb) * 2 : LIVEBUFBLOCKSIZE, feat_cepsize(fcb), sizeof(mfcc_t)); /* This one is actually just an array of pointers to "flatten out" * wraparounds. */ fcb->tmpcepbuf = (mfcc_t**)ckd_calloc(2 * feat_window_size(fcb) + 1, sizeof(*fcb->tmpcepbuf)); return fcb; }
/** * Read Sphinx-II format mfc file (s2mfc = Sphinx-II format MFC data). * If out_mfc is 0, no actual reading will be done, and the number of * frames (plus padding) that would be read is returned. * * It's important that normalization is done before padding because * frames outside the data we are interested in shouldn't be taken * into normalization stats. * * @return # frames read (plus padding) if successful, -1 if * error (e.g., mfc array too small). */ static int32 feat_s2mfc_read_norm_pad(feat_t *fcb, char *file, int32 win, int32 sf, int32 ef, mfcc_t ***out_mfc, int32 maxfr, int32 cepsize) { FILE *fp; int32 n_float32; float32 *float_feat; struct stat statbuf; int32 i, n, byterev; int32 start_pad, end_pad; mfcc_t **mfc; /* Initialize the output pointer to 0, so that any attempts to free() it if we fail before allocating it will not segfault! */ if (out_mfc) *out_mfc = 0; E_INFO("Reading mfc file: '%s'[%d..%d]\n", file, sf, ef); if (ef >= 0 && ef <= sf) { E_ERROR("%s: End frame (%d) <= Start frame (%d)\n", file, ef, sf); return -1; } /* Find filesize; HACK!! To get around intermittent NFS failures, use stat_retry */ if ((stat_retry(file, &statbuf) < 0) || ((fp = fopen(file, "rb")) == 0)) { #ifndef POCKETSPHINX_NET E_ERROR("Failed to open file '%s' for reading: %s\n", file, strerror(errno)); #endif return -1; } /* Read #floats in header */ if (fread_retry(&n_float32, sizeof(int32), 1, fp) != 1) { E_ERROR("%s: fread(#floats) failed\n", file); fclose(fp); return -1; } /* Check if n_float32 matches file size */ byterev = 0; if ((int32) (n_float32 * sizeof(float32) + 4) != (int32) statbuf.st_size) { /* RAH, typecast both sides to remove compile warning */ n = n_float32; SWAP_INT32(&n); if ((int32) (n * sizeof(float32) + 4) != (int32) (statbuf.st_size)) { /* RAH, typecast both sides to remove compile warning */ E_ERROR ("%s: Header size field: %d(%08x); filesize: %d(%08x)\n", file, n_float32, n_float32, statbuf.st_size, statbuf.st_size); fclose(fp); return -1; } n_float32 = n; byterev = 1; } if (n_float32 <= 0) { E_ERROR("%s: Header size field (#floats) = %d\n", file, n_float32); fclose(fp); return -1; } /* Convert n to #frames of input */ n = n_float32 / cepsize; if (n * cepsize != n_float32) { E_ERROR("Header size field: %d; not multiple of %d\n", n_float32, cepsize); fclose(fp); return -1; } /* Check start and end frames */ if (sf > 0) { if (sf >= n) { E_ERROR("%s: Start frame (%d) beyond file size (%d)\n", file, sf, n); fclose(fp); return -1; } } if (ef < 0) ef = n-1; else if (ef >= n) { E_WARN("%s: End frame (%d) beyond file size (%d), will truncate\n", file, ef, n); ef = n-1; } /* Add window to start and end frames */ sf -= win; ef += win; if (sf < 0) { start_pad = -sf; sf = 0; } else start_pad = 0; if (ef >= n) { end_pad = ef - n + 1; ef = n - 1; } else end_pad = 0; /* Limit n if indicated by [sf..ef] */ if ((ef - sf + 1) < n) n = (ef - sf + 1); if (maxfr > 0 && n + start_pad + end_pad > maxfr) { E_ERROR("%s: Maximum output size(%d frames) < actual #frames(%d)\n", file, maxfr, n + start_pad + end_pad); fclose(fp); return -1; } /* If no output buffer was supplied, then skip the actual data reading. */ if (out_mfc != 0) { /* Position at desired start frame and read actual MFC data */ mfc = (mfcc_t **)ckd_calloc_2d(n + start_pad + end_pad, cepsize, sizeof(mfcc_t)); if (sf > 0) fseek(fp, sf * cepsize * sizeof(float32), SEEK_CUR); n_float32 = n * cepsize; #ifdef FIXED_POINT float_feat = ckd_calloc(n_float32, sizeof(float32)); #else float_feat = mfc[start_pad]; #endif if (fread_retry(float_feat, sizeof(float32), n_float32, fp) != n_float32) { E_ERROR("%s: fread(%dx%d) (MFC data) failed\n", file, n, cepsize); ckd_free_2d(mfc); fclose(fp); return -1; } if (byterev) { for (i = 0; i < n_float32; i++) { SWAP_FLOAT32(&float_feat[i]); } } #ifdef FIXED_POINT for (i = 0; i < n_float32; ++i) { mfc[start_pad][i] = FLOAT2MFCC(float_feat[i]); } ckd_free(float_feat); #endif /* Normalize */ feat_cmn(fcb, mfc + start_pad, n, 1, 1); feat_agc(fcb, mfc + start_pad, n, 1, 1); /* Replicate start and end frames if necessary. */ for (i = 0; i < start_pad; ++i) memcpy(mfc[i], mfc[start_pad], cepsize * sizeof(mfcc_t)); for (i = 0; i < end_pad; ++i) memcpy(mfc[start_pad + n + i], mfc[start_pad + n - 1], cepsize * sizeof(mfcc_t)); *out_mfc = mfc; } fclose(fp); return n + start_pad + end_pad; }
int inc_densities(float32 ***new_mixw, vector_t ***new_mean, vector_t ***new_var, vector_t ****new_fullvar, float32 ***mixw, vector_t ***mean, vector_t ***var, vector_t ****fullvar, float32 ***dnom, uint32 n_mixw, uint32 n_mgau, uint32 n_dnom, uint32 n_feat, uint32 n_density, const uint32 *veclen, uint32 n_inc) { uint32 i, j, k, l, r; uint32 **did; float32 max_wt; uint32 max_wt_idx; float32 std; assert(n_mgau <= n_mixw); if (n_mgau < n_mixw) { E_FATAL("Splitting of the tied mixture gaussians is not yet implemented\n"); } /* copy old parameters into new arrays */ for (i = 0; i < n_mgau; i++) { for (j = 0; j < n_feat; j++) { for (k = 0; k < n_density; k++) { memcpy(new_mean[i][j][k], mean[i][j][k], veclen[j]*sizeof(float32)); if (fullvar) memcpy(new_fullvar[i][j][k][0], fullvar[i][j][k][0], veclen[j]*veclen[j]*sizeof(float32)); else memcpy(new_var[i][j][k], var[i][j][k], veclen[j]*sizeof(float32)); new_mixw[i][j][k] = mixw[i][j][k]; } } } /* Over all mixtures: * - Find the largest unsplit component density. * - Split it into two components where the new component * mixw_a = mixw_b = 1/2 mixw * mean_a = mean + 0.2 std * mean_b = mean - 0.2 std * var_a = var_b = var * * New parameters are placed beginning at index n_density */ did = (uint32 **)ckd_calloc_2d(n_feat, n_inc, sizeof(uint32)); for (i = 0; i < n_mgau; i++) { printf("%u:", i); fflush(stdout); for (r = 0; r < n_inc; r++) { for (j = 0; j < n_feat; j++) { did[j][r] = n_density; /* find the density w/ the largest EM count not yet split (i.e. most probable, most occurances) */ for (k = 0, max_wt = -1.0, max_wt_idx = n_density; k < n_density; k++) { if ((max_wt < dnom[i][j][k]) && not_done(k, did[j], r)) { max_wt = dnom[i][j][k]; max_wt_idx = k; } } if ( dnom[i][j][max_wt_idx] < MIN_IEEE_NORM_POS_FLOAT32 ) { E_WARN("(mgau= %u, feat= %u, density= %u) never observed skipping\n", i, j, k); new_mixw[i][j][n_density+r] = 0; if (fullvar) memcpy(new_fullvar[i][j][n_density+r][0], fullvar[i][j][0][0], veclen[j]*veclen[j]*sizeof(float32)); else memcpy(new_var[i][j][n_density+r], var[i][j][0], veclen[j]*sizeof(float32)); memcpy(new_mean[i][j][n_density+r], mean[i][j][0], veclen[j]*sizeof(float32)); continue; } /* mixing weight of prior and new densities == 1/2 prior mixing weight */ new_mixw[i][j][max_wt_idx] /= 2; new_mixw[i][j][n_density+r] = new_mixw[i][j][max_wt_idx]; /* Keep variance of new class same as old */ if (fullvar) memcpy(new_fullvar[i][j][n_density+r][0], fullvar[i][j][max_wt_idx][0], veclen[j]*veclen[j]*sizeof(float32)); else memcpy(new_var[i][j][n_density+r], var[i][j][max_wt_idx], veclen[j]*sizeof(float32)); /* mean_a = mean + 0.2 std */ /* mean_b = mean - 0.2 std */ for (l = 0; l < veclen[j]; l++) { /* Use the stddev of mean[l] itself for full covariances. */ if (fullvar) std = (float32)sqrt(fullvar[i][j][max_wt_idx][l][l]); else std = (float32)sqrt(var[i][j][max_wt_idx][l]); new_mean[i][j][max_wt_idx][l] = mean[i][j][max_wt_idx][l] + 0.2f * std; new_mean[i][j][n_density+r][l] = mean[i][j][max_wt_idx][l] - 0.2f * std; } did[j][r] = max_wt_idx; printf("%u(%.2e)", did[j][r], max_wt); fflush(stdout); } } printf("\n"); } return S3_SUCCESS; }
void segment_audio() { FILE *file; int16 pcm_buf[BLOCKSIZE]; mfcc_t **cep_buf; int16 voiced_buf = NULL; int32 voiced_nsamps, out_frameidx, uttstart = 0; char file_name[1024]; uint8 cur_vad_state, vad_state, writing; int uttno, uttlen, sample_rate; int32 nframes, nframes_tmp; int16 frame_size, frame_shift, frame_rate; size_t k; sample_rate = (int) cmd_ln_float32_r(config, "-samprate"); frame_rate = cmd_ln_int32_r(config, "-frate"); frame_size = (int32) (cmd_ln_float32_r(config, "-wlen") * sample_rate + 0.5); frame_shift = (int32) (sample_rate / cmd_ln_int32_r(config, "-frate") + 0.5); nframes = (BLOCKSIZE - frame_size) / frame_shift; cep_buf = (mfcc_t **) ckd_calloc_2d(nframes, fe_get_output_size(fe), sizeof(mfcc_t)); uttno = 0; uttlen = 0; cur_vad_state = 0; voiced_nsamps = 0; writing = 0; file = NULL; fe_start_stream(fe); fe_start_utt(fe); while ((k = read_audio(pcm_buf, BLOCKSIZE)) > 0) { int16 const *pcm_buf_tmp; pcm_buf_tmp = &pcm_buf[0]; while (k) { nframes_tmp = nframes; fe_process_frames_ext(fe, &pcm_buf_tmp, &k, cep_buf, &nframes_tmp, voiced_buf, &voiced_nsamps, &out_frameidx); if (out_frameidx > 0) { uttstart = out_frameidx; } vad_state = fe_get_vad_state(fe); if (!cur_vad_state && vad_state) { /* silence->speech transition, time to start new file */ uttno++; if (!singlefile) { sprintf(file_name, "%s%04d.raw", infile_path, uttno); if ((file = fopen(file_name, "wb")) == NULL) E_FATAL_SYSTEM("Failed to open '%s' for writing", file_name); } else { sprintf(file_name, "%s.raw", infile_path); if ((file = fopen(file_name, "ab")) == NULL) E_FATAL_SYSTEM("Failed to open '%s' for writing", file_name); } writing = 1; } if (writing && file && voiced_nsamps > 0) { fwrite(voiced_buf, sizeof(int16), voiced_nsamps, file); uttlen += voiced_nsamps; } if (cur_vad_state && !vad_state) { /* speech -> silence transition, time to finish file */ fclose(file); printf("Utterance %04d: file %s start %.1f sec length %d samples ( %.2f sec )\n", uttno, file_name, ((double) uttstart) / frame_rate, uttlen, ((double) uttlen) / sample_rate); fflush(stdout); fe_end_utt(fe, cep_buf[0], &nframes_tmp); writing = 0; uttlen = 0; voiced_nsamps = 0; fe_start_utt(fe); } cur_vad_state = vad_state; } } if (writing) { fclose(file); printf("Utterance %04d: file %s start %.1f sec length %d samples ( %.2f sec )\n", uttno, file_name, ((double) uttstart) / frame_rate, uttlen, ((double) uttlen) / sample_rate); fflush(stdout); } fe_end_utt(fe, cep_buf[0], &nframes); ckd_free_2d(cep_buf); }
/* * 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; }
int main(int argc, char *argv[]) { float32 **a, **ainv, **ii; int i, j; a = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32)); ainv = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32)); ii = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32)); memcpy(a[0], foo, sizeof(float32) * 3 * 3); printf("%d\n", invert(ainv, a, 3)); /* Should see: 0.75 -0.25 -0.25 -0.25 0.75 -0.25 -0.25 -0.25 0.75 */ for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { printf("%.2f ", ainv[i][j]); } printf("\n"); } /* Should see: 1.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 1.00 */ matrixmultiply(ii, ainv, a, 3); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { printf("%.2f ", ii[i][j]); } printf("\n"); } memcpy(a[0], bar, sizeof(float32) * 3 * 3); printf("%d\n", invert(ainv, a, 3)); /* Should see: */ for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { printf("%.2f ", ainv[i][j]); } printf("\n"); } /* Should see: 1.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 1.00 */ memset(ii[0], 0, sizeof(float32) * 3 * 3); matrixmultiply(ii, ainv, a, 3); for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { printf("%.2f ", ii[i][j]); } printf("\n"); } /* Should see: -1 */ a[0][0] = 1.0; printf("%d\n", invert(ainv, a, 3)); memcpy(a[0], foo, sizeof(float32) * 3 * 3); printf("%d\n", invert(a, a, 3)); /* Should see: 0.75 -0.25 -0.25 -0.25 0.75 -0.25 -0.25 -0.25 0.75 */ for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) { printf("%.2f ", a[i][j]); } printf("\n"); } ckd_free_2d((void **)a); ckd_free_2d((void **)ainv); ckd_free_2d((void **)ii); return 0; }
acmod_t * acmod_init(cmd_ln_t *config, logmath_t *lmath, fe_t *fe, feat_t *fcb) { acmod_t *acmod; char const *featparams; acmod = ckd_calloc(1, sizeof(*acmod)); acmod->config = cmd_ln_retain(config); acmod->lmath = lmath; acmod->state = ACMOD_IDLE; /* Look for feat.params in acoustic model dir. */ if ((featparams = cmd_ln_str_r(acmod->config, "-featparams"))) { if (NULL != cmd_ln_parse_file_r(acmod->config, feat_defn, featparams, FALSE)) E_INFO("Parsed model-specific feature parameters from %s\n", featparams); } /* Initialize feature computation. */ if (fe) { if (acmod_fe_mismatch(acmod, fe)) goto error_out; fe_retain(fe); acmod->fe = fe; } else { /* Initialize a new front end. */ acmod->fe = fe_init_auto_r(config); if (acmod->fe == NULL) goto error_out; if (acmod_fe_mismatch(acmod, acmod->fe)) goto error_out; } if (fcb) { if (acmod_feat_mismatch(acmod, fcb)) goto error_out; feat_retain(fcb); acmod->fcb = fcb; } else { /* Initialize a new fcb. */ if (acmod_init_feat(acmod) < 0) goto error_out; } /* Load acoustic model parameters. */ if (acmod_init_am(acmod) < 0) goto error_out; /* The MFCC buffer needs to be at least as large as the dynamic * feature window. */ acmod->n_mfc_alloc = acmod->fcb->window_size * 2 + 1; acmod->mfc_buf = (mfcc_t **) ckd_calloc_2d(acmod->n_mfc_alloc, acmod->fcb->cepsize, sizeof(**acmod->mfc_buf)); /* Feature buffer has to be at least as large as MFCC buffer. */ acmod->n_feat_alloc = acmod->n_mfc_alloc + cmd_ln_int32_r(config, "-pl_window"); acmod->feat_buf = feat_array_alloc(acmod->fcb, acmod->n_feat_alloc); acmod->framepos = ckd_calloc(acmod->n_feat_alloc, sizeof(*acmod->framepos)); acmod->utt_start_frame = 0; /* Senone computation stuff. */ acmod->senone_scores = ckd_calloc(bin_mdef_n_sen(acmod->mdef), sizeof(*acmod->senone_scores)); acmod->senone_active_vec = bitvec_alloc(bin_mdef_n_sen(acmod->mdef)); acmod->senone_active = ckd_calloc(bin_mdef_n_sen(acmod->mdef), sizeof(*acmod->senone_active)); acmod->log_zero = logmath_get_zero(acmod->lmath); acmod->compallsen = cmd_ln_boolean_r(config, "-compallsen"); return acmod; error_out: acmod_free(acmod); return NULL; }
int read_cep(char const *file, float ***cep, int *numframes, int cepsize) { FILE *fp; int n_float; struct stat statbuf; int i, n, byterev, sf, ef; float32 **mfcbuf; if (stat_retry(file, &statbuf) < 0) { printf("stat(%s) failed\n", file); return IO_ERR; } if ((fp = fopen(file, "rb")) == NULL) { printf("fopen(%s, rb) failed\n", file); return IO_ERR; } /* Read #floats in header */ if (fread(&n_float, sizeof(int), 1, fp) != 1) { fclose(fp); return IO_ERR; } /* Check if n_float matches file size */ byterev = FALSE; if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) { n = n_float; SWAP_INT32(&n); if ((int) (n * sizeof(float) + 4) != statbuf.st_size) { printf ("Header size field: %d(%08x); filesize: %d(%08x)\n", n_float, n_float, (int) statbuf.st_size, (int) statbuf.st_size); fclose(fp); return IO_ERR; } n_float = n; byterev = TRUE; } if (n_float <= 0) { printf("Header size field: %d\n", n_float); fclose(fp); return IO_ERR; } /* n = #frames of input */ n = n_float / cepsize; if (n * cepsize != n_float) { printf("Header size field: %d; not multiple of %d\n", n_float, cepsize); fclose(fp); return IO_ERR; } sf = 0; ef = n; mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32)); /* Read mfc data and byteswap if necessary */ n_float = n * cepsize; if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) { printf("Error reading mfc data\n"); fclose(fp); return IO_ERR; } if (byterev) { for (i = 0; i < n_float; i++) SWAP_FLOAT32(&(mfcbuf[0][i])); } fclose(fp); *numframes = n; *cep = mfcbuf; return IO_SUCCESS; }