All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
wav-to-duration.cc File Reference
Include dependency graph for wav-to-duration.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 25 of file wav-to-duration.cc.

References SequentialTableReader< Holder >::Done(), WaveInfo::Duration(), WaveData::Duration(), ParseOptions::GetArg(), WaveInfo::IsStreamed(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

25  {
26  try {
27  using namespace kaldi;
28  const char *usage =
29  "Read wav files and output an archive consisting of a single float:\n"
30  "the duration of each one in seconds.\n"
31  "Usage: wav-to-duration [options...] <wav-rspecifier> <duration-wspecifier>\n"
32  "E.g.: wav-to-duration scp:wav.scp ark,t:-\n"
33  "See also: wav-copy extract-segments feat-to-len\n"
34  "Currently this program may output a lot of harmless warnings regarding\n"
35  "nonzero exit status of pipes\n";
36 
37  bool read_entire_file = false;
38 
39  ParseOptions po(usage);
40 
41  po.Register("read-entire-file", &read_entire_file, "If true, use regular WaveHolder "
42  "instead of WaveInfoHolder to ensure the returned duration is correct.");
43 
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 2) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  std::string wav_rspecifier = po.GetArg(1),
52  duration_wspecifier = po.GetArg(2);
53 
54 
55  double sum_duration = 0.0,
56  min_duration = std::numeric_limits<BaseFloat>::infinity(),
57  max_duration = 0;
58  int32 num_done = 0;
59 
60  BaseFloatWriter duration_writer(duration_wspecifier);
61  if (read_entire_file) {
62  SequentialTableReader<WaveHolder> wav_reader(wav_rspecifier);
63  for (; !wav_reader.Done(); wav_reader.Next()) {
64  std::string key = wav_reader.Key();
65  const WaveData &wave_data = wav_reader.Value();
66  BaseFloat duration = wave_data.Duration();
67  duration_writer.Write(key, duration);
68 
69  sum_duration += duration;
70  min_duration = std::min<double>(min_duration, duration);
71  max_duration = std::max<double>(max_duration, duration);
72  num_done++;
73  }
74  } else {
75  SequentialTableReader<WaveInfoHolder> wav_reader(wav_rspecifier);
76  for (; !wav_reader.Done(); wav_reader.Next()) {
77  std::string key = wav_reader.Key();
78  const WaveInfo &wave_info = wav_reader.Value();
79  if (wave_info.IsStreamed())
80  KALDI_ERR << "Error: member " << key << " has no duration in header. "
81  << "Check the source, and/or try --read-entire-file.";
82  BaseFloat duration = wave_info.Duration();
83  duration_writer.Write(key, duration);
84 
85  sum_duration += duration;
86  min_duration = std::min<double>(min_duration, duration);
87  max_duration = std::max<double>(max_duration, duration);
88  num_done++;
89  }
90  }
91 
92  KALDI_LOG << "Printed duration for " << num_done << " audio files.";
93  if (num_done > 0) {
94  KALDI_LOG << "Mean duration was " << (sum_duration / num_done)
95  << ", min and max durations were " << min_duration << ", "
96  << max_duration;
97  }
98  return (num_done != 0 ? 0 : 1);
99  } catch(const std::exception &e) {
100  std::cerr << e.what();
101  return -1;
102  }
103 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
BaseFloat Duration() const
Approximate duration, seconds. Invalid if IsStreamed() is true.
Definition: wave-reader.h:80
bool IsStreamed() const
Is stream size unknown? Duration and SampleCount not valid if true.
Definition: wave-reader.h:71
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
float BaseFloat
Definition: kaldi-types.h:29
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:127
This class reads and hold wave file header information.
Definition: wave-reader.h:65
This class's purpose is to read in Wave files.
Definition: wave-reader.h:106
BaseFloat Duration() const
Definition: wave-reader.h:129
#define KALDI_LOG
Definition: kaldi-error.h:133