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@268: // This program is free software: you can redistribute it and/or modify
tomwalters@268: // it under the terms of the GNU General Public License as published by
tomwalters@268: // the Free Software Foundation, either version 3 of the License, or
tomwalters@268: // (at your option) any later version.
tomwalters@268: //
tomwalters@268: // This program is distributed in the hope that it will be useful,
tomwalters@268: // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@268: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@268: // GNU General Public License for more details.
tomwalters@268: //
tomwalters@268: // You should have received a copy of the GNU General Public License
tomwalters@268: // along with this program. If not, see .
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