📄 hwave.c
字号:
strcpy((char *) field->data, RetrieveCommandLine()); AddField( &outList, field ); field = NewFieldSpec( DOUBLE, 0 ); field->occurrence=GLOBAL; field->name="recordFreq"; field->data=malloc(sizeof(double)); *((double*)field->data)=1.0E07/w->sampPeriod; AddField( &outList, field ); field = NewFieldSpec( DOUBLE, 0 ); field->occurrence=GLOBAL; field->name="startTime"; field->data=malloc(sizeof(double)); if (w->fmt == ESIG) { RetrieveESIGFieldList(&inList); field1=FindField(inList, "startTime"); *((double*)field->data)=*((double*)field1->data); } else *((double*)field->data)=0.0; AddField( &outList, field ); if (w->fmt == ESIG) { field = NewFieldSpec( NO_TYPE, 0 ); field->occurrence=VIRTUAL; field->name="source_1"; len=FieldListLength(inList); for (i=0;i<len;i++) AddSubfield(field,inList[i]); AddField( &outList, field ); } /* Then create actual record descriptor */ field = NewFieldSpec( SHORT, 1 ); field->dim[0]=1; field->occurrence=REQUIRED; field->name="samples"; AddField( &outList, field ); WriteHeader(outList, (natWriteOrder && vaxOrder) ? NATIVE : EDR1, f, NULL);}/* ---------------------- HTK Format Interface Routines --------------------- */#define WAVEFORM 0 /* HTK sample kind */typedef struct { /* HTK File Header */ int32 nSamples; int32 sampPeriod; short sampSize; short sampKind;} HTKhdr;/* EXPORT ReadHTKHeader: get header from HTK file, return false not HTK */Boolean ReadHTKHeader(FILE *f, long *nSamp, long *sampP, short *sampS, short *kind, Boolean *bSwap){ HTKhdr hdr; int n = sizeof hdr; if (fread(&hdr, 1, n, f) != n) return FALSE; if (!natReadOrder && vaxOrder){ *bSwap = TRUE; }else{ *bSwap=MustSwap(UNKNOWNSO); } if (*bSwap){ SwapInt32(&hdr.nSamples); SwapInt32(&hdr.sampPeriod); SwapShort(&hdr.sampSize); SwapShort(&hdr.sampKind); } if (hdr.sampSize <= 0 || hdr.sampSize > 5000 || hdr.nSamples <= 0 || hdr.sampPeriod <= 0 || hdr.sampPeriod > 1000000) return FALSE; *nSamp = hdr.nSamples; *sampP = hdr.sampPeriod; *sampS = hdr.sampSize; *kind = hdr.sampKind; return TRUE;}/* GetHTKHeaderInfo: get HTK format header and check its a WAVEFORM file */static long GetHTKHeaderInfo(FILE *f, Wave w, InputAction *ia){ Boolean bSwap; short kind,size; long sp; if (!ReadHTKHeader(f,&(w->nSamples),&sp, &size, &kind, &bSwap)){ HRError(6250,"GetHTKHeaderInfo: cannot read HTK Header"); return -1; } w->sampPeriod = sp; if (bSwap) *ia = (InputAction) (*ia | DoBSWAP); if (kind != WAVEFORM){ HRError(6251,"GetHTKHeaderInfo: sample kind is not WAVEFORM"); return -1; } if (size != 2){ HRError(6251,"GetHTKHeaderInfo: samples are not shorts"); return -1; } w->hdrSize = sizeof(HTKhdr); return w->nSamples*2;}/* EXPORT->WriteHTKHeader: Write header info to HTK file f */void WriteHTKHeader(FILE *f, long nSamp, long sampP, short sampS, short kind, Boolean *bSwap){ HTKhdr hdr; int n = sizeof hdr; hdr.nSamples = nSamp; hdr.sampSize = sampS; hdr.sampKind = kind; hdr.sampPeriod = sampP; if (!natWriteOrder && vaxOrder){ SwapInt32(&hdr.nSamples); SwapInt32(&hdr.sampPeriod); SwapShort(&hdr.sampSize); SwapShort(&hdr.sampKind); if (bSwap!=NULL) *bSwap=TRUE; } else if (bSwap!=NULL) *bSwap=FALSE; if (fwrite(&hdr, 1, n, f) != n) HError(6214,"WriteHTKHeader: Cannot write HTK format header"); }/* PutHTKHeaderInfo: write a HTK (WAVEFORM) file header */static void PutHTKHeaderInfo(FILE *f, Wave w){ WriteHTKHeader (f, w->nSamples, (long) w->sampPeriod, 2, WAVEFORM,NULL);} /* ---------------------- Wave Interface Routines --------------------- */ /* EXPORT-> OpenWaveInput: open input waveform file */Wave OpenWaveInput(MemHeap *x, char *fname, FileFormat fmt, HTime winDur, HTime frPeriod, HTime *sampPeriod){ Wave w; /* New Wave Object */ FILE *f; /* Input data file */ InputAction ia=(InputAction)0; /* flags to enable conversions etc */ long fBytes=0; /* Num data bytes in file to load */ HTime t; Boolean isEXF; /* File name is extended */ char actfile[MAXFNAMELEN]; /* actual file name */ long stindex,enindex; /* segment indices */ int sampSize = 2; /* Create Wave Object and open external file */ w = CreateWave(x,fmt); /* extended filename handling */ strcpy(actfile,fname); isEXF = GetFileNameExt(fname,actfile,&stindex,&enindex); if ((f = FOpen(actfile, WaveFilter, &(w->isPipe))) == NULL) { HError(6210,"OpenWaveInput: Cannot open waveform file %s",fname); return NULL; } if (isEXF && w->isPipe && stindex >= 0) { HError(6210,"OpenWaveInput: cannot segment piped input"); return NULL; } /* Get Header */ switch(w->fmt) { case ALIEN: fBytes = GetALIENHeaderInfo (f, w, &ia); break; case NOHEAD: fBytes = GetNOHEADHeaderInfo(f, w, &ia); break; case HTK: fBytes = GetHTKHeaderInfo (f, w, &ia); break; case TIMIT: fBytes = GetTIMITHeaderInfo (f, w, &ia); break; case OGI: fBytes = GetOGIHeaderInfo (f, w, &ia); break; case NIST: fBytes = GetNISTHeaderInfo (f, w, &ia); break; case SCRIBE: fBytes = GetSCRIBEHeaderInfo(f, w, &ia); break; case SDES1: fBytes = GetSDES1HeaderInfo (f, w, &ia); break; case AIFF: fBytes = GetAIFFHeaderInfo (f, w, &ia); break; case SUNAU8: fBytes = GetSUNAU8HeaderInfo(f, w, &ia); sampSize = 1; break; case WAV: fBytes = GetWAVHeaderInfo(f, w, &ia); break; case ESIG: fBytes = GetESIGHeaderInfo(f, w, &ia); break; default: FClose(f,w->isPipe); HRError(6270,"OpenWaveInput: Unknown format %s",fmtmap[w->fmt]); return(NULL); break; } /* If extended indexed file, modify header and skip to segment start */ if (isEXF && stindex >= 0) { if (enindex < 0) enindex = w->nSamples-1; if (w->nSamples < enindex-stindex+1) HError(6210,"OpenWaveInput: EXF segment bigger than file"); w->nSamples = enindex-stindex+1; fseek(f,stindex*sampSize,SEEK_CUR); fBytes = w->nSamples*sampSize; } if(fBytes<0){ FClose(f,w->isPipe); HRError(6213,"OpenWaveInput: Get[format]HeaderInfo failed"); return(NULL); } /* Check for user override of sample period and set frame size/rate */ if (*sampPeriod > 0.0) w->sampPeriod = *sampPeriod; else { if (GetConfFlt(cParm,numParm,"SOURCERATE",&t)) w->sampPeriod = t; } *sampPeriod = w->sampPeriod; if (w->sampPeriod <= 0.0){ FClose(f,w->isPipe); HRError(6230,"OpenWaveInput: cannot determine sample period for %s",fname); return(NULL); } w->frSize = (int) (winDur / w->sampPeriod); w->frRate = (int) (frPeriod / w->sampPeriod); /* Copy Data from file into memory */ if(LoadData(f, w, fBytes)<SUCCESS){ FClose(f,w->isPipe); HRError(6213,"OpenWaveInput: LoadData failed"); return(NULL); } /* If necessary call format dependent conversion routine */ if (ia&DoCVT) switch(w->fmt) { case NIST: if(ConvertNISTData(w,ia)<SUCCESS){ FClose(f,w->isPipe); HRError(6270,"OpenWaveInput: No Convertor for format %s",fmtmap[w->fmt]); return(NULL); } break; case SUNAU8: if(ConvertSUNAU8Data(w)<SUCCESS){ FClose(f,w->isPipe); HRError(6270,"OpenWaveInput: No Convertor for format %s",fmtmap[w->fmt]); return(NULL); } break; case WAV: if(ConvertWAVData(w,&ia)<SUCCESS){ FClose(f,w->isPipe); HRError(6270,"OpenWaveInput: No Convertor for format %s",fmtmap[w->fmt]); return(NULL); } break; default: FClose(f,w->isPipe); HRError(6270,"OpenWaveInput: No Convertor for format %s",fmtmap[w->fmt]); return(NULL); break; } /* If necessary byte swap the waveform data */ if (ia&DoBSWAP) ByteSwap(w); FClose(f,w->isPipe); if (trace&T_OPEN){ printf("HWave: Input Wave Opened from %s\n",fname); ShowWaveInfo(w); } return w;}/* EXPORT->CloseWaveInput: close the given data file and free storage */void CloseWaveInput(Wave w){ Dispose(w->mem, w);}/* EXPORT->ZeroMeanWave: remove mean of wave w */void ZeroMeanWave(Wave w){ int i; short *x; double sum=0.0,y,mean; x = (short *)w->data; for (i=0; i<w->nSamples; i++,x++) sum += *x; mean = sum / (float)w->nSamples; x = (short *)w->data; for (i=0; i<w->nSamples; i++,x++){ y = (float)*x - mean; if (y<-32767){ y = -32767; HError(-6255,"ZeroMeanWave: sample too -ve\n"); } if (y>32767){ y = 32767; HError(-6255,"ZeroMeanWave: sample too +ve\n"); } *x = (short) ((y>0.0) ? y+0.5 : y-0.5); }}/* EXPORT->FramesInWave: return number of frames in given wave */int FramesInWave(Wave w){ if (w->frSize + w->frIdx > w->nSamples ) return 0; return (w->nSamples - w->frSize - w->frIdx) / w->frRate + 1;}/* EXPORT->SampsInWaveFrame: return number of samples per frame */int SampsInWaveFrame(Wave w){ return w->frSize;}/* EXPORT->WaveFormat: return format of given wave */FileFormat WaveFormat(Wave w){ return w->fmt;}/* EXPORT->GetWave: Get next nFrames from w and store in buf */void GetWave(Wave w, int nFrames, float *buf){ int i,k; for (i=0; i<nFrames; i++){ if (w->frIdx+w->frSize > w->nSamples) HError(6271,"GetWave: attempt to read past end of buffer"); for (k=0; k<w->frSize; k++) *buf++ = w->data[w->frIdx+k]; w->frIdx += w->frRate; }}/* EXPORT->GetWaveDirect: return a pointer to the wave in w */short *GetWaveDirect(Wave w, long *nSamples){ *nSamples = w->nSamples; return w->data;}/* EXPORT->OpenWaveOutput: create wave object for output with given bufSize */Wave OpenWaveOutput(MemHeap *x, HTime *sampPeriod, long bufSize){ Wave w; HTime t; w = CreateWave(x,HTK); if (*sampPeriod>0.0) w->sampPeriod = *sampPeriod; else { if (GetConfFlt(cParm,numParm,"TARGETRATE",&t)) w->sampPeriod = *sampPeriod = t; else w->sampPeriod = 0.0; } w->nAvail = bufSize; w->data = (short *)New(w->mem,bufSize*2); if (trace&T_OPEN){ printf("HWave: Output Wave Opened\n"); ShowWaveInfo(w); } return w;}/* EXPORT->PutWaveSample: Append given nSamples in buf to wave w */void PutWaveSample(Wave w, long nSamples, short *buf){ long i,newSize,needed; short *p,*tmp; needed = w->nSamples+nSamples; if (needed > w->nAvail){ /* increase size of data buffer - assume that only w->data has been allocated on w->mem stack */ newSize = w->nAvail + w->nAvail / 2; if (newSize < needed) newSize = needed; tmp = (short *)New(&gstack,w->nSamples*2); memcpy(tmp,w->data,w->nSamples*2); Dispose(w->mem,w->data); p = (short *)New(w->mem,newSize*2); memcpy(p,tmp,w->nSamples*2); Dispose(&gstack,tmp); w->nAvail = newSize; w->data = p; } for (i=0,p=w->data+w->nSamples; i<nSamples; i++) *p++ = buf[i]; w->nSamples += nSamples;}/* EXPORT->CloseWaveOutput: Output w to fname and free mem */ReturnStatus CloseWaveOutput(Wave w, FileFormat fmt, char *fname){ FILE *f; Boolean isPipe; char buf[MAXSTRLEN]; if ((f = FOpen(fname, WaveOFilter, &isPipe)) == NULL){ /* Open file */ HRError(6211,"CloseWaveOutput: Cannot create file %s",fname); return(FAIL); } /* Output Header in appropriate format */ if (fmt == UNDEFF){ if (GetConfStr(cParm,numParm,"TARGETFORMAT",buf)) fmt = Str2Format(buf); else fmt = HTK; } switch(fmt) { case HTK: PutHTKHeaderInfo(f,w); break; case ESIG: PutESIGHeaderInfo(f,w); break; case NOHEAD: /* do nothing */ break; default: FClose(f,isPipe); HRError(6270,"CloseWaveOutput: Cannot save data as %s.", fmtmap[fmt]); return(FAIL); break; } /* If necessary byte swap the waveform data */ if (!natWriteOrder && vaxOrder) ByteSwap(w); /* Write Body of file */ if (fwrite(w->data, 2, w->nSamples, f) != w->nSamples){ FClose(f,isPipe); HRError(6214,"CloseWaveOutput: Cannot write data to %s",fname); return(FAIL); } /* Close file and free memory */ FClose(f,isPipe); Dispose(w->mem, w); return(SUCCESS);}/* -------------------------------- HWave.c ------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -