tomwalters@32: // Copyright 2010, Thomas Walters tomwalters@32: // tomwalters@32: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@32: // http://www.acousticscale.org/AIMC tomwalters@32: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@32: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@32: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@32: tomwalters@32: /*! tomwalters@32: * \author Thomas Walters tomwalters@32: * \date created 2010/02/24 tomwalters@32: * \version \$Id$ tomwalters@32: */ tomwalters@32: tomwalters@32: // Use the boost RNGs to generate Gaussian noise tomwalters@32: #include tomwalters@32: #include tomwalters@32: tomwalters@32: #include "Modules/SNR/ModuleNoise.h" tomwalters@32: tomwalters@32: namespace aimc { tomwalters@32: ModuleNoise::ModuleNoise(Parameters *params) : tomwalters@32: Module(params), tomwalters@32: gaussian_variate_(boost::mt19937(), tomwalters@32: boost::normal_distribution(0.0f, 1.0f)) { tomwalters@32: module_description_ = "Adds noise to a signal"; tomwalters@32: module_identifier_ = "noise"; tomwalters@32: module_type_ = "snr"; tomwalters@32: module_version_ = "$Id$"; tomwalters@32: tomwalters@84: pink_ = parameters_->DefaultBool("noise.pink", true); tomwalters@32: // Noise level relative to unit-variance Gaussian noise (ie. 0dB will give a tomwalters@32: // noise with an RMS level of 1.0) tomwalters@32: float snr_db = parameters_->DefaultFloat("noise.level_db", 0.0f); tomwalters@32: multiplier_ = pow(10.0f, snr_db / 20.0f); tomwalters@32: } tomwalters@32: tomwalters@32: ModuleNoise::~ModuleNoise() { tomwalters@32: } tomwalters@32: tomwalters@32: bool ModuleNoise::InitializeInternal(const SignalBank &input) { tomwalters@32: // Copy the parameters of the input signal bank into internal variables, so tomwalters@32: // that they can be checked later. tomwalters@32: sample_rate_ = input.sample_rate(); tomwalters@32: buffer_length_ = input.buffer_length(); tomwalters@32: channel_count_ = input.channel_count(); tomwalters@32: tomwalters@32: output_.Initialize(input); tomwalters@84: ResetInternal(); tomwalters@32: return true; tomwalters@32: } tomwalters@32: tomwalters@32: void ModuleNoise::ResetInternal() { tomwalters@84: s0_ = 0.0f; tomwalters@84: s1_ = 0.0f; tomwalters@84: s2_ = 0.0f; tomwalters@32: } tomwalters@32: tomwalters@32: void ModuleNoise::Process(const SignalBank &input) { tomwalters@32: // Check to see if the module has been initialized. If not, processing tomwalters@32: // should not continue. tomwalters@32: if (!initialized_) { tomwalters@32: LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str()); tomwalters@32: return; tomwalters@32: } tomwalters@32: tomwalters@32: // Check that ths input this time is the same as the input passed to tomwalters@32: // Initialize() tomwalters@32: if (buffer_length_ != input.buffer_length() tomwalters@32: || channel_count_ != input.channel_count()) { tomwalters@32: LOG_ERROR(_T("Mismatch between input to Initialize() and input to " tomwalters@32: "Process() in module %s."), module_identifier_.c_str()); tomwalters@32: return; tomwalters@32: } tomwalters@32: tomwalters@32: for (int c = 0; c < input.channel_count(); ++c) { tomwalters@32: for (int i = 0; i < input.buffer_length(); ++i) { tomwalters@32: float s = input[c][i]; tomwalters@84: float n = gaussian_variate_(); tomwalters@84: if (pink_) { tomwalters@84: // Pink noise filter coefficients from tomwalters@84: // ccrma.stanford.edu/~jos/sasp/Example_Synthesis_1_F_Noise.html tomwalters@84: // Smith, Julius O. Spectral Audio Signal Processing, October 2008 tomwalters@84: // Draft, http://ccrma.stanford.edu/~jos/sasp/, online book, tomwalters@84: // accessed 2010-02-27. tomwalters@84: float f = 0.049922035 * n + s0_; tomwalters@84: s0_ = -0.095993537 * n - (-2.494956002 * f) + s1_; tomwalters@84: s1_ = 0.050612699 * n - (2.017265875 * f) + s2_; tomwalters@84: s2_ = -0.004408786 * n - (-0.522189400 * f); tomwalters@84: n = f; tomwalters@84: } tomwalters@84: s += multiplier_ * n; tomwalters@32: output_.set_sample(c, i, s); tomwalters@32: } tomwalters@32: } tomwalters@32: PushOutput(); tomwalters@32: } tomwalters@32: } // namespace aimc tomwalters@32: