47 #include <sphinxbase/fe.h> 54 #include <sphinxbase/byteorder.h> 57 #include "sphinx_wave2feat.h" 58 #include "cmd_ln_defn.h" 95 int32 RemainingLength;
117 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
121 if (fread(&hdr,
sizeof(hdr), 1, fh) != 1) {
122 E_ERROR(
"Failed to read RIFF header");
127 if (0 != memcmp(hdr.rifftag,
"RIFF", 4)) {
133 cmd_ln_set_int32_r(wtf->
config,
"-nchans", hdr.numchannels);
134 cmd_ln_set_float32_r(wtf->
config,
"-samprate", hdr.SamplingFreq);
141 open_nist_file(
sphinx_wave2feat_t *wtf,
char const *infile, FILE **out_fh,
int detect_endian)
147 if ((fh = fopen(infile,
"rb")) == NULL) {
151 if (fread(&nist, 1, 7, fh) != 7) {
157 if (0 != strncmp(nist,
"NIST_1A", 7)) {
162 fseek(fh, 0, SEEK_SET);
168 if (strlen(li->buf) == 0) {
175 words = (
char **)
ckd_calloc(nword,
sizeof(*words));
177 if (0 == strcmp(words[0],
"sample_rate")) {
178 cmd_ln_set_float32_r(wtf->
config,
"-samprate",
atof_c(words[2]));
180 if (0 == strcmp(words[0],
"channel_count")) {
181 cmd_ln_set_int32_r(wtf->
config,
"-nchans", atoi(words[2]));
183 if (detect_endian && 0 == strcmp(words[0],
"sample_byte_format")) {
185 (0 == strcmp(words[2],
"10")) ?
"big" :
"little");
190 fseek(fh, 1024, SEEK_SET);
207 if ((rv = open_nist_file(wtf, wtf->
infile, NULL, FALSE)) != TRUE)
212 if ((fh = popen(cmdline,
"r")) == NULL) {
225 E_ERROR(
"popen() not available, cannot run sph2pipe\n");
241 if ((rv = open_nist_file(wtf, wtf->
infile, &fh, TRUE)) != TRUE)
260 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
281 if ((fh = fopen(wtf->
infile,
"rb")) == NULL) {
285 if (fread(&len, 4, 1, fh) != 1) {
290 fseek(fh, 0, SEEK_END);
294 flen = (flen / 4) - 1;
300 E_ERROR(
"Mismatch in header/file lengths: 0x%08x vs 0x%08x\n",
307 ?
"little" :
"big"));
310 fseek(fh, 4, SEEK_SET);
321 E_ERROR(
"Sphinx MFCC file reading requested but -spec2cep/-cep2spec not given\n");
329 mixnpick_channels(int16 *buf, int32 nsamp, int32 nchans, int32 whichchan)
334 for (i = whichchan - 1; i < nsamp; i += nchans)
335 buf[i/nchans] = buf[i];
338 for (i = 0; i < nsamp; i += nchans) {
340 for (j = 0; j < nchans && i + j < nsamp; ++j) {
343 buf[i/nchans] = (int16)(tmp / nchans);
357 int32 n, nfr, nchans, whichchan;
360 nchans = cmd_ln_int32_r(wtf->
config,
"-nchans");
361 whichchan = cmd_ln_int32_r(wtf->
config,
"-whichchan");
362 fe_start_stream(wtf->
fe);
363 fe_start_utt(wtf->
fe);
367 int16
const *inspeech;
371 for (n = 0; n < nsamp; ++n)
372 SWAP_INT16(wtf->
audio + n);
377 nsamp = mixnpick_channels(wtf->
audio, nsamp, nchans, whichchan);
379 inspeech = wtf->
audio;
384 fe_process_frames(wtf->
fe, &inspeech, &nsamp, wtf->
feat, &nfr, NULL);
386 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
391 inspeech = wtf->
audio;
394 fe_end_utt(wtf->
fe, wtf->
feat[0], &nfr);
396 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
401 if (fclose(wtf->
infh) == EOF)
422 while ((n = fread(wtf->
feat[0],
sizeof(**wtf->
feat),
426 E_ERROR(
"Size of file %d not a multiple of veclen %d\n",
432 for (i = 0; i < n; ++i)
433 SWAP_FLOAT32(wtf->
feat[0] + i);
435 fe_float_to_mfcc(wtf->
fe, (float32 **)wtf->
feat, wtf->
feat, nfr);
436 for (i = 0; i < nfr; ++i) {
439 fe_logspec_to_mfcc(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
441 fe_logspec_dct2(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
444 fe_mfcc_dct3(wtf->
fe, wtf->
feat[i], wtf->
feat[i]);
447 if ((n = (*wtf->
ot->output_frames)(wtf, wtf->
feat, nfr)) < 0)
452 if (fclose(wtf->
infh) == EOF)
459 {
"-mswav", &detect_riff, &decode_pcm },
460 {
"-nist", &detect_nist, &decode_pcm },
461 {
"-raw", &detect_raw, &decode_pcm },
462 {
"-sph2pipe", &detect_sph2pipe, &decode_pcm }
464 static const int ntypes =
sizeof(types)/
sizeof(types[0]);
466 "sphinx_mfc", &detect_sphinx_mfc, &decode_sphinx_mfc
477 if (fwrite(&nfloat, 4, 1, wtf->
outfh) != 1) {
494 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
495 for (i = 0; i < nfr; ++i) {
496 if (fwrite(frames[i],
sizeof(float32), wtf->
veclen, wtf->
outfh) != wtf->
veclen) {
506 typedef enum htk_feature_kind_e {
519 } htk_feature_kind_t;
521 typedef enum htk_feature_flag_e {
532 } htk_feature_flag_t;
549 if (swap) SWAP_INT32(&nfloat);
550 if (fwrite(&nfloat, 4, 1, wtf->
outfh) != 1)
553 samp_period = (int32)(1e+7 / cmd_ln_float32_r(wtf->
config,
"-frate"));
554 if (swap) SWAP_INT32(&samp_period);
555 if (fwrite(&samp_period, 4, 1, wtf->
outfh) != 1)
558 samp_size = wtf->
veclen * 4;
559 if (swap) SWAP_INT16(&samp_size);
560 if (fwrite(&samp_size, 2, 1, wtf->
outfh) != 1)
567 param_kind = MFCC | _O;
568 if (swap) SWAP_INT16(¶m_kind);
569 if (fwrite(¶m_kind, 2, 1, wtf->
outfh) != 1)
581 int i, j, swap, htk_reorder, nfloat = 0;
583 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
586 htk_reorder = (0 == strcmp(
"htk", wtf->
ot->name)
589 for (i = 0; i < nfr; ++i) {
591 mfcc_t c0 = frames[i][0];
592 memmove(frames[i] + 1, frames[i], (wtf->
veclen - 1) * 4);
593 frames[i][wtf->
veclen - 1] = c0;
596 for (j = 0; j < wtf->
veclen; ++j)
597 SWAP_FLOAT32(frames[i] + j);
598 if (fwrite(frames[i],
sizeof(float32), wtf->
veclen, wtf->
outfh) != wtf->
veclen) {
614 int i, j, nfloat = 0;
616 fe_mfcc_to_float(wtf->
fe, frames, (float32 **)frames, nfr);
617 for (i = 0; i < nfr; ++i) {
618 for (j = 0; j < wtf->
veclen; ++j) {
619 fprintf(wtf->
outfh,
"%.5g", MFCC2FLOAT(frames[i][j]));
621 fprintf(wtf->
outfh,
"\n");
623 fprintf(wtf->
outfh,
" ");
631 {
"sphinx", &output_header_sphinx, &output_frames_sphinx },
632 {
"htk", &output_header_htk, &output_frames_htk },
633 {
"text", NULL, &output_frames_text }
635 static const int nouttypes =
sizeof(outtypes)/
sizeof(outtypes[0]);
638 sphinx_wave2feat_init(
cmd_ln_t *config)
646 wtf->
fe = fe_init_auto_r(wtf->
config);
648 E_FATAL(
"Failed to create feature extraction\n");
652 for (i = 0; i < nouttypes; ++i) {
654 if (0 == strcmp(
cmd_ln_str_r(config,
"-ofmt"), otype->name)) {
659 if (i == nouttypes) {
660 E_ERROR(
"Unknown output type: '%s'\n",
662 sphinx_wave2feat_free(wtf);
686 if (fclose(wtf->
infh) == EOF)
690 if (fclose(wtf->
outfh) == EOF)
716 int rv = mfcc_type.detect(wtf);
723 for (i = 0; i < ntypes; ++i) {
727 rv = (*atype->detect)(wtf);
736 for (i = 0; i < ntypes; ++i) {
739 rv = (*atype->detect)(wtf);
758 char const *infile,
char const *outfile)
760 int nchans, minfft, nfft, nfloat, veclen;
764 E_INFO(
"Converting %s to %s\n", infile, outfile);
769 if ((atype = detect_audio_type(wtf)) == NULL)
777 minfft = (int)(cmd_ln_float32_r(wtf->
config,
"-samprate")
778 * cmd_ln_float32_r(wtf->
config,
"-wlen") + 0.5);
779 for (nfft = 1; nfft < minfft; nfft <<= 1)
781 if (nfft > cmd_ln_int32_r(wtf->
config,
"-nfft")) {
782 E_WARN(
"Value of -nfft = %d is too small, increasing to %d\n",
783 cmd_ln_int32_r(wtf->
config,
"-nfft"), nfft);
784 cmd_ln_set_int32_r(wtf->
config,
"-nfft", nfft);
786 wtf->
fe = fe_init_auto_r(wtf->
config);
791 wtf->
veclen = fe_get_output_size(wtf->
fe);
794 fe_get_input_size(wtf->
fe, &fshift, &fsize);
798 nchans = cmd_ln_int32_r(wtf->
config,
"-nchans");
800 if (wtf->
blocksize < (fsize + fshift) * nchans) {
801 E_INFO(
"Block size of %d too small, increasing to %d\n",
803 (fsize + fshift) * nchans);
804 wtf->
blocksize = (fsize + fshift) * nchans;
816 if ((wtf->
outfh = fopen(outfile,
"wb")) == NULL) {
821 if (wtf->
ot->output_header &&
822 (*wtf->
ot->output_header)(wtf, 0) < 0) {
828 if ((nfloat = (*atype->decode)(wtf)) < 0) {
833 if (wtf->
ot->output_header) {
834 if (fseek(wtf->
outfh, 0, SEEK_SET) < 0) {
838 if ((*wtf->
ot->output_header)(wtf, nfloat) < 0) {
860 if (fclose(wtf->
outfh) == EOF)
883 if (fclose(wtf->
outfh) == EOF)
891 build_filenames(
cmd_ln_t *config,
char const *basename,
892 char **out_infile,
char **out_outfile)
894 char const *di, *do_, *ei, *eo;
930 int nskip, runlen, npart;
932 if ((ctlfh = fopen(ctlfile,
"r")) == NULL) {
936 nskip = cmd_ln_int32_r(wtf->
config,
"-nskip");
937 runlen = cmd_ln_int32_r(wtf->
config,
"-runlen");
938 if ((npart = cmd_ln_int32_r(wtf->
config,
"-npart"))) {
940 int partlen, part, nlines = 0;
941 part = cmd_ln_int32_r(wtf->
config,
"-part");
944 fseek(ctlfh, 0, SEEK_SET);
945 partlen = nlines / npart;
946 nskip = partlen * (part - 1);
953 E_INFO(
"Processing %d utterances at position %d\n", runlen, nskip);
957 E_INFO(
"Processing all remaining utterances at position %d\n", nskip);
961 char *c, *infile, *outfile;
973 if ((c = strchr(li->buf,
' ')) != NULL)
975 if (strlen(li->buf) == 0) {
976 E_WARN(
"Empty line %d in control file, skipping\n", li->lineno);
979 build_filenames(wtf->
config, li->buf, &infile, &outfile);
982 sphinx_wave2feat_convert_file(wtf, infile, outfile);
997 main(
int argc,
char *argv[])
1008 if (config == NULL) {
1009 E_ERROR(
"Command line parsing failed\n");
1013 if ((wtf = sphinx_wave2feat_init(config)) == NULL) {
1014 E_ERROR(
"Failed to initialize wave2feat object\n");
1022 rv = run_control_file(wtf,
cmd_ln_str_r(config,
"-c"));
1024 rv = sphinx_wave2feat_convert_file(wtf,
cmd_ln_str_r(config,
"-i"),
1027 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.