compute-and-process-kaldi-pitch-feats.cc
Go to the documentation of this file.
1 // featbin/compute-and-process-kaldi-pitch-feats.cc
2 
3 // Copyright 2013 Pegah Ghahremani
4 // 2013-2014 Johns Hopkins University (author: Daniel Povey)
5 //
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "feat/pitch-functions.h"
24 #include "feat/wave-reader.h"
25 
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  const char *usage =
31  "Apply Kaldi pitch extractor and pitch post-processor, starting from wav input.\n"
32  "Equivalent to compute-kaldi-pitch-feats | process-kaldi-pitch-feats, except\n"
33  "that it is able to simulate online pitch extraction; see options like\n"
34  "--frames-per-chunk, --simulate-first-pass-online, --recompute-frame.\n"
35  "\n"
36  "Usage: compute-and-process-kaldi-pitch-feats [options...] <wav-rspecifier> <feats-wspecifier>\n"
37  "e.g.\n"
38  "compute-and-process-kaldi-pitch-feats --simulate-first-pass-online=true \\\n"
39  " --frames-per-chunk=10 --sample-frequency=8000 scp:wav.scp ark:- \n"
40  "See also: compute-kaldi-pitch-feats, process-kaldi-pitch-feats\n";
41 
42  ParseOptions po(usage);
43  PitchExtractionOptions pitch_opts;
44  ProcessPitchOptions process_opts;
45 
46  int32 channel = -1; // Note: this isn't configurable because it's not a very
47  // good idea to control it this way: better to extract the
48  // on the command line (in the .scp file) using sox or
49  // similar.
50 
51  pitch_opts.Register(&po);
52  process_opts.Register(&po);
53 
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  feat_wspecifier = po.GetArg(2);
63 
64  SequentialTableReader<WaveHolder> wav_reader(wav_rspecifier);
65  BaseFloatMatrixWriter feat_writer(feat_wspecifier);
66 
67  int32 num_done = 0, num_err = 0;
68  for (; !wav_reader.Done(); wav_reader.Next()) {
69  std::string utt = wav_reader.Key();
70  const WaveData &wave_data = wav_reader.Value();
71 
72  int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
73  {
74  KALDI_ASSERT(num_chan > 0);
75  // reading code if no channels.
76  if (channel == -1) {
77  this_chan = 0;
78  if (num_chan != 1)
79  KALDI_WARN << "Channel not specified but you have data with "
80  << num_chan << " channels; defaulting to zero";
81  } else {
82  if (this_chan >= num_chan) {
83  KALDI_WARN << "File with id " << utt << " has "
84  << num_chan << " channels but you specified channel "
85  << channel << ", producing no output.";
86  continue;
87  }
88  }
89  }
90 
91  if (pitch_opts.samp_freq != wave_data.SampFreq())
92  KALDI_ERR << "Sample frequency mismatch: you specified "
93  << pitch_opts.samp_freq << " but data has "
94  << wave_data.SampFreq() << " (use --sample-frequency option)";
95 
96 
97  SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
98  Matrix<BaseFloat> features;
99  try {
100  ComputeAndProcessKaldiPitch(pitch_opts, process_opts,
101  waveform, &features);
102  } catch (...) {
103  KALDI_WARN << "Failed to compute pitch for utterance "
104  << utt;
105  num_err++;
106  continue;
107  }
108 
109  feat_writer.Write(utt, features);
110  if (num_done % 50 == 0 && num_done != 0)
111  KALDI_VLOG(2) << "Processed " << num_done << " utterances";
112  num_done++;
113  }
114  KALDI_LOG << "Done " << num_done << " utterances, " << num_err
115  << " with errors.";
116  return (num_done != 0 ? 0 : 1);
117  } catch(const std::exception &e) {
118  std::cerr << e.what();
119  return -1;
120  }
121 }
122 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
BaseFloat SampFreq() const
Definition: wave-reader.h:126
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void Write(const std::string &key, const T &value) const
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
void ComputeAndProcessKaldiPitch(const PitchExtractionOptions &pitch_opts, const ProcessPitchOptions &process_opts, const VectorBase< BaseFloat > &wave, Matrix< BaseFloat > *output)
This function combines ComputeKaldiPitch and ProcessPitch.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
void Register(OptionsItf *opts)
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
void Register(ParseOptions *opts)