📄 uttproc.c
字号:
TotalSpeechTime += searchFrame() * 0.01f;#if defined(_WIN32) && !defined(GNUWINCE) && !defined(CYGWIN) /* ---------------- _WIN32 ---------------- */# ifdef _WIN32_WCE e_stop = (float) GetTickCount() / 1000;# else e_stop = (float) clock() / CLOCKS_PER_SEC; GetProcessTimes(pid, &t_create, &t_exit, &ket, &uet);# endif E_INFOCONT(", %6.2f sec elapsed", (e_stop - e_start)); E_INFOCONT(", %5.2f xRT", (e_stop - e_start) / (searchFrame() * 0.01));# ifndef _WIN32_WCE E_INFOCONT(", %6.2f sec CPU", win32_cputime(&ust, &uet)); E_INFOCONT(", %5.2f xRT", win32_cputime(&ust, &uet) / (searchFrame() * 0.01));# endif TotalCPUTime += (float) win32_cputime(&ust, &uet); TotalElapsedTime += (e_stop - e_start);#else /* ---------------- Unix ---------------- */#if !(defined(_HPUX_SOURCE) || defined(GNUWINCE)) getrusage(RUSAGE_SELF, &stop);#endif gettimeofday(&e_stop, 0); E_INFOCONT(", %6.2f sec elapsed", MakeSeconds(&e_start, &e_stop)); E_INFOCONT(", %5.2f xRT", MakeSeconds(&e_start, &e_stop) / (searchFrame() * 0.01));#ifndef _HPUX_SOURCE E_INFOCONT(", %6.2f sec CPU", MakeSeconds(&start.ru_utime, &stop.ru_utime)); E_INFOCONT(", %5.2f xRT", MakeSeconds(&start.ru_utime, &stop.ru_utime) / (searchFrame() * 0.01));#endif TotalCPUTime += MakeSeconds(&start.ru_utime, &stop.ru_utime); TotalElapsedTime += MakeSeconds(&e_start, &e_stop);#endif E_INFOCONT("\n\n");}/* * One time cleanup before exiting program */static voidtiming_end(void){ E_INFO("\n"); E_INFO("TOTAL Elapsed time %.2f seconds\n", TotalElapsedTime);#ifndef _HPUX_SOURCE E_INFO("TOTAL CPU time %.2f seconds\n", TotalCPUTime);#endif E_INFO("TOTAL Speech %.2f seconds\n", TotalSpeechTime); if (TotalSpeechTime > 0.0) { E_INFO("AVERAGE %.2f xRT(Elapsed)", TotalElapsedTime / TotalSpeechTime);#ifndef _HPUX_SOURCE E_INFOCONT(", %.2f xRT(CPU)", TotalCPUTime / TotalSpeechTime);#endif E_INFOCONT("\n"); }}static voidfeat_alloc(void){ if (feat_buf) { feat_array_free(feat_buf); ckd_free_2d((void **)mfcbuf); } feat_buf = feat_array_alloc(fcb, MAX_UTT_LEN); mfcbuf = (mfcc_t **) ckd_calloc_2d(MAX_UTT_LEN + 10, feat_cepsize(fcb), sizeof(mfcc_t));}static voidwarn_notidle(char const *func){ if (uttstate != UTTSTATE_IDLE) E_WARN("%s called when not in IDLE state\n", func);}int32uttproc_get_featbuf(mfcc_t ****feat){ *feat = feat_buf; return n_featfr;}static voiduttproc_fsg_search_fwd(void){ int32 best; if (cmd_ln_boolean("-compallsen")) { best = senscr_all(feat_buf[n_searchfr], n_searchfr); } else { fsg_search_sen_active(fsg_search); best = senscr_active(feat_buf[n_searchfr], n_searchfr); } /* Note the best senone score for this frame */ search_set_topsen_score(fsg_search_frame(fsg_search), best); fsg_search_frame_fwd(fsg_search);}/* Convert all given mfc vectors to feature vectors, and search one frame */static int32uttproc_frame(void){ int32 pr, frm; char *str; search_hyp_t *hyp; /* Search one frame */ if (fsg_search_mode) uttproc_fsg_search_fwd(); else if (cmd_ln_boolean("-fwdtree")) search_fwd(feat_buf[n_searchfr]); else search_fwdflat_frame(feat_buf[n_searchfr]); ++n_searchfr; pr = cmd_ln_boolean("-phypdump"); if ((pr > 0) && ((n_searchfr % pr) == 1)) { /* Report partial result string */ uttproc_partial_result(&frm, &str); printf("PART[%d]: %s\n", frm, str); fflush(stdout); } pr = cmd_ln_boolean("-phypsegdump"); if ((pr > 0) && ((n_searchfr % pr) == 1)) { /* Report partial result segmentation */ uttproc_partial_result_seg(&frm, &hyp); printf("PARTSEG[%d]:", frm); for (; hyp; hyp = hyp->next) printf(" %s %d %d", hyp->word, hyp->sf, hyp->ef); printf("\n"); fflush(stdout); } return 0;}static voidfwdflat_search(int32 n_frames){ int32 i; search_fwdflat_start(); for (i = 0; i < n_frames; ++i) search_fwdflat_frame(feat_buf[i]); search_fwdflat_finish();}static voidwrite_results(char const *hyp, int32 aborted){ search_hyp_t *seghyp; /* Hyp with word segmentation information */ int32 i; /* Check if need to autonumber utterances */ if (matchfp) { fprintf(matchfp, "%s (%s %s %d)\n", hyp, uttid, aborted ? "[ABORTED]" : "", search_get_score()); fflush(matchfp); } /* Changed this to use Sphinx3 format */ if (matchsegfp) { seghyp = search_get_hyp(); fprintf(matchsegfp, "%s S %d T %d A %d L %d", uttid, 0, /* FIXME: scaling factors not recorded? */ search_get_score(), search_get_score() - search_get_lscr(), search_get_lscr()); for (i = 0; seghyp[i].wid >= 0; i++) { fprintf(matchsegfp, " %d %d %d %s", seghyp[i].sf, seghyp[i].ascr, lm3g_raw_score(seghyp[i].lscr), kb_get_word_str(seghyp[i].wid)); } fprintf(matchsegfp, " %d\n", searchFrame()); fflush(matchsegfp); }#if 0 { char const *dumplatdir; if ((dumplatdir = cmd_ln_str("-dumplatdir")) != NULL) { char fplatfile[1024]; sprintf(fplatfile, "%s/%s.fplat", dumplatdir, uttid); search_dump_lattice_ascii(fplatfile); } }#endif /* 0 */}static voiduttproc_windup(int32 * fr, char **hyp){ /* Wind up first pass and run next pass, if necessary */ if (fsg_search_mode) fsg_search_utt_end(fsg_search); else { if (cmd_ln_boolean("-fwdtree")) { search_finish_fwd(); if (cmd_ln_boolean("-fwdflat") && (searchFrame() > 0)) fwdflat_search(n_featfr); } else search_fwdflat_finish(); /* Run bestpath pass if specified */ /* FIXME: If we are doing N-best we also need to do this. */ if ((searchFrame() > 0) && cmd_ln_boolean("-bestpath")) bestpath_search(); } search_result(fr, hyp); write_results(*hyp, 0); timing_stop(*fr); uttstate = UTTSTATE_IDLE;}/* * One time initialization */static fe_t *fe;int32uttproc_init(void){ char const *fn; if (uttstate != UTTSTATE_UNDEF) { E_ERROR("uttproc_init called when not in UNDEF state\n"); return -1; } fe = fe_init_auto(); if (!fe) return -1; uttid = ckd_calloc(UTTIDSIZE, 1); if ((fn = cmd_ln_str("-hyp")) != NULL) { if ((matchfp = fopen(fn, "w")) == NULL) E_ERROR("fopen(%s,w) failed\n", fn); } if ((fn = cmd_ln_str("-hypseg")) != NULL) { if ((matchsegfp = fopen(fn, "w")) == NULL) E_ERROR("fopen(%s,w) failed\n", fn); } timing_init(); uttstate = UTTSTATE_IDLE; utt_ofl = 0; uttno = 0; /* Initialize the FSG search module */ { char *fsgfile; char *fsgname; char *fsgctlfile; FILE *ctlfp; char line[16384], word[16384]; fsg_search = fsg_search_init(NULL); fsgfile = cmd_ln_str("-fsg"); fsg_search_mode = (fsgfile != NULL); if (fsg_search_mode) { fsgname = uttproc_load_fsgfile(fsgfile); if (!fsgname) E_FATAL("Error loading FSG file '%s'\n", fsgfile); /* Make this FSG the currently active one */ if (uttproc_set_fsg(fsgname) < 0) E_FATAL("Error setting current FSG to '%s'\n", fsgname); E_INFO ("FSG Mode; lextree, flat, bestpath searches disabled\n"); } fsgctlfile = cmd_ln_str("-fsgctlfn"); if (fsgctlfile) { if ((ctlfp = fopen(fsgctlfile, "r")) == NULL) { /* Should this be E_ERROR?? */ E_FATAL("fopen(%s,r) failed\n", fsgctlfile); } while (fgets(line, sizeof(line), ctlfp) != NULL) { if ((line[0] == '#') /* Commented out */ ||(sscanf(line, "%s", word) != 1)) /* Blank line */ continue; fsgfile = word; fsgname = uttproc_load_fsgfile(fsgfile); if (!fsgname) { /* Should this be E_ERROR?? */ E_FATAL("Error loading FSG file '%s'\n", fsgfile); } } fclose(ctlfp); } } return 0;}/* * One time cleanup */int32uttproc_end(void){ if (uttstate != UTTSTATE_IDLE) { E_ERROR("uttproc_end called when not in IDLE state\n"); return -1; } if (matchfp) fclose(matchfp); if (matchsegfp) fclose(matchsegfp); timing_end(); return 0;}int32uttproc_begin_utt(char const *id){ char filename[1024]; int32 i; for (i = 0; i < 5; i++) samp_hist[i] = 0; max_samp = 0; if (uttstate != UTTSTATE_IDLE) { E_ERROR("uttproc_begin_utt called when not in IDLE state\n"); return -1; } if (fe_start_utt(fe) < 0) return -1; inputtype = INPUT_UNKNOWN; livemode = !(nosearch || (fcb->cmn == CMN_CURRENT) || ((fcb->agc != AGC_EMAX) && (fcb->agc != AGC_NONE))); E_INFO("%s\n", livemode ? "Livemode" : "Batchmode"); n_cepfr = n_featfr = n_searchfr = 0; utt_ofl = 0; uttno++; if (!id) sprintf(uttid, "%s%08d", uttid_prefix ? uttid_prefix : "", uttno); else strcpy(uttid, id); if (rawlogdir) { sprintf(filename, "%s/%s.raw", rawlogdir, uttid); if ((rawfp = fopen(filename, "wb")) == NULL) E_ERROR("fopen(%s,wb) failed\n", filename); else { strcpy(rawfilename, filename); E_INFO("Rawfile: %s\n", filename); } } if (mfclogdir) { int32 k = 0; sprintf(filename, "%s/%s.mfc", mfclogdir, uttid); if ((mfcfp = fopen(filename, "wb")) == NULL) E_ERROR("fopen(%s,wb) failed\n", filename); else fwrite(&k, sizeof(int32), 1, mfcfp); } timing_start(); if (!nosearch) { if (fsg_search_mode) fsg_search_utt_start(fsg_search); else if (cmd_ln_boolean("-fwdtree")) search_start_fwd(); else search_fwdflat_start(); } uttstate = UTTSTATE_BEGUN; uttstart = TRUE; return 0;}int32uttproc_rawdata(int16 * raw, int32 len, int32 block){ int32 i, k, v, nfr; mfcc_t **temp_mfc; for (i = 0; i < len; i++) { v = raw[i]; if (v < 0) v = -v; if (v > max_samp) max_samp = v; if (v < 4096) samp_hist[0]++; else if (v < 8192) samp_hist[1]++; else if (v < 16384) samp_hist[2]++; else if (v < 30720) samp_hist[3]++; else samp_hist[4]++; } if (uttstate != UTTSTATE_BEGUN) { E_ERROR("uttproc_rawdata called when utterance not begun\n"); return -1; } if (inputtype == INPUT_MFC) { E_ERROR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -