From: fujishita t. <fjs...@us...> - 2016-10-26 08:19:01
|
Update of /cvsroot/sp-tk/SPTK4/src In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv13988 Modified Files: Makefile Added Files: all_pole_lattice_digital_filter.cc all_pole_lattice_digital_filter.h ltcdf.cc Log Message: add ltcdf command Index: Makefile =================================================================== RCS file: /cvsroot/sp-tk/SPTK4/src/Makefile,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** Makefile 14 Oct 2016 06:17:44 -0000 1.17 --- Makefile 26 Oct 2016 08:18:58 -0000 1.18 *************** *** 46,49 **** --- 46,50 ---- SOURCES = all_pole_digital_filter.cc \ + all_pole_lattice_digital_filter.cc \ all_zero_digital_filter.cc \ autocorrelation.cc \ --- NEW FILE: all_pole_lattice_digital_filter.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-2016 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 "all_pole_lattice_digital_filter.h" #include <cstring> // std::size_t namespace sptk { bool AllPoleLatticeDigitalFilter::Run( const std::vector<double>& filter_coefficients, double filter_input, double* filter_output, AllPoleLatticeDigitalFilter::StoredSignals* stored_signals) const { // check inputs if (filter_coefficients.size() != static_cast<std::size_t>(num_filter_order_ + 1) || NULL == filter_output || NULL == stored_signals || !is_valid_) { return false; } // prepare memory if (stored_signals->signals_.size() != static_cast<std::size_t>(num_filter_order_)) { stored_signals->signals_.resize(num_filter_order_); std::fill(stored_signals->signals_.begin(), stored_signals->signals_.end(), 0.0); } // set value const double gained_input(filter_input * filter_coefficients[0]); if (0 == num_filter_order_) { *filter_output = gained_input; return true; } // get values const double* coefficients(&(filter_coefficients[0])); double* signals(&stored_signals->signals_[0]); // apply filter double sum(gained_input); sum -= coefficients[num_filter_order_] * signals[num_filter_order_ - 1]; for (int i(num_filter_order_ - 1); 0 < i; --i) { sum -= coefficients[i] * signals[i - 1]; signals[i] = signals[i - 1] + coefficients[i] * sum; } signals[0] = sum; // save result *filter_output = sum; return true; } } // namespace sptk --- NEW FILE: all_pole_lattice_digital_filter.h --- // ----------------------------------------------------------------- // // 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-2016 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. // // ----------------------------------------------------------------- // #ifndef SPTK_SRC_ALL_POLE_LATTICE_DIGITAL_FILTER_H_ #define SPTK_SRC_ALL_POLE_LATTICE_DIGITAL_FILTER_H_ #include <algorithm> // std::fill #include <vector> // std::vector #include "sptk_utils.h" namespace sptk { class AllPoleLatticeDigitalFilter { public: class StoredSignals { public: // StoredSignals() { } // ~StoredSignals() { } // void Clear() { std::fill(signals_.begin(), signals_.end(), 0.0); } private: // std::vector<double> signals_; // friend class AllPoleLatticeDigitalFilter; // DISALLOW_COPY_AND_ASSIGN(StoredSignals); }; // explicit AllPoleLatticeDigitalFilter(int num_filter_order) : num_filter_order_(num_filter_order), is_valid_(true) { if (num_filter_order_ < 0) { is_valid_ = false; } } // virtual ~AllPoleLatticeDigitalFilter() { } // int GetNumFilterOrder() const { return num_filter_order_; } // bool IsValid() const { return is_valid_; } // bool Run(const std::vector<double>& filter_coefficients, double filter_input, double* filter_output, AllPoleLatticeDigitalFilter::StoredSignals* signals) const; private: // const int num_filter_order_; // bool is_valid_; // DISALLOW_COPY_AND_ASSIGN(AllPoleLatticeDigitalFilter); }; } // namespace sptk #endif // SPTK_SRC_ALL_POLE_LATTICE_DIGITAL_FILTER_H_ --- NEW FILE: ltcdf.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-2016 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 <unistd.h> #include <fstream> #include <iostream> #include <sstream> #include <vector> #include "all_pole_lattice_digital_filter.h" #include "input_source_from_stream.h" #include "input_source_interpolation.h" #include "input_source_preprocessing_for_filter_gain.h" #include "sptk_utils.h" namespace { const int kDefaultNumFilterOrder(25); const int kDefaultFramePeriod(100); const int kDefaultInterpolationPeriod(1); const bool kDefaultGainFlag(true); void PrintUsage(std::ostream* stream) { // clang-format off *stream << std::endl; *stream << " ltcdf - all-pole lattice digital filter for speech synthesis" << std::endl; // NOLINT *stream << std::endl; *stream << " usage:" << std::endl; *stream << " ltcdf [ options ] kfile [ infile ] > stdout" << std::endl; *stream << " options:" << std::endl; *stream << " -m m : order of filter coefficients [" << kDefaultNumFilterOrder << "]" << std::endl; // NOLINT *stream << " -p p : frame period [" << kDefaultFramePeriod << "]" << std::endl; // NOLINT *stream << " -i i : interpolation period [" << kDefaultInterpolationPeriod << "]" << std::endl; // NOLINT *stream << " -k : filtering without gain [" << sptk::ConvertBooleanToString(!kDefaultGainFlag) << "]" << std::endl; // NOLINT *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " filter input (double) [stdin]" << std::endl; *stream << " stdout:" << std::endl; *stream << " filter output (double)" << std::endl; *stream << " kfile:" << std::endl; *stream << " filter (PARCOR) coefficients (double)" << 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 num_filter_order(kDefaultNumFilterOrder); int frame_period(kDefaultFramePeriod); int interpolation_period(kDefaultInterpolationPeriod); bool gain_flag(kDefaultGainFlag); for (;;) { const char option_char(getopt(argc, argv, "m:p:i:tkh")); if (-1 == option_char) break; switch (option_char) { case 'm': { if (!sptk::ConvertStringToInteger(optarg, &num_filter_order) || num_filter_order < 0) { std::ostringstream error_message; error_message << "The argument for the -m option must be a " << "non-negative integer"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } break; } case 'p': { if (!sptk::ConvertStringToInteger(optarg, &frame_period) || frame_period <= 0) { std::ostringstream error_message; error_message << "The argument for the -p option must be a positive integer"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } break; } case 'i': { if (!sptk::ConvertStringToInteger(optarg, &interpolation_period) || interpolation_period < 0) { std::ostringstream error_message; error_message << "The argument for the -i option must be a " << "non-negative integer"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } break; } case 'k': { gain_flag = false; break; } case 'h': { PrintUsage(&std::cout); return 0; } default: { PrintUsage(&std::cerr); return 1; } } } if (frame_period / 2 < interpolation_period) { std::ostringstream error_message; error_message << "Interpolation period should not be greater than half frame period"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } // Get input file names. const char* filter_coefficients_file; const char* filter_input_file; const int num_rest_args(argc - optind); if (2 == num_rest_args) { filter_coefficients_file = argv[argc - 2]; filter_input_file = argv[argc - 1]; } else if (1 == num_rest_args) { filter_coefficients_file = argv[argc - 1]; filter_input_file = NULL; } else { std::ostringstream error_message; error_message << "Just two input files, kfile and infile, are required"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } // Open stream for reading filter coeffcients. std::ifstream ifs1; ifs1.open(filter_coefficients_file, std::ios::in | std::ios::binary); if (ifs1.fail()) { std::ostringstream error_message; error_message << "Cannot open file " << filter_coefficients_file; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } std::istream& stream_for_filter_coefficients(ifs1); // Open stream for reading input signals. std::ifstream ifs2; ifs2.open(filter_input_file, std::ios::in | std::ios::binary); if (ifs2.fail() && NULL != filter_input_file) { std::ostringstream error_message; error_message << "Cannot open file " << filter_input_file; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } std::istream& stream_for_filter_input(ifs2.fail() ? std::cin : ifs2); // Prepare variables for filtering. const int filter_length(num_filter_order + 1); std::vector<double> filter_coefficients(filter_length); sptk::InputSourceFromStream input_source(false, filter_length, &stream_for_filter_coefficients); const sptk::InputSourcePreprocessingForFilterGain::FilterGainType gain_type( gain_flag ? sptk::InputSourcePreprocessingForFilterGain::FilterGainType::kLinear : sptk::InputSourcePreprocessingForFilterGain::FilterGainType:: kUnity); sptk::InputSourcePreprocessingForFilterGain preprocessing(gain_type, &input_source); sptk::InputSourceInterpolation interpolation( frame_period, interpolation_period, true, &preprocessing); double filter_input, filter_output; sptk::AllPoleLatticeDigitalFilter filter(num_filter_order); sptk::AllPoleLatticeDigitalFilter::StoredSignals stored_signals; if (!interpolation.IsValid() || !filter.IsValid()) { std::ostringstream error_message; error_message << "Failed to set the conditions for filtering"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } while (sptk::ReadStream(&filter_input, &stream_for_filter_input)) { if (!interpolation.Get(&filter_coefficients)) { std::ostringstream error_message; error_message << "Cannot get filter coefficients"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } if (!filter.Run(filter_coefficients, filter_input, &filter_output, &stored_signals)) { std::ostringstream error_message; error_message << "Failed to apply all-pole lattice digital filter"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } if (!sptk::WriteStream(filter_output, &std::cout)) { std::ostringstream error_message; error_message << "Failed to write a filter output"; sptk::PrintErrorMessage("ltcdf", error_message); return 1; } } return 0; } |