long ps_decode_raw(ps_decoder_t *ps, FILE *rawfh, long maxsamps) { int16 *data; long total, pos, endpos; ps_start_stream(ps); ps_start_utt(ps); /* If this file is seekable or maxsamps is specified, then decode * the whole thing at once. */ if (maxsamps != -1) { data = ckd_calloc(maxsamps, sizeof(*data)); total = fread(data, sizeof(*data), maxsamps, rawfh); ps_process_raw(ps, data, total, FALSE, TRUE); ckd_free(data); } else if ((pos = ftell(rawfh)) >= 0) { fseek(rawfh, 0, SEEK_END); endpos = ftell(rawfh); fseek(rawfh, pos, SEEK_SET); maxsamps = endpos - pos; data = ckd_calloc(maxsamps, sizeof(*data)); total = fread(data, sizeof(*data), maxsamps, rawfh); ps_process_raw(ps, data, total, FALSE, TRUE); ckd_free(data); } else { /* Otherwise decode it in a stream. */ total = 0; while (!feof(rawfh)) { int16 data[256]; size_t nread; nread = fread(data, sizeof(*data), sizeof(data)/sizeof(*data), rawfh); ps_process_raw(ps, data, nread, FALSE, FALSE); total += nread; } } ps_end_utt(ps); return total; }
static int process_ctl_line(ps_decoder_t *ps, cmd_ln_t *config, char const *file, char const *uttid, int32 sf, int32 ef) { FILE *infh; char const *cepdir, *cepext; char *infile; if (ef != -1 && ef < sf) { E_ERROR("End frame %d is < start frame %d\n", ef, sf); return -1; } cepdir = cmd_ln_str_r(config, "-cepdir"); cepext = cmd_ln_str_r(config, "-cepext"); /* Build input filename. */ infile = string_join(cepdir ? cepdir : "", "/", file, cepext ? cepext : "", NULL); if (uttid == NULL) uttid = file; if ((infh = fopen(infile, "rb")) == NULL) { E_ERROR_SYSTEM("Failed to open %s", infile); ckd_free(infile); return -1; } /* Build output directories. */ if (cmd_ln_boolean_r(config, "-build_outdirs")) build_outdirs(config, uttid); if (cmd_ln_boolean_r(config, "-senin")) { /* start and end frames not supported. */ ps_decode_senscr(ps, infh); } else if (cmd_ln_boolean_r(config, "-adcin")) { if (ef != -1) { ef = (int32)((ef - sf) * (cmd_ln_float32_r(config, "-samprate") / cmd_ln_int32_r(config, "-frate")) + (cmd_ln_float32_r(config, "-samprate") * cmd_ln_float32_r(config, "-wlen"))); } sf = (int32)(sf * (cmd_ln_float32_r(config, "-samprate") / cmd_ln_int32_r(config, "-frate"))); fseek(infh, cmd_ln_int32_r(config, "-adchdr") + sf * sizeof(int16), SEEK_SET); ps_decode_raw(ps, infh, ef); } else { mfcc_t **mfcs; int nfr; if (NULL == (mfcs = read_mfc_file(infh, sf, ef, &nfr, cmd_ln_int32_r(config, "-ceplen")))) { E_ERROR("Failed to read MFCC from the file '%s'\n", infile); fclose(infh); ckd_free(infile); return -1; } ps_start_stream(ps); ps_start_utt(ps); ps_process_cep(ps, mfcs, nfr, FALSE, TRUE); ps_end_utt(ps); ckd_free_2d(mfcs); } fclose(infh); ckd_free(infile); return 0; }