All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
compute-mfcc-feats.cc File Reference
Include dependency graph for compute-mfcc-feats.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file compute-mfcc-feats.cc.

References VectorBase< Real >::AddRowSumMat(), VectorBase< Real >::AddVec(), OfflineFeatureTpl< F >::Compute(), WaveData::Data(), SequentialTableReader< Holder >::Done(), WaveData::Duration(), MfccOptions::frame_opts, ParseOptions::GetArg(), RandomAccessTableReaderMapped< Holder >::HasKey(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), ParseOptions::Read(), MfccOptions::Register(), ParseOptions::Register(), FrameExtractionOptions::samp_freq, WaveData::SampFreq(), MfccOptions::use_energy, SequentialTableReader< Holder >::Value(), RandomAccessTableReaderMapped< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  const char *usage =
30  "Create MFCC feature files.\n"
31  "Usage: compute-mfcc-feats [options...] <wav-rspecifier> <feats-wspecifier>\n";
32 
33  // construct all the global objects
34  ParseOptions po(usage);
35  MfccOptions mfcc_opts;
36  bool subtract_mean = false;
37  BaseFloat vtln_warp = 1.0;
38  std::string vtln_map_rspecifier;
39  std::string utt2spk_rspecifier;
40  int32 channel = -1;
41  BaseFloat min_duration = 0.0;
42  // Define defaults for gobal options
43  std::string output_format = "kaldi";
44 
45  // Register the MFCC option struct
46  mfcc_opts.Register(&po);
47 
48  // Register the options
49  po.Register("output-format", &output_format, "Format of the output "
50  "files [kaldi, htk]");
51  po.Register("subtract-mean", &subtract_mean, "Subtract mean of each "
52  "feature file [CMS]; not recommended to do it this way. ");
53  po.Register("vtln-warp", &vtln_warp, "Vtln warp factor (only applicable "
54  "if vtln-map not specified)");
55  po.Register("vtln-map", &vtln_map_rspecifier, "Map from utterance or "
56  "speaker-id to vtln warp factor (rspecifier)");
57  po.Register("utt2spk", &utt2spk_rspecifier, "Utterance to speaker-id map "
58  "rspecifier (if doing VTLN and you have warps per speaker)");
59  po.Register("channel", &channel, "Channel to extract (-1 -> expect mono, "
60  "0 -> left, 1 -> right)");
61  po.Register("min-duration", &min_duration, "Minimum duration of segments "
62  "to process (in seconds).");
63 
64  po.Read(argc, argv);
65 
66  if (po.NumArgs() != 2) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  std::string wav_rspecifier = po.GetArg(1);
72 
73  std::string output_wspecifier = po.GetArg(2);
74 
75  Mfcc mfcc(mfcc_opts);
76 
77  SequentialTableReader<WaveHolder> reader(wav_rspecifier);
78  BaseFloatMatrixWriter kaldi_writer; // typedef to TableWriter<something>.
80 
81  if (utt2spk_rspecifier != "")
82  KALDI_ASSERT(vtln_map_rspecifier != "" && "the utt2spk option is only "
83  "needed if the vtln-map option is used.");
84  RandomAccessBaseFloatReaderMapped vtln_map_reader(vtln_map_rspecifier,
85  utt2spk_rspecifier);
86 
87  if (output_format == "kaldi") {
88  if (!kaldi_writer.Open(output_wspecifier))
89  KALDI_ERR << "Could not initialize output with wspecifier "
90  << output_wspecifier;
91  } else if (output_format == "htk") {
92  if (!htk_writer.Open(output_wspecifier))
93  KALDI_ERR << "Could not initialize output with wspecifier "
94  << output_wspecifier;
95  } else {
96  KALDI_ERR << "Invalid output_format string " << output_format;
97  }
98 
99  int32 num_utts = 0, num_success = 0;
100  for (; !reader.Done(); reader.Next()) {
101  num_utts++;
102  std::string utt = reader.Key();
103  const WaveData &wave_data = reader.Value();
104  if (wave_data.Duration() < min_duration) {
105  KALDI_WARN << "File: " << utt << " is too short ("
106  << wave_data.Duration() << " sec): producing no output.";
107  continue;
108  }
109  int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
110  { // This block works out the channel (0=left, 1=right...)
111  KALDI_ASSERT(num_chan > 0); // should have been caught in
112  // reading code if no channels.
113  if (channel == -1) {
114  this_chan = 0;
115  if (num_chan != 1)
116  KALDI_WARN << "Channel not specified but you have data with "
117  << num_chan << " channels; defaulting to zero";
118  } else {
119  if (this_chan >= num_chan) {
120  KALDI_WARN << "File with id " << utt << " has "
121  << num_chan << " channels but you specified channel "
122  << channel << ", producing no output.";
123  continue;
124  }
125  }
126  }
127  BaseFloat vtln_warp_local; // Work out VTLN warp factor.
128  if (vtln_map_rspecifier != "") {
129  if (!vtln_map_reader.HasKey(utt)) {
130  KALDI_WARN << "No vtln-map entry for utterance-id (or speaker-id) "
131  << utt;
132  continue;
133  }
134  vtln_warp_local = vtln_map_reader.Value(utt);
135  } else {
136  vtln_warp_local = vtln_warp;
137  }
138  if (mfcc_opts.frame_opts.samp_freq != wave_data.SampFreq())
139  KALDI_ERR << "Sample frequency mismatch: you specified "
140  << mfcc_opts.frame_opts.samp_freq << " but data has "
141  << wave_data.SampFreq() << " (use --sample-frequency "
142  << "option). Utterance is " << utt;
143 
144  SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
145  Matrix<BaseFloat> features;
146  try {
147  mfcc.Compute(waveform, vtln_warp_local, &features, NULL);
148  } catch (...) {
149  KALDI_WARN << "Failed to compute features for utterance "
150  << utt;
151  continue;
152  }
153  if (subtract_mean) {
154  Vector<BaseFloat> mean(features.NumCols());
155  mean.AddRowSumMat(1.0, features);
156  mean.Scale(1.0 / features.NumRows());
157  for (int32 i = 0; i < features.NumRows(); i++)
158  features.Row(i).AddVec(-1.0, mean);
159  }
160  if (output_format == "kaldi") {
161  kaldi_writer.Write(utt, features);
162  } else {
163  std::pair<Matrix<BaseFloat>, HtkHeader> p;
164  p.first.Resize(features.NumRows(), features.NumCols());
165  p.first.CopyFromMat(features);
166  HtkHeader header = {
167  features.NumRows(),
168  100000, // 10ms shift
169  static_cast<int16>(sizeof(float)*(features.NumCols())),
170  static_cast<uint16>( 006 | // MFCC
171  (mfcc_opts.use_energy ? 0100 : 020000)) // energy; otherwise c0
172  };
173  p.second = header;
174  htk_writer.Write(utt, p);
175  }
176  if (num_utts % 10 == 0)
177  KALDI_LOG << "Processed " << num_utts << " utterances";
178  KALDI_VLOG(2) << "Processed features for key " << utt;
179  num_success++;
180  }
181  KALDI_LOG << " Done " << num_success << " out of " << num_utts
182  << " utterances.";
183  return (num_success != 0 ? 0 : 1);
184  } catch(const std::exception &e) {
185  std::cerr << e.what();
186  return -1;
187  }
188 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Write(const std::string &key, const T &value) const
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:431
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:85
BaseFloat SampFreq() const
Definition: wave-reader.h:87
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
This class's purpose is to read in Wave files.
Definition: wave-reader.h:65
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
void Register(OptionsItf *opts)
Definition: feature-mfcc.h:61
BaseFloat Duration() const
Definition: wave-reader.h:90
This templated class is intended for offline feature extraction, i.e.
#define KALDI_LOG
Definition: kaldi-error.h:133
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
A structure containing the HTK header.
Definition: kaldi-matrix.h:875
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:482