예제 #1
0
static void
gst_pocketsphinx_get_property (GObject * object, guint prop_id,
			       GValue * value, GParamSpec * pspec)
{
    switch (prop_id) {
    case PROP_HMM_DIR:
        g_value_set_string(value, cmd_ln_str("-hmm"));
        break;
    case PROP_LM_FILE:
        g_value_set_string(value, cmd_ln_str("-lm"));
        break;
    case PROP_DICT_FILE:
        g_value_set_string(value, cmd_ln_str("-dict"));
        break;
    case PROP_FSG_FILE:
        g_value_set_string(value, cmd_ln_str("-fsg"));
        break;
    case PROP_FWDFLAT:
        g_value_set_boolean(value, cmd_ln_boolean("-fwdflat"));
        break;
    case PROP_BESTPATH:
        g_value_set_boolean(value, cmd_ln_boolean("-bestpath"));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}
예제 #2
0
/*
 * Build a filename int buf as follows (without file extension):
 *     if dir ends with ,CTLand ctlspec does not begin with /, filename is dir/ctlspec
 *     if dir ends with ,CTL and ctlspec DOES begin with /, filename is ctlspec
 *     if dir does not end with ,CTL, filename is dir/uttid,
 * where ctlspec is the complete utterance spec in the input control file, and
 * uttid is the last component of ctlspec.
 */
static void
build_output_uttfile(char *buf, char *dir, char *uttid, char *ctlspec)
{
    int32 k;

    k = strlen(dir);
    if ((k > 4) && (strcmp(dir + k - 4, ",CTL") == 0)) {        /* HACK!! Hardwired ,CTL */
        if (ctlspec[0] != '/') {
            strcpy(buf, dir);
            buf[k - 4] = '/';
            strcpy(buf + k - 3, ctlspec);
        }
        else
            strcpy(buf, ctlspec);
    }
    else {
        strcpy(buf, dir);
        buf[k] = '/';
        strcpy(buf + k + 1, uttid);
    }
    /* Build output directory structure if possible/requested (it is
     * by default). */
    if (cmd_ln_boolean("-build_outdirs")) {
        char *dirname = ckd_salloc(buf);
        path2dirname(buf, dirname);
        build_directory(dirname);
        ckd_free(dirname);
    }
}
예제 #3
0
int
main(int argc, char *argv[])
{
    cmd_ln_parse(defs, orig_argc, orig_argv, TRUE);
    cmd_ln_parse(defs, argc, argv, FALSE);
    printf("%d %s %d %f\n",
           cmd_ln_int32("-a"),
           cmd_ln_str("-b") ? cmd_ln_str("-b") : "(null)",
           cmd_ln_boolean("-c"),
           cmd_ln_float64("-d"));
           
    return 0;
}
예제 #4
0
파일: main.c 프로젝트: Ankit77/cmusphinx
int
main_initialize(int argc,
		char *argv[],
		lexicon_t **out_lex,
		model_def_t **out_omdef,
		model_def_t **out_dmdef,
		feat_t** out_feat)
{
    model_def_t *dmdef = NULL;
    model_def_t *omdef = NULL;
    lexicon_t *lex = NULL;
    feat_t *feat;
    const char *fn;
    uint32 n_ts;
    uint32 n_cb;
    const char *ts2cbfn;

    parse_cmd_ln(argc, argv);

    feat = 
        feat_init(cmd_ln_str("-feat"),
                  cmn_type_from_str(cmd_ln_str("-cmn")),
                  cmd_ln_boolean("-varnorm"),
                  agc_type_from_str(cmd_ln_str("-agc")),
                  1, cmd_ln_int32("-ceplen"));


    if (cmd_ln_str("-lda")) {
        E_INFO("Reading linear feature transformation from %s\n",
               cmd_ln_str("-lda"));
        if (feat_read_lda(feat,
                          cmd_ln_str("-lda"),
                          cmd_ln_int32("-ldadim")) < 0)
            return -1;
    }

    if (cmd_ln_str("-svspec")) {
        int32 **subvecs;
        E_INFO("Using subvector specification %s\n", 
               cmd_ln_str("-svspec"));
        if ((subvecs = parse_subvecs(cmd_ln_str("-svspec"))) == NULL)
            return -1;
        if ((feat_set_subvecs(feat, subvecs)) < 0)
            return -1;
    }

    if (cmd_ln_exists("-agcthresh")
        && 0 != strcmp(cmd_ln_str("-agc"), "none")) {
        agc_set_threshold(feat->agc_struct,
                          cmd_ln_float32("-agcthresh"));
    }

    if (feat->cmn_struct
        && cmd_ln_exists("-cmninit")) {
        char *c, *cc, *vallist;
        int32 nvals;

        vallist = ckd_salloc(cmd_ln_str("-cmninit"));
        c = vallist;
        nvals = 0;
        while (nvals < feat->cmn_struct->veclen
               && (cc = strchr(c, ',')) != NULL) {
            *cc = '\0';
            feat->cmn_struct->cmn_mean[nvals] = FLOAT2MFCC(atof(c));
            c = cc + 1;
            ++nvals;
        }
        if (nvals < feat->cmn_struct->veclen && *c != '\0') {
            feat->cmn_struct->cmn_mean[nvals] = FLOAT2MFCC(atof(c));
        }
        ckd_free(vallist);
    }
    *out_feat = feat;


    if (cmd_ln_str("-omoddeffn")) {
	E_INFO("Reading output model definitions: %s\n", cmd_ln_str("-omoddeffn"));
	
	/* Read in the model definitions.  Defines the set of
	   CI phones and context dependent phones.  Defines the
	   transition matrix tying and state level tying. */
	if (model_def_read(&omdef,
			   cmd_ln_str("-omoddeffn")) != S3_SUCCESS) {
	    return S3_ERROR;
	}

	if (cmd_ln_str("-dmoddeffn")) {
	    E_INFO("Reading dump model definitions: %s\n", cmd_ln_str("-dmoddeffn"));
	
	    if (model_def_read(&dmdef,
			       cmd_ln_str("-dmoddeffn")) != S3_SUCCESS) {
		return S3_ERROR;
	    }
	    setup_d2o_map(dmdef, omdef);
	}
	else {
	    E_INFO("Assuming dump and output model definitions are identical\n");
	}

	ts2cbfn = cmd_ln_str("-ts2cbfn");
	if (ts2cbfn) {
	    if (strcmp(SEMI_LABEL, ts2cbfn) == 0) {
		omdef->cb = semi_ts2cb(omdef->n_tied_state);
		n_ts = omdef->n_tied_state;
		n_cb = 1;
	    }
	    else if (strcmp(CONT_LABEL, ts2cbfn) == 0) {
		omdef->cb = cont_ts2cb(omdef->n_tied_state);
		n_ts = omdef->n_tied_state;
		n_cb = omdef->n_tied_state;
	    }
	    else if (strcmp(PTM_LABEL, ts2cbfn) == 0) {
		omdef->cb = ptm_ts2cb(omdef);
		n_ts = omdef->n_tied_state;
		n_cb = omdef->acmod_set->n_ci;
	    }
	    else if (s3ts2cb_read(cmd_ln_str("-ts2cbfn"),
				  &omdef->cb,
				  &n_ts,
				  &n_cb) != S3_SUCCESS) {
		return S3_ERROR;
	    }
	    
	    if (omdef->n_tied_state != n_ts) {
		E_FATAL("Model definition file n_tied_state = %u, but %u mappings in ts2cb\n",
			omdef->n_tied_state, n_ts);
	    }
	}
    }
    else {
	E_INFO("No mdef files.  Assuming 1-class init\n");
    }

    *out_omdef = omdef;
    *out_dmdef = dmdef;

    fn = cmd_ln_str("-dictfn");
    if (fn) {
	E_INFO("Reading main lexicon: %s\n", fn);
	     

	lex = lexicon_read(NULL,
			   fn,
			   omdef->acmod_set);
	if (lex == NULL)
	    return S3_ERROR;
    }
    
    fn = cmd_ln_str("-fdictfn");
    if (fn) {
	E_INFO("Reading filler lexicon: %s\n", fn);
	(void)lexicon_read(lex,
			   fn,
			   omdef->acmod_set);
    }
    
    *out_lex = lex;
    
    stride = cmd_ln_int32("-stride");

    return S3_SUCCESS;
}
예제 #5
0
static int
normalize()
{
    char file_name[MAXPATHLEN+1];
    float32 ***mixw_acc = NULL;
    float32 ***in_mixw = NULL;
    float64 s;
    uint32 n_mixw;
    uint32 n_stream;
    uint32 n_mllr_class;
    uint32 n_density;
    float32 ***tmat_acc = NULL;
    uint32 n_tmat;
    uint32 n_state_pm;
    uint32 i, j, k;
    vector_t ***in_mean = NULL;
    vector_t ***wt_mean = NULL;
    vector_t ***in_var = NULL;
    vector_t ***wt_var = NULL;
    vector_t ****in_fullvar = NULL;
    vector_t ****wt_fullvar = NULL;
    int32 pass2var = FALSE;
    int32 var_is_full = FALSE;
    float32 ***dnom = NULL;
    uint32 n_mgau;
    uint32 n_gau_stream;
    uint32 n_gau_density;
    const uint32 *veclen = NULL;
    const char **accum_dir;
    const char *oaccum_dir;
    const char *in_mixw_fn;
    const char *out_mixw_fn;
    const char *out_tmat_fn;
    const char *in_mean_fn;
    const char *out_mean_fn;
    const char *in_var_fn;
    const char *out_var_fn;
    const char *out_dcount_fn;
    int err;
    uint32 mllr_mult;
    uint32 mllr_add;
    float32 *****regl = NULL;
    float32 ****regr = NULL;
    uint32 no_retries=0;

    
    accum_dir = cmd_ln_str_list("-accumdir");
    oaccum_dir = cmd_ln_str("-oaccumdir");

    out_mixw_fn = cmd_ln_str("-mixwfn");
    out_tmat_fn = cmd_ln_str("-tmatfn");
    out_mean_fn = cmd_ln_str("-meanfn");
    out_var_fn = cmd_ln_str("-varfn");
    in_mixw_fn = cmd_ln_str("-inmixwfn");
    in_mean_fn = cmd_ln_str("-inmeanfn");
    in_var_fn = cmd_ln_str("-invarfn");
    out_dcount_fn = cmd_ln_str("-dcountfn");
    var_is_full = cmd_ln_int32("-fullvar");

    /* must be at least one accum dir */
    assert(accum_dir[0] != NULL);

    if (out_mixw_fn == NULL) {
	E_INFO("No -mixwfn specified, will skip if any\n");
    }
    if (out_tmat_fn == NULL) {
	E_INFO("No -tmatfn specified, will skip if any\n");
    }
    if (out_mean_fn == NULL) {
	E_INFO("No -meanfn specified, will skip if any\n");
    }
    if (out_var_fn == NULL) {
	E_INFO("No -varfn specified, will skip if any\n");
    }
    if (in_mixw_fn != NULL) {
	E_INFO("Selecting unseen mixing weight parameters from %s\n",
	       in_mixw_fn);
    }

    if (in_mean_fn != NULL) {
	E_INFO("Selecting unseen density mean parameters from %s\n",
	       in_mean_fn);

	if (s3gau_read(in_mean_fn,
		       &in_mean,
		       &n_mgau,
		       &n_gau_stream,
		       &n_gau_density,
		       &veclen) != S3_SUCCESS) {
	  E_FATAL_SYSTEM("Couldn't read %s", in_mean_fn);
	}
	ckd_free((void *)veclen);
	veclen = NULL;
    }

    if (in_var_fn != NULL) {
	E_INFO("Selecting unseen density variance parameters from %s\n",
	       in_var_fn);

	if (var_is_full) {
	    if (s3gau_read_full(in_var_fn,
			   &in_fullvar,
			   &n_mgau,
			   &n_gau_stream,
			   &n_gau_density,
			   &veclen) != S3_SUCCESS) {
		E_FATAL_SYSTEM("Couldn't read %s", in_var_fn);
	    }
	}
	else {
	    if (s3gau_read(in_var_fn,
			   &in_var,
			   &n_mgau,
			   &n_gau_stream,
			   &n_gau_density,
			   &veclen) != S3_SUCCESS) {
		E_FATAL_SYSTEM("Couldn't read %s", in_var_fn);
	    }
	}
	ckd_free((void *)veclen);
	veclen = NULL;
    }

    n_stream = 0;
    for (i = 0; accum_dir[i]; i++) {
	E_INFO("Reading and accumulating counts from %s\n", accum_dir[i]);

	if (out_mixw_fn) {
	    rdacc_mixw(accum_dir[i],
		       &mixw_acc, &n_mixw, &n_stream, &n_density);
	}

	if (out_tmat_fn) {
	    rdacc_tmat(accum_dir[i],
		       &tmat_acc, &n_tmat, &n_state_pm);
	}

	if (out_mean_fn || out_var_fn) {
	    if (var_is_full)
		rdacc_den_full(accum_dir[i],
			       &wt_mean,
			       &wt_fullvar,
			       &pass2var,
			       &dnom,
			       &n_mgau,
			       &n_gau_stream,
			       &n_gau_density,
			       &veclen);
	    else
		rdacc_den(accum_dir[i],
			  &wt_mean,
			  &wt_var,
			  &pass2var,
			  &dnom,
			  &n_mgau,
			  &n_gau_stream,
			  &n_gau_density,
			  &veclen);

	    if (out_mixw_fn) {
		if (n_stream != n_gau_stream) {
		    E_ERROR("mixw inconsistent w/ densities WRT # "
			    "streams (%u != %u)\n",
			    n_stream, n_gau_stream);
		}

		if (n_density != n_gau_density) {
		    E_ERROR("mixw inconsistent w/ densities WRT # "
			    "den/mix (%u != %u)\n",
			    n_density, n_gau_density);
		}
	    }
	    else {
		n_stream = n_gau_stream;
		n_density = n_gau_density;
	    }
	}
    }

    if (oaccum_dir && mixw_acc) {
	/* write the total mixing weight reest. accumulators */

	err = 0;
	sprintf(file_name, "%s/mixw_counts", oaccum_dir);

	if (in_mixw_fn) {
	    if (s3mixw_read(in_mixw_fn,
			    &in_mixw,
			    &i,
			    &j,
			    &k) != S3_SUCCESS) {
		E_FATAL_SYSTEM("Unable to read %s", in_mixw_fn);
	    }
	    if (i != n_mixw) {
		E_FATAL("# mixw in input mixw file != # mixw in output mixw file\n");
	    }
	    if (j != n_stream) {
		E_FATAL("# stream in input mixw file != # stream in output mixw file\n");
	    }
	    if (k != n_density) {
		E_FATAL("# density in input mixw file != # density in output mixw file\n");
	    }
	    
	    for (i = 0; i < n_mixw; i++) {
		for (j = 0; j < n_stream; j++) {
		    for (k = 0, s = 0; k < n_density; k++) {
			s += mixw_acc[i][j][k];
		    }
		    if ((s == 0) && in_mixw) {
			for (k = 0, s = 0; k < n_density; k++) {
			    mixw_acc[i][j][k] = in_mixw[i][j][k];
			}
			E_INFO("set mixw %u stream %u to input mixw value\n", i, j);
		    }
		}
	    }
	}

	do {
	    /* Write out the accumulated reestimation sums */
	    if (s3mixw_write(file_name,
			     mixw_acc,
			     n_mixw,
			     n_stream,
			     n_density) != S3_SUCCESS) {
		if (err == 0) {
		    E_ERROR("Unable to write %s; Retrying...\n", file_name);
		}
		++err;
		sleep(3);
		no_retries++;
		if(no_retries>10){ 
		  E_FATAL("Failed to get the files after 10 retries(about 30 seconds).\n ");
		}
	    }
	} while (err > 1);
    }

    if (pass2var)
	    E_INFO("-2passvar yes\n");
    if (oaccum_dir && (wt_mean || wt_var || wt_fullvar)) {
	/* write the total mixing Gau. den reest. accumulators */

	err = 0;
	sprintf(file_name, "%s/gauden_counts", oaccum_dir);
	do {
	    int32 rv;

	    if (var_is_full)
		rv = s3gaucnt_write_full(file_name,
					 wt_mean,
					 wt_fullvar,
					 pass2var,
					 dnom,
					 n_mgau,
					 n_gau_stream,
					 n_gau_density,
					 veclen);
	    else
		rv = s3gaucnt_write(file_name,
				    wt_mean,
				    wt_var,
				    pass2var,
				    dnom,
				    n_mgau,
				    n_gau_stream,
				    n_gau_density,
				    veclen);
		
	    if (rv != S3_SUCCESS) {
		if (err == 0) {
		    E_ERROR("Unable to write %s; Retrying...\n", file_name);
		}
		++err;
		sleep(3);
		no_retries++;
		if(no_retries>10){ 
		  E_FATAL("Failed to get the files after 10 retries(about 5 minutes).\n ");
		}
	    }
	} while (err > 1);
    }

    if (oaccum_dir && tmat_acc) {
	/* write the total transition matrix reest. accumulators */

	err = 0;
	sprintf(file_name, "%s/tmat_counts", oaccum_dir);
	do {
	    if (s3tmat_write(file_name,
			     tmat_acc,
			     n_tmat,
			     n_state_pm) != S3_SUCCESS) {
		if (err == 0) {
		    E_ERROR("Unable to write %s; Retrying...\n", file_name);
		}
		++err;
		sleep(3);
		no_retries++;
		if(no_retries>10){ 
		  E_FATAL("Failed to get the files after 10 retries(about 5 minutes).\n ");
		}
	    }
	} while (err > 1);
    }

    if (oaccum_dir && regr && regl) {
	/* write the total MLLR regression matrix accumulators */

	err = 0;
	sprintf(file_name, "%s/regmat_counts", oaccum_dir);
	do {
	    if (s3regmatcnt_write(file_name,
				  regr,
				  regl,
				  n_mllr_class,
				  n_stream,
				  veclen,
				  mllr_mult,
				  mllr_add) != S3_SUCCESS) {
		if (err == 0) {
		    E_ERROR("Unable to write %s; Retrying...\n", file_name);
		}
		++err;
		sleep(3);
		no_retries++;
		if(no_retries>10){ 
		  E_FATAL("Failed to get the files after 10 retries(about 5 minutes).\n ");
		}
	    }
	} while (err > 1);
    }

    if (wt_mean || wt_var || wt_fullvar) {
	if (out_mean_fn) {
	    E_INFO("Normalizing mean for n_mgau= %u, n_stream= %u, n_density= %u\n",
		   n_mgau, n_stream, n_density);

	    gauden_norm_wt_mean(in_mean, wt_mean, dnom,
				n_mgau, n_stream, n_density, veclen);
	}
	else {
	    if (wt_mean) {
		E_INFO("Ignoring means since -meanfn not specified\n");
	    }
	}

	if (out_var_fn) {
	    if (var_is_full) {
		if (wt_fullvar) {
		    E_INFO("Normalizing fullvar\n");
		    gauden_norm_wt_fullvar(in_fullvar, wt_fullvar, pass2var, dnom,
					   wt_mean,	/* wt_mean now just mean */
					   n_mgau, n_stream, n_density, veclen,
					   cmd_ln_boolean("-tiedvar"));
		}
	    }
	    else {
		if (wt_var) {
		    E_INFO("Normalizing var\n");
		    gauden_norm_wt_var(in_var, wt_var, pass2var, dnom,
				       wt_mean,	/* wt_mean now just mean */
				       n_mgau, n_stream, n_density, veclen,
				       cmd_ln_boolean("-tiedvar"));
		}
	    }
	}
	else {
	    if (wt_var || wt_fullvar) {
		E_INFO("Ignoring variances since -varfn not specified\n");
	    }
	}
    }
    else {
	E_INFO("No means or variances to normalize\n");
    }

    /*
     * Write the parameters to files
     */

    if (out_mixw_fn) {
	if (mixw_acc) {
	    if (s3mixw_write(out_mixw_fn,
			     mixw_acc,
			     n_mixw,
			     n_stream,
			     n_density) != S3_SUCCESS) {
		return S3_ERROR;
	    }
	}
	else {
	    E_WARN("NO mixing weight accumulators seen, but -mixwfn specified.\n");
	}
    }
    else {
	if (mixw_acc) {
	    E_INFO("Mixing weight accumulators seen, but -mixwfn NOT specified.\n");
	}
    }

    if (out_tmat_fn) {
	if (tmat_acc) {
	    if (s3tmat_write(out_tmat_fn,
			     tmat_acc,
			     n_tmat,
			     n_state_pm) != S3_SUCCESS) {
		return S3_ERROR;
	    }
	}
	else {
	    E_WARN("NO transition matrix accumulators seen, but -tmatfn specified.\n");
	}
    }
    else {
	if (tmat_acc) 
	    E_INFO("Transition matrix accumulators seen, but -tmatfn NOT specified\n");
    }

    
    if (out_mean_fn) {
	if (wt_mean) {
	    if (s3gau_write(out_mean_fn,
			    (const vector_t ***)wt_mean,
			    n_mgau,
			    n_stream,
			    n_density,
			    veclen) != S3_SUCCESS)
		return S3_ERROR;
	    
	    if (out_dcount_fn) {
		if (s3gaudnom_write(out_dcount_fn,
				    dnom,
				    n_mgau,
				    n_stream,
				    n_density) != S3_SUCCESS)
		    return S3_ERROR;
	    }
	}
	else
	    E_WARN("NO reestimated means seen, but -meanfn specified\n");
    }
    else {
	if (wt_mean) {
	    E_INFO("Reestimated means seen, but -meanfn NOT specified\n");
	}
    }
    
    if (out_var_fn) {
	if (var_is_full) {
	    if (wt_fullvar) {
		if (s3gau_write_full(out_var_fn,
				     (const vector_t ****)wt_fullvar,
				     n_mgau,
				     n_stream,
				     n_density,
				     veclen) != S3_SUCCESS)
		    return S3_ERROR;
	    }
	    else
		E_WARN("NO reestimated variances seen, but -varfn specified\n");
	}
	else {
	    if (wt_var) {
		if (s3gau_write(out_var_fn,
				(const vector_t ***)wt_var,
				n_mgau,
				n_stream,
				n_density,
				veclen) != S3_SUCCESS)
		    return S3_ERROR;
	    }
	    else
		E_WARN("NO reestimated variances seen, but -varfn specified\n");
	}
    }
    else {
	if (wt_var) {
	    E_INFO("Reestimated variances seen, but -varfn NOT specified\n");
	}
    }

    if (veclen)
	ckd_free((void *)veclen);

    return S3_SUCCESS;
}
static int
run_control_file(const char *ctl)
{
    FILE *ctlfh;
    char *line;
    char *di, *dout, *ei, *eio;
    size_t len;
    int rv, guess_type, guess_sps, guess_endian;
    int32 skip, runlen;

    skip = cmd_ln_int32("-nskip");
    runlen = cmd_ln_int32("-runlen");

    /* Whether to guess file types */
    guess_type = !(cmd_ln_boolean("-raw")
                   || cmd_ln_boolean("-mswav")
                   || cmd_ln_boolean("-nist"));
    /* Whether to guess sampling rate */
    guess_sps = (cmd_ln_int32("-samprate") == 0);
    /* Whether to guess endian */
    guess_endian = (cmd_ln_str("-input_endian") == NULL);

    if ((ctlfh = fopen(ctl, "r")) == NULL) {
        E_ERROR_SYSTEM("Failed to open control file %s", ctl);
        return -1;
    }
    if (cmd_ln_str("-di"))
        di = string_join(cmd_ln_str("-di"), "/", NULL);
    else
        di = ckd_salloc("");
    if (cmd_ln_str("-do"))
        dout = string_join(cmd_ln_str("-do"), "/", NULL);
    else
        dout = ckd_salloc("");
    if (cmd_ln_str("-ei"))
        ei = string_join(".", cmd_ln_str("-ei"), NULL);
    else
        ei = ckd_salloc("");
    if (cmd_ln_str("-eo"))
        eio = string_join(".", cmd_ln_str("-eo"), NULL);
    else
        eio = ckd_salloc("");
    rv = 0;
    while ((line = fread_line(ctlfh, &len)) != NULL) {
        char *infile, *outfile;

        if (skip-- > 0) {
            ckd_free(line);
            continue;
        }
        if (runlen == 0) {
            ckd_free(line);
            break;
        }
        --runlen;

        if (line[len-1] == '\n')
            line[len-1] = '\0';

        infile = string_join(di, line, ei, NULL);
        outfile = string_join(dout, line, eio, NULL);

        /* Reset various guessed information */
        if (guess_type) {
            cmd_ln_set_boolean("-nist", FALSE);
            cmd_ln_set_boolean("-mswav", FALSE);
            cmd_ln_set_boolean("-raw", FALSE);
        }
        if (guess_sps)
            cmd_ln_set_int32("-samprate", 0);
        if (guess_endian)
            cmd_ln_set_str("-input_endian", NULL);

        rv = extract_pitch(infile, outfile);

        ckd_free(infile);
        ckd_free(outfile);
        ckd_free(line);

        if (rv != 0)
            break;
    }
    ckd_free(di);
    ckd_free(dout);
    ckd_free(ei);
    ckd_free(eio);
    fclose(ctlfh);
    return rv;
}
static int
extract_pitch(const char *in, const char *out)
{
    FILE *infh = NULL, *outfh = NULL;
    size_t flen, fshift, nsamps;
    int16 *buf = NULL;
    yin_t *yin = NULL;
    uint16 period, bestdiff;
    int32 sps;

    if (out) {
        if ((outfh = fopen(out, "w")) == NULL) {
            E_ERROR_SYSTEM("Failed to open %s for writing", out);
            goto error_out;
        }
    }
    else {
        outfh = stdout;
    }
    if ((infh = fopen(in, "rb")) == NULL) {
        E_ERROR_SYSTEM("Failed to open %s for reading", in);
        goto error_out;
    }

    /* If we weren't told what the file type is, weakly try to
     * determine it (actually it's pretty obvious) */
    if (!(cmd_ln_boolean("-raw")
          || cmd_ln_boolean("-mswav")
          || cmd_ln_boolean("-nist"))) {
        if (guess_file_type(in, infh) < 0)
            goto error_out;
    }
    
    /* Grab the sampling rate and byte order from the header and also
     * make sure this is 16-bit linear PCM. */
    if (cmd_ln_boolean("-mswav")) {
        if (read_riff_header(infh) < 0)
            goto error_out;
    }
    else if (cmd_ln_boolean("-nist")) {
        if (read_nist_header(infh) < 0)
            goto error_out;
    }
    else if (cmd_ln_boolean("-raw")) {
        /* Just use some defaults for sampling rate and endian. */
        if (cmd_ln_str("-input_endian") == NULL) {
            if (WORDS_BIGENDIAN)
                cmd_ln_set_str("-input_endian", "big");
            else
                cmd_ln_set_str("-input_endian", "little");
        }
        if (cmd_ln_int32("-samprate") == 0)
            cmd_ln_set_int32("-samprate", 16000);
    }

    /* Now read frames and write pitch estimates. */
    sps = cmd_ln_int32("-samprate");
    flen = (size_t)(0.5 + sps * cmd_ln_float32("-flen"));
    fshift = (size_t)(0.5 + sps * cmd_ln_float32("-fshift"));
    yin = yin_init(flen, cmd_ln_float32("-voice_thresh"),
                   cmd_ln_float32("-search_range"),
                   cmd_ln_int32("-smooth_window"));
    if (yin == NULL) {
        E_ERROR("Failed to initialize YIN\n");
        goto error_out;
    }
    buf = ckd_calloc(flen, sizeof(*buf));
    /* Read the first full frame of data. */
    if (fread(buf, sizeof(*buf), flen, infh) != flen) {
        /* Fail silently, which is probably okay. */
    }
    yin_start(yin);
    nsamps = 0;
    while (!feof(infh)) {
        /* Process a frame of data. */
        yin_write(yin, buf);
        if (yin_read(yin, &period, &bestdiff)) {
            fprintf(outfh, "%.3f %.2f %.2f\n",
                    /* Time point. */
                    (double)nsamps/sps,
                    /* "Probability" of voicing. */
                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
                    /* Pitch (possibly bogus) */
                    period == 0 ? sps : (double)sps / period);
            nsamps += fshift;
        }
        /* Shift it back and get the next frame's overlap. */
        memmove(buf, buf + fshift, (flen - fshift) * sizeof(*buf));
        if (fread(buf + flen - fshift, sizeof(*buf), fshift, infh) != fshift) {
            /* Fail silently (FIXME: really?) */
        }
    }
    yin_end(yin);
    /* Process trailing frames of data. */
    while (yin_read(yin, &period, &bestdiff)) {
            fprintf(outfh, "%.3f %.2f %.2f\n",
                    /* Time point. */
                    (double)nsamps/sps,
                    /* "Probability" of voicing. */
                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
                    /* Pitch (possibly bogus) */
                    period == 0 ? sps : (double)sps / period);
    }

    if (yin)
        yin_free(yin);
    ckd_free(buf);
    fclose(infh);
    if (outfh != stdout)
        fclose(outfh);
    return 0;

error_out:
    yin_free(yin);
    ckd_free(buf);
    if (infh) fclose(infh);
    if (outfh && outfh != stdout) fclose(outfh);
    return -1;
}
예제 #8
0
static void
gst_pocketsphinx_set_property (GObject * object, guint prop_id,
			       const GValue * value, GParamSpec * pspec)
{
    GstPocketSphinx *sink = GST_POCKETSPHINX(object);

    switch (prop_id) {
    case PROP_HMM_DIR:
        gst_pocketsphinx_set_string(sink, "-hmm", value);
        break;
    case PROP_LM_FILE:
        /* FSG and LM are mutually exclusive. */
        gst_pocketsphinx_set_string(sink, "-fsg", NULL);
        gst_pocketsphinx_set_string(sink, "-lm", value);
        /* Switch to this new LM if the decoder is active. */
        if (sink->ad.initialized) {
            lm_read(g_value_get_string(value),
                    g_value_get_string(value),
                    cmd_ln_float32("-lw"),
                    cmd_ln_float32("-uw"),
                    cmd_ln_float32("-wip"));
            uttproc_set_lm(g_value_get_string(value));
        }
        break;
    case PROP_DICT_FILE:
        gst_pocketsphinx_set_string(sink, "-dict", value);
        break;
    case PROP_FSG_FILE:
        /* FSG and LM are mutually exclusive */
        gst_pocketsphinx_set_string(sink, "-lm", NULL);
        gst_pocketsphinx_set_string(sink, "-fsg", value);
        /* Switch to this new FSG if the decoder is active. */
        if (sink->ad.initialized) {
            char *fsgname;

            fsgname = uttproc_load_fsgfile((char *)
                                           g_value_get_string(value));
            if (fsgname)
                uttproc_set_fsg(fsgname);
        }
        break;
    case PROP_S2_FSG:
    {
        s2_fsg_t *fsg = g_value_get_pointer(value);

        uttproc_del_fsg(fsg->name);
        uttproc_load_fsg(g_value_get_pointer(value),
                         cmd_ln_boolean("-fsgusealtpron"),
                         cmd_ln_boolean("-fsgusefiller"),
                         cmd_ln_float32("-silpen"),
                         cmd_ln_float32("-fillpen"),
                         cmd_ln_float32("-lw"));
        uttproc_set_fsg(fsg->name);
        break;
    }
    case PROP_FWDFLAT:
        gst_pocketsphinx_set_boolean(sink, "-fwdflat", value);
        break;
    case PROP_BESTPATH:
        gst_pocketsphinx_set_boolean(sink, "-bestpath", value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        return;
    }
}
예제 #9
0
static int
initialize(int argc,
	   char *argv[])
{
    const char *fdictfn;
    const char *dictfn;
    const char *ts2cbfn;
    uint32 n_ts;
    uint32 n_cb;

    /* define, parse and (partially) validate the command line */
    parse_cmd_ln(argc, argv);

    feat = 
        feat_init(cmd_ln_str("-feat"),
                  cmn_type_from_str(cmd_ln_str("-cmn")),
                  cmd_ln_boolean("-varnorm"),
                  agc_type_from_str(cmd_ln_str("-agc")),
                  1, cmd_ln_int32("-ceplen"));


    if (cmd_ln_str("-lda")) {
        E_INFO("Reading linear feature transformation from %s\n",
               cmd_ln_str("-lda"));
        if (feat_read_lda(feat,
                          cmd_ln_str("-lda"),
                          cmd_ln_int32("-ldadim")) < 0)
            return -1;
    }

    if (cmd_ln_str("-svspec")) {
        int32 **subvecs;
        E_INFO("Using subvector specification %s\n", 
               cmd_ln_str("-svspec"));
        if ((subvecs = parse_subvecs(cmd_ln_str("-svspec"))) == NULL)
            return -1;
        if ((feat_set_subvecs(feat, subvecs)) < 0)
            return -1;
    }

    if (cmd_ln_exists("-agcthresh")
        && 0 != strcmp(cmd_ln_str("-agc"), "none")) {
        agc_set_threshold(feat->agc_struct,
                          cmd_ln_float32("-agcthresh"));
    }

    if (feat->cmn_struct
        && cmd_ln_exists("-cmninit")) {
        char *c, *cc, *vallist;
        int32 nvals;

        vallist = ckd_salloc(cmd_ln_str("-cmninit"));
        c = vallist;
        nvals = 0;
        while (nvals < feat->cmn_struct->veclen
               && (cc = strchr(c, ',')) != NULL) {
            *cc = '\0';
            feat->cmn_struct->cmn_mean[nvals] = FLOAT2MFCC(atof(c));
            c = cc + 1;
            ++nvals;
        }
        if (nvals < feat->cmn_struct->veclen && *c != '\0') {
            feat->cmn_struct->cmn_mean[nvals] = FLOAT2MFCC(atof(c));
        }
        ckd_free(vallist);
    }


    if (cmd_ln_str("-segdir"))
	corpus_set_seg_dir(cmd_ln_str("-segdir"));
    if (cmd_ln_str("-segext"))
	corpus_set_seg_ext(cmd_ln_str("-segext"));

    corpus_set_mfcc_dir(cmd_ln_str("-cepdir"));
    corpus_set_mfcc_ext(cmd_ln_str("-cepext"));

    if (cmd_ln_str("-lsnfn"))
	corpus_set_lsn_filename(cmd_ln_str("-lsnfn"));

    corpus_set_ctl_filename(cmd_ln_str("-ctlfn"));

    if (cmd_ln_int32("-nskip") && cmd_ln_int32("-runlen")) {
        corpus_set_interval(cmd_ln_int32("-nskip"),
			    cmd_ln_int32("-runlen"));
    } else if (cmd_ln_int32("-part") && cmd_ln_int32("-npart")) {
	corpus_set_partition(cmd_ln_int32("-part"),
			     cmd_ln_int32("-npart"));
    }
    

    if (corpus_init() != S3_SUCCESS) {
	return S3_ERROR;
    }
    
    if (cmd_ln_str("-moddeffn")) {
	E_INFO("Reading %s\n", cmd_ln_str("-moddeffn"));
    
	/* Read in the model definitions.  Defines the set of
	   CI phones and context dependent phones.  Defines the
	   transition matrix tying and state level tying. */
	if (model_def_read(&mdef,
			   cmd_ln_str("-moddeffn")) != S3_SUCCESS) {
	    return S3_ERROR;
	}
	
	ts2cbfn = cmd_ln_str("-ts2cbfn");
	if (strcmp(SEMI_LABEL, ts2cbfn) == 0) {
	    mdef->cb = semi_ts2cb(mdef->n_tied_state);
	    n_ts = mdef->n_tied_state;
	    n_cb = 1;
	}
	else if (strcmp(CONT_LABEL, ts2cbfn) == 0) {
	    mdef->cb = cont_ts2cb(mdef->n_tied_state);
	    n_ts = mdef->n_tied_state;
	    n_cb = mdef->n_tied_state;
	}
	else if (strcmp(PTM_LABEL, ts2cbfn) == 0) {
	    mdef->cb = ptm_ts2cb(mdef);
	    n_ts = mdef->n_tied_state;
	    n_cb = mdef->acmod_set->n_ci;
	}
	else if (s3ts2cb_read(ts2cbfn,
			      &mdef->cb,
			      &n_ts,
			      &n_cb) != S3_SUCCESS) {
	    return S3_ERROR;
	}

	dictfn = cmd_ln_str("-dictfn");

	if (dictfn == NULL) {
	    E_FATAL("You must specify a content dictionary using -dictfn\n");
	}

	E_INFO("Reading %s\n", dictfn);
	
	lex = lexicon_read(NULL,	/* no lexicon to start */
			   dictfn,
			   mdef->acmod_set);
	if (lex == NULL)
	    return S3_ERROR;
    
	fdictfn = cmd_ln_str("-fdictfn");

	if (fdictfn) {
	    E_INFO("Reading %s\n", fdictfn);
	    
	    (void)lexicon_read(lex,	/* add filler words content lexicon */
			       fdictfn,
			       mdef->acmod_set);
	}
    }

    return S3_SUCCESS;
}
예제 #10
0
/*********************************************************************
   FUNCTION:   fe_init_auto
   PARAMETERS: fe_t *
   RETURNS:    nothing
   DESCRIPTION: automatically grab front-end parameters from command
   line arguments and initializes the front-end structure
**********************************************************************/
fe_t *
fe_init_auto()
{
    param_t p;

    fe_init_params(&p);

    p.SAMPLING_RATE = cmd_ln_float32("-samprate");
    p.FRAME_RATE = cmd_ln_int32("-frate");
    p.WINDOW_LENGTH = cmd_ln_float32("-wlen");
    if (strcmp("mel_scale", cmd_ln_str("-fbtype")) == 0)
        p.FB_TYPE = MEL_SCALE;
    else if (strcmp("log_linear", cmd_ln_str("-fbtype")) == 0)
        p.FB_TYPE = LOG_LINEAR;
    else {
        E_WARN("Invalid fbtype\n");
        return NULL;
    }

    p.NUM_CEPSTRA = cmd_ln_int32("-ncep");
    p.NUM_FILTERS = cmd_ln_int32("-nfilt");
    p.FFT_SIZE = cmd_ln_int32("-nfft");

    p.UPPER_FILT_FREQ = cmd_ln_float32("-upperf");
    p.LOWER_FILT_FREQ = cmd_ln_float32("-lowerf");
    p.PRE_EMPHASIS_ALPHA = cmd_ln_float32("-alpha");
    if (cmd_ln_boolean("-dither")) {
        p.dither = 1;
        p.seed = cmd_ln_int32("-seed");
    }
    else
        p.dither = 0;

#ifdef WORDS_BIGENDIAN
    p.swap = strcmp("big", cmd_ln_str("-input_endian")) == 0 ? 0 : 1;
#else        
    p.swap = strcmp("little", cmd_ln_str("-input_endian")) == 0 ? 0 : 1;
#endif

    if (cmd_ln_boolean("-logspec"))
        p.logspec = RAW_LOG_SPEC;
    if (cmd_ln_boolean("-smoothspec"))
        p.logspec = SMOOTH_LOG_SPEC;
    p.doublebw = cmd_ln_boolean("-doublebw");
    p.unit_area = cmd_ln_boolean("-unit_area");
    p.round_filters = cmd_ln_boolean("-round_filters");
    p.remove_dc = cmd_ln_boolean("-remove_dc");
    p.verbose = cmd_ln_boolean("-verbose");

    if (0 == strcmp(cmd_ln_str("-transform"), "dct"))
        p.transform = DCT_II;
    else if (0 == strcmp(cmd_ln_str("-transform"), "legacy"))
        p.transform = LEGACY_DCT;
    else if (0 == strcmp(cmd_ln_str("-transform"), "htk"))
        p.transform = DCT_HTK;
    else {
        E_WARN("Invalid transform type (values are 'dct', 'legacy', 'htk')\n");
        return NULL;
    }

    p.warp_type = cmd_ln_str("-warp_type");
    p.warp_params = cmd_ln_str("-warp_params");

    p.lifter_val = cmd_ln_int32("-lifter");

    return fe_init(&p);

}