select-voiced-frames.cc
Go to the documentation of this file.
1 // ivectorbin/select-voiced-frames.cc
2 
3 // Copyright 2013 Daniel Povey
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "matrix/kaldi-matrix.h"
25 #include "feat/feature-functions.h"
26 
27 
28 int main(int argc, char *argv[]) {
29  try {
30  using namespace kaldi;
31  using kaldi::int32;
32 
33  const char *usage =
34  "Select a subset of frames of the input files, based on the output of\n"
35  "compute-vad or a similar program (a vector of length num-frames,\n"
36  "containing 1.0 for voiced, 0.0 for unvoiced). Caution: this is\n"
37  "mostly useful only in speaker identification applications.\n"
38  "Usage: select-voiced-frames [options] <feats-rspecifier> "
39  " <vad-rspecifier> <feats-wspecifier>\n"
40  "E.g.: select-voiced-frames [options] scp:feats.scp scp:vad.scp ark:-\n";
41 
42  ParseOptions po(usage);
43  po.Read(argc, argv);
44 
45  if (po.NumArgs() != 3) {
46  po.PrintUsage();
47  exit(1);
48  }
49 
50  std::string feat_rspecifier = po.GetArg(1),
51  vad_rspecifier = po.GetArg(2),
52  feat_wspecifier = po.GetArg(3);
53 
54  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
55  RandomAccessBaseFloatVectorReader vad_reader(vad_rspecifier);
56  BaseFloatMatrixWriter feat_writer(feat_wspecifier);
57 
58  int32 num_done = 0, num_err = 0;
59 
60  for (;!feat_reader.Done(); feat_reader.Next()) {
61  std::string utt = feat_reader.Key();
62  const Matrix<BaseFloat> &feat = feat_reader.Value();
63  if (feat.NumRows() == 0) {
64  KALDI_WARN << "Empty feature matrix for utterance " << utt;
65  num_err++;
66  continue;
67  }
68  if (!vad_reader.HasKey(utt)) {
69  KALDI_WARN << "No VAD input found for utterance " << utt;
70  num_err++;
71  continue;
72  }
73  const Vector<BaseFloat> &voiced = vad_reader.Value(utt);
74 
75  if (feat.NumRows() != voiced.Dim()) {
76  KALDI_WARN << "Mismatch in number for frames " << feat.NumRows()
77  << " for features and VAD " << voiced.Dim()
78  << ", for utterance " << utt;
79  num_err++;
80  continue;
81  }
82  if (voiced.Sum() == 0.0) {
83  KALDI_WARN << "No features were judged as voiced for utterance "
84  << utt;
85  num_err++;
86  continue;
87  }
88  int32 dim = 0;
89  for (int32 i = 0; i < voiced.Dim(); i++)
90  if (voiced(i) != 0.0)
91  dim++;
92  Matrix<BaseFloat> voiced_feat(dim, feat.NumCols());
93  int32 index = 0;
94  for (int32 i = 0; i < feat.NumRows(); i++) {
95  if (voiced(i) != 0.0) {
96  KALDI_ASSERT(voiced(i) == 1.0); // should be zero or one.
97  voiced_feat.Row(index).CopyFromVec(feat.Row(i));
98  index++;
99  }
100  }
101  KALDI_ASSERT(index == dim);
102  feat_writer.Write(utt, voiced_feat);
103  num_done++;
104  }
105 
106  KALDI_LOG << "Done selecting voiced frames; processed "
107  << num_done << " utterances, "
108  << num_err << " had errors.";
109  return (num_done != 0 ? 0 : 1);
110  } catch(const std::exception &e) {
111  std::cerr << e.what();
112  return -1;
113  }
114 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
const T & Value(const std::string &key)
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_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.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
bool HasKey(const std::string &key)
Real Sum() const
Returns sum of the elements.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#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
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153