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

References VectorBase< Real >::AddRowSumMat(), VectorBase< Real >::AddVec(), OfflineFeatureTpl< F >::ComputeFeatures(), WaveData::Data(), SequentialTableReader< Holder >::Done(), WaveData::Duration(), SpectrogramOptions::frame_opts, FrameExtractionOptions::frame_shift_ms, ParseOptions::GetArg(), 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(), SpectrogramOptions::Register(), ParseOptions::Register(), WaveData::SampFreq(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29  const char *usage =
30  "Create spectrogram feature files.\n"
31  "Usage: compute-spectrogram-feats [options...] <wav-rspecifier> <feats-wspecifier>\n";
32 
33  // construct all the global objects
34  ParseOptions po(usage);
35  SpectrogramOptions spec_opts;
36  bool subtract_mean = false;
37  int32 channel = -1;
38  BaseFloat min_duration = 0.0;
39  // Define defaults for gobal options
40  std::string output_format = "kaldi";
41 
42  // Register the option struct
43  spec_opts.Register(&po);
44  // Register the options
45  po.Register("output-format", &output_format, "Format of the output files [kaldi, htk]");
46  po.Register("subtract-mean", &subtract_mean, "Subtract mean of each feature file [CMS]; not recommended to do it this way. ");
47  po.Register("channel", &channel, "Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right)");
48  po.Register("min-duration", &min_duration, "Minimum duration of segments to process (in seconds).");
49 
50  // OPTION PARSING ..........................................................
51  //
52 
53  // parse options (+filling the registered variables)
54  po.Read(argc, argv);
55 
56  if (po.NumArgs() != 2) {
57  po.PrintUsage();
58  exit(1);
59  }
60 
61  std::string wav_rspecifier = po.GetArg(1);
62 
63  std::string output_wspecifier = po.GetArg(2);
64 
65  Spectrogram spec(spec_opts);
66 
67  SequentialTableReader<WaveHolder> reader(wav_rspecifier);
68  BaseFloatMatrixWriter kaldi_writer; // typedef to TableWriter<something>.
70 
71  if (output_format == "kaldi") {
72  if (!kaldi_writer.Open(output_wspecifier))
73  KALDI_ERR << "Could not initialize output with wspecifier "
74  << output_wspecifier;
75  } else if (output_format == "htk") {
76  if (!htk_writer.Open(output_wspecifier))
77  KALDI_ERR << "Could not initialize output with wspecifier "
78  << output_wspecifier;
79  } else {
80  KALDI_ERR << "Invalid output_format string " << output_format;
81  }
82 
83  int32 num_utts = 0, num_success = 0;
84  for (; !reader.Done(); reader.Next()) {
85  num_utts++;
86  std::string utt = reader.Key();
87  const WaveData &wave_data = reader.Value();
88  if (wave_data.Duration() < min_duration) {
89  KALDI_WARN << "File: " << utt << " is too short ("
90  << wave_data.Duration() << " sec): producing no output.";
91  continue;
92  }
93  int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
94  { // This block works out the channel (0=left, 1=right...)
95  KALDI_ASSERT(num_chan > 0); // should have been caught in
96  // reading code if no channels.
97  if (channel == -1) {
98  this_chan = 0;
99  if (num_chan != 1)
100  KALDI_WARN << "Channel not specified but you have data with "
101  << num_chan << " channels; defaulting to zero";
102  } else {
103  if (this_chan >= num_chan) {
104  KALDI_WARN << "File with id " << utt << " has "
105  << num_chan << " channels but you specified channel "
106  << channel << ", producing no output.";
107  continue;
108  }
109  }
110  }
111 
112  SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
113  Matrix<BaseFloat> features;
114  try {
115  spec.ComputeFeatures(waveform, wave_data.SampFreq(), 1.0, &features);
116  } catch (...) {
117  KALDI_WARN << "Failed to compute features for utterance "
118  << utt;
119  continue;
120  }
121  if (subtract_mean) {
122  Vector<BaseFloat> mean(features.NumCols());
123  mean.AddRowSumMat(1.0, features);
124  mean.Scale(1.0 / features.NumRows());
125  for (int32 i = 0; i < features.NumRows(); i++)
126  features.Row(i).AddVec(-1.0, mean);
127  }
128  if (output_format == "kaldi") {
129  kaldi_writer.Write(utt, features);
130  } else {
131  std::pair<Matrix<BaseFloat>, HtkHeader> p;
132  p.first.Resize(features.NumRows(), features.NumCols());
133  p.first.CopyFromMat(features);
134  int32 frame_shift = spec_opts.frame_opts.frame_shift_ms * 10000;
135  HtkHeader header = {
136  features.NumRows(),
137  frame_shift,
138  static_cast<int16>(sizeof(float)*features.NumCols()),
139  007 | 020000
140  };
141  p.second = header;
142  htk_writer.Write(utt, p);
143  }
144  if(num_utts % 10 == 0)
145  KALDI_LOG << "Processed " << num_utts << " utterances";
146  KALDI_VLOG(2) << "Processed features for key " << utt;
147  num_success++;
148  }
149  KALDI_LOG << " Done " << num_success << " out of " << num_utts
150  << " utterances.";
151  return (num_success != 0 ? 0 : 1);
152  } catch(const std::exception& e) {
153  std::cerr << e.what();
154  return -1;
155  }
156  return 0;
157 }
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void Register(OptionsItf *opts)
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
BaseFloat SampFreq() const
Definition: wave-reader.h:126
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:106
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:129
This templated class is intended for offline feature extraction, i.e.
SpectrogramOptions contains basic options for computing spectrogram features.
#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
FrameExtractionOptions frame_opts