tomwalters@268: // Copyright 2007-2010, Thomas Walters tomwalters@268: // tomwalters@268: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@268: // http://www.acousticscale.org/AIMC tomwalters@268: // tomwalters@318: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@318: // you may not use this file except in compliance with the License. tomwalters@318: // You may obtain a copy of the License at tomwalters@268: // tomwalters@318: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@268: // tomwalters@318: // Unless required by applicable law or agreed to in writing, software tomwalters@318: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@318: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@318: // See the License for the specific language governing permissions and tomwalters@318: // limitations under the License. tomwalters@268: tomwalters@268: /*! tomwalters@268: * \file tomwalters@268: * \brief Halfwave rectification, compression and lowpass filtering. tomwalters@268: * tomwalters@283: * \author Thomas Walters tomwalters@268: * \date created 2007/03/07 tomwalters@296: * \version \$Id$ tomwalters@268: */ tomwalters@268: tomwalters@287: #include tomwalters@268: tomwalters@268: #include "Modules/NAP/ModuleHCL.h" tomwalters@268: tomwalters@268: namespace aimc { tomwalters@268: ModuleHCL::ModuleHCL(Parameters *parameters) : Module(parameters) { tomwalters@268: module_identifier_ = "hcl"; tomwalters@268: module_type_ = "nap"; tomwalters@268: module_description_ = "Halfwave rectification, compression " tomwalters@268: "and lowpass filtering"; tomwalters@296: module_version_ = "$Id$"; tomwalters@268: tomwalters@290: do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", true); tomwalters@273: do_log_ = parameters_->DefaultBool("nap.do_log_compression", false); tomwalters@273: lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0); tomwalters@273: lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2); tomwalters@268: } tomwalters@268: tomwalters@268: ModuleHCL::~ModuleHCL() { tomwalters@268: } tomwalters@268: tomwalters@268: bool ModuleHCL::InitializeInternal(const SignalBank &input) { tomwalters@268: time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_); tomwalters@268: channel_count_ = input.channel_count(); tomwalters@268: output_.Initialize(input); tomwalters@275: ResetInternal(); tomwalters@268: return true; tomwalters@268: } tomwalters@268: tomwalters@275: void ModuleHCL::ResetInternal() { tomwalters@268: xn_ = 0.0f; tomwalters@268: yn_ = 0.0f; tomwalters@268: yns_.clear(); tomwalters@268: yns_.resize(channel_count_); tomwalters@268: for (int c = 0; c < channel_count_; ++c) { tomwalters@268: yns_[c].resize(lowpass_order_, 0.0f); tomwalters@268: } tomwalters@268: } tomwalters@268: tomwalters@268: /* With do_log, the signal is first scaled up so that values <1.0 become tomwalters@268: * negligible. This just rescales the sample values to fill the range of a tomwalters@268: * 16-bit signed integer, then we lose the bottom bit of resolution. If the tomwalters@268: * signal was sampled at 16-bit resolution, there shouldn't be anything to tomwalters@268: * speak of there anyway. If it was sampled using a higher resolution, then tomwalters@268: * some data will be discarded. tomwalters@268: */ tomwalters@268: void ModuleHCL::Process(const SignalBank &input) { tomwalters@268: output_.set_start_time(input.start_time()); tomwalters@268: for (int c = 0; c < input.channel_count(); ++c) { tomwalters@268: for (int i = 0; i < input.buffer_length(); ++i) { tomwalters@268: if (input[c][i] < 0.0f) { tomwalters@268: output_.set_sample(c, i, 0.0f); tomwalters@268: } else { tomwalters@268: float s = input[c][i]; tomwalters@268: if (do_log_) { tomwalters@280: s *= pow(2.0f, 15); tomwalters@268: if (s < 1.0f) s = 1.0f; tomwalters@268: s = 20.0f * log10(s); tomwalters@268: } tomwalters@268: output_.set_sample(c, i, s); tomwalters@268: } tomwalters@268: } tomwalters@268: if (do_lowpass_) { tomwalters@280: float b = exp(-1.0f / (input.sample_rate() * time_constant_)); tomwalters@268: float gain = 1.0f / (1.0f - b); tomwalters@268: for (int j = 0; j < lowpass_order_; j++) { tomwalters@268: for (int k = 0; k < output_.buffer_length(); ++k) { tomwalters@268: xn_ = output_[c][k]; tomwalters@268: yn_ = xn_ + b * yns_[c][j]; tomwalters@268: yns_[c][j] = yn_; tomwalters@268: output_.set_sample(c, k, yn_ / gain); tomwalters@268: } tomwalters@268: } tomwalters@268: } tomwalters@268: } tomwalters@268: PushOutput(); tomwalters@268: } tomwalters@268: } // namespace aimc