compute-kaldi-pitch-feats.cc
Go to the documentation of this file.
1 // featbin/compute-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, 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 }
118 
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].
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...
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.
#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