tomwalters@0: // Copyright 2007-2010, Thomas Walters tomwalters@0: // tomwalters@0: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@0: // http://www.acousticscale.org/AIMC tomwalters@0: // tomwalters@0: // This program is free software: you can redistribute it and/or modify tomwalters@0: // it under the terms of the GNU General Public License as published by tomwalters@0: // the Free Software Foundation, either version 3 of the License, or tomwalters@0: // (at your option) any later version. tomwalters@0: // tomwalters@0: // This program is distributed in the hope that it will be useful, tomwalters@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of tomwalters@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tomwalters@0: // GNU General Public License for more details. tomwalters@0: // tomwalters@0: // You should have received a copy of the GNU General Public License tomwalters@0: // along with this program. If not, see . tomwalters@0: tomwalters@0: /*! tomwalters@0: * \file tomwalters@0: * \brief Halfwave rectification, compression and lowpass filtering. tomwalters@0: * tomwalters@0: * \author Tom Walters tomwalters@0: * \date created 2007/03/07 tomwalters@0: * \version \$Id: ModuleHCL.cc 4 2010-02-03 18:44:58Z tcw $ tomwalters@0: */ tomwalters@0: tomwalters@0: #include tomwalters@0: tomwalters@0: #include "Modules/NAP/ModuleHCL.h" tomwalters@0: tomwalters@0: namespace aimc { tomwalters@0: ModuleHCL::ModuleHCL(Parameters *parameters) : Module(parameters) { tomwalters@0: module_identifier_ = "hcl"; tomwalters@0: module_type_ = "nap"; tomwalters@0: module_description_ = "Halfwave rectification, compression " tomwalters@0: "and lowpass filtering"; tomwalters@0: module_version_ = "$Id: ModuleHCL.cc 4 2010-02-03 18:44:58Z tcw $"; tomwalters@0: tomwalters@1: do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", false); tomwalters@1: do_log_ = parameters_->DefaultBool("nap.do_log_compression", false); tomwalters@1: lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0); tomwalters@1: lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2); tomwalters@0: } tomwalters@0: tomwalters@0: ModuleHCL::~ModuleHCL() { tomwalters@0: } tomwalters@0: tomwalters@0: bool ModuleHCL::InitializeInternal(const SignalBank &input) { tomwalters@0: time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_); tomwalters@0: channel_count_ = input.channel_count(); tomwalters@0: output_.Initialize(input); tomwalters@3: ResetInternal(); tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@3: void ModuleHCL::ResetInternal() { tomwalters@0: xn_ = 0.0f; tomwalters@0: yn_ = 0.0f; tomwalters@0: yns_.clear(); tomwalters@0: yns_.resize(channel_count_); tomwalters@0: for (int c = 0; c < channel_count_; ++c) { tomwalters@0: yns_[c].resize(lowpass_order_, 0.0f); tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: /* With do_log, the signal is first scaled up so that values <1.0 become tomwalters@0: * negligible. This just rescales the sample values to fill the range of a tomwalters@0: * 16-bit signed integer, then we lose the bottom bit of resolution. If the tomwalters@0: * signal was sampled at 16-bit resolution, there shouldn't be anything to tomwalters@0: * speak of there anyway. If it was sampled using a higher resolution, then tomwalters@0: * some data will be discarded. tomwalters@0: */ tomwalters@0: void ModuleHCL::Process(const SignalBank &input) { tomwalters@0: output_.set_start_time(input.start_time()); tomwalters@0: for (int c = 0; c < input.channel_count(); ++c) { tomwalters@0: for (int i = 0; i < input.buffer_length(); ++i) { tomwalters@0: if (input[c][i] < 0.0f) { tomwalters@0: output_.set_sample(c, i, 0.0f); tomwalters@0: } else { tomwalters@0: float s = input[c][i]; tomwalters@0: if (do_log_) { tomwalters@8: s *= pow(2.0f, 15); tomwalters@0: if (s < 1.0f) s = 1.0f; tomwalters@0: s = 20.0f * log10(s); tomwalters@0: } tomwalters@0: output_.set_sample(c, i, s); tomwalters@0: } tomwalters@0: } tomwalters@0: if (do_lowpass_) { tomwalters@8: float b = exp(-1.0f / (input.sample_rate() * time_constant_)); tomwalters@0: float gain = 1.0f / (1.0f - b); tomwalters@0: for (int j = 0; j < lowpass_order_; j++) { tomwalters@0: for (int k = 0; k < output_.buffer_length(); ++k) { tomwalters@0: xn_ = output_[c][k]; tomwalters@0: yn_ = xn_ + b * yns_[c][j]; tomwalters@0: yns_[c][j] = yn_; tomwalters@0: output_.set_sample(c, k, yn_ / gain); tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@0: PushOutput(); tomwalters@0: } tomwalters@0: } // namespace aimc