// 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] ( | )\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, extract-rows,\n" " subset-feats, subsample-feats, splice-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 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 > 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 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 > 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 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; } }