static int write_lattice(ps_decoder_t *ps, char const *latdir, char const *uttid) { ps_lattice_t *lat; logmath_t *lmath; cmd_ln_t *config; char *outfile; int32 beam; if ((lat = ps_get_lattice(ps)) == NULL) { E_ERROR("Failed to obtain word lattice for utterance %s\n", uttid); return -1; } config = ps_get_config(ps); outfile = string_join(latdir, "/", uttid, cmd_ln_str_r(config, "-outlatext"), NULL); /* Prune lattice. */ lmath = ps_get_logmath(ps); beam = logmath_log(lmath, cmd_ln_float64_r(config, "-outlatbeam")); ps_lattice_posterior_prune(lat, beam); if (0 == strcmp("htk", cmd_ln_str_r(config, "-outlatfmt"))) { if (ps_lattice_write_htk(lat, outfile) < 0) { E_ERROR("Failed to write lattice to %s\n", outfile); return -1; } } else { if (ps_lattice_write(lat, outfile) < 0) { E_ERROR("Failed to write lattice to %s\n", outfile); return -1; } } return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; fsg_search_t *fsgs; ps_lattice_t *dag; ps_seg_t *seg; int32 score; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", DATADIR "/tidigits/hmm", "-fsg", DATADIR "/tidigits/lm/tidigits.fsg", "-dict", DATADIR "/tidigits/lm/tidigits.dic", "-bestpath", "no", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); fsgs = (fsg_search_t *)ps->search; acmod = ps->acmod; setbuf(stdout, NULL); { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; char const *hyp; int nfr; TEST_ASSERT(rawfh = fopen(DATADIR "/numbers.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); fsg_search_start(ps_search_base(fsgs)); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { fsg_search_step(ps_search_base(fsgs), acmod->output_frame); acmod_advance(acmod); } } } fsg_search_finish(ps_search_base(fsgs)); hyp = fsg_search_hyp(ps_search_base(fsgs), &score, NULL); printf("FSG: %s (%d)\n", hyp, score); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } for (seg = ps_seg_iter(ps); seg; seg = ps_seg_next(seg)) { char const *word; int sf, ef; int32 post, lscr, ascr, lback; word = ps_seg_word(seg); ps_seg_frames(seg, &sf, &ef); post = ps_seg_prob(seg, &ascr, &lscr, &lback); printf("%s (%d:%d) P(w|o) = %f ascr = %d lscr = %d lback = %d\n", word, sf, ef, logmath_exp(ps_get_logmath(ps), post), ascr, lscr, lback); } /* Now get the DAG and play with it. */ dag = ps_get_lattice(ps); ps_lattice_write(dag, "test_fsg3.lat"); printf("BESTPATH: %s\n", ps_lattice_hyp(dag, ps_lattice_bestpath(dag, NULL, 1.0, 15.0))); ps_lattice_posterior(dag, NULL, 15.0); ps_free(ps); cmd_ln_free_r(config); return 0; }
int test_decode(ps_decoder_t *ps) { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; ps_lattice_t *dag; acmod_t *acmod; ngram_search_t *ngs; int i, j; ps_latlink_t *link; ps_latnode_t *node; latlink_list_t *x; int32 norm, post; ngs = (ngram_search_t *)ps->search; acmod = ps->acmod; /* Decode stuff and build a DAG. */ TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ngram_fwdtree_start(ngs); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdtree_search(ngs, acmod->output_frame); acmod_advance(acmod); } } } ngram_fwdtree_finish(ngs); printf("FWDTREE: %s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); dag = ngram_search_lattice(ps->search); if (dag == NULL) { E_ERROR("Failed to build DAG!\n"); return -1; } /* Write lattice to disk. */ TEST_EQUAL(0, ps_lattice_write(dag, "test_posterior.lat")); /* Do a bunch of checks on the DAG generation and traversal code: */ /* Verify that forward and backward iteration give the same number of edges. */ i = j = 0; for (link = ps_lattice_traverse_edges(dag, NULL, NULL); link; link = ps_lattice_traverse_next(dag, NULL)) { ++i; } for (link = ps_lattice_reverse_edges(dag, NULL, NULL); link; link = ps_lattice_reverse_next(dag, NULL)) { ++j; } printf("%d forward edges, %d reverse edges\n", i, j); TEST_EQUAL(i,j); /* Verify that the same links are reachable via entries and exits. */ for (node = dag->nodes; node; node = node->next) { for (x = node->exits; x; x = x->next) x->link->alpha = -42; } for (node = dag->nodes; node; node = node->next) { for (x = node->entries; x; x = x->next) TEST_EQUAL(x->link->alpha, -42); } /* Verify that forward iteration is properly ordered. */ for (link = ps_lattice_traverse_edges(dag, NULL, NULL); link; link = ps_lattice_traverse_next(dag, NULL)) { link->alpha = 0; for (x = link->from->entries; x; x = x->next) { TEST_EQUAL(x->link->alpha, 0); } } /* Verify that backward iteration is properly ordered. */ for (node = dag->nodes; node; node = node->next) { for (x = node->exits; x; x = x->next) x->link->alpha = -42; } for (link = ps_lattice_reverse_edges(dag, NULL, NULL); link; link = ps_lattice_reverse_next(dag, NULL)) { link->alpha = 0; for (x = link->to->exits; x; x = x->next) { TEST_EQUAL(x->link->alpha, 0); } } /* Find and print best path. */ link = ps_lattice_bestpath(dag, ngs->lmset, 1.0, 1.0/20.0); printf("BESTPATH: %s\n", ps_lattice_hyp(dag, link)); /* Calculate betas. */ post = ps_lattice_posterior(dag, ngs->lmset, 1.0/20.0); printf("Best path score: %d\n", link->path_scr + dag->final_node_ascr); printf("P(S|O) = %d\n", post); /* Verify that sum of final alphas and initial alphas+betas is * sufficiently similar. */ norm = logmath_get_zero(acmod->lmath); for (x = dag->start->exits; x; x = x->next) norm = logmath_add(acmod->lmath, norm, x->link->beta + x->link->alpha); E_INFO("Sum of final alphas+betas = %d\n", dag->norm); E_INFO("Sum of initial alphas+betas = %d\n", norm); TEST_EQUAL_LOG(dag->norm, norm); /* Print posterior probabilities for each link in best path. */ while (link) { printf("P(%s,%d) = %d = %f\n", dict_wordstr(ps->search->dict, link->from->wid), link->ef, link->alpha + link->beta - dag->norm, logmath_exp(acmod->lmath, link->alpha + link->beta - dag->norm)); link = link->best_prev; } return 0; }
static gboolean gst_pocketsphinx_event(GstPad *pad, GstEvent *event) { GstPocketSphinx *ps; ps = GST_POCKETSPHINX(GST_OBJECT_PARENT(pad)); /* Pick out VAD events. */ switch (event->type) { case GST_EVENT_NEWSEGMENT: /* Initialize the decoder once the audio starts, if it's not * there yet. */ if (ps->ps == NULL) { ps->ps = ps_init(ps->config); if (ps->ps == NULL) { GST_ELEMENT_ERROR(GST_ELEMENT(ps), LIBRARY, INIT, ("Failed to initialize PocketSphinx"), ("Failed to initialize PocketSphinx")); return FALSE; } } return gst_pad_event_default(pad, event); case GST_EVENT_VADER_START: ps->listening = TRUE; ps_start_utt(ps->ps, NULL); /* Forward this event. */ return gst_pad_event_default(pad, event); case GST_EVENT_EOS: case GST_EVENT_VADER_STOP: { GstBuffer *buffer; int32 score; char const *hyp; char const *uttid; hyp = NULL; if (ps->listening) { ps->listening = FALSE; ps_end_utt(ps->ps); hyp = ps_get_hyp(ps->ps, &score, &uttid); /* Dump the lattice if requested. */ if (ps->latdir) { char *latfile = string_join(ps->latdir, "/", uttid, ".lat", NULL); ps_lattice_t *dag; if ((dag = ps_get_lattice(ps->ps))) ps_lattice_write(dag, latfile); ckd_free(latfile); } } if (hyp) { /* Emit a signal for applications. */ g_signal_emit(ps, gst_pocketsphinx_signals[SIGNAL_RESULT], 0, hyp, uttid); /* Forward this result in a buffer. */ buffer = gst_buffer_new_and_alloc(strlen(hyp) + 2); strcpy((char *)GST_BUFFER_DATA(buffer), hyp); GST_BUFFER_DATA(buffer)[strlen(hyp)] = '\n'; GST_BUFFER_DATA(buffer)[strlen(hyp)+1] = '\0'; GST_BUFFER_TIMESTAMP(buffer) = GST_EVENT_TIMESTAMP(event); gst_buffer_set_caps(buffer, GST_PAD_CAPS(ps->srcpad)); gst_pad_push(ps->srcpad, buffer); } /* Forward this event. */ return gst_pad_event_default(pad, event); } default: /* Don't bother with other events. */ return gst_pad_event_default(pad, event); } }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; fsg_search_t *fsgs; jsgf_t *jsgf; jsgf_rule_t *rule; fsg_model_t *fsg; ps_seg_t *seg; ps_lattice_t *dag; FILE *rawfh; char const *hyp, *uttid; int32 score, prob; clock_t c; int i; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); jsgf = jsgf_parse_file(DATADIR "/goforward.gram", NULL); TEST_ASSERT(jsgf); rule = jsgf_get_rule(jsgf, "<goforward.move2>"); TEST_ASSERT(rule); fsg = jsgf_build_fsg(jsgf, rule, ps->lmath, 7.5); TEST_ASSERT(fsg); fsg_model_write(fsg, stdout); ps_set_fsg(ps, "<goforward.move2>", fsg); ps_set_search(ps, "<goforward.move2>"); acmod = ps->acmod; fsgs = (fsg_search_t *) fsg_search_init(fsg, config, acmod, ps->dict, ps->d2p); setbuf(stdout, NULL); c = clock(); for (i = 0; i < 5; ++i) { int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; int is_final; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); fsg_search_start(ps_search_base(fsgs)); is_final = FALSE; while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { fsg_search_step(ps_search_base(fsgs), acmod->output_frame); acmod_advance(acmod); } } hyp = fsg_search_hyp(ps_search_base(fsgs), &score, &is_final); printf("FSG: %s (%d) frame %d final %s\n", hyp, score, acmod->output_frame, is_final ? "FINAL" : ""); TEST_EQUAL (is_final, (acmod->output_frame > 170)); } fsg_search_finish(ps_search_base(fsgs)); hyp = fsg_search_hyp(ps_search_base(fsgs), &score, NULL); printf("FSG: %s (%d)\n", hyp, score); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } TEST_EQUAL(0, strcmp("go forward ten meters", fsg_search_hyp(ps_search_base(fsgs), &score, NULL))); ps->search = (ps_search_t *)fsgs; for (seg = ps_seg_iter(ps, &score); seg; seg = ps_seg_next(seg)) { char const *word; int sf, ef; word = ps_seg_word(seg); ps_seg_frames(seg, &sf, &ef); printf("%s %d %d\n", word, sf, ef); } c = clock() - c; printf("5 * fsg search in %.2f sec\n", (double)c / CLOCKS_PER_SEC); dag = ps_get_lattice(ps); ps_lattice_write(dag, "test_jsgf.lat"); jsgf_grammar_free(jsgf); fsg_search_free(ps_search_base(fsgs)); ps_free(ps); cmd_ln_free_r(config); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/goforward.gram", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); ps_decode_raw(ps, rawfh, "goforward", -1); hyp = ps_get_hyp(ps, &score, &uttid); prob = ps_get_prob(ps, &uttid); printf("%s: %s (%d, %d)\n", uttid, hyp, score, prob); TEST_EQUAL(0, strcmp("go forward ten meters", hyp)); ps_free(ps); fclose(rawfh); cmd_ln_free_r(config); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/goforward.gram", "-toprule", "goforward.move2", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); ps_decode_raw(ps, rawfh, "goforward", -1); hyp = ps_get_hyp(ps, &score, &uttid); prob = ps_get_prob(ps, &uttid); printf("%s: %s (%d, %d)\n", uttid, hyp, score, prob); TEST_EQUAL(0, strcmp("go forward ten meters", hyp)); ps_free(ps); cmd_ln_free_r(config); fclose(rawfh); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/defective.gram", NULL)); TEST_ASSERT(NULL == ps_init(config)); cmd_ln_free_r(config); return 0; }