forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-feats.cc
More file actions
128 lines (112 loc) · 5.17 KB
/
copy-feats.cc
File metadata and controls
128 lines (112 loc) · 5.17 KB
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
121
122
123
124
125
126
// featbin/copy-feats.cc
// Copyright 2009-2011 Microsoft Corporation
// 2013 Johns Hopkins University (author: 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"
int main(int argc, char *argv[]) {
try {
using namespace kaldi;
const char *usage =
"Copy features [and possibly change format]\n"
"Usage: copy-feats [options] <feature-rspecifier> <feature-wspecifier>\n"
"or: copy-feats [options] <feats-rxfilename> <feats-wxfilename>\n"
"e.g.: copy-feats ark:- ark,scp:foo.ark,foo.scp\n"
" or: copy-feats ark:foo.ark ark,t:txt.ark\n"
"See also: copy-matrix, copy-feats-to-htk, copy-feats-to-sphinx, select-feats,\n"
"extract-rows, subset-feats, subsample-feats, splice-feats, append-feats\n";
ParseOptions po(usage);
bool binary = true;
bool htk_in = false;
bool sphinx_in = false;
bool compress = false;
po.Register("htk-in", &htk_in, "Read input as HTK features");
po.Register("sphinx-in", &sphinx_in, "Read input as Sphinx features");
po.Register("binary", &binary, "Binary-mode output (not relevant if writing "
"to archive)");
po.Register("compress", &compress, "If true, write output in compressed form"
"(only currently supported for wxfilename, i.e. archive/script,"
"output)");
po.Read(argc, argv);
if (po.NumArgs() != 2) {
po.PrintUsage();
exit(1);
}
int32 num_done = 0;
if (ClassifyRspecifier(po.GetArg(1), NULL, NULL) != kNoRspecifier) {
// Copying tables of features.
std::string rspecifier = po.GetArg(1);
std::string wspecifier = po.GetArg(2);
if (!compress) {
BaseFloatMatrixWriter kaldi_writer(wspecifier);
if (htk_in) {
SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
for (; !htk_reader.Done(); htk_reader.Next(), num_done++)
kaldi_writer.Write(htk_reader.Key(), htk_reader.Value().first);
} else if (sphinx_in) {
SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++)
kaldi_writer.Write(sphinx_reader.Key(), sphinx_reader.Value());
} else {
SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++)
kaldi_writer.Write(kaldi_reader.Key(), kaldi_reader.Value());
}
} else {
CompressedMatrixWriter kaldi_writer(wspecifier);
if (htk_in) {
SequentialTableReader<HtkMatrixHolder> htk_reader(rspecifier);
for (; !htk_reader.Done(); htk_reader.Next(), num_done++)
kaldi_writer.Write(htk_reader.Key(),
CompressedMatrix(htk_reader.Value().first));
} else if (sphinx_in) {
SequentialTableReader<SphinxMatrixHolder<> > sphinx_reader(rspecifier);
for (; !sphinx_reader.Done(); sphinx_reader.Next(), num_done++)
kaldi_writer.Write(sphinx_reader.Key(),
CompressedMatrix(sphinx_reader.Value()));
} else {
SequentialBaseFloatMatrixReader kaldi_reader(rspecifier);
for (; !kaldi_reader.Done(); kaldi_reader.Next(), num_done++)
kaldi_writer.Write(kaldi_reader.Key(),
CompressedMatrix(kaldi_reader.Value()));
}
}
KALDI_LOG << "Copied " << num_done << " feature matrices.";
return (num_done != 0 ? 0 : 1);
} else {
KALDI_ASSERT(!compress && "Compression not yet supported for single files");
std::string feat_rxfilename = po.GetArg(1), feat_wxfilename = po.GetArg(2);
Matrix<BaseFloat> feat_matrix;
if (htk_in) {
Input ki(feat_rxfilename); // Doesn't look for read binary header \0B, because
// no bool* pointer supplied.
HtkHeader header; // we discard this info.
ReadHtk(ki.Stream(), &feat_matrix, &header);
} else if (sphinx_in) {
KALDI_ERR << "For single files, sphinx input is not yet supported.";
} else {
ReadKaldiObject(feat_rxfilename, &feat_matrix);
}
WriteKaldiObject(feat_matrix, feat_wxfilename, binary);
KALDI_LOG << "Copied features from " << PrintableRxfilename(feat_rxfilename)
<< " to " << PrintableWxfilename(feat_wxfilename);
}
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}