47 #include <sphinxbase/fe.h>
54 #include <sphinxbase/byteorder.h>
57 #include "sphinx_wave2feat.h"
58 #include "cmd_ln_defn.h"
95 int32 RemainingLength;
118 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
122 if (fread(&hdr,
sizeof(hdr), 1, fh) != 1) {
123 E_ERROR(
"Failed to read RIFF header");
128 if (0 != memcmp(hdr.rifftag,
"RIFF", 4)) {
132 if (cmd_ln_int32_r(wtf->
config,
"-nchans") != hdr.numchannels) {
133 E_ERROR(
"Number of channels %d does not match configured value in file '%s'\n", hdr.numchannels, wtf->
infile);
137 samprate = cmd_ln_float32_r(wtf->
config,
"-samprate");
138 if (samprate != hdr.SamplingFreq) {
139 E_ERROR(
"Sample rate %d does not match configured value %.1f in file '%s'\n",
140 hdr.SamplingFreq, samprate, wtf->
infile);
150 open_nist_file(
sphinx_wave2feat_t *wtf,
char const *infile, FILE **out_fh,
int detect_endian)
156 if ((fh = fopen(infile,
"rb")) == NULL) {
160 if (fread(&nist, 1, 7, fh) != 7) {
166 if (0 != strncmp(nist,
"NIST_1A", 7)) {
171 fseek(fh, 0, SEEK_SET);
177 if (strlen(li->buf) == 0) {
184 words = (
char **)
ckd_calloc(nword,
sizeof(*words));
186 if (0 == strcmp(words[0],
"sample_rate")) {
187 float samprate =
atof_c(words[2]);
188 if (cmd_ln_float32_r(wtf->
config,
"-samprate") != samprate) {
189 E_ERROR(
"Sample rate %.1f does not match configured value in file '%s'\n", samprate, infile);
195 if (0 == strcmp(words[0],
"channel_count")) {
196 int nchans = atoi(words[2]);
197 if (cmd_ln_int32_r(wtf->
config,
"-nchans") != nchans) {
198 E_ERROR(
"Number of channels %d does not match configured value in file '%s'\n", nchans, infile);
204 if (detect_endian && 0 == strcmp(words[0],
"sample_byte_format")) {
205 const char *endian = (0 == strcmp(words[2],
"10")) ?
"big" :
"little";
207 E_ERROR(
"Input endian %s does not match configured value in file '%s'\n", endian, infile);
216 fseek(fh, 1024, SEEK_SET);
233 if ((rv = open_nist_file(wtf, wtf->
infile, NULL, FALSE)) != TRUE)
238 if ((fh = popen(cmdline,
"r")) == NULL) {
251 E_ERROR(
"popen() not available, cannot run sph2pipe\n");
267 if ((rv = open_nist_file(wtf, wtf->
infile, &fh, TRUE)) != TRUE)
286 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
307 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
311 if (fread(&len, 4, 1, fh) != 1) {
316 fseek(fh, 0, SEEK_END);
320 flen = (flen / 4) - 1;
326 E_ERROR(
"Mismatch in header/file lengths: 0x%08x vs 0x%08x\n",
333 ?
"little" :
"big"));
336 fseek(fh, 4, SEEK_SET);
347 E_ERROR(
"Sphinx MFCC file reading requested but -spec2cep/-cep2spec not given\n");
355 mixnpick_channels(int16 *buf, int32 nsamp, int32 nchans, int32 whichchan)
360 for (i = whichchan - 1; i < nsamp; i += nchans)
361 buf[i/nchans] = buf[i];
364 for (i = 0; i < nsamp; i += nchans) {
366 for (j = 0; j < nchans && i + j < nsamp; ++j) {
369 buf[i/nchans] = (int16)(tmp / nchans);
383 int32 n, nfr, nchans, whichchan;
386 nchans = cmd_ln_int32_r(wtf->
config,
"-nchans");
387 whichchan = cmd_ln_int32_r(wtf->
config,
"-whichchan");
388 fe_start_stream(wtf->
fe);
389 fe_start_utt(wtf->
fe);
393 int16
const *inspeech;
397 for (n = 0; n < nsamp; ++n)
398 SWAP_INT16(wtf->
audio + n);
403 nsamp = mixnpick_channels(wtf->
audio, nsamp, nchans, whichchan);
405 inspeech = wtf->
audio;
410 fe_process_frames(wtf->
fe, &inspeech, &nsamp, wtf->
feat, &nfr, NULL);
412 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
417 inspeech = wtf->
audio;
420 fe_end_utt(wtf->
fe, wtf->
feat[0], &nfr);
422 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
427 if (fclose(wtf->
infh) == EOF)
448 while ((n = fread(wtf->
feat[0],
sizeof(**wtf->
feat),
452 E_ERROR(
"Size of file %d not a multiple of veclen %d\n",
458 for (i = 0; i < n; ++i)
459 SWAP_FLOAT32(wtf->
feat[0] + i);
461 fe_float_to_mfcc(wtf->
fe, (float32 **)wtf->
feat, wtf->
feat, nfr);
462 for (i = 0; i < nfr; ++i) {
465 fe_logspec_to_mfcc(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
467 fe_logspec_dct2(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
470 fe_mfcc_dct3(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
473 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
478 if (fclose(wtf->
infh) == EOF)
485 {
"-mswav", &detect_riff, &decode_pcm },
486 {
"-nist", &detect_nist, &decode_pcm },
487 {
"-raw", &detect_raw, &decode_pcm },
488 {
"-sph2pipe", &detect_sph2pipe, &decode_pcm }
490 static const int ntypes =
sizeof(types)/
sizeof(types[0]);
492 "sphinx_mfc", &detect_sphinx_mfc, &decode_sphinx_mfc
503 if (fwrite(&nfloat, 4, 1, wtf->
outfh) != 1) {
520 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
521 for (i = 0; i < nfr; ++i) {
522 if (fwrite(frames[i],
sizeof(float32), wtf->
veclen, wtf->
outfh) != wtf->
veclen) {
532 typedef enum htk_feature_kind_e {
545 } htk_feature_kind_t;
547 typedef enum htk_feature_flag_e {
558 } htk_feature_flag_t;
575 if (swap) SWAP_INT32(&nfloat);
576 if (fwrite(&nfloat, 4, 1, wtf->
outfh) != 1)
579 samp_period = (int32)(1e+7 / cmd_ln_float32_r(wtf->
config,
"-frate"));
580 if (swap) SWAP_INT32(&samp_period);
581 if (fwrite(&samp_period, 4, 1, wtf->
outfh) != 1)
584 samp_size = wtf->
veclen * 4;
585 if (swap) SWAP_INT16(&samp_size);
586 if (fwrite(&samp_size, 2, 1, wtf->
outfh) != 1)
593 param_kind = MFCC | _O;
594 if (swap) SWAP_INT16(¶m_kind);
595 if (fwrite(¶m_kind, 2, 1, wtf->
outfh) != 1)
607 int i, j, swap, htk_reorder, nfloat = 0;
609 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
612 htk_reorder = (0 == strcmp(
"htk", wtf->
ot->name)
615 for (i = 0; i < nfr; ++i) {
617 mfcc_t c0 = frames[i][0];
618 memmove(frames[i] + 1, frames[i], (wtf->
veclen - 1) * 4);
619 frames[i][wtf->
veclen - 1] = c0;
622 for (j = 0; j < wtf->
veclen; ++j)
623 SWAP_FLOAT32(frames[i] + j);
624 if (fwrite(frames[i],
sizeof(float32), wtf->
veclen, wtf->
outfh) != wtf->
veclen) {
640 int i, j, nfloat = 0;
642 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
643 for (i = 0; i < nfr; ++i) {
644 for (j = 0; j < wtf->
veclen; ++j) {
645 fprintf(wtf->
outfh,
"%.5g", MFCC2FLOAT(frames[i][j]));
647 fprintf(wtf->
outfh,
"\n");
649 fprintf(wtf->
outfh,
" ");
657 {
"sphinx", &output_header_sphinx, &output_frames_sphinx },
658 {
"htk", &output_header_htk, &output_frames_htk },
659 {
"text", NULL, &output_frames_text }
661 static const int nouttypes =
sizeof(outtypes)/
sizeof(outtypes[0]);
664 sphinx_wave2feat_init(
cmd_ln_t *config)
672 wtf->
fe = fe_init_auto_r(wtf->
config);
674 E_FATAL(
"Failed to create feature extraction\n");
678 for (i = 0; i < nouttypes; ++i) {
680 if (0 == strcmp(
cmd_ln_str_r(config,
"-ofmt"), otype->name)) {
685 if (i == nouttypes) {
686 E_ERROR(
"Unknown output type: '%s'\n",
688 sphinx_wave2feat_free(wtf);
712 if (fclose(wtf->
infh) == EOF)
716 if (fclose(wtf->
outfh) == EOF)
742 int rv = mfcc_type.detect(wtf);
749 for (i = 0; i < ntypes; ++i) {
753 rv = (*atype->detect)(wtf);
762 for (i = 0; i < ntypes; ++i) {
765 rv = (*atype->detect)(wtf);
784 char const *infile,
char const *outfile)
786 int nchans, nfloat, veclen;
790 E_INFO(
"Converting %s to %s\n", infile, outfile);
795 if ((atype = detect_audio_type(wtf)) == NULL)
804 wtf->
veclen = fe_get_output_size(wtf->
fe);
807 fe_get_input_size(wtf->
fe, &fshift, &fsize);
811 nchans = cmd_ln_int32_r(wtf->
config,
"-nchans");
813 if (wtf->
blocksize < (fsize + fshift) * nchans) {
814 E_INFO(
"Block size of %d too small, increasing to %d\n",
816 (fsize + fshift) * nchans);
817 wtf->
blocksize = (fsize + fshift) * nchans;
829 if ((wtf->
outfh = fopen(outfile,
"wb")) == NULL) {
834 if (wtf->
ot->output_header &&
835 (*wtf->
ot->output_header)(wtf, 0) < 0) {
841 if ((nfloat = (*atype->decode)(wtf)) < 0) {
846 if (wtf->
ot->output_header) {
847 if (fseek(wtf->
outfh, 0, SEEK_SET) < 0) {
851 if ((*wtf->
ot->output_header)(wtf, nfloat) < 0) {
873 if (fclose(wtf->
outfh) == EOF)
896 if (fclose(wtf->
outfh) == EOF)
904 build_filenames(
cmd_ln_t *config,
char const *basename,
905 char **out_infile,
char **out_outfile)
907 char const *di, *do_, *ei, *eo;
943 int nskip, runlen, npart;
945 if ((ctlfh = fopen(ctlfile,
"r")) == NULL) {
949 nskip = cmd_ln_int32_r(wtf->
config,
"-nskip");
950 runlen = cmd_ln_int32_r(wtf->
config,
"-runlen");
951 if ((npart = cmd_ln_int32_r(wtf->
config,
"-npart"))) {
953 int partlen, part, nlines = 0;
954 part = cmd_ln_int32_r(wtf->
config,
"-part");
957 fseek(ctlfh, 0, SEEK_SET);
958 partlen = nlines / npart;
959 nskip = partlen * (part - 1);
966 E_INFO(
"Processing %d utterances at position %d\n", runlen, nskip);
970 E_INFO(
"Processing all remaining utterances at position %d\n", nskip);
974 char *c, *infile, *outfile;
986 if ((c = strchr(li->buf,
' ')) != NULL)
988 if (strlen(li->buf) == 0) {
989 E_WARN(
"Empty line %d in control file, skipping\n", li->lineno);
992 build_filenames(wtf->
config, li->buf, &infile, &outfile);
995 sphinx_wave2feat_convert_file(wtf, infile, outfile);
1010 main(
int argc,
char *argv[])
1021 if (config == NULL) {
1022 E_ERROR(
"Command line parsing failed\n");
1026 if ((wtf = sphinx_wave2feat_init(config)) == NULL) {
1027 E_ERROR(
"Failed to initialize wave2feat object\n");
1035 rv = run_control_file(wtf,
cmd_ln_str_r(config,
"-c"));
1037 rv = sphinx_wave2feat_convert_file(wtf,
cmd_ln_str_r(config,
"-i"),
1040 sphinx_wave2feat_free(wtf);
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
Command-line and other configurationparsing and handling.
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_retain(cmd_ln_t *cmdln)
Retain ownership of a command-line argument set.
Miscellaneous useful string functions.
#define E_INFO(...)
Print logging information to standard error stream.
hash_entry_t * ent
Current entry in that table.
SPHINXBASE_EXPORT int32 hash_table_lookup(hash_table_t *h, const char *key, void **val)
Look up a key in a hash table and optionally return the associated value.
int veclen
Length of each output vector.
#define ckd_calloc_2d(d1, d2, sz)
Macro for ckd_calloc_2d
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
output_type_t const * ot
Output type object.
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT int cmd_ln_free_r(cmd_ln_t *cmdln)
Release a command-line argument set and all associated strings.
File names related operation.
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_parse_r(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 argc, char *argv[], int32 strict)
Parse a list of strings into argumetns.
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter(hash_table_t *h)
Start iterating over key-value pairs in a hash table.
#define ckd_salloc(ptr)
Macro for ckd_salloc
#define hash_entry_val(e)
Access macros.
SPHINXBASE_EXPORT char const * cmd_ln_str_r(cmd_ln_t *cmdln, char const *name)
Retrieve a string from a command-line object.
SPHINXBASE_EXPORT hash_table_t * hash_table_new(int32 size, int32 casearg)
Allocate a new hash table for a given expected size.
FILE * infh
Input file handle.
int refcount
Reference count.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
char * outfile
Path to output file.
SPHINXBASE_EXPORT void hash_table_free(hash_table_t *h)
Free the specified hash table; the caller is responsible for freeing the key strings pointed to by th...
SPHINXBASE_EXPORT int build_directory(const char *path)
Create a directory and all of its parent directories, as needed.
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
int featsize
Size of feature buffer.
SPHINXBASE_EXPORT void lineiter_free(lineiter_t *li)
Stop reading lines from a file.
FILE * outfh
Output file handle.
SPHINXBASE_EXPORT lineiter_t * lineiter_next(lineiter_t *li)
Move to the next line in the file.
int byteswap
Whether byteswapping is necessary.
mfcc_t ** feat
Feature buffer.
SPHINXBASE_EXPORT lineiter_t * lineiter_start(FILE *fh)
Start reading lines from a file.
int in_veclen
Length of each input vector (for cep<->spec).
Implementation of logging routines.
SPHINXBASE_EXPORT void * hash_table_enter(hash_table_t *h, const char *key, void *val)
Try to add a new entry with given key and associated value to hash table h.
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_parse_file_r(cmd_ln_t *inout_cmdln, arg_t const *defn, char const *filename, int32 strict)
Parse an arguments file by deliminating on " \r\t\n" and putting each tokens into an argv[] for cmd_l...
#define E_WARN(...)
Print warning message to error log.
short * audio
Audio buffer.
SPHINXBASE_EXPORT int32 str2words(char *line, char **wptr, int32 n_wptr)
Convert a line to an array of "words", based on whitespace separators.
Opaque structure used to hold the results of command-line parsing.
char * infile
Path to input file.
SPHINXBASE_EXPORT hash_iter_t * hash_table_iter_next(hash_iter_t *itor)
Get the next key-value pair in iteration.
SPHINXBASE_EXPORT char * string_join(const char *base,...)
Concatenate a NULL-terminated argument list of strings, returning a newly allocated string...
SPHINXBASE_EXPORT void ckd_free_2d(void *ptr)
Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.
int blocksize
Size of audio buffer.
#define cmd_ln_boolean_r(c, n)
Retrieve a boolean value from a command-line object.
cmd_ln_t * config
Configuration parameters.
Hash table implementation.
#define E_FATAL(...)
Exit with non-zero status after error message.
Structure for the front-end computation.
SPHINXBASE_EXPORT void cmd_ln_set_str_r(cmd_ln_t *cmdln, char const *name, char const *str)
Set a string in a command-line object.
SPHINXBASE_EXPORT char * string_trim(char *string, enum string_edge_e which)
Remove whitespace from a string, modifying it in-place.
fe_t * fe
Front end object.
SPHINXBASE_EXPORT void path2dirname(const char *path, char *dir)
Strip off filename from the given path and copy the directory name into dir Caller must have allocate...
file IO related operations.