All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
compute-fbank-feats.cc File Reference
Include dependency graph for compute-fbank-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 27 of file compute-fbank-feats.cc.

References VectorBase< Real >::AddRowSumMat(), VectorBase< Real >::AddVec(), OfflineFeatureTpl< F >::Compute(), WaveData::Data(), SequentialTableReader< Holder >::Done(), WaveData::Duration(), FbankOptions::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(), FbankOptions::Register(), ParseOptions::Register(), FrameExtractionOptions::samp_freq, WaveData::SampFreq(), FbankOptions::use_energy, SequentialTableReader< Holder >::Value(), RandomAccessTableReaderMapped< Holder >::Value(), and TableWriter< Holder >::Write().

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