From: Takenori Y. <tak...@us...> - 2017-05-12 08:29:31
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv14231 Added Files: decimate.cc Log Message: add decimate command --- NEW FILE: decimate.cc --- // ----------------------------------------------------------------- // // The Speech Signal Processing Toolkit (SPTK) // // developed by SPTK Working Group // // http://sp-tk.sourceforge.net/ // // ----------------------------------------------------------------- // // // // Copyright (c) 1984-2007 Tokyo Institute of Technology // // Interdisciplinary Graduate School of // // Science and Engineering // // // // 1996-2017 Nagoya Institute of Technology // // Department of Computer Science // // // // All rights reserved. // // // // Redistribution and use in source and binary forms, with or // // without modification, are permitted provided that the following // // conditions are met: // // // // - Redistributions of source code must retain the above copyright // // notice, this list of conditions and the following disclaimer. // // - Redistributions in binary form must reproduce the above // // copyright notice, this list of conditions and the following // // disclaimer in the documentation and/or other materials provided // // with the distribution. // // - Neither the name of the SPTK working group nor the names of its // // contributors may be used to endorse or promote products derived // // from this software without specific prior written permission. // // // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND // // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS // // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED // // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON // // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY // // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // // POSSIBILITY OF SUCH DAMAGE. // // ----------------------------------------------------------------- // #include <getopt.h> #include <fstream> #include <iostream> #include <sstream> #include <vector> #include "sptk_utils.h" namespace { const int kDefaultStartIndex(0); const int kDefaultVectorLength(1); const int kDefaultDecimationPeriod(10); void PrintUsage(std::ostream* stream) { // clang-format off *stream << std::endl; *stream << " decimate - data decimation" << std::endl; *stream << std::endl; *stream << " usage:" << std::endl; *stream << " decimate [ options ] [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -s s : start index [" << kDefaultStartIndex << "]" << std::endl; // NOLINT *stream << " -l l : length of vector [" << kDefaultVectorLength << "]" << std::endl; // NOLINT *stream << " -p p : decimation period [" << kDefaultDecimationPeriod << "]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " data sequence [stdin]" << std::endl; *stream << " stdout:" << std::endl; *stream << " decimated data sequence" << std::endl; *stream << std::endl; *stream << " SPTK: version " << sptk::kVersion << std::endl; *stream << std::endl; // clang-format on } } // namespace int main(int argc, char* argv[]) { int start_index(kDefaultStartIndex); int vector_length(kDefaultVectorLength); int decimation_period(kDefaultDecimationPeriod); for (;;) { const int option_char(getopt_long(argc, argv, "s:l:p:h", NULL, NULL)); if (-1 == option_char) break; switch (option_char) { case 's': { if (!sptk::ConvertStringToInteger(optarg, &start_index) || start_index < 0) { std::ostringstream error_message; error_message << "The argument for the -s option must be a " << "non-negative integer"; sptk::PrintErrorMessage("decimate", error_message); return 1; } break; } case 'l': { if (!sptk::ConvertStringToInteger(optarg, &vector_length) || vector_length <= 0) { std::ostringstream error_message; error_message << "The argument for the -l option must be a positive integer"; sptk::PrintErrorMessage("decimate", error_message); return 1; } break; } case 'p': { if (!sptk::ConvertStringToInteger(optarg, &decimation_period) || decimation_period <= 0) { std::ostringstream error_message; error_message << "The argument for the -p option must be a positive integer"; sptk::PrintErrorMessage("decimate", error_message); return 1; } break; } case 'h': { PrintUsage(&std::cout); return 0; } default: { PrintUsage(&std::cerr); return 1; } } } // get input file const char* input_file((optind < argc) ? argv[argc - 1] : NULL); // open stream std::ifstream ifs; ifs.open(input_file, std::ios::in | std::ios::binary); if (ifs.fail() && NULL != input_file) { std::ostringstream error_message; error_message << "Cannot open file " << input_file; sptk::PrintErrorMessage("decimate", error_message); return 1; } std::istream& input_stream(ifs.fail() ? std::cin : ifs); std::vector<double> input_data(vector_length); // skip data for (int frame_index(0); frame_index < start_index; ++frame_index) { if (!sptk::ReadStream(false, 0, 0, vector_length, &input_data, &input_stream)) { std::ostringstream error_message; error_message << "Start index exceeds data length"; sptk::PrintErrorMessage("decimate", error_message); return 1; } } for (int frame_index(0); sptk::ReadStream(false, 0, 0, vector_length, &input_data, &input_stream); ++frame_index) { if (0 == frame_index % decimation_period) { if (!sptk::WriteStream(0, vector_length, input_data, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write decimated data sequence"; sptk::PrintErrorMessage("decimate", error_message); return 1; } frame_index = 0; } } return 0; } |