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

📄 hslab.c

📁 该压缩包为最新版htk的源代码,htk是现在比较流行的语音处理软件,请有兴趣的朋友下载使用
💻 C
📖 第 1 页 / 共 5 页
字号:
}/* CreatePlotBuf: create the waveform-plot buffer */void CreatePlotBuf(int size){}/* PreparePlot: fill the plot buffer with st -> en samples from the data */void PreparePlot(RectWin *win, short *data, int st, int en){   int i, cent, nPoints;   float s_st, s_en;   long nSamples, t;   short a, b, min, max, sample;   float WaveScale;   Boolean rising=FALSE;   nPoints   = win->w - 1; cent = win->y + win->h/2; nSamples = en - st;   WaveScale = (float) win->h / (float) 65535.0;   samplesPt = (float) nSamples / (float)nPoints;   s_st = (float) st;   for (i=0; i<nPoints; i++){      s_en = s_st + samplesPt;   min = MAX_AMPL;   max = MIN_AMPL;      for (t=(int)s_st; t<(int)s_en; t++){         sample = data[t];         sample = ClipSample(sample, scValues[scIndex]);         if (sample < min) { min = sample; rising = FALSE; }         if (sample > max) { max = sample; rising = TRUE; }      }      if (rising){ a = min; b = max; } else { a = max; b = min; }      plotBuf[2*i] = (int) (cent - a*WaveScale);      plotBuf[2*i+1] = (int) (cent - b*WaveScale); s_st = s_en;   }}/* PlotWaveForm: plot the waveform in the waveWin */void PlotWaveForm(RectWin *win, int st_pt, int en_pt){   int x, i, s;   HSetColour(win->fg);   HSetLineWidth(0);   x = win->x;   s = (st_pt-1)*2;   for (i=st_pt; i <= en_pt; i++){      if (i > 1)         HDrawLine(x+i-1, plotBuf[s-1], x+i, plotBuf[s]);      HDrawLine(x+i, plotBuf[s], x+i, plotBuf[s+1]);      s += 2;   }}/* PlotWaveWin: plot the waveform window */ void PlotWaveWin(void){   WPtrOff();   HSetXMode(GCOPY);   DrawRectWin(&waveWin);   HSetColour(waveWin.fg);   HSetLineWidth(0);   PlotWaveForm(&waveWin, 1, waveWin.w - 1);   regnMarked = FALSE;}/* -------------------------- Miscellaneous Processing --------------------- *//* TrackWPtr: update the waveform window pointer position */void TrackWPtr(void){   int x, y;   if (!HMousePos(&x, &y) || !IsInWin(&waveWin, x, y)){      WPtrOff();      return;   }   lastWpos = thisWpos; thisWpos = x;   if (thisWpos==lastWpos)      return;   if (wavePtrOn){      PlotWaveWinPtr(lastWpos);   }   PlotWaveWinPtr(thisWpos);   wavePtrOn = TRUE;}/* InvertRegion: invert the window region a - b, taking care of the WavePtr */void InvertRegion(RectWin *win, int a, int b){   Boolean wasOn=FALSE;   if (wavePtrOn) { wasOn = TRUE; WPtrOff(); }   HSetXMode(GINVERT);   HFillRectangle(a, win->y+1, b, win->y + win->h - 1);   HSetXMode(GCOPY);   regnMarked = !regnMarked;   if (wasOn) WPtrOn();}/* PlotFileWin: plot the file name window */ void PlotFileWin(void){   char sbuf[SLEN], nullchar = '\0';   int sx, sy, pos, pad = 4;   double rate;   DrawRectWin(&fileWin);   HSetColour(fileWin.fg);   rate = 1.0E+04/(double)sampPeriod;   sprintf(sbuf, "Waveform: %s, Label: %s, Num samples %ld, HTK sampling rate: %.3fKHz",            spfn, labfn, T, rate);   pos = strlen(sbuf); while(HTextWidth(sbuf) > (fileWin.w - 2*pad)) sbuf[--pos]=nullchar;   sx = fileWin.x + pad; sy = CentreY(fileWin.y + fileWin.h/2, sbuf);   HPrintf(sx, sy, "%s", sbuf);}/* hRedrawWindow: redraw the main window */void hRedrawWindow(void){   Boolean wasMarked;   wasMarked = regnMarked;   PlotWaveWin();   PlotLabWin();   PlotFileWin();   DrawRectWin(&io_Win);   RedrawHButtonList(btnList);   if (wasMarked){      regnMarked=FALSE;      InvertRegion(&waveWin, markA, markB);   }}/* GetWavePtrPos: returns the waveform window pointer position after a button press */Boolean GetWavePtrPos(void){   HEventRec hev;   do {      hev = HGetEvent(FALSE, NULL);      switch (hev.event) {      case HMOUSEDOWN:         if (!IsInWin(&waveWin, hev.x, hev.y))            return FALSE;         do {            hev = HGetEvent(TRUE, NULL);            if (hev.event==HMOUSEMOVE)               TrackWPtr();         } while (hev.event!=HMOUSEUP);         if (IsInWin(&waveWin, hev.x, hev.y))            return TRUE;         else            return FALSE;         break;      case HREDRAW:         hRedrawWindow();         break;      case HMOUSEMOVE:         TrackWPtr();          break;      default:         break;      }   } while (TRUE);   return FALSE;}/* CalcEnergy: some dude wrote this */float CalcEnergy(int st,int en){   int i,sample;   double sq, av;      st=Point2Sample(&waveWin,st);   en=Point2Sample(&waveWin,en);   if (en==st)       return(0.0);   else       if (st>en) {         i=st; st=en; en=i;      }   sq=av=0.0;   for (i=st;i<=en;i++) {      sample = data[i];      sq+=sample*sample;      av+=sample;   }   av=av/(en-st+1);   sq=sqrt((sq/(en-st+1))-av*av);   return(sq);}/* GetRegionAB: prompts the user for 2 points on the wave form window */Boolean GetRegionAB(int *pa, int *pb){   int a;   char buf[80];   if (regnMarked) {      /* if region marked already then use markA and markB */      *pa = markA;      *pb = markB;      InvertRegion(&waveWin, *pa, *pb);   } else {      /* otherwisw prompt for a and b */      PrintMsg(&io_Win, "Select point A...");      if (!GetWavePtrPos())         return FALSE;      *pa = thisWpos;      PlotWaveWinPtr(*pa);      PrintMsg(&io_Win, "Select point B...");      if (!GetWavePtrPos()){         PlotWaveWinPtr(*pa);         return FALSE;      }      PlotWaveWinPtr(*pa);      *pb = thisWpos;      if (*pa > *pb){         a = *pb; *pb = *pa; *pa = a;      }   }   sprintf(buf,"Region Energy %6.1f (%d..%d)",           CalcEnergy(markA, markB),           (int)Point2Sample(&waveWin,markA),           (int)Point2Sample(&waveWin,markB));   PrintMsg(&io_Win, buf);   return TRUE;}static int mmWPos;/* Drag: drag the mouse from MarkA */void Drag(void){   int x,y;      HMousePos(&x,&y);   if (mmWPos >=0) PlotWaveWinPtr(mmWPos);   mmWPos = x;   PlotWaveWinPtr(mmWPos); }/* MouseMark: mark a region directly using the mouse */void MouseMark(int x, int *markA, int *markB){   HEventRec hev;   Boolean done;   char buf[80];      /* check if region marked, if so unmark */   if (regnMarked)      InvertRegion(&waveWin, *markA, *markB);   *markA = x;   /* wait for mouse release */   mmWPos = -1;   do {      hev = HGetEvent(TRUE, Drag);      done = (hev.event==HMOUSEUP);   } while (!done);   if (mmWPos >=0) PlotWaveWinPtr(mmWPos);   *markB = hev.x;   if (*markA != *markB) {      InvertRegion(&waveWin, *markA, *markB);   }   sprintf(buf,"Region Energy %6.1f (%d..%d)",           CalcEnergy(*markA, *markB),           (int)Point2Sample(&waveWin,*markA),           (int)Point2Sample(&waveWin,*markB));   PrintMsg(&io_Win, buf);}/* GetString:  prompt user to type in string of min length minlen and max length maxlen               pressing escape returns the original string str, the routine returns                TRUE if the length of the typed string is greater than minlen, FALSE                otherwise.  The minlen concept is introduced in order to allow for                prompts which will not be deleted by the back space character.  If                the caller supplies a string whose length is less than minlen                backspacing will be allowed until the length of the current string                exceeds minlen, from then on backspace will not delete any characters                which will make the length < minlen.  */Boolean GetString(RectWin *win, char *str, short minlen, short maxlen){   HEventRec hev;   int x, y, sx, sy, cptr, ofs;   char cursor    = '_';   char nullchar = '\0';   char buf[SLEN];   int pad = 4;   Boolean done, hitesc;#define STR_FG   { HSetColour(win->fg); HPrintf(sx, sy, "%s", buf+ofs); }#define STR_BG   { HSetColour(win->bg); HPrintf(sx, sy, "%s", buf+ofs); }#define ADJ_OFS {ofs = 0; while (HTextWidth(buf+ofs) > (win->w - 2*pad)) ofs++;}   DrawRectWin(win);   strcpy(buf, str);   cptr = strlen(buf); buf[cptr]=cursor;   buf[cptr+1]=nullchar;   sx = win->x + pad; sy = CentreY(win->y + win->h/2, buf);   ADJ_OFS   STR_FG   done = hitesc = FALSE;   do {      hev = HGetEvent(FALSE, NULL);      switch (hev.event) {      case HKEYPRESS:         if (!HMousePos(&x, &y))            break;         /* if (!IsInWin(win, x, y))            break; */         switch (hev.ktype) {         case ENTERKEY:            buf[cptr]=nullchar; strcpy(str, buf);            done = TRUE;            break;         case ESCKEY:            done = hitesc = TRUE;            break;         case NORMALKEY:            if (cptr==maxlen)               break;            STR_BG               buf[cptr++]=hev.c; buf[cptr]=cursor;             buf[cptr+1]=nullchar;             ADJ_OFS   STR_FG               break;         case DELKEY:            if (cptr==minlen || cptr==0)               break;            STR_BG               buf[--cptr]=cursor; buf[cptr+1]=nullchar;             ADJ_OFS   STR_FG               break;         default:            break;         }         break;      case HREDRAW :         hRedrawWindow();         STR_FG            break;      case HMOUSEMOVE:         TrackWPtr();          break;      default:         break;      }   } while (!done);   DrawRectWin(win);   return (!hitesc) && (strlen(str) > minlen); }/* FileExists: check to see if a file exsists */Boolean FileExists(char *fn, char *fmode){   Boolean isEXF;               /* File name is extended */   char actfile[MAXFNAMELEN];   /* actual file name */   long stindex,enindex;        /* segment indices */   FILE *f;      strcpy (actfile, fn);   isEXF = GetFileNameExt (fn, actfile, &stindex, &enindex);   if ((f=fopen(actfile, fmode)) == NULL)      return FALSE;   fclose(f);   return TRUE;}/* LoadLabs: load transcription */void LoadLabs(void){   LabList *ll;   LLink p;   ResetHeap(&labStack);   MakeFN(spfn, labDir, labExt, labfn);   /* read labels if label file exists */   if ((!newLabFile) && (NumMLFFiles()>0 || FileExists(labfn, "r"))){      trans = LOpen(&labStack,labfn,ifmt);      llist = trans->head; numSet = trans->numLists;       for (ll=trans->head; ll!=NULL; ll=ll->next)         for (p=ll->head->succ; p->succ!=NULL; p=p->succ) {            p->start /= sampPeriod; p->end /= sampPeriod;         }     } else {      trans = CreateTranscription(&labStack);      llist = CreateLabelList(&labStack, 10);      AddLabelList(llist, trans); numSet = 1;   }   labsModified = FALSE;   zoomLev = 0; undoEmpty = TRUE; labSet = 0;}/* LoadData: read data file from disk */ void LoadData(void){   int i;   short sample0 = 0;   ResetHeap(&wavStack);   /* load existing file or create null waveform */   if (!FileExists(spfn, "r")) {      nSamples = 10000; sampPeriod = 625;      wave = OpenWaveOutput(&wavStack, &sampPeriod, nSamples);      for (i=0; i<nSamples; i++)         PutWaveSample(wave,1,&sample0);   } else {      sampPeriod = 0.0;      if((wave = OpenWaveInput(&wavStack, spfn, wfmt, 0, 0, &sampPeriod))==NULL)         HError(1514,"LoadData: OpenWaveInput failed");   }   data = GetWaveDirect(wave, &nSamples);   T = nSamples; sStart = 0; sEnd = T;   PreparePlot(&waveWin, data, sStart, sEnd);   zoomLev = 0; undoEmpty = TRUE;      newData = TRUE;}/* LoadFiles: load the speech & label files */void LoadFiles(void){   LoadData(); LoadLabs();}   /* ------------------------- Undo handling ---------------------------- *//* RecordOp: record details of the current label operation */void RecordOp(OpType op, LLink p){   switch (op) {   case CREATE_OP:       undo.ptr = p;      break;

⌨️ 快捷键说明

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