Example #1
0
/**
 * Load a noise spectrum from file.
 *
 * @param filename [in] path name of noise spectrum file
 * @param slen [out] length of the returned buffer
 *
 * @return a newly allocated buffer that holds the loaded noise spectrum.
 */
float *
new_SS_load_from_file(char *filename, int *slen)
{
    FILE *fp;
    int num;
    float *sbuf;

    /* open file */
    jlog("Stat: ss: reading Noise Spectrum for SS\n");
    if ((fp = fopen_readfile(filename)) == NULL) {
        jlog("Error: ss: failed to open \"%s\"\n", filename);
        return(NULL);
    }
    /* read length */
    if (myread(&num, sizeof(int), 1, fp) == FALSE) {
        jlog("Error: ss: failed to read \"%s\"\n", filename);
        return(NULL);
    }
    /* allocate */
    sbuf = (float *)mymalloc(sizeof(float) * num);
    /* read data */
    if (myread(sbuf, sizeof(float), num, fp) == FALSE) {
        jlog("Error: ss: failed to read \"%s\"\n", filename);
        return(NULL);
    }
    /* close file */
    fclose_readfile(fp);

    *slen = num;
    jlog("Stat: ss: done\n");
    return(sbuf);
}
Example #2
0
void
init_term(char *filename, char **termname)
{
  FILE *fd;
  int n;
  static char buf[512];
  char *p;
  
  fprintf(stderr, "Reading in term file (optional)...");
  
  if ((fd = fopen_readfile(filename)) == NULL) {
    termname[0] = NULL;
    fprintf(stderr, "not found\n");
    return;
  }

  while (getl(buf, sizeof(buf), fd) != NULL) {
    if ((p = strtok(buf, DELM)) == NULL) {
      fprintf(stderr, "Error: term file failed to parse, corrupted or invalid data?\n");
      return;
    }
    n = atoi(p);
    if ((p = strtok(NULL, DELM)) == NULL) {
      fprintf(stderr, "Error: term file failed to parse, corrupted or invalid data?\n");
      return;
    }
    termname[n] = strdup(p);
  }
  if (fclose_readfile(fd) == -1) {
    fprintf(stderr, "close error\n");
    exit(1);
  }

  fprintf(stderr, "done\n");
}
Example #3
0
/** 
 * Load and initialize a word list for isolated word recognition.
 * 
 * @param winfo [out] pointer to a word dictionary data to store the read data
 * @param filename [in] file name of the word dictionary to read
 * @param hmminfo [in] %HMM definition data, needed for triphone conversion.
 * @param headphone [in] word head silence phone name
 * @param tailphone [in] word tail silence phone name
 * @param conextphone [in] silence context name at head and tail phoneme
 * @param force_dict [in] TRUE if want to ignore the error words in the dictionary
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
init_wordlist(WORD_INFO *winfo, char *filename, HTK_HMM_INFO *hmminfo, char *headphone, char *tailphone, char *contextphone, boolean force_dict)
{
  FILE *fd;

  jlog("Stat: init_wordlist: reading in word list\n");
  if ((fd = fopen_readfile(filename)) == NULL) {
    jlog("Error: init_wordlist: failed to open %s\n",filename);
    return(FALSE);
  }
  if (!voca_load_wordlist(fd, winfo, hmminfo, headphone, tailphone, contextphone)) {
    if (force_dict) {
      jlog("Warning: init_wordlist: the word errors are ignored\n");
    } else {
      jlog("Error: init_wordlist: error in reading %s: %d words failed out of %d words\n",filename, winfo->errnum, winfo->num);
      fclose_readfile(fd);
      return(FALSE);
    }
  }
  if (fclose_readfile(fd) == -1) {
    jlog("Error: init_wordlist: failed to close\n");
    return(FALSE);
  }

  jlog("Stat: init_wordlist: read %d words\n", winfo->num);
  return(TRUE);
}
Example #4
0
/** 
 * Load and initialize a word dictionary.
 * 
 * @param winfo [out] pointer to a word dictionary data to store the read data
 * @param filename [in] file name of the word dictionary to read
 * @param hmminfo [in] %HMM definition data, needed for triphone conversion.
 * @param not_conv_tri [in] TRUE if not converting monophone to triphone.
 * @param force_dict [in] TRUE if want to ignore the error words in the dictionary
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
init_voca(WORD_INFO *winfo, char *filename, HTK_HMM_INFO *hmminfo, boolean not_conv_tri, boolean force_dict)
{
  FILE *fd;

  if ((fd = fopen_readfile(filename)) == NULL) {
    jlog("Error: init_voca: failed to open %s\n",filename);
    return(FALSE);
  }
  if (!voca_load_htkdict(fd, winfo, hmminfo, not_conv_tri)) {
    if (force_dict) {
      jlog("Warning: init_voca: the word errors are ignored\n");
    } else {
      jlog("Error: init_voca: error in reading %s: %d words failed out of %d words\n",filename, winfo->errnum, winfo->num);
      fclose_readfile(fd);
      return(FALSE);
    }
  }
  if (fclose_readfile(fd) == -1) {
    jlog("Error: init_voca: failed to close\n");
    return(FALSE);
  }

  jlog("Stat: init_voca: read %d words\n", winfo->num);
  return(TRUE);
}
Example #5
0
/** 
 * Top function to read a HTK parameter file.
 * 
 * @param filename [in] HTK parameter file name 
 * @param pinfo [in] parameter data (already allocated by new_param())
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
rdparam(char *filename, HTK_Param *pinfo)
{
  FILE *fp;
  boolean retflag;
  
  if ((fp = fopen_readfile(filename)) == NULL) return(FALSE);
  retflag = read_param(fp, pinfo);
  if (fclose_readfile(fp) < 0) return (FALSE);
  return (retflag);
}
Example #6
0
/** 
 * Read in a grammar file and set to DFA grammar structure
 * 
 * @param dinfo [i/o] a blank DFA data
 * @param filename [in] DFA grammar file name
 */
boolean
init_dfa(DFA_INFO *dinfo, char *filename)
{
  FILE *fd;
  
  if ((fd = fopen_readfile(filename)) == NULL) {
    jlog("Error: init_dfa: failed to open %s\n",filename);
    return FALSE;
  }
  if (!rddfa(fd, dinfo)) {
    jlog("Error; init_dfa: error in reading %s\n",filename);
    return FALSE;
  }
  if (fclose_readfile(fd) == -1) {
    jlog("Error: init_dfa: failed to close %s\n", filename);
    return FALSE;
  }

  return TRUE;
}
Example #7
0
/** 
 * Load CMN parameter from file.  If the number of MFCC dimension in the
 * file does not match the specified one, an error will occur.
 *
 * Format can be either HTK ascii format or binary format (made by Julius older than ver.4.2.3)
 * 
 * @param c [i/o] CMN calculation work area
 * @param filename [in] file name
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
CMN_load_from_file(CMNWork *c, char *filename)
{
  FILE *fp;
  int veclen;
  char ch[5];
  char *buf;

  jlog("Stat: wav2mfcc-pipe: reading initial cepstral mean/variance from file \"%s\"\n", filename);
  if ((fp = fopen_readfile(filename)) == NULL) {
    jlog("Error: wav2mfcc-pipe: failed to open %s\n", filename);
    return(FALSE);
  }

  /* detect file format */
  if (myread(&ch, sizeof(char), 5, fp) == FALSE) {
    jlog("Error: wav2mfcc-pipe: failed to read CMN/CVN file\n");
    fclose_readfile(fp);
    return(FALSE);
  }

  myfrewind(fp);
  if (ch[0] == '<' &&
      (ch[1] == 'C' || ch[1] == 'c') &&
      (ch[2] == 'E' || ch[2] == 'e') &&
      (ch[3] == 'P' || ch[3] == 'p') &&
      (ch[4] == 'S' || ch[4] == 's') ) {
    /* ascii HTK format (>=4.3) */
    char *p;
    int mode;
    int d, dv, len;

    jlog("Stat: wav2mfcc-pipe: reading HTK-format cepstral vectors\n");
    buf = (char *)mymalloc(MAXBUFLEN);
    mode = 0;
    while(getl(buf, MAXBUFLEN, fp) != NULL) {
      for (p = mystrtok_quote(buf, "<> \t\r\n"); p; p = mystrtok_quote(NULL, "<> \t\r\n")) {
	switch(mode){
	case 0:
	  if (strmatch(p, "MEAN")) {
	    mode = 1;
	  } else if (strmatch(p, "VARIANCE")) {
	    mode = 3;
	  }
	  break;
	case 1:
	  len = atof(p);
	  if (len != c->veclen && len != c->mfcc_dim) {
	    jlog("Error: wav2mfcc-pipe: cepstral dimension mismatch\n");
	    jlog("Error: wav2mfcc-pipe: process = %d (%d), file = %d\n", c->veclen, c->mfcc_dim, len);
	    free(buf); fclose_readfile(fp);
	    return(FALSE);
	  }
	  for (d = 0; d < c->veclen; d++) c->cmean_init[d] = 0.0;
	  d = 0;
	  mode = 2;
	  break;
	case 2:
	  if (strmatch(p, "VARIANCE")) {
	    mode = 3;
	  } else {
	    if (d >= len) {
	      jlog("Error: wav2mfcc-pipe: corrupted data\n");
	      free(buf); fclose_readfile(fp);
	      return(FALSE);
	    }
	    c->cmean_init[d++] = atof(p);
	  }
	  break;
	case 3:
	  len = atof(p);
	  if (len != c->veclen) {
	    jlog("Error: wav2mfcc-pipe: cepstral dimension mismatch\n");
	    jlog("Error: wav2mfcc-pipe: process = %d, file = %d\n", c->veclen, len);
	    free(buf); fclose_readfile(fp);
	    return(FALSE);
	  }
	  dv = 0;
	  mode = 4;
	  break;
	case 4:
	  if (dv >= len) {
	    jlog("Error: wav2mfcc-pipe: corrupted data\n");
	    free(buf); fclose_readfile(fp);
	    return(FALSE);
	  }
	  c->cvar_init[dv++] = atof(p);
	  break;
	}
      }
    }
    free(buf);
    if (d != len || (mode >= 3 && dv != len)) {
      jlog("Error: wav2mfcc-pipe: corrupted data\n");
      fclose_readfile(fp);
      return(FALSE);
    }
  } else {
    /* binary (<4.3) */
    jlog("Stat: wav2mfcc-pipe: reading binary-format cepstral vectors\n");
    /* read header */
    if (myread(&veclen, sizeof(int), 1, fp) == FALSE) {
      jlog("Error: wav2mfcc-pipe: failed to read header\n");
      fclose_readfile(fp);
      return(FALSE);
    }
    /* check length */
    if (veclen != c->veclen) {
      jlog("Error: wav2mfcc-pipe: cepstral dimension mismatch\n");
      jlog("Error: wav2mfcc-pipe: process = %d, file = %d\n", c->veclen, veclen);
      fclose_readfile(fp);
      return(FALSE);
    }
    /* read body */
    if (myread(c->cmean_init, sizeof(float), c->veclen, fp) == FALSE) {
      jlog("Error: wav2mfcc-pipe: failed to read mean for CMN\n");
      fclose_readfile(fp);
      return(FALSE);
    }
    if (c->var) {
      if (myread(c->cvar_init, sizeof(float), c->veclen, fp) == FALSE) {
	jlog("Error: wav2mfcc-pipe: failed to read variance for CVN\n");
	fclose_readfile(fp);
	return(FALSE);
      }
    }
  }

  if (fclose_readfile(fp) == -1) {
    jlog("Error: wav2mfcc-pipe: failed to close\n");
    return(FALSE);
  }

  c->cmean_init_set = TRUE;
  c->loaded_from_file = TRUE;
  jlog("Stat: wav2mfcc-pipe: finished reading CMN/CVN parameter\n");

  return(TRUE);
}