compute-kaldi-pitch-feats.cc File Reference
Include dependency graph for compute-kaldi-pitch-feats.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 27 of file compute-kaldi-pitch-feats.cc.

References kaldi::ComputeKaldiPitch(), WaveData::Data(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), PitchExtractionOptions::Register(), PitchExtractionOptions::samp_freq, WaveData::SampFreq(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  const char *usage =
31  "Apply Kaldi pitch extractor, starting from wav input. Output is 2-dimensional\n"
32  "features consisting of (NCCF, pitch in Hz), where NCCF is between -1 and 1, and\n"
33  "higher for voiced frames. You will typically pipe this into\n"
34  "process-kaldi-pitch-feats.\n"
35  "Usage: compute-kaldi-pitch-feats [options...] <wav-rspecifier> <feats-wspecifier>\n"
36  "e.g.\n"
37  "compute-kaldi-pitch-feats --sample-frequency=8000 scp:wav.scp ark:- \n"
38  "\n"
39  "See also: process-kaldi-pitch-feats, compute-and-process-kaldi-pitch-feats\n";
40 
41  ParseOptions po(usage);
42  PitchExtractionOptions pitch_opts;
43  int32 channel = -1; // Note: this isn't configurable because it's not a very
44  // good idea to control it this way: better to extract the
45  // on the command line (in the .scp file) using sox or
46  // similar.
47 
48  pitch_opts.Register(&po);
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 2) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string wav_rspecifier = po.GetArg(1),
58  feat_wspecifier = po.GetArg(2);
59 
60  SequentialTableReader<WaveHolder> wav_reader(wav_rspecifier);
61  BaseFloatMatrixWriter feat_writer(feat_wspecifier);
62 
63  int32 num_done = 0, num_err = 0;
64  for (; !wav_reader.Done(); wav_reader.Next()) {
65  std::string utt = wav_reader.Key();
66  const WaveData &wave_data = wav_reader.Value();
67 
68  int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
69  {
70  KALDI_ASSERT(num_chan > 0);
71  // reading code if no channels.
72  if (channel == -1) {
73  this_chan = 0;
74  if (num_chan != 1)
75  KALDI_WARN << "Channel not specified but you have data with "
76  << num_chan << " channels; defaulting to zero";
77  } else {
78  if (this_chan >= num_chan) {
79  KALDI_WARN << "File with id " << utt << " has "
80  << num_chan << " channels but you specified channel "
81  << channel << ", producing no output.";
82  continue;
83  }
84  }
85  }
86 
87  if (pitch_opts.samp_freq != wave_data.SampFreq())
88  KALDI_ERR << "Sample frequency mismatch: you specified "
89  << pitch_opts.samp_freq << " but data has "
90  << wave_data.SampFreq() << " (use --sample-frequency "
91  << "option). Utterance is " << utt;
92 
93 
94  SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
95  Matrix<BaseFloat> features;
96  try {
97  ComputeKaldiPitch(pitch_opts, waveform, &features);
98  } catch (...) {
99  KALDI_WARN << "Failed to compute pitch for utterance "
100  << utt;
101  num_err++;
102  continue;
103  }
104 
105  feat_writer.Write(utt, features);
106  if (num_done % 50 == 0 && num_done != 0)
107  KALDI_VLOG(2) << "Processed " << num_done << " utterances";
108  num_done++;
109  }
110  KALDI_LOG << "Done " << num_done << " utterances, " << num_err
111  << " with errors.";
112  return (num_done != 0 ? 0 : 1);
113  } catch(const std::exception &e) {
114  std::cerr << e.what();
115  return -1;
116  }
117 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ComputeKaldiPitch(const PitchExtractionOptions &opts, const VectorBase< BaseFloat > &wave, Matrix< BaseFloat > *output)
This function extracts (pitch, NCCF) per frame, using the pitch extraction method described in "A Pit...
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
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:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void Register(OptionsItf *opts)
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#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