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

📄 fe.c

📁 CMU大名鼎鼎的SPHINX-3大词汇量连续语音识别系统
💻 C
📖 第 1 页 / 共 3 页
字号:
                hdr_buf->SamplingFreq = SWAPL(&(hdr_buf->SamplingFreq));                hdr_buf->BytesPerSec = SWAPL(&(hdr_buf->BytesPerSec));            }            /* Check Format */            if (hdr_buf->data_format != 1 || hdr_buf->BitsPerSample != 16){                fprintf(stderr,"MS WAV file not in 16-bit PCM format\n");                return (FE_INPUT_FILE_READ_ERROR);            }            len = hdr_buf->datalength / sizeof(short);            P->nchans = hdr_buf->numchannels;          /* DEBUG: Dump Info */            fprintf(stderr,"Reading MS Wav file %s:\n", infile);            fprintf(stderr,"\t16 bit PCM data, %d channels %d samples\n",P->nchans,len);            fprintf(stderr,"\tSampled at %d\n",hdr_buf->SamplingFreq);            free(hdr_buf);        }        else {            fprintf(stderr,"Unknown input file format\n");            return(FE_INPUT_FILE_OPEN_ERROR);        }    }    len = len/P->nchans;    *nsamps = len;    *fp_in = fp;    numblocks = (int)((float)len/(float)P->blocksize);    if (numblocks*P->blocksize<len)        numblocks++;        numframes = fe_count_frames(FE,len,COUNT_PARTIAL);    *nblocks = numblocks;      *nframes = numframes;      return 0;}int32 fe_openfiles(param_t *P, fe_t *FE, char *infile, int32 *fp_in, int32 *nsamps, 		   int32 *nframes, int32 *nblocks, char *outfile, int32 *fp_out){    struct stat filestats;    int fp=0, len=0, outlen, numframes, numblocks;    FILE *fp2;    char line[MAXCHARS];    int got_it=0;            /* Note: this is kind of a hack to read the byte format from the    NIST header */    if (P->input_format == NIST){        if ((fp2 = fopen(infile,"rb")) == NULL){            E_ERROR("Cannot read %s\n",infile);            return(FE_INPUT_FILE_READ_ERROR);        }        *line=0;        got_it = 0;        while(strcmp(line,"end_head") && !got_it){            fscanf(fp2,"%s",line);            if (!strcmp(line,"sample_byte_format")){                fscanf(fp2,"%s",line);                if (!strcmp(line,"-s2")){                    fscanf(fp2,"%s",line);                    if (!strcmp(line,"01")){                        P->input_endian=LITTLE;                        got_it=1;                    }                    else if(!strcmp(line,"10")){                        P->input_endian=BIG;                        got_it = 1;                    }                    else                        E_ERROR("Unknown/unsupported byte order\n");                }                else                     E_ERROR("Error determining byte format\n");            }        }        if (!got_it){            E_WARN("Can't find byte format in header, setting to machine's endian\n");            P->input_endian = P->machine_endian;        }	            fclose(fp2);    }    else if (P->input_format == RAW){    /*    P->input_endian = P->machine_endian;        */    }    else if (P->input_format == MSWAV){        P->input_endian = LITTLE; // Default for MS WAV riff files    }            if ((fp = open(infile, O_RDONLY | O_BINARY, 0644))<0){        fprintf(stderr,"Cannot open %s\n",infile);        return (FE_INPUT_FILE_OPEN_ERROR);    }    else{        if (fstat(fp,&filestats)!=0) printf("fstat failed\n");                if (P->input_format == NIST){            short *hdr_buf;                        len = (filestats.st_size-HEADER_BYTES)/sizeof(short);            /* eat header */            hdr_buf = (short *)calloc(HEADER_BYTES/sizeof(short),sizeof(short));            if (read(fp,hdr_buf,HEADER_BYTES)!=HEADER_BYTES){                E_ERROR("Cannot read %s\n",infile);                return (FE_INPUT_FILE_READ_ERROR);            }            free(hdr_buf);            }        else if (P->input_format == RAW){            len = filestats.st_size/sizeof(int16);        }        else if (P->input_format == MSWAV){            /* Read the header */            MSWAV_hdr *hdr_buf;            if ((hdr_buf = (MSWAV_hdr*) calloc(1,sizeof(MSWAV_hdr))) == NULL){                E_ERROR("Cannot allocate for input file header\n");                return (FE_INPUT_FILE_READ_ERROR);            }            if (read(fp,hdr_buf,sizeof(MSWAV_hdr)) != sizeof(MSWAV_hdr)){                E_ERROR("Cannot allocate for input file header\n");                return (FE_INPUT_FILE_READ_ERROR);            }            /* Check header */            if (strncmp(hdr_buf->rifftag,"RIFF",4)!=0 ||                strncmp(hdr_buf->wavefmttag,"WAVEfmt",7)!=0) {                E_ERROR("Error in mswav file header\n");                return (FE_INPUT_FILE_READ_ERROR);            }            if (strncmp(hdr_buf->datatag,"data",4)!=0) {            /* In this case, there are other "chunks" before the            * data chunk, which we can ignore. We have to find the            * start of the data chunk, which begins with the string            * "data".                */                int16 found=OFF;                char readChar;                char *dataString = "data";                int16 charPointer = 0;                printf("LENGTH: %d\n", strlen(dataString));                while (found != ON) {                    if (read(fp,&readChar,sizeof(char)) != sizeof(char)){                        E_ERROR("Failed reading wav file.\n");                        return (FE_INPUT_FILE_READ_ERROR);                    }                    if (readChar == dataString[charPointer]) {                        charPointer++;                    }                    if (charPointer == (int)strlen(dataString)) {                        found = ON;                        strcpy(hdr_buf->datatag, dataString);                        if (read(fp,&(hdr_buf->datalength),sizeof(int32)) != sizeof(int32)){                            E_ERROR("Failed reading wav file.\n");                            return (FE_INPUT_FILE_READ_ERROR);                        }                    }                }            }            if (P->input_endian!=P->machine_endian) { // If machine is Big Endian                hdr_buf->datalength = SWAPL(&(hdr_buf->datalength));                hdr_buf->data_format = SWAPW(&(hdr_buf->data_format));                hdr_buf->numchannels = SWAPW(&(hdr_buf->numchannels));                hdr_buf->BitsPerSample = SWAPW(&(hdr_buf->BitsPerSample));                hdr_buf->SamplingFreq = SWAPL(&(hdr_buf->SamplingFreq));                hdr_buf->BytesPerSec = SWAPL(&(hdr_buf->BytesPerSec));            }            /* Check Format */            if (hdr_buf->data_format != 1 || hdr_buf->BitsPerSample != 16){                E_ERROR("MS WAV file not in 16-bit PCM format\n");                return (FE_INPUT_FILE_READ_ERROR);            }            len = hdr_buf->datalength / sizeof(short);            P->nchans = hdr_buf->numchannels;            /* DEBUG: Dump Info */            E_INFO("Reading MS Wav file %s:\n", infile);            E_INFO("\t16 bit PCM data, %d channels %d samples\n",P->nchans,len);            E_INFO("\tSampled at %d\n",hdr_buf->SamplingFreq);            free(hdr_buf);        }        else {            E_ERROR("Unknown input file format\n");            return(FE_INPUT_FILE_OPEN_ERROR);        }    }            len = len/P->nchans;    *nsamps = len;    *fp_in = fp;        numblocks = (int)((float)len/(float)P->blocksize);    if (numblocks*P->blocksize<len)        numblocks++;        *nblocks = numblocks;          if ((fp = open(outfile, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0644)) < 0) {        E_ERROR("Unable to open %s for writing features\n",outfile);        return(FE_OUTPUT_FILE_OPEN_ERROR);    }    else{        /* compute number of frames and write cepfile header */        numframes = fe_count_frames(FE,len,COUNT_PARTIAL);        if (P->logspec != ON)            outlen = numframes*FE->NUM_CEPSTRA;        else            outlen = numframes*FE->MEL_FB->num_filters;        if (P->output_endian != P->machine_endian)            SWAPL(&outlen);        if  (write(fp, &outlen, 4) != 4) {            E_ERROR("Data write error on %s\n",outfile);            close(fp);            return(FE_OUTPUT_FILE_WRITE_ERROR);        }        if (P->output_endian != P->machine_endian)            SWAPL(&outlen);    }        *nframes = numframes;      *fp_out = fp;        return 0;}int32 fe_readblock_spch(param_t *P, int32 fp, int32 nsamps, int16 *buf){    int32 bytes_read, cum_bytes_read, nreadbytes, actsamps, offset, i, j, k;    int16 *tmpbuf;    int32 nchans, whichchan;        nchans = P->nchans;    whichchan = P->whichchan;        if (nchans==1){        if (P->input_format==RAW || P->input_format==NIST || P->input_format==MSWAV){    	    nreadbytes = nsamps*sizeof(int16);	    if ((bytes_read = read(fp,buf,nreadbytes))!=nreadbytes){  	        E_ERROR("error reading block\n");		return(0);	    }		}	else{  	    E_ERROR("unknown input file format\n");	    return(0);	}	cum_bytes_read = bytes_read;    }    else if (nchans>1){		if (nsamps<P->blocksize){	    actsamps = nsamps*nchans;	    tmpbuf = (int16 *)calloc(nsamps*nchans,sizeof(int16));	    cum_bytes_read = 0;	    if (P->input_format==RAW || P->input_format==NIST){    				k=0;		nreadbytes = actsamps*sizeof(int16);				if ((bytes_read = read(fp,tmpbuf,nreadbytes))!=nreadbytes){		    E_ERROR("error reading block (got %d not %d)\n",bytes_read,nreadbytes);		    return(0);		}				for (j=whichchan-1;j<actsamps;j=j+nchans){		    buf[k] = tmpbuf[j];		    k++;		}		cum_bytes_read += bytes_read/nchans;	    }	    else{		E_ERROR("unknown input file format\n");		return(0);	    }	    free(tmpbuf);	}	else{	    tmpbuf = (int16 *)calloc(nsamps,sizeof(int16));	    actsamps = nsamps/nchans;	    	    cum_bytes_read = 0;	    	    if (actsamps*nchans != nsamps){		E_WARN("Blocksize %d is not an integer multiple of Number of channels %d\n",nsamps, nchans);	    }	    	    if (P->input_format==RAW || P->input_format==NIST){    		for (i=0;i<nchans;i++){		    		    offset = i*actsamps;		    k=0;		    nreadbytes = nsamps*sizeof(int16);		    		    if ((bytes_read = read(fp,tmpbuf,nreadbytes))!=nreadbytes){			E_ERROR("error reading block (got %d not %d)\n",bytes_read,nreadbytes);			return(0);		    }		    		    for (j=whichchan-1;j<nsamps;j=j+nchans){			buf[offset+k] = tmpbuf[j];			k++;		    }		    cum_bytes_read += bytes_read/nchans;		}	    }	    else{		E_ERROR("unknown input file format\n");		return(0);	    }	    free(tmpbuf);	}    }        else{	E_ERROR("unknown number of channels!\n");	return(0);    }      if (P->input_endian!=P->machine_endian){         for(i=0;i<nsamps;i++)	    SWAPW(&buf[i]);    }    if (P->dither==ON)        fe_dither(buf,nsamps);    return(cum_bytes_read/sizeof(int16));}int32 fe_writeblock_feat(param_t *P, fe_t *FE, int32 fp, int32 nframes, float32 **feat){       int32 i, length, nwritebytes;    if (P->logspec == ON)        length = nframes*FE->MEL_FB->num_filters;    else        length = nframes*FE->NUM_CEPSTRA;        if (P->output_endian != P->machine_endian){	for (i=0;i<length;++i) SWAPF(feat[0]+i);    }        nwritebytes = length*sizeof(float32);    if  (write(fp, feat[0], nwritebytes) != nwritebytes) {        close(fp);        E_FATAL("Error writing block of features\n");    }    if (P->output_endian != P->machine_endian){	for (i=0;i<length;++i) SWAPF(feat[0]+i);    }        return(length);}int32 fe_closefiles(int32 fp_in, int32 fp_out){    close(fp_in);    close(fp_out);    return 0;}void fe_init_dither(int32 seed){  if(seed<0){    E_INFO("You are using the internal mechanism to generate the seed.");    s3_rand_seed((long)time(0));  }else{    E_INFO("You are using %d as the seed.",seed);    s3_rand_seed(seed);  }}/* adds 1/2-bit noise */int32 fe_dither(int16 *buffer,int32 nsamps){  int32 i;  for (i=0;i<nsamps;i++)    buffer[i] += (short)((!(s3_rand_int31()%4))?1:0);    return 0;}int32 fe_free_param(param_t *P){    /* but no one calls it (29/09/00) */    return 0;}

⌨️ 快捷键说明

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