tomwalters@305
|
1 // Copyright 2010, Thomas Walters
|
tomwalters@305
|
2 //
|
tomwalters@305
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@305
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@305
|
5 //
|
tomwalters@305
|
6 // This program is free software: you can redistribute it and/or modify
|
tomwalters@305
|
7 // it under the terms of the GNU General Public License as published by
|
tomwalters@305
|
8 // the Free Software Foundation, either version 3 of the License, or
|
tomwalters@305
|
9 // (at your option) any later version.
|
tomwalters@305
|
10 //
|
tomwalters@305
|
11 // This program is distributed in the hope that it will be useful,
|
tomwalters@305
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@305
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@305
|
14 // GNU General Public License for more details.
|
tomwalters@305
|
15 //
|
tomwalters@305
|
16 // You should have received a copy of the GNU General Public License
|
tomwalters@305
|
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@305
|
18
|
tomwalters@305
|
19 /*!
|
tomwalters@305
|
20 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@305
|
21 * \date created 2010/02/24
|
tomwalters@305
|
22 * \version \$Id$
|
tomwalters@305
|
23 */
|
tomwalters@305
|
24
|
tomwalters@305
|
25 // Use the boost RNGs to generate Gaussian noise
|
tomwalters@305
|
26 #include <boost/random.hpp>
|
tomwalters@305
|
27 #include <math.h>
|
tomwalters@305
|
28
|
tomwalters@305
|
29 #include "Modules/SNR/ModuleNoise.h"
|
tomwalters@305
|
30
|
tomwalters@305
|
31 namespace aimc {
|
tomwalters@305
|
32 ModuleNoise::ModuleNoise(Parameters *params) :
|
tomwalters@305
|
33 Module(params),
|
tomwalters@305
|
34 gaussian_variate_(boost::mt19937(),
|
tomwalters@305
|
35 boost::normal_distribution<float>(0.0f, 1.0f)) {
|
tomwalters@305
|
36 module_description_ = "Adds noise to a signal";
|
tomwalters@305
|
37 module_identifier_ = "noise";
|
tomwalters@305
|
38 module_type_ = "snr";
|
tomwalters@305
|
39 module_version_ = "$Id$";
|
tomwalters@305
|
40
|
tomwalters@305
|
41 // Noise level relative to unit-variance Gaussian noise (ie. 0dB will give a
|
tomwalters@305
|
42 // noise with an RMS level of 1.0)
|
tomwalters@305
|
43 float snr_db = parameters_->DefaultFloat("noise.level_db", 0.0f);
|
tomwalters@305
|
44 multiplier_ = pow(10.0f, snr_db / 20.0f);
|
tomwalters@305
|
45 }
|
tomwalters@305
|
46
|
tomwalters@305
|
47 ModuleNoise::~ModuleNoise() {
|
tomwalters@305
|
48 }
|
tomwalters@305
|
49
|
tomwalters@305
|
50 bool ModuleNoise::InitializeInternal(const SignalBank &input) {
|
tomwalters@305
|
51 // Copy the parameters of the input signal bank into internal variables, so
|
tomwalters@305
|
52 // that they can be checked later.
|
tomwalters@305
|
53 sample_rate_ = input.sample_rate();
|
tomwalters@305
|
54 buffer_length_ = input.buffer_length();
|
tomwalters@305
|
55 channel_count_ = input.channel_count();
|
tomwalters@305
|
56
|
tomwalters@305
|
57 output_.Initialize(input);
|
tomwalters@305
|
58 return true;
|
tomwalters@305
|
59 }
|
tomwalters@305
|
60
|
tomwalters@305
|
61 void ModuleNoise::ResetInternal() {
|
tomwalters@305
|
62
|
tomwalters@305
|
63 }
|
tomwalters@305
|
64
|
tomwalters@305
|
65 void ModuleNoise::Process(const SignalBank &input) {
|
tomwalters@305
|
66 // Check to see if the module has been initialized. If not, processing
|
tomwalters@305
|
67 // should not continue.
|
tomwalters@305
|
68 if (!initialized_) {
|
tomwalters@305
|
69 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
|
tomwalters@305
|
70 return;
|
tomwalters@305
|
71 }
|
tomwalters@305
|
72
|
tomwalters@305
|
73 // Check that ths input this time is the same as the input passed to
|
tomwalters@305
|
74 // Initialize()
|
tomwalters@305
|
75 if (buffer_length_ != input.buffer_length()
|
tomwalters@305
|
76 || channel_count_ != input.channel_count()) {
|
tomwalters@305
|
77 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
|
tomwalters@305
|
78 "Process() in module %s."), module_identifier_.c_str());
|
tomwalters@305
|
79 return;
|
tomwalters@305
|
80 }
|
tomwalters@305
|
81
|
tomwalters@305
|
82 for (int c = 0; c < input.channel_count(); ++c) {
|
tomwalters@305
|
83 for (int i = 0; i < input.buffer_length(); ++i) {
|
tomwalters@305
|
84 float s = input[c][i];
|
tomwalters@305
|
85 s += (multiplier_ * gaussian_variate_());
|
tomwalters@305
|
86 output_.set_sample(c, i, s);
|
tomwalters@305
|
87 }
|
tomwalters@305
|
88 }
|
tomwalters@305
|
89 PushOutput();
|
tomwalters@305
|
90 }
|
tomwalters@305
|
91 } // namespace aimc
|
tomwalters@305
|
92
|