wav-copy.cc
Go to the documentation of this file.
1 // featbin/wav-copy.cc
2 
3 // Copyright 2013-2014 Daniel Povey
4 // 2016 Aalto University (author: Peter Smit)
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/feature-mfcc.h"
24 #include "feat/wave-reader.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  const char *usage =
30  "Copy wave file or archives of wave files\n"
31  "\n"
32  "Usage: wav-copy [options] <wav-rspecifier> <wav-wspecifier>\n"
33  " or: wav-copy [options] <wav-rxfilename> <wav-wxfilename>\n"
34  "e.g. wav-copy scp:wav.scp ark:-\n"
35  " wav-copy wav.ark:123456 -\n"
36  "See also: wav-to-duration extract-segments\n";
37 
38  ParseOptions po(usage);
39 
40  po.Read(argc, argv);
41 
42  if (po.NumArgs() != 2) {
43  po.PrintUsage();
44  exit(1);
45  }
46 
47  std::string wav_in_fn = po.GetArg(1),
48  wav_out_fn = po.GetArg(2);
49 
50  bool in_is_rspecifier = (ClassifyRspecifier(wav_in_fn, NULL, NULL)
51  != kNoRspecifier),
52  out_is_wspecifier = (ClassifyWspecifier(wav_out_fn, NULL, NULL, NULL)
53  != kNoWspecifier);
54 
55  if (in_is_rspecifier != out_is_wspecifier)
56  KALDI_ERR << "Cannot mix archives with regular files";
57 
58  if (in_is_rspecifier) {
59  int32 num_done = 0;
60 
61  SequentialTableReader<WaveHolder> wav_reader(wav_in_fn);
62  TableWriter<WaveHolder> wav_writer(wav_out_fn);
63 
64  for (; !wav_reader.Done(); wav_reader.Next()) {
65  wav_writer.Write(wav_reader.Key(), wav_reader.Value());
66  num_done++;
67  }
68  KALDI_LOG << "Copied " << num_done << " wave files";
69  return (num_done != 0 ? 0 : 1);
70  } else {
71  bool binary = true;
72  Input ki(wav_in_fn, &binary);
73  Output ko(wav_out_fn, binary, false);
74  WaveHolder wh;
75  if (!wh.Read(ki.Stream())) {
76  KALDI_ERR << "Read failure from "
77  << PrintableRxfilename(wav_in_fn);
78  }
79  if (!WaveHolder::Write(ko.Stream(), true, wh.Value())) {
80  KALDI_ERR << "Write failure to "
81  << PrintableWxfilename(wav_out_fn);
82  }
83  }
84  } catch(const std::exception &e) {
85  std::cerr << e.what();
86  return -1;
87  }
88 }
89 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Read(std::istream &is)
Definition: wave-reader.h:191
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
int main(int argc, char *argv[])
Definition: wav-copy.cc:26
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
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
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
int NumArgs() const
Number of positional parameters (c.f. argc-1).
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73
#define KALDI_LOG
Definition: kaldi-error.h:153
static bool Write(std::ostream &os, bool binary, const T &t)
Definition: wave-reader.h:162