#include #include #include #include /* ================================================================= */ #define FRAMESIZE 80 #define OBSDIM 25 #define OBSTYPE 454 /* MFCC_E_D_N */ #define SAMPRATE 8000.0 #define SAMPSIZE sizeof(short) /* --- Defines the ISOLET header structure --- */ /**/ typedef struct { short hdrsize; short version; short channels; short rate; int nsamples; short endian; } header; header ISOLET; /* The ISOLET database's header */ /* ================================================================= */ main(argc,argv) int argc; char *argv[]; { FILE *fpi; FILE *fpo; struct stat filerec; char *infile; char *outfile; short *inbuf; short *outbuf; short *vcflag,x; float samprate; float srate=8000.0; /* default sampling frequency */ int sampsize; int framesize; int numsamples; int numobs; int obsdim; short osize=2; /* Bytes per sample */ short otype=0; /* Waveform type */ int isolet=0; int oflag=0; int rint=0; int sflag=0; int tflag=0; int i,j,k,m,noheader=0; int GetNISTheader (); /* === parse the command line === */ if (argc==1){ printf("Usage: wav2htk [options] infile outfile\n\n"); printf("Options Default\n"); printf("-t Add an ISOLET header HTK\n"); printf("-l Add no header HTK\n\n"); exit(1); } if(!scanargs(argc,argv,"adheader l%- t%- infile!s outfile!s",&noheader,&isolet,&infile,&outfile)) exit(1); /* === open output observation file === */ fpi = fopen(infile,"rb"); if (fpi == NULL) { printf("Error: Unable to open input observation file (%s).\n",infile); exit(1); } /* === Read NIST header, compute number of samples in file === */ numsamples = GetNISTheader(fpi,&srate); /* === open output file === */ fpo = fopen(outfile,"wb"); if (fpo == NULL) { printf("Error: Unable to create observation file (%s).\n",outfile); exit(1); } /* === allocate space for observations and read them === */ inbuf = (short *) malloc(numsamples*sizeof(short)); k=fread(inbuf,sizeof(short),numsamples,fpi); /* === write observation file header, if any === */ if (!noheader) { if (isolet) { ISOLET.nsamples = numsamples; x= 1000000.0/(srate*0.25); ISOLET.rate=x; ISOLET.hdrsize=8; ISOLET.version=1; ISOLET.channels=1; ISOLET.endian=0; fwrite(&ISOLET,1,sizeof(ISOLET),fpo); } else { fwrite(&numsamples,sizeof(int),1,fpo); /* number of observations */ i = 10.0e+6/srate; fwrite(&i,sizeof(int),1,fpo); /* frame rate sec/frm (x100 nS) */ fwrite(&osize,sizeof(short),1,fpo); /* bytes per feature */ fwrite(&otype,sizeof(short),1,fpo); /* (htk) type of feature */ } } /* === write output file === */ fwrite(inbuf,numsamples,sizeof(short),fpo); fclose(fpo); } int GetNISTheader (fpin,srate) FILE *fpin; float *srate; { int num_samples,i,done=0; char nst[40]; fscanf(fpin,"%s",nst); /* Check if it is a valid NIST file */ if (strcmp(nst,"NIST_1A") == 0) { /* proceed */ done=0; while (!done) { fscanf(fpin,"%s",nst); /* Get next sring */ if ( strcmp(nst,"sample_count") == 0) { fscanf(fpin,"%s",nst); /* Get the -i string */ fscanf(fpin,"%s",nst); /* Get number of samples */ num_samples=atoi(nst); } if ( strcmp(nst,"end_head") == 0) { printf("ERROR! Could not find NIST keywords in file..\n"); exit(0); } if ( strcmp(nst,"sample_rate") == 0) { fscanf(fpin,"%s",nst); /* Skip the -i option */ fscanf(fpin,"%s",nst); /* Get sampling frequency */ *srate=(float) atoi(nst); /* Get the Sampling frequency */ if (*srate < 5000.0 || *srate > 21000.0){ printf("Sampling rate out of range: %f\n",*srate); exit(1);} done=1; /* exit */ } } fseek(fpin,1024,0); /* Skip the 1024 byte header */ } else { printf("ERROR! Invalid numbers in NIST headers: %s\n",nst); exit(0); } return(num_samples); }