tomwalters@12
|
1 // Copyright 2010, Thomas Walters
|
tomwalters@12
|
2 //
|
tomwalters@12
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@12
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@12
|
5 //
|
tomwalters@12
|
6 // This program is free software: you can redistribute it and/or modify
|
tomwalters@12
|
7 // it under the terms of the GNU General Public License as published by
|
tomwalters@12
|
8 // the Free Software Foundation, either version 3 of the License, or
|
tomwalters@12
|
9 // (at your option) any later version.
|
tomwalters@12
|
10 //
|
tomwalters@12
|
11 // This program is distributed in the hope that it will be useful,
|
tomwalters@12
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@12
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@12
|
14 // GNU General Public License for more details.
|
tomwalters@12
|
15 //
|
tomwalters@12
|
16 // You should have received a copy of the GNU General Public License
|
tomwalters@12
|
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@12
|
18
|
tomwalters@12
|
19 /*!
|
tomwalters@12
|
20 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@12
|
21 * \date created 2010/02/19
|
tomwalters@12
|
22 * \version \$Id$
|
tomwalters@12
|
23 */
|
tomwalters@12
|
24
|
tomwalters@15
|
25 #include <cmath>
|
tomwalters@15
|
26
|
tomwalters@12
|
27 #include "Modules/SSI/ModuleSSI.h"
|
tomwalters@12
|
28
|
tomwalters@12
|
29 namespace aimc {
|
tomwalters@12
|
30 ModuleSSI::ModuleSSI(Parameters *params) : Module(params) {
|
tomwalters@12
|
31 module_description_ = "Size-shape image (aka the 'sscAI')";
|
tomwalters@12
|
32 module_identifier_ = "ssi";
|
tomwalters@12
|
33 module_type_ = "ssi";
|
tomwalters@12
|
34 module_version_ = "$Id$";
|
tomwalters@12
|
35
|
tomwalters@17
|
36 // do_pitch_cutoff_ = parameters_->DefaultBool("ssi.pitch_cutoff", false);
|
tomwalters@15
|
37 ssi_width_cycles_ = parameters_->DefaultFloat("ssi.width_cycles", 20.0f);
|
tomwalters@12
|
38 }
|
tomwalters@12
|
39
|
tomwalters@12
|
40 ModuleSSI::~ModuleSSI() {
|
tomwalters@12
|
41 }
|
tomwalters@12
|
42
|
tomwalters@12
|
43 bool ModuleSSI::InitializeInternal(const SignalBank &input) {
|
tomwalters@12
|
44 // Copy the parameters of the input signal bank into internal variables, so
|
tomwalters@12
|
45 // that they can be checked later.
|
tomwalters@12
|
46 sample_rate_ = input.sample_rate();
|
tomwalters@12
|
47 buffer_length_ = input.buffer_length();
|
tomwalters@12
|
48 channel_count_ = input.channel_count();
|
tomwalters@12
|
49
|
tomwalters@15
|
50 float lowest_cf = input.centre_frequency(0);
|
tomwalters@15
|
51 ssi_width_samples_ = sample_rate_ * ssi_width_cycles_ / lowest_cf;
|
tomwalters@15
|
52 if (ssi_width_samples_ > buffer_length_) {
|
tomwalters@15
|
53 ssi_width_samples_ = buffer_length_;
|
tomwalters@15
|
54 float cycles = ssi_width_samples_ * lowest_cf / sample_rate_;
|
tomwalters@15
|
55 LOG_INFO(_T("Requested SSI width of %f cycles is too long for the "
|
tomwalters@15
|
56 "input buffer length of %d samples. The SSI will be "
|
tomwalters@15
|
57 "truncated at %d samples wide. This corresponds to a width "
|
tomwalters@15
|
58 "of %f cycles."), ssi_width_cycles_, buffer_length_,
|
tomwalters@15
|
59 ssi_width_samples_, cycles);
|
tomwalters@15
|
60 ssi_width_cycles_ = cycles;
|
tomwalters@15
|
61 }
|
tomwalters@15
|
62 output_.Initialize(channel_count_, ssi_width_samples_, sample_rate_);
|
tomwalters@12
|
63 return true;
|
tomwalters@12
|
64 }
|
tomwalters@12
|
65
|
tomwalters@12
|
66 void ModuleSSI::ResetInternal() {
|
tomwalters@12
|
67 }
|
tomwalters@12
|
68
|
tomwalters@12
|
69 void ModuleSSI::Process(const SignalBank &input) {
|
tomwalters@12
|
70 // Check to see if the module has been initialized. If not, processing
|
tomwalters@12
|
71 // should not continue.
|
tomwalters@12
|
72 if (!initialized_) {
|
tomwalters@13
|
73 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
|
tomwalters@12
|
74 return;
|
tomwalters@12
|
75 }
|
tomwalters@12
|
76
|
tomwalters@12
|
77 // Check that ths input this time is the same as the input passed to
|
tomwalters@12
|
78 // Initialize()
|
tomwalters@12
|
79 if (buffer_length_ != input.buffer_length()
|
tomwalters@12
|
80 || channel_count_ != input.channel_count()) {
|
tomwalters@12
|
81 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
|
tomwalters@13
|
82 "Process() in module %s."), module_identifier_.c_str());
|
tomwalters@12
|
83 return;
|
tomwalters@12
|
84 }
|
tomwalters@12
|
85
|
tomwalters@15
|
86 output_.set_start_time(input.start_time());
|
tomwalters@12
|
87
|
tomwalters@15
|
88 for (int ch = 0; ch < channel_count_; ++ch) {
|
tomwalters@15
|
89 // Copy the buffer from input to output, addressing by h-value
|
tomwalters@15
|
90 for (int i = 0; i < ssi_width_samples_; ++i) {
|
tomwalters@15
|
91 float h = static_cast<float>(i) * ssi_width_cycles_
|
tomwalters@15
|
92 / static_cast<float>(ssi_width_samples_);
|
tomwalters@15
|
93 float cycle_samples = sample_rate_ / input.centre_frequency(ch);
|
tomwalters@12
|
94
|
tomwalters@15
|
95 // The index into the input array is a floating-point number, which is
|
tomwalters@15
|
96 // split into a whole part and a fractional part. The whole part and
|
tomwalters@15
|
97 // fractional part are found, and are used to linearly interpolate
|
tomwalters@15
|
98 // between input samples to yield an output sample.
|
tomwalters@15
|
99 double whole_part;
|
tomwalters@15
|
100 float frac_part = modf(h * cycle_samples, &whole_part);
|
tomwalters@15
|
101 int sample = static_cast<int>(whole_part);
|
tomwalters@15
|
102
|
tomwalters@15
|
103 float val;
|
tomwalters@15
|
104 if (sample < buffer_length_ - 1) {
|
tomwalters@15
|
105 float curr_sample = input.sample(ch, sample);
|
tomwalters@15
|
106 float next_sample = input.sample(ch, sample + 1);
|
tomwalters@15
|
107 val = curr_sample + frac_part * (next_sample - curr_sample);
|
tomwalters@15
|
108 } else {
|
tomwalters@15
|
109 val = 0.0f;
|
tomwalters@15
|
110 }
|
tomwalters@15
|
111 output_.set_sample(ch, i, val);
|
tomwalters@15
|
112 }
|
tomwalters@15
|
113 }
|
tomwalters@12
|
114 PushOutput();
|
tomwalters@12
|
115 }
|
tomwalters@12
|
116 } // namespace aimc
|
tomwalters@12
|
117
|