Пример #1
0
/** 
 * Save the current CMN vector to a file.
 * 
 * @param c [i/o] CMN calculation work area
 * @param filename [in] filename to save the data.
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
CMN_save_to_file(CMNWork *c, char *filename)
{
  FILE *fp;
  int d;

  /* save in HTK ascii format */

  /* open file for writing */
  if ((fp = fopen_writefile(filename)) == NULL) {
    jlog("Error: wav2mfcc-pipe: failed to open \"%s\" to write current cepstral data\n", filename);
    return(FALSE);
  }

  fprintf(fp, "<CEPSNORM> <>\n");
  /* unlike HTK, full mean will be written for variance estimation */
  fprintf(fp, "<MEAN> %d\n", c->veclen);
  for(d=0;d<c->veclen;d++) {
    fprintf(fp, " %e\n", c->cmean_init[d]);
  }
  if (c->var) {
    fprintf(fp, "<VARIANCE> %d\n", c->veclen);
    for(d=0;d<c->veclen;d++) {
      fprintf(fp, " %e\n", c->cvar_init[d]);
    }
  }

  fclose_writefile(fp);

  jlog("Stat: wav2mfcc-pipe: cepstral mean");
  if (c->var) jlog(" and variance");
  jlog(" written to \"%s\"\n", filename);
  
  return(TRUE);
}
Пример #2
0
/** 
 * @brief  Close the file.
 *
 * The frame length in the header part is overwritten by the actual value
 * before file close.
 * 
 * @param fp [in] file pointer to close, previously opened by wrwav_open().
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
wrwav_close(FILE *fp)
{
  unsigned int i;
  
  /* overwrite data length after recording */
  /* at 5-8(len+36), 41-44 (len) */
  if (fseek(fp, 40, SEEK_SET) != 0) { /* error */
    jlog("Error: wrwav: failed to seek for header\n");
    return(FALSE);
  }
  i = totallen * sizeof(SP16);
  if (!mywrite(&i, 4, 1, fp)) {
    jlog("Error: wrwav: failed to re-write header\n");
    return(FALSE);
  }
  if (fseek(fp, 4, SEEK_SET) != 0) { /* error */
    jlog("Error: wrwav: failed to seek for header\n");
    return(FALSE);
  }
  i = totallen * sizeof(SP16) + 36;
  if (!mywrite(&i, 4, 1, fp)) {
    jlog("Error: wrwav: failed to re-write header\n");
    return(FALSE);
  }
  
  /* close file */
  fclose_writefile(fp);

  return(TRUE);
}
Пример #3
0
int
main(int argc, char *argv[])
{
  FILE *fp;
  char *hmmdefs_file;
  char *hmmlist_file;
  char *outfile;
  int i;

  hmmdefs_file = hmmlist_file = outfile = NULL;
  for(i=1;i<argc;i++) {
    if (hmmdefs_file == NULL) {
      hmmdefs_file = argv[i];
    } else if (hmmlist_file == NULL) {
      hmmlist_file = argv[i];
    } else if (outfile == NULL) {
      outfile = argv[i];
    } else {
      usage(argv[0]);
      return -1;
    }
  }
  if (hmmdefs_file == NULL || hmmlist_file == NULL || outfile == NULL) {
    usage(argv[0]);
    return -1;
  }

  hmminfo = hmminfo_new();

  printf("---- reading hmmdefs ----\n");
  printf("filename: %s\n", hmmdefs_file);

  /* read hmmdef file */
  undef_para(&para);
  if (init_hmminfo(hmminfo, hmmdefs_file, hmmlist_file, &para) == FALSE) {
    fprintf(stderr, "--- terminated\n");
    return -1;
  }

  if (hmminfo->is_triphone) {
    fprintf(stderr, "making pseudo bi/mono-phone for IW-triphone\n");
    if (make_cdset(hmminfo) == FALSE) {
      fprintf(stderr, "ERROR: m_fusion: failed to make context-dependent state set\n");
      return -1;
    }
  }

  printf("\n------------------------------------------------------------\n");
  print_hmmdef_info(stdout, hmminfo);
  printf("\n");

  printf("------------------------------------------------------------\n");

  printf("---- writing logical-to-physical mapping and pseudo phone info ----\n");
  printf("filename: %s\n", outfile);

  if ((fp = fopen_writefile(outfile)) == NULL) {
    fprintf(stderr, "failed to open %s for writing\n", outfile);
    return -1;
  }
  if (save_hmmlist_bin(fp, hmminfo) == FALSE) {
    fprintf(stderr, "failed to write to %s\n", outfile);
    return -1;
  }
  if (fclose_writefile(fp) != 0) {
    fprintf(stderr, "failed to close %s\n", outfile);
    return -1;
  }

  printf("\n");
  printf("binary HMMList and pseudo phone definitions are written to \"%s\"\n", outfile);

  return 0;
}
Пример #4
0
int
main(int argc, char *argv[])
{
  FILE *fp;
  char header[512];
  time_t now;
  char *binfile, *lrfile, *rlfile, *outfile;
  int i;
  char *from_code, *to_code, *buf;
  boolean charconv_enabled = FALSE;
  boolean force_swap = FALSE;
  WORD_ID w;

  binfile = lrfile = rlfile = outfile = NULL;
  from_code = to_code = NULL;
  if (argc <= 1) {
    usage(argv[0]);
    return -1;
  }

  for(i=1;i<argc;i++) {
    if (argv[i][0] == '-') {
      if (argv[i][1] == 'd') {
	if (++i >= argc) {
	  printf("Error: no argument for option \"%s\"\n", argv[i]);
	  usage(argv[0]);
	  return -1;
	}
	binfile = argv[i];
      } else if (argv[i][1] == 'n') {
	switch(argv[i][2]) {
	case 'l':
	  if (++i >= argc) {
	    printf("Error: no argument for option \"%s\"\n", argv[i]);
	    usage(argv[0]);
	    return -1;
	  }
	  lrfile = argv[i];
	  break;
	case 'r':
	  if (++i >= argc) {
	    printf("Error: no argument for option \"%s\"\n", argv[i]);
	    usage(argv[0]);
	    return -1;
	  }
	  rlfile = argv[i];
	  break;
	default:
	  printf("Error: no such option \"%s\"\n", argv[i]);
	  usage(argv[0]);
	  return -1;
	}
      } else if (argv[i][1] == 'c') {
	if (++i >= argc) {
	  printf("Error: no argument for option \"%s\"\n", argv[i]);
	  usage(argv[0]);
	  return -1;
	}
	from_code = strcpy((char*)mymalloc(strlen(argv[i])+1), argv[i]);
	if (++i >= argc) {
	  printf("Error: no argument for option \"%s\"\n", argv[i]);
	  usage(argv[0]);
	  free(from_code);
	  return -1;
	}
	to_code = strcpy((char*)mymalloc(strlen(argv[i])+1),argv[i]);
	charconv_enabled = TRUE;
      } else if (argv[i][1] == 's') {
	force_swap = TRUE;
      }
    } else {
      if (outfile == NULL) {
	outfile = argv[i];
      } else {
	printf("Error: more than one output file\n");
	usage(argv[0]);
	return -1;
      }
    }
  }

  if (!outfile) {
    printf("Error: no output file specified\n");
    usage(argv[0]);
    return -1;
  }

  if (binfile) {
    if (lrfile || rlfile) {
      printf("Error: both binary file and ARPA file are specified\n");
      usage(argv[0]);
      return -1;
    }
    printf("bingram: %s\n", binfile);
  } else {
    if (rlfile) {
      printf("backward n-gram: %s\n", rlfile);
      if (lrfile) {
	printf("additional forward 2-gram for 1st pass: %s\n", lrfile);
      }
    } else if (lrfile) {
      printf("forward n-gram: %s\n", lrfile);
    } else {
      printf("Error: no input N-gram file specified\n");
      usage(argv[0]);
      return -1;
    }
  }

  printf("\nSTART LOADING\n\n");

  /* make header string */
  now = time(NULL);
  if (binfile) {
    sprintf(header, "converted at %s\nfrom bingram = %s\n", ctime(&now), binfile);
  } else {
    if (rlfile && lrfile) {
      sprintf(header, "converted at %s\nfrom n-gram = %s, LR 2-gram = %s\n", ctime(&now),  rlfile, lrfile);
    } else if (rlfile) {
      sprintf(header, "converted at %s\nfrom n-gram = %s\n", ctime(&now),  rlfile);
    } else {
      sprintf(header, "converted at %s\nfrom n-gram = %s\n", ctime(&now),  lrfile);
    }
  }

  ngram = ngram_info_new();
  if (binfile) {
    /* read in bingram */
    if (init_ngram_bin(ngram, binfile) == FALSE) return -1;
  } else {
    /* read in ARPA n-gram */
    if (force_swap) {
      ngram->bos_eos_swap = TRUE;
    }
    if (rlfile) {
      if (init_ngram_arpa(ngram, rlfile, DIR_RL) == FALSE) return -1;
      if (lrfile) {
	if (init_ngram_arpa_additional(ngram, lrfile) == FALSE) return -1;
      }
    } else if (lrfile) {
      if (init_ngram_arpa(ngram, lrfile, DIR_LR) == FALSE) return -1;
    }
  }

  print_ngram_info(stdout, ngram);
  
  if (charconv_enabled == TRUE) {
    /* do character conversion */
    if (charconv_setup(from_code, to_code) == -1) {
      fprintf(stderr, "failed to setup character convertsion\n");
      return -1;
    }
    buf = (char *)mymalloc(4096);
    for (w = 0; w < ngram->max_word_num; w++) {
      charconv(ngram->wname[w], buf, 4096);
      ngram->wname[w] = mybmalloc2(strlen(buf)+1, &(ngram->mroot));
      strcpy(ngram->wname[w], buf);
    }
    free(buf);
  }

  /* write in JULIUS binary format */
  if ((fp = fopen_writefile(outfile)) == NULL) {
    fprintf(stderr, "failed to open \"%s\"\n", outfile);
    return -1;
  }
  printf("\nWriting in v5 format to \"%s\"...\n", outfile);
  if (ngram_write_bin(fp, ngram, header) == FALSE){/* failed */
    fprintf(stderr, "failed to write \"%s\"\n",outfile);
    return -1;
  }
  fclose_writefile(fp);

  printf("completed\n");
  
  return 0;

}
Пример #5
0
int
main(int argc, char *argv[])
{
  FILE *fp;
  char *infile;
  char *outfile;
  char *conffile;
  int i;

  infile = outfile = conffile = NULL;
  for(i=1;i<argc;i++) {
    if (strmatch(argv[i], "-C") || strmatch(argv[i], "-htkconf")) {
      if (++i >= argc) {
	usage(argv[0]);
	return -1;
      }
      conffile = argv[i];
    } else {
      if (infile == NULL) {
	infile = argv[i];
      } else if (outfile == NULL) {
	outfile = argv[i];
      } else {
	usage(argv[0]);
	return -1;
      }
    }
  }
  if (infile == NULL || outfile == NULL) {
    usage(argv[0]);
    return -1;
  }

  hmminfo = hmminfo_new();

  printf("---- reading hmmdefs ----\n");
  printf("filename: %s\n", infile);

  /* read hmmdef file */
  undef_para(&para);
  if (init_hmminfo(hmminfo, infile, NULL, &para) == FALSE) {
    fprintf(stderr, "--- terminated\n");
    return -1;
  }

  if (conffile != NULL) {
    /* if input HMMDEFS already has embedded parameter
       they will be overridden by the parameters in the config file */
    printf("\n---- reading HTK Config ----\n");
    if (para.loaded == 1) {
      printf("Warning: input hmmdefs has acoustic analysis parameter information\n");
      printf("Warning: they are overridden by the HTK Config file...\n");
    }
    /* load HTK config file */
    undef_para(&para);
    if (htk_config_file_parse(conffile, &para) == FALSE) {
      fprintf(stderr, "Error: failed to read %s\n", conffile);
      return(-1);
    }
    /* set some parameters from HTK HMM header information */
    printf("\nsetting TARGETKIND and NUMCEPS from HMM definition header...");
    calc_para_from_header(&para, hmminfo->opt.param_type, hmminfo->opt.vec_size);
    printf("done\n");
    /* fulfill unspecified values with HTK defaults */
    printf("fulfill unspecified values with HTK defaults...");
    undef_para(&para_htk);
    make_default_para_htk(&para_htk);
    apply_para(&para, &para_htk);
    printf("done\n");
  }

  printf("\n------------------------------------------------------------\n");
  print_hmmdef_info(stdout, hmminfo);
  printf("\n");

  if (para.loaded == 1) {
    put_para(stdout, &para);
  }
  printf("------------------------------------------------------------\n");

  printf("---- writing ----\n");
  printf("filename: %s\n", outfile);
  
  if ((fp = fopen_writefile(outfile)) == NULL) {
    fprintf(stderr, "failed to open %s for writing\n", outfile);
    return -1;
  }
  if (write_binhmm(fp, hmminfo, (para.loaded == 1) ? &para : NULL) == FALSE) {
    fprintf(stderr, "failed to write to %s\n", outfile);
    return -1;
  }
  if (fclose_writefile(fp) != 0) {
    fprintf(stderr, "failed to close %s\n", outfile);
    return -1;
  }

  printf("\n");
  if (para.loaded == 1) {
    printf("binary HMM written to \"%s\", with acoustic parameters embedded for Julius.\n", outfile);
  } else {
    printf("binary HMM written to \"%s\"\n", outfile);
  }

  return 0;
}