예제 #1
0
int cst_track_save_est_binary(cst_track *t, const char *filename)
{
    cst_file fd;
    float foo;
    int i, j;

    if ((fd = cst_fopen(filename, CST_OPEN_WRITE | CST_OPEN_BINARY)) == NULL)
    {
        cst_errmsg("cst_track_save_est_binary: can't open file \"%s\"\n",
                   filename);
        return -1;
    }

    cst_fprintf(fd, "EST_File Track\n");
    cst_fprintf(fd, "DataType binary\n");
    cst_fprintf(fd, "ByteOrder %s\n",
                CST_LITTLE_ENDIAN ? BYTE_ORDER_LITTLE : BYTE_ORDER_BIG);
    cst_fprintf(fd, "NumFrames %d\n", t->num_frames);
    cst_fprintf(fd, "NumChannels %d\n", t->num_channels);
    cst_fprintf(fd, "BreaksPresent true\n");
    cst_fprintf(fd, "EST_Header_End\n");

    foo = 1.0;                  /* put a bogus 'breaks' value in for now */
    for (i = 0; i < t->num_frames; i++)
    {
        cst_fwrite(fd, t->times + i, sizeof(float), 1);
        cst_fwrite(fd, &foo, sizeof(float), 1);
        for (j = 0; j < t->num_channels; j++)
            cst_fwrite(fd, &(t->frames[i][j]), sizeof(float), 1);
    }

    cst_fclose(fd);

    return 0;
}
cst_filemap *cst_read_whole_file(const char *path)
{
    cst_filemap *fmap;
    cst_file fh;

    if ((fh = cst_fopen(path, CST_OPEN_READ)) < 0) {
	cst_errmsg("cst_read_whole_file: Failed to open file\n");
	return NULL;
    }

    fmap = cst_alloc(cst_filemap, 1);
    fmap->fh = fh;
    fmap->mapsize = cst_filesize(fmap->fh);
    fmap->mem = cst_alloc(char, fmap->mapsize);
    if (cst_fread(fmap->fh, fmap->mem, 1, fmap->mapsize) < fmap->mapsize)
    {
	cst_errmsg("cst_read_whole_file: read() failed\n");
	cst_fclose(fmap->fh);
	cst_free(fmap->mem);
	cst_free(fmap);
	return NULL;
    }

    return fmap;
}
예제 #3
0
파일: cst_rel_io.c 프로젝트: zeehio/mimic
int relation_save(cst_relation *r, const char *filename)
{
    cst_file fd;
    cst_item *item;

    if (cst_streq(filename, "-"))
        fd = stdout;
    else

    if ((fd = cst_fopen(filename, CST_OPEN_WRITE)) == 0)
    {
        cst_errmsg("relation_save: can't open file \"%s\" for writing\n",
                   filename);
        return CST_ERROR_FORMAT;
    }

    for (item = relation_head(r); item; item = item_next(item))
    {
        if (item_feat_present(item, "end"))
            cst_fprintf(fd, "%f ", item_feat_float(item, "end"));
        else
            cst_fprintf(fd, "%f ", 0.00);
        if (item_feat_present(item, "name"))
            cst_fprintf(fd, "%s ", item_feat_string(item, "name"));
        else
            cst_fprintf(fd, "%s ", "_");
        cst_fprintf(fd, "\n");
    }
    if (fd != stdout)
        cst_fclose(fd);

    return CST_OK_FORMAT;
}
예제 #4
0
int cst_track_save_est(cst_track *t, const char *filename)
{
    cst_file fd;
    int i, j;

    if ((fd = cst_fopen(filename, CST_OPEN_WRITE | CST_OPEN_BINARY)) == NULL)
    {
        cst_errmsg("cst_track_save_est: can't open file \"%s\"\n", filename);
        return -1;
    }

    cst_fprintf(fd, "EST_File Track\n");
    cst_fprintf(fd, "DataType ascii\n");
    cst_fprintf(fd, "NumFrames %d\n", t->num_frames);
    cst_fprintf(fd, "NumChannels %d\n", t->num_channels);
    cst_fprintf(fd, "BreaksPresent true\n");
    cst_fprintf(fd, "EST_Header_End\n");

    for (i = 0; i < t->num_frames; i++)
    {
        cst_fprintf(fd, "%f\t1 \t", t->times[i]);
        for (j = 0; j < t->num_channels; j++)
            cst_fprintf(fd, "%f ", t->frames[i][j]);
        cst_fprintf(fd, "\n");
    }

    cst_fclose(fd);

    return 0;
}
int cst_free_part_file(cst_filemap *fmap)
{
    if (cst_fclose(fmap->fh) < 0) {
	cst_errmsg("cst_munmap_file: cst_fclose() failed\n");
	return -1;
    }
    cst_free(fmap);
    return 0;
}
int cst_free_whole_file(cst_filemap *fmap)
{
    if (cst_fclose(fmap->fh) < 0) {
	cst_errmsg("cst_free_whole_file: close() failed\n");
	return -1;
    }
    cst_free(fmap->mem);
    cst_free(fmap);
    return 0;
}
예제 #7
0
int cst_free_whole_file(cst_filemap *fmap)
{
    if (cst_fclose(fmap->fh) < 0) {
	cst_errmsg("cst_read_whole_file: cst_fclose() failed\n");
	return -1;
    }
    VirtualFree(fmap->mem, fmap->mapsize, MEM_DECOMMIT);
    cst_free(fmap);
    return 0;
}
예제 #8
0
void ts_close(cst_tokenstream *ts)
{
    if (ts->fd != NULL)
    {
#ifndef UNDER_CE
	if (ts->fd != stdin)
#endif
	    cst_fclose(ts->fd);
	ts->fd = NULL; /* just in case close gets called twice */
    }
    if (ts->string_buffer != NULL)
    {
        cst_free(ts->string_buffer);
	ts->string_buffer = NULL;
    }
    delete_tokenstream(ts);
}
cst_voice *cst_cg_load_voice(const char *filename,
                             const cst_lang *lang_table)
{
    cst_voice *vox;
    cst_lexicon *lex = NULL;
    int i, end_of_features;
    const char *language;
    const char *xname;
    cst_cg_db *cg_db;
    char* fname;
    char* fval;
    cst_file vd;

    vd = cst_fopen(filename,CST_OPEN_READ | CST_OPEN_BINARY);
    if (vd == NULL)
    {
        cst_errmsg("Error load voice: can't open file %s\n",filename);
	return NULL;
    }

    if (cst_cg_read_header(vd) != 0)
    {
        cst_errmsg("Error load voice: %s does not have expected header\n",filename);
        cst_fclose(vd);
        return NULL;
    }

    vox = new_voice();

    /* Read voice features from the external file */
    /* Read until the feature is "end_of_features" */
    fname="";
    end_of_features = 0;
    while (end_of_features == 0)
    {
	cst_read_voice_feature(vd,&fname, &fval);
        if (cst_streq(fname,"end_of_features"))
            end_of_features = 1;
        else
        {
            xname = feat_own_string(vox->features,fname);
            flite_feat_set_string(vox->features,xname, fval);
        }
        cst_free(fname);
        cst_free(fval);
    }

    /* Load up cg_db from external file */
    cg_db = cst_cg_load_db(vox,vd);

    if (cg_db == NULL)
    {
	cst_fclose(vd);
        return NULL;
    }

    /* Use the language feature to initialize the correct voice */
    language = flite_get_param_string(vox->features, "language", "");

    /* Search Lang table for lang_init() and lex_init(); */
    for (i=0; lang_table[i].lang; i++)
    {
        if (cst_streq(language,lang_table[i].lang))
        {
            (lang_table[i].lang_init)(vox);
            lex = (lang_table[i].lex_init)();
            break;
        }
    }
    if (lex == NULL)
    {   /* Language is not supported */
	/* Delete allocated memory in cg_db */
	cst_cg_free_db(vd,cg_db);
	cst_fclose(vd);
        cst_errmsg("Error load voice: lang/lex %s not supported in this binary\n",language);
	return NULL;	
    }
    
    /* Things that weren't filled in already. */
    vox->name = cg_db->name;
    flite_feat_set_string(vox->features,"name",cg_db->name);
    flite_feat_set_string(vox->features,"pathname",filename);
    
    flite_feat_set(vox->features,"lexicon",lexicon_val(lex));
    flite_feat_set(vox->features,"postlex_func",uttfunc_val(lex->postlex));

    /* No standard segment durations are needed as its done at the */
    /* HMM state level */
    flite_feat_set_string(vox->features,"no_segment_duration_model","1");
    flite_feat_set_string(vox->features,"no_f0_target_model","1");

    /* Waveform synthesis */
    flite_feat_set(vox->features,"wave_synth_func",uttfunc_val(&cg_synth));
    flite_feat_set(vox->features,"cg_db",cg_db_val(cg_db));
    flite_feat_set_int(vox->features,"sample_rate",cg_db->sample_rate);

    cst_fclose(vd);
    return vox;
}
예제 #10
0
static int play_wave_from_socket(snd_header *header,int audiostream)
{
    /* Read audio from stream and play it to audio device, converting */
    /* it to pcm if required                                          */
    int num_samples;
    int sample_width;
    cst_audiodev *audio_device;
    int q,i,n,r;
    unsigned char bytes[CST_AUDIOBUFFSIZE];
    short shorts[CST_AUDIOBUFFSIZE];
    cst_file fff;

    fff = cst_fopen("/tmp/awb.wav",CST_OPEN_WRITE|CST_OPEN_BINARY);

    if ((audio_device = audio_open(header->sample_rate,1,
				   (header->encoding == CST_SND_SHORT) ?
				   CST_AUDIO_LINEAR16 : CST_AUDIO_LINEAR8)) == NULL)
    {
	cst_errmsg("play_wave_from_socket: can't open audio device\n");
	return -1;
    }

    if (header->encoding == CST_SND_SHORT)
	sample_width = 2;
    else
	sample_width = 1;

    num_samples = header->data_size / sample_width;
    /* we naively let the num_channels sort itself out */
    for (i=0; i < num_samples; i += r/2)
    {
	if (num_samples > i+CST_AUDIOBUFFSIZE)
	    n = CST_AUDIOBUFFSIZE;
	else
	    n = num_samples-i;
	if (header->encoding == CST_SND_ULAW)
	{
	    r = read(audiostream,bytes,n);
	    for (q=0; q<r; q++)
		shorts[q] = cst_ulaw_to_short(bytes[q]);
	    r *= 2;
	}
	else /* if (header->encoding == CST_SND_SHORT) */
	{
	    r = read(audiostream,shorts,n*2);
	    if (CST_LITTLE_ENDIAN)
		for (q=0; q<r/2; q++)
		    shorts[q] = SWAPSHORT(shorts[q]);
	}
	
	if (r <= 0)
	{   /* I'm not getting any data from the server */
	    audio_close(audio_device);
	    return CST_ERROR_FORMAT;
	}
	
	for (q=r; q > 0; q-=n)
	{
	    n = audio_write(audio_device,shorts,q);
	    cst_fwrite(fff,shorts,2,q);
	    if (n <= 0)
	    {
		audio_close(audio_device);
		return CST_ERROR_FORMAT;
	    }
	}
    }
    audio_close(audio_device);
    cst_fclose(fff);

    return CST_OK_FORMAT;

}