Blame view

src/ivectorbin/select-voiced-frames.cc 3.69 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  // ivectorbin/select-voiced-frames.cc
  
  // Copyright   2013   Daniel Povey
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  
  
  #include "base/kaldi-common.h"
  #include "util/common-utils.h"
  #include "matrix/kaldi-matrix.h"
  #include "feat/feature-functions.h"
  
  
  int main(int argc, char *argv[]) {
    try {
      using namespace kaldi;
      using kaldi::int32;
  
      const char *usage =
          "Select a subset of frames of the input files, based on the output of
  "
          "compute-vad or a similar program (a vector of length num-frames,
  "
          "containing 1.0 for voiced, 0.0 for unvoiced).  Caution: this is
  "
          "mostly useful only in speaker identification applications.
  "
          "Usage: select-voiced-frames [options] <feats-rspecifier> "
          " <vad-rspecifier> <feats-wspecifier>
  "
          "E.g.: select-voiced-frames [options] scp:feats.scp scp:vad.scp ark:-
  ";
  
      ParseOptions po(usage);
      po.Read(argc, argv);
  
      if (po.NumArgs() != 3) {
        po.PrintUsage();
        exit(1);
      }
  
      std::string feat_rspecifier = po.GetArg(1),
          vad_rspecifier = po.GetArg(2),
          feat_wspecifier = po.GetArg(3);
  
      SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
      RandomAccessBaseFloatVectorReader vad_reader(vad_rspecifier);
      BaseFloatMatrixWriter feat_writer(feat_wspecifier);
  
      int32 num_done = 0, num_err = 0;
  
      for (;!feat_reader.Done(); feat_reader.Next()) {
        std::string utt = feat_reader.Key();
        const Matrix<BaseFloat> &feat = feat_reader.Value();
        if (feat.NumRows() == 0) {
          KALDI_WARN << "Empty feature matrix for utterance " << utt;
          num_err++;
          continue;
        }
        if (!vad_reader.HasKey(utt)) {
          KALDI_WARN << "No VAD input found for utterance " << utt;
          num_err++;
          continue;
        }
        const Vector<BaseFloat> &voiced = vad_reader.Value(utt);
  
        if (feat.NumRows() != voiced.Dim()) {
          KALDI_WARN << "Mismatch in number for frames " << feat.NumRows()
                     << " for features and VAD " << voiced.Dim()
                     << ", for utterance " << utt;
          num_err++;
          continue;
        }
        if (voiced.Sum() == 0.0) {
          KALDI_WARN << "No features were judged as voiced for utterance "
                     << utt;
          num_err++;
          continue;
        }
        int32 dim = 0;
        for (int32 i = 0; i < voiced.Dim(); i++)
          if (voiced(i) != 0.0)
            dim++;
        Matrix<BaseFloat> voiced_feat(dim, feat.NumCols());
        int32 index = 0;
        for (int32 i = 0; i < feat.NumRows(); i++) {
          if (voiced(i) != 0.0) {
            KALDI_ASSERT(voiced(i) == 1.0); // should be zero or one.
            voiced_feat.Row(index).CopyFromVec(feat.Row(i));
            index++;
          }
        }
        KALDI_ASSERT(index == dim);
        feat_writer.Write(utt, voiced_feat);
        num_done++;
      }
  
      KALDI_LOG << "Done selecting voiced frames; processed "
                << num_done << " utterances, "
                << num_err << " had errors.";
      return (num_done != 0 ? 0 : 1);
    } catch(const std::exception &e) {
      std::cerr << e.what();
      return -1;
    }
  }