Beispiel #1
0
int32
swap_check(FILE *fp)
{
    uint32 magic;
    uint32 ret = -1;

    if (fread_retry(&magic, sizeof(uint32), 1, fp) != 1) {
	E_ERROR("error while reading bo_magic\n");

	ret = -1;
	
	return ret;
    }

    if (magic != BYTE_ORDER_MAGIC) {
	/* either need to swap or got bogus magic number */
	
	SWAP_INT32(&magic);
	
	if (magic == BYTE_ORDER_MAGIC) {
	    ret = 1;
	}
	else {
	    /* could not get magic number by swapping, so it is bogus */
	    E_ERROR("Expected to read 0x%x (or byte permuted) byte order indicator.  Instead read 0x%x\n",
		    BYTE_ORDER_MAGIC, SWAP_INT32(&magic));
	    
	    ret = -1;
	}
    }
    else {
	/* BYTE_ORDER_MAGIC was read; so no need to swap */
	ret = 0;
    }

    return ret;
}
Beispiel #2
0
/**
 * Read Sphinx-II format mfc file (s2mfc = Sphinx-II format MFC data).
 * If out_mfc is 0, no actual reading will be done, and the number of 
 * frames (plus padding) that would be read is returned.
 * 
 * It's important that normalization is done before padding because
 * frames outside the data we are interested in shouldn't be taken
 * into normalization stats.
 *
 * @return # frames read (plus padding) if successful, -1 if
 * error (e.g., mfc array too small).  
 */
static int32
feat_s2mfc_read_norm_pad(feat_t *fcb, char *file, int32 win,
            		 int32 sf, int32 ef,
            		 mfcc_t ***out_mfc,
            		 int32 maxfr,
            		 int32 cepsize)
{
    FILE *fp;
    int32 n_float32;
    float32 *float_feat;
    struct stat statbuf;
    int32 i, n, byterev;
    int32 start_pad, end_pad;
    mfcc_t **mfc;

    /* Initialize the output pointer to 0, so that any attempts to
       free() it if we fail before allocating it will not segfault! */
    if (out_mfc)
        *out_mfc = 0;
    E_INFO("Reading mfc file: '%s'[%d..%d]\n", file, sf, ef);
    if (ef >= 0 && ef <= sf) {
        E_ERROR("%s: End frame (%d) <= Start frame (%d)\n", file, ef, sf);
        return -1;
    }

    /* Find filesize; HACK!! To get around intermittent NFS failures, use stat_retry */
    if ((stat_retry(file, &statbuf) < 0)
        || ((fp = fopen(file, "rb")) == 0)) {
#ifndef POCKETSPHINX_NET
         E_ERROR("Failed to open file '%s' for reading: %s\n", file, strerror(errno));
#endif
        return -1;
    }

    /* Read #floats in header */
    if (fread_retry(&n_float32, sizeof(int32), 1, fp) != 1) {
        E_ERROR("%s: fread(#floats) failed\n", file);
        fclose(fp);
        return -1;
    }

    /* Check if n_float32 matches file size */
    byterev = 0;
    if ((int32) (n_float32 * sizeof(float32) + 4) != (int32) statbuf.st_size) { /* RAH, typecast both sides to remove compile warning */
        n = n_float32;
        SWAP_INT32(&n);

        if ((int32) (n * sizeof(float32) + 4) != (int32) (statbuf.st_size)) {   /* RAH, typecast both sides to remove compile warning */
            E_ERROR
                ("%s: Header size field: %d(%08x); filesize: %d(%08x)\n",
                 file, n_float32, n_float32, statbuf.st_size,
                 statbuf.st_size);
            fclose(fp);
            return -1;
        }

        n_float32 = n;
        byterev = 1;
    }
    if (n_float32 <= 0) {
        E_ERROR("%s: Header size field (#floats) = %d\n", file, n_float32);
        fclose(fp);
        return -1;
    }

    /* Convert n to #frames of input */
    n = n_float32 / cepsize;
    if (n * cepsize != n_float32) {
        E_ERROR("Header size field: %d; not multiple of %d\n", n_float32,
                cepsize);
        fclose(fp);
        return -1;
    }

    /* Check start and end frames */
    if (sf > 0) {
        if (sf >= n) {
            E_ERROR("%s: Start frame (%d) beyond file size (%d)\n", file,
                    sf, n);
            fclose(fp);
            return -1;
        }
    }
    if (ef < 0)
        ef = n-1;
    else if (ef >= n) {
        E_WARN("%s: End frame (%d) beyond file size (%d), will truncate\n",
               file, ef, n);
        ef = n-1;
    }

    /* Add window to start and end frames */
    sf -= win;
    ef += win;
    if (sf < 0) {
        start_pad = -sf;
        sf = 0;
    }
    else
        start_pad = 0;
    if (ef >= n) {
        end_pad = ef - n + 1;
        ef = n - 1;
    }
    else
        end_pad = 0;

    /* Limit n if indicated by [sf..ef] */
    if ((ef - sf + 1) < n)
        n = (ef - sf + 1);
    if (maxfr > 0 && n + start_pad + end_pad > maxfr) {
        E_ERROR("%s: Maximum output size(%d frames) < actual #frames(%d)\n",
                file, maxfr, n + start_pad + end_pad);
        fclose(fp);
        return -1;
    }

    /* If no output buffer was supplied, then skip the actual data reading. */
    if (out_mfc != 0) {
        /* Position at desired start frame and read actual MFC data */
        mfc = (mfcc_t **)ckd_calloc_2d(n + start_pad + end_pad, cepsize, sizeof(mfcc_t));
        if (sf > 0)
            fseek(fp, sf * cepsize * sizeof(float32), SEEK_CUR);
        n_float32 = n * cepsize;
#ifdef FIXED_POINT
        float_feat = ckd_calloc(n_float32, sizeof(float32));
#else
        float_feat = mfc[start_pad];
#endif
        if (fread_retry(float_feat, sizeof(float32), n_float32, fp) != n_float32) {
            E_ERROR("%s: fread(%dx%d) (MFC data) failed\n", file, n, cepsize);
            ckd_free_2d(mfc);
            fclose(fp);
            return -1;
        }
        if (byterev) {
            for (i = 0; i < n_float32; i++) {
                SWAP_FLOAT32(&float_feat[i]);
            }
        }
#ifdef FIXED_POINT
        for (i = 0; i < n_float32; ++i) {
            mfc[start_pad][i] = FLOAT2MFCC(float_feat[i]);
        }
        ckd_free(float_feat);
#endif

        /* Normalize */
        feat_cmn(fcb, mfc + start_pad, n, 1, 1);
        feat_agc(fcb, mfc + start_pad, n, 1, 1);

        /* Replicate start and end frames if necessary. */
        for (i = 0; i < start_pad; ++i)
            memcpy(mfc[i], mfc[start_pad], cepsize * sizeof(mfcc_t));
        for (i = 0; i < end_pad; ++i)
            memcpy(mfc[start_pad + n + i], mfc[start_pad + n - 1],
                   cepsize * sizeof(mfcc_t));

        *out_mfc = mfc;
    }

    fclose(fp);
    return n + start_pad + end_pad;
}
Beispiel #3
0
main (int argc, char *argv[])
{
    struct stat statbuf;
    char *file;
    FILE *fp;
    int32 byterev;
    int32 i, n, n_float32, fr, numbered, sd, ed, smooth;
    float32 cep[13], cep_1[13], cep_2[13], c;	/* Hack!! Hardwired 13 */
    
    if (argc < 2)
	usagemsg (argv[0]);

    numbered = 0;
    file = NULL;
    sd = -1;	/* Start and end dimensions to output */
    ed = -1;
    smooth = 0;
    for (i = 1; i < argc; i++) {
	if (argv[i][0] == '-') {
	    switch (argv[i][1]) {
	    case 'n':
		if (numbered)
		    usagemsg (argv[0]);
		numbered = 1;
		break;
	    case 'l':
		if (smooth)
		    usagemsg (argv[0]);
		smooth = 1;
		break;
	    case 's':
		if ((sd >= 0) || (sscanf (&(argv[i][2]), "%d", &sd) != 1) ||
		    (sd < 0) || (sd > 12))
		    usagemsg (argv[0]);
		break;
	    case 'e':
		if ((ed >= 0) || (sscanf (&(argv[i][2]), "%d", &ed) != 1) ||
		    (ed < 0) || (ed > 12))
		    usagemsg (argv[0]);
		break;
	    default:
		usagemsg (argv[0]);
		break;
	    }
	} else {
	    if (file)
		usagemsg (argv[0]);
	    file = argv[i];
	}
    }
    if (! file)
	usagemsg (argv[0]);
    if (sd < 0)
	sd = 0;
    if (ed < 0)
	ed = 12;
    
    if (stat (file, &statbuf) != 0) {
	E_ERROR("stat(%s) failed\n", file);
	return -1;
    }
    
    if ((fp = fopen(file, "rb")) == NULL) {
	E_ERROR("fopen(%s,rb) failed\n", file);
	return -1;
    }
    
    /* Read #floats in header */
    if (fread_retry (&n_float32, sizeof(int32), 1, fp) != 1) {
	fclose (fp);
	return -1;
    }
    
    /* Check of n_float32 matches file size */
    byterev = FALSE;
    if ((n_float32*sizeof(float32) + 4) != statbuf.st_size) {
	n = n_float32;
	SWAP_INT32(&n);

	if ((n*sizeof(float32) + 4) != statbuf.st_size) {
	    E_ERROR("Header size field: %d(%08x); filesize: %d(%08x)\n",
		    n_float32, n_float32, statbuf.st_size, statbuf.st_size);
	    fclose (fp);
	    return -1;
	}

	n_float32 = n;
	byterev = TRUE;
    }
    if (n_float32 <= 0) {
	E_ERROR("Header size field: %d\n",  n_float32);
	fclose (fp);
	return -1;
    }
    if (byterev)
	E_INFO("Byte-reversing %s\n", file);
    
    E_INFO("Dimensions %d..%d%s\n", sd, ed, (smooth ? " (LPFed)" : ""));

    fr = 0;
    while (fread (cep, sizeof(float32), 13, fp) == 13) {
	if (byterev) {
	    for (i = 0; i < 13; i++) {
		SWAP_FLOAT32(cep+i);
	    }
	}
	
	if (smooth) {
	    /* Smoothing (LPF-ing): 0.25, 0.5, 0.25; except at the ends: 0.5, 0.5 */
	    if ((fr > 0) && numbered)
		printf ("%10d ", fr-1);
	    
	    if (fr == 1) {
		for (i = sd; i <= ed; i++) {
		    c = 0.5 * cep_1[i] + 0.5 * cep[i];
		    printf (" %11.7f", c);
		}
	    } else if (fr > 1) {
		for (i = sd; i <= ed; i++) {
		    c = 0.25 * cep_2[i] + 0.5 * cep_1[i] + 0.25 * cep[i];
		    printf (" %11.7f", c);
		}
	    }

	    memcpy (cep_2, cep_1, sizeof(float32) * 13);
	    memcpy (cep_1, cep, sizeof(float32) * 13);
	} else {
	    if (numbered)
		printf ("%10d ", fr);
	    
	    for (i = sd; i <= ed; i++) {
		printf (" %11.7f", cep[i]);
	    }
	}
	
	if ((! smooth) || (fr > 0))
	    printf ("\n");
	fflush (stdout);
	
	fr++;
    }
    
    if (smooth && (fr > 0)) {
	if (numbered)
	    printf ("%10d ", fr-1);
	
	if (fr > 1) {
	    for (i = sd; i <= ed; i++) {
		c = 0.5 * cep_2[i] + 0.5 * cep_1[i];
		printf (" %11.7f", c);
	    }
	} else if (fr == 1) {
	    for (i = sd; i <= ed; i++)
		printf (" %11.7f", cep_1[i]);
	}

	printf ("\n");
	fflush (stdout);
    }
}
/*
 * Read specified segment [sf..ef] of Sphinx-II format mfc file read and return
 * #frames read.  Return -1 if error.
 */
int32 s2mfc_read (char *file, int32 sf, int32 ef, float32 ***mfc, int32 veclen)
{
    FILE *fp;
    int32 n_float32;
    struct stat statbuf;
    int32 i, n, byterev, cepsize;
    char tmp;
    
    E_INFO("Reading mfc file: %s\n", file);
    
    assert (fcb.cepsize != NULL);
    cepsize = ( *(fcb.cepsize) ) (veclen);
    
    /* Find filesize; HACK!! To get around intermittent NFS failures, use stat_retry */
    if (stat_retry (file, &statbuf) < 0) {
	E_ERROR("stat_retry(%s) failed\n", file);
	return -1;
    }

    if ((fp = fopen(file, "rb")) == NULL) {
	E_ERROR("fopen(%s,rb) failed\n", file);
	return -1;
    }
    
    /* Read #floats in header */
    if (fread_retry (&n_float32, sizeof(int32), 1, fp) != 1) {
	fclose (fp);
	return -1;
    }
    

    /* Check if n_float32 matches file size */
/* pip: this is a hack to get it to run on simulator:
	we always know the endianess of the sim (big-e),
	so always swap_int32

    byterev = FALSE;
    if ((n_float32*sizeof(float32) + 4) != statbuf.st_size) {
	n = n_float32;
	SWAP_INT32(&n);

	if ((n*sizeof(float32) + 4) != statbuf.st_size) {
	    E_ERROR("Header size field: %d(%08x); filesize: %d(%08x)\n",
		    n_float32, n_float32, statbuf.st_size, statbuf.st_size);
	    fclose (fp);
	    return -1;
	}

	n_float32 = n;
	byterev = TRUE;
    }
pip */
/* pip : */ byterev = FALSE;


    if (n_float32 <= 0) {
	E_ERROR("Header size field: %d\n",  n_float32);
	fclose (fp);
	return -1;
    }
    
	E_INFO("pip: n = %d, n_float32 = %d, cepsize = %d, veclen = %d\n", n, n_float32, cepsize, veclen);
	E_INFO("pip: forcing cepsize to feat_s3_1x39_cepsize()\n");
	cepsize = ((*feat_s3_1x39_cepsize))(veclen);
	E_INFO("pip again: n = %d, n_float32 = %d, cepsize = %d\n", n, n_float32, cepsize);
	E_INFO("pip: forcing cepsize to 13\n");
	cepsize = 13; 
	E_INFO("pip again: n = %d, n_float32 = %d, cepsize = %d\n", n, n_float32, cepsize);


    /* n = #frames of input */
    n = n_float32/cepsize;
    if (n * cepsize != n_float32) {
	E_ERROR("Header size field: %d; not multiple of %d\n", n_float32, cepsize);
	fclose (fp);
	return -1;
    }
    if (sf > 0) {
	if (sf >= n) {
	    E_ERROR("Start frame (%d) beyond MFC file size (%d)\n", sf, n);
	    fclose (fp);
	    return -1;
	}
	n -= sf;
    }

    /* Limit n if indicated by [sf..ef] */
    if ((ef-sf+1) < n)
	n = (ef-sf+1);

    if (n > mfcbufsize) {
	if (mfcbufsize > 0)
	    E_FATAL("Increase MAX_MFCBUFSIZE\n");

	mfcbufsize = MAX_MFCBUFSIZE;
	mfcbuf = (float32 **) ckd_calloc_2d (mfcbufsize, cepsize, sizeof(float32));
    }
    
    if (sf > 0)
	fseek (fp, sf*cepsize*sizeof(float32), SEEK_CUR);
    
    /* Read mfc data and byteswap if necessary */
    n_float32 = n * cepsize;
    if (fread_retry (mfcbuf[0], sizeof(float32), n_float32, fp) != n_float32) {
	E_ERROR("Error reading mfc data\n");
	fclose (fp);
	return -1;
    }
    if (byterev) {
	for (i = 0; i < n_float32; i++)
	    SWAP_FLOAT32(&(mfcbuf[0][i]));
    }

    fclose (fp);
    
    *mfc = mfcbuf;
    
    return n;
}
Beispiel #5
0
static int32 gauden_param_read(float32 *****out_param,
			       int32 *out_n_mgau,
			       int32 *out_n_feat,
			       int32 *out_n_density,
			       int32 **out_veclen,
			       const char *file_name)
{
    char version[1024], tmp;
    FILE *fp;
    int32 i, j, k, l, blk, n;
    int32 n_mgau;
    int32 n_feat;
    int32 n_density;
    int32 *veclen;
    int32 needs_reorder;
    float32 ****out;
    float32 *buf;

    E_INFO("Reading mixture gaussian parameter: %s\n", file_name);
    
    if ((fp = fopen(file_name, "rb")) == NULL)
	E_FATAL_SYSTEM("fopen(%s,rb) failed\n", file_name);

    if (fscanf(fp, "%s", version) != 1)
	E_FATAL("Unable to read version id\n");
    
    if (strcmp(version, GAUDEN_PARAM_VERSION) != 0)
	E_FATAL("Version mismatch: %s, expecting %s\n", version, GAUDEN_PARAM_VERSION);
    
    if (bcomment_read(fp) != S3_SUCCESS)
	E_FATAL("bcomment_read() failed\n");
    
    if ((needs_reorder = swap_check(fp)) < 0)
	E_FATAL("swap_check() failed\n");

    /* #Codebooks */
    if (fread_retry(&n_mgau, sizeof(uint32), 1, fp) != 1)
	E_FATAL("Error reading #codebooks\n");
    if (needs_reorder) {
	SWAP_INT32(&n_mgau);
    }
    *out_n_mgau = n_mgau;

    /* #Features/codebook */
    if (fread_retry(&n_feat, sizeof(uint32), 1, fp) != 1)
	E_FATAL("Error reading #features/codebook\n");
    if (needs_reorder) {
	SWAP_INT32(&n_feat);
    }
    *out_n_feat = n_feat;

    /* #Gaussian densities/feature in each codebook */
    if (fread_retry(&n_density, sizeof(uint32), 1, fp) != 1)
	E_FATAL("Error reading #densities/codebook-feature\n");
    if (needs_reorder) {
	SWAP_INT32(&n_density);
    }
    *out_n_density = n_density;
    
    /* #Dimensions in each feature stream */
    veclen = ckd_calloc(n_feat, sizeof(uint32));
    *out_veclen = veclen;
    if (fread_retry(veclen, sizeof(uint32), n_feat, fp) != n_feat)
	E_FATAL("Error reading feature vector lengths\n");
    if (needs_reorder) {
	for (i = 0; i < n_feat; i++)
	    SWAP_INT32(&veclen[i]);
    }

    /* blk = total vector length of all feature streams */
    for (i = 0, blk = 0; i < n_feat; i++)
	blk += veclen[i];

    /* #Floats to follow; for the ENTIRE SET of CODEBOOKS */
    if (fread_retry(&n, sizeof(uint32), 1, fp) != 1)
	E_FATAL("Error reading #floats\n");
    if (needs_reorder) {
	SWAP_INT32(&n);
    }
    assert(n == n_mgau * n_density * blk);
    
    /* Allocate memory for mixture gaussian densities */
    out = (float32 ****) ckd_calloc_3d (n_mgau, n_feat, n_density,
					sizeof(float32 *));
    buf = (float32 *) ckd_calloc (n, sizeof(float));
    for (i = 0, l = 0; i < n_mgau; i++) {
	for (j = 0; j < n_feat; j++) {
	    for (k = 0; k < n_density; k++) {
		out[i][j][k] = &buf[l];

		l += veclen[j];
	    }
	}
    }

    /* Read mixture gaussian densities data */
    if (fread_retry (buf, sizeof(float32), n, fp) != n)
	E_FATAL("Error reading gaussian data\n");
    if (needs_reorder)
	for (i = 0; i < n; i++)
	    SWAP_FLOAT32(&buf[i]);
    
    E_INFO("%d codebook, %d feature, size",
	   n_mgau, n_feat);
    for (i = 0; i < n_feat; i++)
	printf (" %dx%d", n_density, veclen[i]);
    printf ("\n");

    if (fread (&tmp, 1, 1, fp) == 1)
	E_WARN("Non-empty file beyond end of data\n");

    *out_param = out;
    
    fclose(fp);

    return 0;
}