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@318
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@318
|
8 // You may obtain a copy of the License at
|
tomwalters@305
|
9 //
|
tomwalters@318
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@305
|
11 //
|
tomwalters@318
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
15 // See the License for the specific language governing permissions and
|
tomwalters@318
|
16 // limitations under the License.
|
tomwalters@305
|
17
|
tomwalters@305
|
18 /*!
|
tomwalters@305
|
19 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@305
|
20 * \date created 2010/02/24
|
tomwalters@305
|
21 * \version \$Id$
|
tomwalters@305
|
22 */
|
tomwalters@305
|
23
|
tomwalters@305
|
24 // Use the boost RNGs to generate Gaussian noise
|
tomwalters@305
|
25 #include <boost/random.hpp>
|
tomwalters@305
|
26 #include <math.h>
|
tomwalters@305
|
27
|
tomwalters@305
|
28 #include "Modules/SNR/ModuleNoise.h"
|
tomwalters@305
|
29
|
tomwalters@305
|
30 namespace aimc {
|
tomwalters@305
|
31 ModuleNoise::ModuleNoise(Parameters *params) :
|
tomwalters@305
|
32 Module(params),
|
tomwalters@305
|
33 gaussian_variate_(boost::mt19937(),
|
tomwalters@305
|
34 boost::normal_distribution<float>(0.0f, 1.0f)) {
|
tomwalters@305
|
35 module_description_ = "Adds noise to a signal";
|
tomwalters@305
|
36 module_identifier_ = "noise";
|
tomwalters@305
|
37 module_type_ = "snr";
|
tomwalters@305
|
38 module_version_ = "$Id$";
|
tomwalters@305
|
39
|
tomwalters@365
|
40 pink_ = parameters_->DefaultBool("noise.pink", true);
|
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@365
|
58 ResetInternal();
|
tomwalters@305
|
59 return true;
|
tomwalters@305
|
60 }
|
tomwalters@305
|
61
|
tomwalters@305
|
62 void ModuleNoise::ResetInternal() {
|
tomwalters@365
|
63 s0_ = 0.0f;
|
tomwalters@365
|
64 s1_ = 0.0f;
|
tomwalters@365
|
65 s2_ = 0.0f;
|
tomwalters@305
|
66 }
|
tomwalters@305
|
67
|
tomwalters@305
|
68 void ModuleNoise::Process(const SignalBank &input) {
|
tomwalters@305
|
69 // Check to see if the module has been initialized. If not, processing
|
tomwalters@305
|
70 // should not continue.
|
tomwalters@305
|
71 if (!initialized_) {
|
tomwalters@305
|
72 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
|
tomwalters@305
|
73 return;
|
tomwalters@305
|
74 }
|
tomwalters@305
|
75
|
tomwalters@305
|
76 // Check that ths input this time is the same as the input passed to
|
tomwalters@305
|
77 // Initialize()
|
tomwalters@305
|
78 if (buffer_length_ != input.buffer_length()
|
tomwalters@305
|
79 || channel_count_ != input.channel_count()) {
|
tomwalters@305
|
80 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
|
tomwalters@305
|
81 "Process() in module %s."), module_identifier_.c_str());
|
tomwalters@305
|
82 return;
|
tomwalters@305
|
83 }
|
tomwalters@305
|
84
|
tomwalters@305
|
85 for (int c = 0; c < input.channel_count(); ++c) {
|
tomwalters@305
|
86 for (int i = 0; i < input.buffer_length(); ++i) {
|
tomwalters@305
|
87 float s = input[c][i];
|
tomwalters@365
|
88 float n = gaussian_variate_();
|
tomwalters@365
|
89 if (pink_) {
|
tomwalters@365
|
90 // Pink noise filter coefficients from
|
tomwalters@365
|
91 // ccrma.stanford.edu/~jos/sasp/Example_Synthesis_1_F_Noise.html
|
tomwalters@365
|
92 // Smith, Julius O. Spectral Audio Signal Processing, October 2008
|
tomwalters@365
|
93 // Draft, http://ccrma.stanford.edu/~jos/sasp/, online book,
|
tomwalters@365
|
94 // accessed 2010-02-27.
|
tomwalters@365
|
95 float f = 0.049922035 * n + s0_;
|
tomwalters@365
|
96 s0_ = -0.095993537 * n - (-2.494956002 * f) + s1_;
|
tomwalters@365
|
97 s1_ = 0.050612699 * n - (2.017265875 * f) + s2_;
|
tomwalters@365
|
98 s2_ = -0.004408786 * n - (-0.522189400 * f);
|
tomwalters@365
|
99 n = f;
|
tomwalters@365
|
100 }
|
tomwalters@365
|
101 s += multiplier_ * n;
|
tomwalters@305
|
102 output_.set_sample(c, i, s);
|
tomwalters@305
|
103 }
|
tomwalters@305
|
104 }
|
tomwalters@305
|
105 PushOutput();
|
tomwalters@305
|
106 }
|
tomwalters@305
|
107 } // namespace aimc
|
tomwalters@305
|
108
|