select-voiced-frames.cc File Reference
Include dependency graph for select-voiced-frames.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 28 of file select-voiced-frames.cc.

References VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_ASSERT, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), MatrixBase< Real >::Row(), VectorBase< Real >::Sum(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
Real Sum() const
Returns sum of the elements.
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
#define KALDI_LOG
Definition: kaldi-error.h:153