Пример #1
0
int
main(int argc, char *argv[])
{
	bin_mdef_t *mdef;
	dict_t *dict;
	cmd_ln_t *config;

	int i;
	char buf[100];

	TEST_ASSERT(config = cmd_ln_init(NULL, NULL, FALSE,
						   "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
						   "-fdict", MODELDIR "/en-us/en-us/noisedict",
						   NULL));

	/* Test dictionary in standard fashion. */
	TEST_ASSERT(mdef = bin_mdef_read(NULL, MODELDIR "/en-us/en-us/mdef"));
	TEST_ASSERT(dict = dict_init(config, mdef, NULL));

	printf("Word ID (CARNEGIE) = %d\n",
	       dict_wordid(dict, "CARNEGIE"));
	printf("Word ID (ASDFASFASSD) = %d\n",
	       dict_wordid(dict, "ASDFASFASSD"));

	TEST_EQUAL(0, dict_write(dict, "_cmu07a.dic", NULL));
	TEST_EQUAL(0, system("diff -uw " MODELDIR "/en-us/cmudict-en-us.dict _cmu07a.dic"));

	dict_free(dict);
	bin_mdef_free(mdef);

	/* Now test an empty dictionary. */
	TEST_ASSERT(dict = dict_init(NULL, NULL, NULL));
	printf("Word ID(<s>) = %d\n", dict_wordid(dict, "<s>"));
	TEST_ASSERT(BAD_S3WID != dict_add_word(dict, "FOOBIE", NULL, 0));
	TEST_ASSERT(BAD_S3WID != dict_add_word(dict, "BLETCH", NULL, 0));
	printf("Word ID(FOOBIE) = %d\n", dict_wordid(dict, "FOOBIE"));
	printf("Word ID(BLETCH) = %d\n", dict_wordid(dict, "BLETCH"));
	TEST_ASSERT(dict_real_word(dict, dict_wordid(dict, "FOOBIE")));
	TEST_ASSERT(dict_real_word(dict, dict_wordid(dict, "BLETCH")));
	TEST_ASSERT(!dict_real_word(dict, dict_wordid(dict, "</s>")));
	dict_free(dict);

	/* Test to add 500k words. */
	TEST_ASSERT(dict = dict_init(NULL, NULL, NULL));
	for (i = 0; i < 500000; i++) {
	    sprintf(buf, "word_%d", i);
    	    TEST_ASSERT(BAD_S3WID != dict_add_word(dict, buf, NULL, 0));
	}
	dict_free(dict);

	cmd_ln_free_r(config);

	return 0;
}
Пример #2
0
int
main(int argc, char *argv[])
{
    ngram_trie_t *t;
    dict_t *dict;
    bin_mdef_t *mdef;
    logmath_t *lmath;
    cmd_ln_t *config;
    FILE *arpafh;

    config = cmd_ln_init(NULL, ps_args(), TRUE,
                         "-hmm", TESTDATADIR "/hub4wsj_sc_8k",
                         "-dict", TESTDATADIR "/bn10000.homos.dic",
                         NULL);
    ps_init_defaults(config);

    lmath = logmath_init(cmd_ln_float32_r(config, "-logbase"),
                         0, FALSE);
    mdef = bin_mdef_read(config, cmd_ln_str_r(config, "-mdef"));
    dict = dict_init(config, mdef);

    t = ngram_trie_init(dict, lmath);
    arpafh = fopen(TESTDATADIR "/bn10000.3g.arpa", "r");
    ngram_trie_read_arpa(t, arpafh);
    fclose(arpafh);

    /* Test 1, 2, 3-gram probs without backoff. */
    test_lookups(t, lmath);

    arpafh = fopen("tmp.bn10000.3g.arpa", "w");
    ngram_trie_write_arpa(t, arpafh);
    fclose(arpafh);
    ngram_trie_free(t);

    t = ngram_trie_init(dict, lmath);
    arpafh = fopen("tmp.bn10000.3g.arpa", "r");
    ngram_trie_read_arpa(t, arpafh);
    fclose(arpafh);

    /* Test 1, 2, 3-gram probs without backoff. */
    test_lookups(t, lmath);

    /* Test adding nodes. */
    test_add_nodes(t, lmath);

    ngram_trie_free(t);

    dict_free(dict);
    logmath_free(lmath);
    bin_mdef_free(mdef);
    cmd_ln_free_r(config);

    return 0;
}
Пример #3
0
int
main(int argc, char *argv[])
{
    cmd_ln_t *config;

    TEST_ASSERT(config =
            cmd_ln_init(NULL, ps_args(), TRUE,
                "-hmm", MODELDIR "/en-us/en-us",
                "-kws", DATADIR "/goforward.kws",
                "-dict", MODELDIR "/en-us/cmudict-en-us.dict", NULL));
    return ps_decoder_test(config, "KEYPHRASE", "forward");
}
Пример #4
0
int
main(int argc, char *argv[])
{
    ps_decoder_t *ps;
    cmd_ln_t *config;
    FILE *fh;
    char const *hyp, *uttid;
    int16 buf[512];
    int rv;
    int32 score;

    config = cmd_ln_init(NULL, ps_args(), TRUE,
                 "-hmm", MODELDIR "/en-us/en-us",
                 "-keyphrase", "marieta",
                 "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                 "-kws_threshold", "1e-30",
                 NULL);
    if (config == NULL) {
        fprintf(stderr, "Failed to create config object, see log for details\n");
        return -1;
    }
    
    ps = ps_init(config);
    if (ps == NULL) {
        fprintf(stderr, "Failed to create recognizer, see log for details\n");
        return -1;
    }

    fh = fopen("data/marieta.raw", "rb");
    if (fh == NULL) {
        fprintf(stderr, "Unable to open input file goforward.raw\n");
        return -1;
    }

    rv = ps_start_utt(ps);
    
    while (!feof(fh)) {
        size_t nsamp;
        nsamp = fread(buf, 2, 512, fh);
        rv = ps_process_raw(ps, buf, nsamp, FALSE, FALSE);
    }
    
    rv = ps_end_utt(ps);
    hyp = ps_get_hyp(ps, &score);
    printf("Recognized: %s\n", hyp);

    fclose(fh);
    ps_free(ps);
    cmd_ln_free_r(config);
    
    return 0;
}
int
ps_load_dict(ps_decoder_t *ps, char const *dictfile,
             char const *fdictfile, char const *format)
{
    dict2pid_t *d2p;
    dict_t *dict;
    hash_iter_t *search_it;
    cmd_ln_t *newconfig;

    /* Create a new scratch config to load this dict (so existing one
     * won't be affected if it fails) */
    newconfig = cmd_ln_init(NULL, ps_args(), TRUE, NULL);
    cmd_ln_set_boolean_r(newconfig, "-dictcase",
                         cmd_ln_boolean_r(ps->config, "-dictcase"));
    cmd_ln_set_str_r(newconfig, "-dict", dictfile);
    if (fdictfile)
        cmd_ln_set_str_extra_r(newconfig, "_fdict", fdictfile);
    else
        cmd_ln_set_str_extra_r(newconfig, "_fdict",
                               cmd_ln_str_r(ps->config, "_fdict"));

    /* Try to load it. */
    if ((dict = dict_init(newconfig, ps->acmod->mdef)) == NULL) {
        cmd_ln_free_r(newconfig);
        return -1;
    }

    /* Reinit the dict2pid. */
    if ((d2p = dict2pid_build(ps->acmod->mdef, dict)) == NULL) {
        cmd_ln_free_r(newconfig);
        return -1;
    }

    /* Success!  Update the existing config to reflect new dicts and
     * drop everything into place. */
    cmd_ln_free_r(newconfig);
    dict_free(ps->dict);
    ps->dict = dict;
    dict2pid_free(ps->d2p);
    ps->d2p = d2p;

    /* And tell all searches to reconfigure themselves. */
    for (search_it = hash_table_iter(ps->searches); search_it;
       search_it = hash_table_iter_next(search_it)) {
        if (ps_search_reinit(hash_entry_val(search_it->ent), dict, d2p) < 0) {
            hash_table_iter_free(search_it);
            return -1;
        }
    }

    return 0;
}
Пример #6
0
int
main(int argc, char *argv[])
{
    cmd_ln_t *config;

    TEST_ASSERT(config =
            cmd_ln_init(NULL, ps_args(), TRUE,
                "-hmm", MODELDIR "/en-us/en-us",
                "-allphone", MODELDIR "/en-us/en-us-phone.lm.bin",
                "-beam", "1e-20", "-pbeam", "1e-10", "-allphone_ci", "no", "-lw", "2.0",
                NULL));
    return ps_decoder_test(config, "ALLPHONE", "SIL G OW F AO R W ER D T AE N M IY IH ZH ER Z S V SIL");
}
SPLEXPORT bool spInitListener(  const char *hmm_path,
                                const char *kws_path,
                                const char *lm_path,
                                const char *dict_path,
                                int32_t sample_rate,
                                int delay) {
    sp_log.open("splog.txt", std::ios_base::app);
    sp_error = "";

    if (kws_path) {
        config = cmd_ln_init(NULL, ps_args(), TRUE,
            "-hmm", hmm_path,
            "-kws", kws_path,
            "-dict", dict_path,
            NULL);
    }
    else {
        config = cmd_ln_init(NULL, ps_args(), TRUE,
            "-hmm", hmm_path,
            "-lm", lm_path,
            "-dict", dict_path,
            NULL);
    }

    if (config == NULL) {
        spFatal("pocketsphinx command line initialization failed :(");
        return false;
    }
    
    if ((ps = ps_init(config)) == NULL) {
        spFatal("pocketsphinx decoder initialization failed :(");
        return false;
    }

    // Record on new thread
    listen_thread = std::thread(spListen, sample_rate, delay);
    return true;
}
Пример #8
0
void Recognizer::initialize(const ci::fs::path& hmmPath, const ci::fs::path& dictPath)
{
    // Configure recognizer:
    mConfig = cmd_ln_init( NULL, ps_args(), true, "-hmm", hmmPath.c_str(), "-dict", dictPath.c_str(), "-logfn", "/dev/null", NULL );

    if( mConfig == NULL )
        throw std::runtime_error( "Could not configure speech recognizer" );

    // Initialize recognizer:
    mDecoder = ps_init( mConfig );

    if( mDecoder == NULL )
        throw std::runtime_error( "Could not initialize speech recognizer" );
}
static void test_reinit_fsg_missing()
{
       ps_decoder_t *ps;
       cmd_ln_t *config;
       config = cmd_ln_init(NULL, ps_args(), TRUE,
			    "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
			    "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
                            NULL);
       ps = ps_init(config);
       cmd_ln_set_str_r(config, "-fsg", "/some/fsg");
       ps_update_fsgset (ps);
       ps_free(ps);
       cmd_ln_free_r(config);
}
static void test_reinit_lm()
{
        ps_decoder_t *ps;
        cmd_ln_t *config;
        ngram_model_t *model;
        config = cmd_ln_init(NULL, ps_args(), TRUE,
    			     "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
			     "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
			     "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
                             NULL);
        ps = ps_init(config);
        model = ps_update_lmset (ps, NULL);
        ps_free(ps);
        cmd_ln_free_r(config);
}
Пример #11
0
PocketSphinxServer::PocketSphinxServer()
{

	cmd_ln_t* cfg = cmd_ln_init(nullptr, ps_args(), TRUE,
			"-hmm", "/usr/share/pocketsphinx/model/hmm/lium_french_f0",
			"-lm", "/usr/share/pocketsphinx/model/lm/french3g62k.lm.dmp",
			"-dict", "/usr/share/pocketsphinx/model/lm/frenchWords62k.dic",
			nullptr);

	if (!cfg)
		throw std::runtime_error("Error in cmd_ln_init");

	ps = ps_init(cfg);
	if (!ps)
		throw std::runtime_error("Error in ps_init");
}
Пример #12
0
int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	ps_nbest_t *nbest;
	cmd_ln_t *config;
	FILE *rawfh;
	char const *hyp;
	int32 score, n;

	TEST_ASSERT(config =
		    cmd_ln_init(NULL, ps_args(), TRUE,
				"-hmm", MODELDIR "/en-us/en-us",
				"-lm", MODELDIR "/en-us/en-us.lm.bin",
				"-dict", MODELDIR "/en-us/cmudict-en-us.dict",
				"-fwdtree", "yes",
				"-fwdflat", "yes",
				"-bestpath", "yes",
				"-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, -1);
	fclose(rawfh);
	hyp = ps_get_hyp(ps, &score);
	printf("BESTPATH: %s (%d)\n", hyp, score);

	for (n = 1, nbest = ps_nbest(ps); nbest && n < 10; nbest = ps_nbest_next(nbest), n++) {
		ps_seg_t *seg;
		hyp = ps_nbest_hyp(nbest, &score);
		printf("NBEST %d: %s (%d)\n", n, hyp, score);
		for (seg = ps_nbest_seg(nbest); 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);
		}
	}
	if (nbest)
	    ps_nbest_free(nbest);
	ps_free(ps);
	cmd_ln_free_r(config);
	return 0;
}
int
main(int argc, char *argv[])
{
    cmd_ln_t *config;

    TEST_ASSERT(config =
            cmd_ln_init(NULL, ps_args(), TRUE,
                "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
                "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
                "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
                "-fwdtree", "yes",
                "-fwdflat", "no",
                "-bestpath", "yes",
                "-input_endian", "little",
                "-samprate", "16000", NULL));
    return ps_decoder_test(config, "BESTPATH", "go forward ten readers");
}
Пример #14
0
int
main(int argc, char *argv[])
{
  ps_decoder_t *ps;
  cmd_ln_t *config;
  FILE *fh;
  char const *hyp, *uttid;
  int16 buf[512];
  int rv;
  int32 score;

  config = cmd_ln_init(NULL, ps_args(), TRUE,
		       "-hmm", MODELDIR "/en-us/en-us",
		       "-lm", MODELDIR "/en-us/en-us.lm.dmp",
		       "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
		       NULL);
  if (config == NULL)
    return 1;
  ps = ps_init(config);
  if (ps == NULL)
    return 1;

  fh = fopen("goforward.raw", "rb");
  if (fh == NULL)
    return -1;
  rv = ps_start_utt(ps);
  if (rv < 0)
    return 1;
  while (!feof(fh)) {
    size_t nsamp;
    nsamp = fread(buf, 2, 512, fh);
    rv = ps_process_raw(ps, buf, nsamp, FALSE, FALSE);
  }
  rv = ps_end_utt(ps);
  if (rv < 0)
    return 1;
  hyp = ps_get_hyp(ps, &score);
  if (hyp == NULL)
    return 1;
  printf("Recognized: %s\n", hyp);

  fclose(fh);
  ps_free(ps);
  cmd_ln_free_r(config);
  return 0;
}
int init()
{
    cmd_ln_t *config;
    
    printf("Initializing audio device...\n");
    
    config = cmd_ln_init(NULL, ps_args(), TRUE, 
                             "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
                             "-lm", MODELDIR "/lm/en/turtle.DMP",
                             "-dict", MODELDIR "/lm/en/turtle.dic",
                             NULL);
    if(config == NULL) {
        fprintf(stderr, "Could not init sphinx config.\n");
        return 1;
    }
    
    psDecoder = ps_init(config);
    if(psDecoder == NULL) {
        fprintf(stderr, "Could not init psDecoder.\n");
        return 2;
    }
    
    audioDevice = ad_open();
    if(audioDevice == NULL) {
        fprintf(stderr, "Could not open Audio Device.\n");
        return 3;
    }
    continousAudoDevice = cont_ad_init(audioDevice, ad_read);
    if(continousAudoDevice == NULL) {
        fprintf(stderr, "Could not open Audio Device.\n");
        return 4;
    }
    
    if(ad_start_rec(audioDevice) < 0) {
        fprintf(stderr,"Failed to start recording.\n");
        return 5;
    }
    
    if(cont_ad_calib(continousAudoDevice) < 0) {
        fprintf(stderr,"Failed to calibrate voice activity detection.\n");
        return 6;
    }
    
    return 0;
}
Пример #16
0
bool ofApp::engineInit()
{
   config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-lm", MODELDIR "/en-us/en-us.lm.dmp", "-dict", MODELDIR "/en-us/cmudict-en-us.dict", NULL);
    
    
    //this is the setup you can use for microphone
    //config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-allphone", MODELDIR "/en-us/en-us-phone.lm.dmp", "-backtrace", "yes", "-beam", "1e-20", "-pbeam", "1e-20", "-lw", "2.0", NULL);
    
    if (config == NULL)
    {
        return false;
    }
    ps = ps_init(config);
    
    if (ps == NULL)
    {
        return false;
    }
}
Пример #17
0
cmd_ln_t* SphinxRecognitionConfiguration::getSphinxConfig()
{
  kDebug()<<"Creating sphinx configuration";
  kDebug()<<m_ModelDir;
  kDebug()<<m_Grammar;
  kDebug()<<m_Dictionary;
  QByteArray model = m_ModelDir.toUtf8();
  QByteArray grammar = m_Grammar.toUtf8();
  QByteArray dict = m_Dictionary.toUtf8();
  QByteArray samprate = QString::number(m_Samprate).toUtf8();

  cmd_ln_t *config = cmd_ln_init(NULL, ps_args(), TRUE,
                               "-hmm", model.data(),
                               "-jsgf", grammar.data(),
                               "-dict", dict.data(),
                               "-samprate", samprate.data(),
                               NULL);
  return config;
}
int
main(int argc, char *argv[])
{
    cmd_ln_t *config;

    TEST_ASSERT(config =
                    cmd_ln_init(NULL, ps_args(), TRUE,
                                "-hmm", MODELDIR "/en-us/en-us",
                                "-lm", MODELDIR "/en-us/en-us.lm.bin",
                                "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                                "-lw", "6.5",
                                "-fwdflatlw", "6.5",
                                "-fwdtree", "yes",
                                "-fwdflat", "yes",
                                "-bestpath", "no",
                                "-input_endian", "little",
                                "-samprate", "16000", NULL));
    return ps_decoder_test(config, "FWDFLAT", "go forward ten meters");
}
Пример #19
0
int
main(int argc, char *argv[])
{
	bin_mdef_t *mdef;
	dict_t *dict;
	dict2pid_t *d2p;
	cmd_ln_t *config;

	TEST_ASSERT(config = cmd_ln_init(NULL, NULL, FALSE,
						   "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
						   "-fdict", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/noisedict",
						   NULL));
	TEST_ASSERT(mdef = bin_mdef_read(NULL, MODELDIR "/hmm/en_US/hub4wsj_sc_8k/mdef"));
	TEST_ASSERT(dict = dict_init(config, mdef));
	TEST_ASSERT(d2p = dict2pid_build(mdef, dict));

	dict_free(dict);
	dict2pid_free(d2p);
	bin_mdef_free(mdef);
	cmd_ln_free_r(config);

	return 0;
}
Пример #20
0
int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	cmd_ln_t *config;

	TEST_ASSERT(config =
		    cmd_ln_init(NULL, ps_args(), TRUE,
				"-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
				"-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
				"-dict", MODELDIR "/lm/en_US/cmu07a.dic",
				"-fwdtree", "yes",
				"-fwdflat", "yes",
				"-bestpath", "yes",
				"-input_endian", "little",
				"-samprate", "16000", NULL));
	TEST_ASSERT(ps = ps_init(config));

	ps_free(ps);
	cmd_ln_free_r(config);

	return 0;
}
int sphinxInit()
{
    config = cmd_ln_init(NULL, ps_args(), TRUE,
                         "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
                         "-lm", "1048.lm",
                         "-dict", "1048.dic",
                         //"-lm", MODELDIR "/lm/en/turtle.DMP",
                         //"-dict", MODELDIR "/lm/en/turtle.dic",
                         NULL);

    if (config == NULL) {
        printf("Error getting cmd config.\n");
        return 0;
    }

    ps = ps_init(config);
    if (ps == NULL) {
        printf("Error calling init.\n");
        return 0;
    }

    return 1;
}
Пример #22
0
int
main(int argc, char *argv[])
{
	dict_t *dict;
	dict2pid_t *d2p;
	bin_mdef_t *mdef;
	glextree_t *tree;
	hmm_context_t *ctx;
	logmath_t *lmath;
	tmat_t *tmat;
	int i;

	TEST_ASSERT(mdef = bin_mdef_read(NULL, MODELDIR "/hmm/en_US/hub4wsj_sc_8k/mdef"));
	TEST_ASSERT(dict = dict_init(cmd_ln_init(NULL, NULL, FALSE,
						 "-dict", DATADIR "/turtle.dic",
						 "-dictcase", "no", NULL),
				       mdef));
	TEST_ASSERT(d2p = dict2pid_build(mdef, dict));
	lmath = logmath_init(1.0001, 0, TRUE);
	tmat = tmat_init(MODELDIR "/hmm/en_US/hub4wsj_sc_8k/transition_matrices",
			 lmath, 1e-5, TRUE);
	ctx = hmm_context_init(bin_mdef_n_emit_state(mdef), tmat->tp, NULL, mdef->sseq);
	TEST_ASSERT(tree = glextree_build(ctx, dict, d2p, NULL, NULL));

	/* Check that a path exists for all dictionary words. */
	for (i = 0; i < dict_size(dict); ++i)
		TEST_ASSERT(glextree_has_word(tree, i));

	dict_free(dict);
	dict2pid_free(d2p);
	bin_mdef_free(mdef);
	tmat_free(tmat);
	hmm_context_free(ctx);
	glextree_free(tree);
	return 0;
}
Пример #23
0
int
main(int argc, char *argv[])
{
	ps_decoder_t *ps;
	cmd_ln_t *config;
	acmod_t *acmod;
	ps_search_t *ngs, *pls;
	clock_t c;
	int32 score;
	int i;

	TEST_ASSERT(config =
		    cmd_ln_init(NULL, ps_args(), TRUE,
				"-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
				"-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
				"-dict", MODELDIR "/lm/en_US/cmu07a.dic",
				"-fwdtree", "yes",
				"-fwdflat", "no",
				"-bestpath", "no",
				"-pl_window", "6",
				"-input_endian", "little",
				"-samprate", "16000", NULL));
	TEST_ASSERT(ps = ps_init(config));

	ngs = ps->search;
	pls = ps->phone_loop;
	acmod = ps->acmod;

	setbuf(stdout, NULL);
	c = clock();
	for (i = 0; i < 5; ++i) {
		FILE *rawfh;
		int16 buf[2048];
		size_t nread;
		int16 const *bptr;
		int nfr, n_searchfr;

		TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
		TEST_EQUAL(0, acmod_start_utt(acmod));
		ps_search_start(ngs);
		ps_search_start(pls);
		n_searchfr = 0;
		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) {
					ps_search_step(pls, n_searchfr);
					if (n_searchfr >= 6)
						ps_search_step(ngs, n_searchfr - 6);
					acmod_advance(acmod);
					++n_searchfr;
				}
			}
		}
		for (nfr = n_searchfr - 6; nfr < n_searchfr; ++nfr) {
			ps_search_step(ngs, nfr);
		}
		ps_search_finish(pls);
		ps_search_finish(ngs);
		printf("%s\n", ps_search_hyp(ngs, &score));

		TEST_ASSERT(acmod_end_utt(acmod) >= 0);
		fclose(rawfh);
	}
	printf("%s\n", ps_search_hyp(ngs, &score));
	TEST_EQUAL(0, strcmp("go forward ten years", ps_search_hyp(ngs, &score)));
	c = clock() - c;
	printf("5 * fwdtree search in %.2f sec\n",
	       (double)c / CLOCKS_PER_SEC);
	ps_free(ps);

	return 0;
}
Пример #24
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;
}
Пример #25
0
int
main(int argc, char *argv[])
{
    ps_decoder_t *ps;
    cmd_ln_t *config;
    acmod_t *acmod;
    ngram_search_t *ngs;

    TEST_ASSERT(config =
            cmd_ln_init(NULL, ps_args(), TRUE,
                "-hmm", MODELDIR "/en-us/en-us",
                "-lm", MODELDIR "/en-us/en-us.lm.dmp",
                "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
                "-fwdtree", "no",
                "-fwdflat", "yes",
                "-bestpath", "no",
                "-input_endian", "little",
                "-samprate", "16000", NULL));
    TEST_ASSERT(ps = ps_init(config));

    ngs = (ngram_search_t *)ps->search;
    acmod = ps->acmod;

    setbuf(stdout, NULL);
    {
        FILE *rawfh, *senfh;
        int16 buf[2048];
        size_t nread;
        int16 const *bptr;
        int nfr;

        TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
        TEST_EQUAL(0, acmod_start_utt(acmod));
        TEST_ASSERT(senfh = fopen("goforward.sen", "wb"));
        TEST_EQUAL(0, acmod_set_senfh(acmod, senfh));
        ngram_fwdflat_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_fwdflat_search(ngs, acmod->output_frame);
                    acmod_advance(acmod);
                }
            }
        }
        ngram_fwdflat_finish(ngs);
        printf("%s\n",
               ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

        TEST_ASSERT(acmod_end_utt(acmod) >= 0);
        fclose(rawfh);
        TEST_EQUAL(0, strcmp("go forward ten meters",
                     ngram_search_bp_hyp(ngs,
                             ngram_search_find_exit(ngs, -1, NULL, NULL))));

        TEST_EQUAL(0, acmod_set_senfh(acmod, NULL));
        TEST_EQUAL(0, acmod_start_utt(acmod));
        TEST_ASSERT(senfh = fopen("goforward.sen", "rb"));
        TEST_EQUAL(0, acmod_set_insenfh(acmod, senfh));
        ngram_fwdflat_start(ngs);
        while ((nfr = acmod_read_scores(acmod)) > 0) {
            while (acmod->n_feat_frame > 0) {
                ngram_fwdflat_search(ngs, acmod->output_frame);
                acmod_advance(acmod);
            }
        }
        ngram_fwdflat_finish(ngs);
        printf("%s\n",
               ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)));

        TEST_ASSERT(acmod_end_utt(acmod) >= 0);
        TEST_EQUAL(0, strcmp("go forward ten meters",
                     ngram_search_bp_hyp(ngs,
                             ngram_search_find_exit(ngs, -1, NULL, NULL))));
        fclose(senfh);
    }
    ps_free(ps);
    cmd_ln_free_r(config);

    return 0;
}
SWIGINTERN Decoder *new_Decoder__SWIG_0(){
		Decoder *d = ps_init(cmd_ln_init(NULL, ps_args(), FALSE, NULL));
		return d;
	}
SWIGINTERN Config *new_Config__SWIG_0(){
		Config *c = cmd_ln_init(NULL, ps_args(), FALSE, NULL);
		return c;
	}
Пример #28
0
    void Node::run() {
        // declare variables
        const int BUFFER_SIZE = 2048;
        ad_rec_t *reader;
        size_t read_samples;
        int16 read_buffer[BUFFER_SIZE];
        cmd_ln_t *read_decoder_config;
        ps_decoder_t *read_decoder;

        // open audio record
        reader = ad_open_dev(NULL, 16000);
        if (reader == NULL) {
            throw NodeException("Failed to open read audio device");
        }

        // start audio record
        if (ad_start_rec(reader) < 0) {
            throw NodeException("Failed to start read audio device");
        }

        // instance audio decoder
        read_decoder_config = cmd_ln_init(
                NULL,
                ps_args(),
                TRUE,
                "-logfn", "/dev/null", // turn off console log
                "-hmm", "/usr/local/share/pocketsphinx/model/en-us/en-us",
                "-lm", "/usr/local/share/pocketsphinx/model/en-us/en-us.lm.dmp",
                "-dict", "/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict",
                NULL
        );
        read_decoder = ps_init(read_decoder_config);
        if (read_decoder == NULL) {
            throw NodeException("Failed to initialize audio decoder");
        }

        // start utterance
        if (ps_start_utt(read_decoder) < 0) {
            throw NodeException("Failed to start reader utterance");
        }
        bool utt_started = false;
        bool in_speech = false;
        char const *hypothesis;

        while (true) {
            try {
                // read from input device
                read_samples = ad_read(
                        reader,
                        read_buffer,
                        BUFFER_SIZE
                );
                if (read_samples < 0) {
                    throw NodeException("Failed to read audio");
                }

                // add data buffered from device to read decoder
                ps_process_raw(
                        read_decoder,
                        read_buffer,
                        read_samples,
                        false,
                        false
                );

                // check device in speech
                in_speech = ps_get_in_speech(read_decoder);
                if (in_speech && !utt_started) {
                    utt_started = true;
                }

                // check device in speech then add data to decoder
                if (!in_speech && utt_started) {
                    ps_end_utt(read_decoder);
                    hypothesis = ps_get_hyp(read_decoder, NULL);
                    if (hypothesis != NULL) {
                        cout<<"ENV: "<<hypothesis<<endl;
                        Command core_command = this->spec_.first("node-core");
                        int port = atoi(core_command.option("--port").c_str());
                        SocketWriter socket(core_command.option("--host"), port);
                        string response = socket.write_to(hypothesis);
                        cout << "IN: " << response << endl;
                    }
                    if (ps_start_utt(read_decoder) < 0) {
                        throw NodeException("Failed to start reader utterance");
                    }
                    utt_started = false;
                }
                sleep_milli_seconds(100);
            } catch (NodeException e) {
                cout<<"ERROR: "<<e.what()<<endl;
            }

        }
    }
Пример #29
0
int
main(int argc, char *argv[])
{
    acmod_t *acmod;
    logmath_t *lmath;
    cmd_ln_t *config;
    FILE *rawfh;
    int16 *buf;
    int16 const *bptr;
    mfcc_t **cepbuf, **cptr;
    size_t nread, nsamps;
    int nfr;
    int frame_counter;
    int bestsen1[270];

    lmath = logmath_init(1.0001, 0, 0);
    config = cmd_ln_init(NULL, ps_args(), TRUE,
                 "-featparams", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/feat.params",
                 "-mdef", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/mdef",
                 "-mean", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/means",
                 "-var", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/variances",
                 "-tmat", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/transition_matrices",
                 "-sendump", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/sendump",
                 "-compallsen", "true",
                 "-cmn", "prior",
                 "-tmatfloor", "0.0001",
                 "-mixwfloor", "0.001",
                 "-varfloor", "0.0001",
                 "-mmap", "no",
                 "-topn", "4",
                 "-ds", "1",
                 "-input_endian", "little",
                 "-samprate", "16000", NULL);
    TEST_ASSERT(config);
    TEST_ASSERT(acmod = acmod_init(config, lmath, NULL, NULL));
    cmn_prior_set(acmod->fcb->cmn_struct, prior);

    nsamps = 2048;
    frame_counter = 0;
    buf = ckd_calloc(nsamps, sizeof(*buf));
    TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
    TEST_EQUAL(0, acmod_start_utt(acmod));
    E_INFO("Incremental(2048):\n");
    while (!feof(rawfh)) {
        nread = fread(buf, sizeof(*buf), nsamps, rawfh);
        bptr = buf;
        while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0 || nread > 0) {
            int16 const *senscr;
            int16 best_score;
            int frame_idx = -1, best_senid;
            while (acmod->n_feat_frame > 0) {
                senscr = acmod_score(acmod, &frame_idx);
                acmod_advance(acmod);
                best_score = acmod_best_score(acmod, &best_senid);
                E_INFO("Frame %d best senone %d score %d\n",
                       frame_idx, best_senid, best_score);
                TEST_EQUAL(frame_counter, frame_idx);
                if (frame_counter < 190)
                    bestsen1[frame_counter] = best_score;
                ++frame_counter;
                frame_idx = -1;
            }
        }
    }
    TEST_EQUAL(0, acmod_end_utt(acmod));
    nread = 0;
    {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
                   frame_idx, best_senid, best_score);
            if (frame_counter < 190)
                bestsen1[frame_counter] = best_score;
            TEST_EQUAL(frame_counter, frame_idx);
            ++frame_counter;
            frame_idx = -1;
        }
    }

    /* Now try to process the whole thing at once. */
    E_INFO("Whole utterance:\n");
    cmn_prior_set(acmod->fcb->cmn_struct, prior);
    nsamps = ftell(rawfh) / sizeof(*buf);
    clearerr(rawfh);
    fseek(rawfh, 0, SEEK_SET);
    buf = ckd_realloc(buf, nsamps * sizeof(*buf));
    TEST_EQUAL(nsamps, fread(buf, sizeof(*buf), nsamps, rawfh));
    bptr = buf;
    TEST_EQUAL(0, acmod_start_utt(acmod));
    acmod_process_raw(acmod, &bptr, &nsamps, TRUE);
    TEST_EQUAL(0, acmod_end_utt(acmod));
    {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        frame_counter = 0;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
               frame_idx, best_senid, best_score);
            if (frame_counter < 190)
                TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]);
            TEST_EQUAL(frame_counter, frame_idx);
            ++frame_counter;
            frame_idx = -1;
        }
    }

    /* Now process MFCCs and make sure we get the same results. */
    cepbuf = ckd_calloc_2d(frame_counter,
                   fe_get_output_size(acmod->fe),
                   sizeof(**cepbuf));
    fe_start_utt(acmod->fe);
    nsamps = ftell(rawfh) / sizeof(*buf);
    bptr = buf;
    nfr = frame_counter;
    fe_process_frames(acmod->fe, &bptr, &nsamps, cepbuf, &nfr);
    fe_end_utt(acmod->fe, cepbuf[frame_counter-1], &nfr);

    E_INFO("Incremental(MFCC):\n");
    cmn_prior_set(acmod->fcb->cmn_struct, prior);
    TEST_EQUAL(0, acmod_start_utt(acmod));
    cptr = cepbuf;
    nfr = frame_counter;
    frame_counter = 0;
    while ((acmod_process_cep(acmod, &cptr, &nfr, FALSE)) > 0) {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
                   frame_idx, best_senid, best_score);
            TEST_EQUAL(frame_counter, frame_idx);
            if (frame_counter < 190)
                TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]);
            ++frame_counter;
            frame_idx = -1;
        }
    }
    TEST_EQUAL(0, acmod_end_utt(acmod));
    nfr = 0;
    acmod_process_cep(acmod, &cptr, &nfr, FALSE);
    {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
                   frame_idx, best_senid, best_score);
            TEST_EQUAL(frame_counter, frame_idx);
            if (frame_counter < 190)
                TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]);
            ++frame_counter;
            frame_idx = -1;
        }
    }

    /* Note that we have to process the whole thing again because
     * !#@$@ s2mfc2feat modifies its argument (not for long) */
    fe_start_utt(acmod->fe);
    nsamps = ftell(rawfh) / sizeof(*buf);
    bptr = buf;
    nfr = frame_counter;
    fe_process_frames(acmod->fe, &bptr, &nsamps, cepbuf, &nfr);
    fe_end_utt(acmod->fe, cepbuf[frame_counter-1], &nfr);

    E_INFO("Whole utterance (MFCC):\n");
    cmn_prior_set(acmod->fcb->cmn_struct, prior);
    TEST_EQUAL(0, acmod_start_utt(acmod));
    cptr = cepbuf;
    nfr = frame_counter;
    acmod_process_cep(acmod, &cptr, &nfr, TRUE);
    TEST_EQUAL(0, acmod_end_utt(acmod));
    {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        frame_counter = 0;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
                   frame_idx, best_senid, best_score);
            if (frame_counter < 190)
                TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]);
            TEST_EQUAL(frame_counter, frame_idx);
            ++frame_counter;
            frame_idx = -1;
        }
    }

    E_INFO("Rewound (MFCC):\n");
    TEST_EQUAL(0, acmod_rewind(acmod));
    {
        int16 const *senscr;
        int16 best_score;
        int frame_idx = -1, best_senid;
        frame_counter = 0;
        while (acmod->n_feat_frame > 0) {
            senscr = acmod_score(acmod, &frame_idx);
            acmod_advance(acmod);
            best_score = acmod_best_score(acmod, &best_senid);
            E_INFO("Frame %d best senone %d score %d\n",
                   frame_idx, best_senid, best_score);
            if (frame_counter < 190)
                TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]);
            TEST_EQUAL(frame_counter, frame_idx);
            ++frame_counter;
            frame_idx = -1;
        }
    }

    /* Clean up, go home. */
    ckd_free_2d(cepbuf);
    fclose(rawfh);
    ckd_free(buf);
    acmod_free(acmod);
    logmath_free(lmath);
    cmd_ln_free_r(config);
    return 0;
}
Пример #30
0
int main(int argc, char** argv)
{
    /* init ps */
    fprintf(stderr, "INIT PS\n");
    cmd_ln_t* psconfig = cmd_ln_init(NULL, ps_args(), TRUE,
        "-hmm", MODELDIR "/en-us/en-us",
        "-lm", MODELDIR "/en-us/en-us.lm.bin",
        "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
        NULL);
    if (psconfig == NULL) {
        fprintf(stderr, "Cannot create config for PS\n");
        return -1;
    }
    
    ps_decoder_t* ps = ps_init(psconfig);
    if (ps == NULL) {
        fprintf(stderr, "Cannot create PS decoder\n");
        return -1;
    }

    fprintf(stderr, "INIT AL\n");
    /* init openal and create microphone */
    ALCdevice* aldevice = alcCaptureOpenDevice(NULL,AL_MIC_FREQ,AL_FORMAT_MONO16,AL_MIC_FREQ/2);
    if (aldevice == NULL) {
        fprintf(stderr, "Cannot open AL device");
        return -1;
    }
    alcCaptureStart(aldevice);

    /* conna capture some wods */
    ALCint samplesIn = 0;
    short buff[AL_MIC_FREQ*2];
    int ignoreCounter = 20;
    uint8 utt_started;
    uint8 in_speech;
    utt_started = FALSE;
    ps_start_utt(ps);
    const char* hyp;
    for (;;) {
        /* poll some data */
        alcGetIntegerv(aldevice,ALC_CAPTURE_SAMPLES,1,&samplesIn);
        if(samplesIn>AL_MIC_CAP) {
            alcCaptureSamples(aldevice,buff,AL_MIC_CAP);
            /* actual voice processing */
            ps_process_raw(ps, buff, AL_MIC_CAP, FALSE, FALSE);
            in_speech = ps_get_in_speech(ps);
            if (in_speech && !utt_started) {
                utt_started = TRUE;
                fprintf(stderr, "Hearing something i guess...\n");
            }
            if (!in_speech && utt_started) {
                fprintf(stderr, "Processing it now...\n");
                ps_end_utt(ps);
                hyp = ps_get_hyp(ps,NULL);
                if (hyp != NULL) {
                    fprintf(stderr, "Here we go!\n\n");
                    fprintf(stdout,"%s\n",hyp);
                    fflush(stdout);
                    fprintf(stderr,"\n\nTo the next round!\n");
                }
                ps_start_utt(ps);
                utt_started = FALSE;
            }

        }

    }
    return 0;
}