annotate src/Modules/NAP/ModuleHCL.cc @ 45:c5f5e9569863

- Modified licence from GPL 3 to Apache v2
author tomwalters
date Tue, 30 Mar 2010 22:06:24 +0000
parents 491b1b1d1dc5
children
rev   line source
tomwalters@0 1 // Copyright 2007-2010, Thomas Walters
tomwalters@0 2 //
tomwalters@0 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@0 4 // http://www.acousticscale.org/AIMC
tomwalters@0 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@0 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@0 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@0 17
tomwalters@0 18 /*!
tomwalters@0 19 * \file
tomwalters@0 20 * \brief Halfwave rectification, compression and lowpass filtering.
tomwalters@0 21 *
tomwalters@11 22 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@0 23 * \date created 2007/03/07
tomwalters@23 24 * \version \$Id$
tomwalters@0 25 */
tomwalters@0 26
tomwalters@15 27 #include <cmath>
tomwalters@0 28
tomwalters@0 29 #include "Modules/NAP/ModuleHCL.h"
tomwalters@0 30
tomwalters@0 31 namespace aimc {
tomwalters@0 32 ModuleHCL::ModuleHCL(Parameters *parameters) : Module(parameters) {
tomwalters@0 33 module_identifier_ = "hcl";
tomwalters@0 34 module_type_ = "nap";
tomwalters@0 35 module_description_ = "Halfwave rectification, compression "
tomwalters@0 36 "and lowpass filtering";
tomwalters@23 37 module_version_ = "$Id$";
tomwalters@0 38
tomwalters@18 39 do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", true);
tomwalters@1 40 do_log_ = parameters_->DefaultBool("nap.do_log_compression", false);
tomwalters@1 41 lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0);
tomwalters@1 42 lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2);
tomwalters@0 43 }
tomwalters@0 44
tomwalters@0 45 ModuleHCL::~ModuleHCL() {
tomwalters@0 46 }
tomwalters@0 47
tomwalters@0 48 bool ModuleHCL::InitializeInternal(const SignalBank &input) {
tomwalters@0 49 time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_);
tomwalters@0 50 channel_count_ = input.channel_count();
tomwalters@0 51 output_.Initialize(input);
tomwalters@3 52 ResetInternal();
tomwalters@0 53 return true;
tomwalters@0 54 }
tomwalters@0 55
tomwalters@3 56 void ModuleHCL::ResetInternal() {
tomwalters@0 57 xn_ = 0.0f;
tomwalters@0 58 yn_ = 0.0f;
tomwalters@0 59 yns_.clear();
tomwalters@0 60 yns_.resize(channel_count_);
tomwalters@0 61 for (int c = 0; c < channel_count_; ++c) {
tomwalters@0 62 yns_[c].resize(lowpass_order_, 0.0f);
tomwalters@0 63 }
tomwalters@0 64 }
tomwalters@0 65
tomwalters@0 66 /* With do_log, the signal is first scaled up so that values <1.0 become
tomwalters@0 67 * negligible. This just rescales the sample values to fill the range of a
tomwalters@0 68 * 16-bit signed integer, then we lose the bottom bit of resolution. If the
tomwalters@0 69 * signal was sampled at 16-bit resolution, there shouldn't be anything to
tomwalters@0 70 * speak of there anyway. If it was sampled using a higher resolution, then
tomwalters@0 71 * some data will be discarded.
tomwalters@0 72 */
tomwalters@0 73 void ModuleHCL::Process(const SignalBank &input) {
tomwalters@0 74 output_.set_start_time(input.start_time());
tomwalters@0 75 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@0 76 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@0 77 if (input[c][i] < 0.0f) {
tomwalters@0 78 output_.set_sample(c, i, 0.0f);
tomwalters@0 79 } else {
tomwalters@0 80 float s = input[c][i];
tomwalters@0 81 if (do_log_) {
tomwalters@8 82 s *= pow(2.0f, 15);
tomwalters@0 83 if (s < 1.0f) s = 1.0f;
tomwalters@0 84 s = 20.0f * log10(s);
tomwalters@0 85 }
tomwalters@0 86 output_.set_sample(c, i, s);
tomwalters@0 87 }
tomwalters@0 88 }
tomwalters@0 89 if (do_lowpass_) {
tomwalters@8 90 float b = exp(-1.0f / (input.sample_rate() * time_constant_));
tomwalters@0 91 float gain = 1.0f / (1.0f - b);
tomwalters@0 92 for (int j = 0; j < lowpass_order_; j++) {
tomwalters@0 93 for (int k = 0; k < output_.buffer_length(); ++k) {
tomwalters@0 94 xn_ = output_[c][k];
tomwalters@0 95 yn_ = xn_ + b * yns_[c][j];
tomwalters@0 96 yns_[c][j] = yn_;
tomwalters@0 97 output_.set_sample(c, k, yn_ / gain);
tomwalters@0 98 }
tomwalters@0 99 }
tomwalters@0 100 }
tomwalters@0 101 }
tomwalters@0 102 PushOutput();
tomwalters@0 103 }
tomwalters@0 104 } // namespace aimc