⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 feat.c

📁 CMU大名鼎鼎的SPHINX-3大词汇量连续语音识别系统
💻 C
📖 第 1 页 / 共 3 页
字号:
/* ==================================================================== * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * This work was supported in part by funding from the Defense Advanced  * Research Projects Agency and the National Science Foundation of the  * United States of America, and the CMU Sphinx Speech Consortium. * * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ==================================================================== * *//* * feat.c -- Feature vector description and cepstra->feature computation. * * ********************************************** * CMU ARPA Speech Project * * Copyright (c) 1996 Carnegie Mellon University. * ALL RIGHTS RESERVED. * ********************************************** *  * HISTORY *  * 20.Apr.2001  RAH (rhoughton@mediasite.com, ricky.houghton@cs.cmu.edu) *              Adding feat_free() to free allocated memory * * 02-Jan-2001	Rita Singh (rsingh@cs.cmu.edu) at Carnegie Mellon University *		Modified feat_s2mfc2feat_block() to handle empty buffers at *		the end of an utterance * * 30-Dec-2000	Rita Singh (rsingh@cs.cmu.edu) at Carnegie Mellon University *		Added feat_s2mfc2feat_block() to allow feature computation *		from sequences of blocks of cepstral vectors * * 12-Jun-98	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Major changes to accommodate arbitrary feature input types.  Added * 		feat_read(), moved various cep2feat functions from other files into *		this one.  Also, made this module object-oriented with the feat_t type. * 		Changed definition of s2mfc_read to let the caller manage MFC buffers. *  * 03-Oct-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Added unistd.h include. *  * 02-Oct-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Added check for sf argument to s2mfc_read being within file size. *  * 18-Sep-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Added sf, ef parameters to s2mfc_read(). *  * 10-Jan-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Added feat_cepsize(). * 		Added different feature-handling (s2_4x, s3_1x39 at this point). * 		Moved feature-dependent functions to feature-dependent files. *  * 09-Jan-96	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Moved constant declarations from feat.h into here. *  * 04-Nov-95	M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * 		Created. *//* * This module encapsulates different feature streams used by the Sphinx group.  New * stream types can be added by augmenting feat_init() and providing an accompanying * compute_feat function.  It also provides a "generic" feature vector definition for * handling "arbitrary" speech input feature types (see the last section in feat_init()). * In this case the speech input data should already be feature vectors; no computation, * such as MFC->feature conversion, is available or needed. */#include "feat.h"#include "bio.h"#include "cmn.h"#include "cmn_prior.h"#include "agc.h"#include "s3types.h"#if (! WIN32)#include <sys/file.h>#include <sys/errno.h>#include <sys/param.h>#else#include <fcntl.h>#endif#define FEAT_VERSION	"1.0"#define N_FEAT			1#define FEAT_DCEP_WIN		2int32 feat_readfile (feat_t *fcb, char *file, int32 sf, int32 ef, float32 ***feat, int32 maxfr){    FILE *fp;    int32 i, l, k, nfr;    int32 byteswap, chksum_present;    uint32 chksum;    char **argname, **argval;        E_INFO("Reading feature file: '%s'[%d..%d]\n", file, sf, ef);    assert (fcb);    if (ef <= sf) {	E_ERROR("%s: End frame (%d) <= Start frame (%d)\n", file, ef, sf);	return -1;    }        if ((fp = fopen(file, "rb")) == NULL) {	E_ERROR("fopen(%s,rb) failed\n", file);	return -1;    }        /* Read header */    if (bio_readhdr (fp, &argname, &argval, &byteswap) < 0) {	E_ERROR("bio_readhdr(%s) failed\n", file);	fclose (fp);	return -1;    }        /* Parse header info (although nothing much is done with it) */    chksum_present = 0;    for (i = 0; argname[i]; i++) {	if (strcmp (argname[i], "version") == 0) {	    if (strcmp(argval[i], FEAT_VERSION) != 0)		E_WARN("%s: Version mismatch: %s, expecting %s\n",		       file, argval[i], FEAT_VERSION);	} else if (strcmp (argname[i], "chksum0") == 0) {	    chksum_present = 1;	/* Ignore the associated value */	}    }    bio_hdrarg_free (argname, argval);    argname = argval = NULL;        chksum = 0;        /* #Frames */    if (bio_fread (&nfr, sizeof(int32), 1, fp, byteswap, &chksum) != 1) {	E_ERROR("%s: fread(#frames) failed\n", file);	fclose (fp);	return -1;    }        /* #Feature streams */    if ((bio_fread (&l, sizeof(int32), 1, fp, byteswap, &chksum) != 1) ||	(l != feat_n_stream(fcb))) {	E_ERROR("%s: Missing or bad #feature streams\n", file);	fclose (fp);	return -1;    }        /* Feature stream lengths */    k = 0;    for (i = 0; i < feat_n_stream(fcb); i++) {	if ((bio_fread (&l, sizeof(int32), 1, fp, byteswap, &chksum) != 1) ||	    (l != feat_stream_len (fcb, i))) {	    E_ERROR("%s: Missing or bad feature stream size\n", file);	    fclose (fp);	    return -1;	}	k += l;    }        /* Check sf/ef specified */    if (sf > 0) {	if (sf >= nfr) {	    E_ERROR("%s: Start frame (%d) beyond file size (%d)\n", file, sf, nfr);	    fclose (fp);	    return -1;	}	nfr -= sf;    }        /* Limit nfr as indicated by [sf..ef] */    if ((ef-sf+1) < nfr)	nfr = (ef-sf+1);    if (nfr > maxfr) {	E_ERROR("%s: Feature buffer size(%d frames) < actual #frames(%d)\n",		file, maxfr, nfr);	fclose (fp);	return -1;    }        /* Position at desired start frame and read feature data */    if (sf > 0)	fseek (fp, sf * k * sizeof(float32), SEEK_CUR);    if (bio_fread (feat[0][0], sizeof(float32), nfr*k, fp, byteswap, &chksum) != nfr*k) {	E_ERROR("%s: fread(%dx%d) (feature data) failed\n", file, nfr, k);	fclose (fp);	return -1;    }        fclose (fp);	/* NOTE: checksum NOT verified; we might read only part of file */        return nfr;}int32 feat_writefile (feat_t *fcb, char *file, float32 ***feat, int32 nfr){    FILE *fp;    int32 i, k;        E_INFO ("Writing feature file: '%s'\n", file);    assert (fcb);        if ((fp = fopen (file, "wb")) == NULL) {	E_ERROR("fopen(%s,wb) failed\n", file);	return -1;    }        /* Write header */    bio_writehdr_version (fp, FEAT_VERSION);        fwrite (&nfr, sizeof(int32), 1, fp);    fwrite (&(fcb->n_stream), sizeof(int32), 1, fp);    k = 0;    for (i = 0; i < feat_n_stream(fcb); i++) {	fwrite (&(fcb->stream_len[i]), sizeof(int32), 1, fp);	k += feat_stream_len(fcb, i);    }        /* Feature data is assumed to be in a single block, starting at feat[0][0][0] */    if ((int32) fwrite (feat[0][0], sizeof(float32), nfr*k, fp) != nfr*k) {	E_ERROR("%s: fwrite(%dx%d feature data) failed\n", file, nfr, k);	fclose (fp);	return -1;    }        fclose (fp);        return 0;}/* * Read specified segment [sf..ef] of Sphinx-II format mfc file read and return * #frames read.  Return -1 if error. */int32 feat_s2mfc_read (char *file, int32 sf, int32 ef, float32 **mfc, int32 maxfr){    FILE *fp;    int32 n_float32;    struct stat statbuf;    int32 i, n, byterev, cepsize;        if (ef < 0)	ef = (int32)0x7fff0000;		/* Hack!! hardwired constant */        E_INFO("Reading mfc file: '%s'[%d..%d]\n", file, sf, ef);    if (ef <= sf) {	E_ERROR("%s: End frame (%d) <= Start frame (%d)\n", file, ef, sf);	return -1;    }        cepsize = 13;	/* Hack!! hardwired constant */        /* Find filesize; HACK!! To get around intermittent NFS failures, use stat_retry */    if ((stat_retry (file, &statbuf) < 0) || ((fp = fopen(file, "rb")) == NULL)) {	E_ERROR("stat_retry/fopen(%s) failed\n", file);	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 = FALSE;    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 = TRUE;    }    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 sf/ef specified */    if (sf > 0) {	if (sf >= n) {	    E_ERROR("%s: Start frame (%d) beyond file size (%d)\n", file, 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 > maxfr) {	E_ERROR("%s: MFC buffer size(%d frames) < actual #frames(%d)\n", file, maxfr, n);	fclose (fp);	return -1;    }        /* Position at desired start frame and read MFC data */    if (sf > 0)	fseek (fp, sf * cepsize * sizeof(float32), SEEK_CUR);    n_float32 = n * cepsize;    if (fread_retry (mfc[0], sizeof(float32), n_float32, fp) != n_float32) {	E_ERROR("%s: fread(%dx%d) (MFC data) failed\n", file, n, cepsize);	fclose (fp);	return -1;    }    if (byterev) {	for (i = 0; i < n_float32; i++)	    SWAP_FLOAT32(&(mfc[0][i]));    }    fclose (fp);        return n;}static int32 feat_stream_len_sum (feat_t *fcb){    int32 i, k;        k = 0;    for (i = 0; i < feat_n_stream(fcb); i++)	k += feat_stream_len (fcb, i);    return k;}float32 **feat_vector_alloc (feat_t *fcb){    int32 i, k;    float32 *data, **feat;        assert (fcb);        if ((k = feat_stream_len_sum(fcb)) <= 0) {	E_ERROR("Sum(feature stream lengths) = %d\n", k);	return NULL;    }        /* Allocate feature data array so that data is in one block from feat[0][0] */    feat = (float32 **) ckd_calloc (feat_n_stream(fcb), sizeof(float32 *));    data = (float32 *) ckd_calloc (k, sizeof(float32));        for (i = 0; i < feat_n_stream(fcb); i++) {	feat[i] = data;	data += feat_stream_len (fcb, i);    }        return feat;}float32 ***feat_array_alloc (feat_t *fcb, int32 nfr){    int32 i, j, k;    float32 *data, ***feat;        assert (fcb);    assert (nfr > 0);        if ((k = feat_stream_len_sum(fcb)) <= 0) {	E_ERROR("Sum(feature stream lengths) = %d\n", k);	return NULL;    }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -