tomwalters@32
|
1 // Copyright 2010, Thomas Walters
|
tomwalters@32
|
2 //
|
tomwalters@32
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@32
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@32
|
5 //
|
tomwalters@32
|
6 // This program is free software: you can redistribute it and/or modify
|
tomwalters@32
|
7 // it under the terms of the GNU General Public License as published by
|
tomwalters@32
|
8 // the Free Software Foundation, either version 3 of the License, or
|
tomwalters@32
|
9 // (at your option) any later version.
|
tomwalters@32
|
10 //
|
tomwalters@32
|
11 // This program is distributed in the hope that it will be useful,
|
tomwalters@32
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
tomwalters@32
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
tomwalters@32
|
14 // GNU General Public License for more details.
|
tomwalters@32
|
15 //
|
tomwalters@32
|
16 // You should have received a copy of the GNU General Public License
|
tomwalters@32
|
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
tomwalters@32
|
18
|
tomwalters@32
|
19 /*!
|
tomwalters@32
|
20 * \file
|
tomwalters@32
|
21 * \brief
|
tomwalters@32
|
22 *
|
tomwalters@32
|
23 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@32
|
24 * \date created 2010/02/23
|
tomwalters@32
|
25 * \version \$Id$
|
tomwalters@32
|
26 */
|
tomwalters@32
|
27
|
tomwalters@32
|
28 #include <math.h>
|
tomwalters@32
|
29 #include "Modules/Strobes/ModuleLocalMax.h"
|
tomwalters@32
|
30
|
tomwalters@32
|
31 namespace aimc {
|
tomwalters@32
|
32 ModuleLocalMax::ModuleLocalMax(Parameters *params) : Module(params) {
|
tomwalters@32
|
33 module_description_ = "Local maximum strobe criterion: decaying threshold "
|
tomwalters@32
|
34 "with timeout";
|
tomwalters@32
|
35 module_identifier_ = "local_max";
|
tomwalters@32
|
36 module_type_ = "strobes";
|
tomwalters@32
|
37 module_version_ = "$Id$";
|
tomwalters@32
|
38
|
tomwalters@32
|
39 decay_time_ms_ = parameters_->DefaultFloat("strobes.decay_time_ms", 20.0f);
|
tomwalters@32
|
40 timeout_ms_ = parameters_->DefaultFloat("strobes.timeout_ms", 3.0f);
|
tomwalters@32
|
41 }
|
tomwalters@32
|
42
|
tomwalters@32
|
43 ModuleLocalMax::~ModuleLocalMax() {
|
tomwalters@32
|
44 }
|
tomwalters@32
|
45
|
tomwalters@32
|
46 bool ModuleLocalMax::InitializeInternal(const SignalBank &input) {
|
tomwalters@32
|
47 // Copy the parameters of the input signal bank into internal variables, so
|
tomwalters@32
|
48 // that they can be checked later.
|
tomwalters@32
|
49 sample_rate_ = input.sample_rate();
|
tomwalters@32
|
50 buffer_length_ = input.buffer_length();
|
tomwalters@32
|
51 channel_count_ = input.channel_count();
|
tomwalters@32
|
52 output_.Initialize(input);
|
tomwalters@32
|
53 strobe_timeout_samples_ = floor(timeout_ms_ * sample_rate_ / 1000.0f);
|
tomwalters@32
|
54 strobe_decay_samples_ = floor(decay_time_ms_ * sample_rate_ / 1000.0f);
|
tomwalters@32
|
55 ResetInternal();
|
tomwalters@32
|
56 return true;
|
tomwalters@32
|
57 }
|
tomwalters@32
|
58
|
tomwalters@32
|
59 void ModuleLocalMax::ResetInternal() {
|
tomwalters@32
|
60 threshold_.clear();
|
tomwalters@32
|
61 threshold_.resize(channel_count_, 0.0f);
|
tomwalters@32
|
62
|
tomwalters@32
|
63 decay_constant_.clear();
|
tomwalters@32
|
64 decay_constant_.resize(channel_count_, 1.0f);
|
tomwalters@32
|
65
|
tomwalters@32
|
66 prev_sample_.clear();
|
tomwalters@32
|
67 prev_sample_.resize(channel_count_, 10000.0f);
|
tomwalters@32
|
68 curr_sample_.clear();
|
tomwalters@32
|
69 curr_sample_.resize(channel_count_, 5000.0f);
|
tomwalters@32
|
70 next_sample_.clear();
|
tomwalters@32
|
71 next_sample_.resize(channel_count_, 0.0f);
|
tomwalters@32
|
72 }
|
tomwalters@32
|
73
|
tomwalters@32
|
74 void ModuleLocalMax::Process(const SignalBank &input) {
|
tomwalters@32
|
75 // Check to see if the module has been initialized. If not, processing
|
tomwalters@32
|
76 // should not continue.
|
tomwalters@32
|
77 if (!initialized_) {
|
tomwalters@32
|
78 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
|
tomwalters@32
|
79 return;
|
tomwalters@32
|
80 }
|
tomwalters@32
|
81
|
tomwalters@32
|
82 // Check that ths input this time is the same as the input passed to
|
tomwalters@32
|
83 // Initialize()
|
tomwalters@32
|
84 if (buffer_length_ != input.buffer_length()
|
tomwalters@32
|
85 || channel_count_ != input.channel_count()) {
|
tomwalters@32
|
86 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
|
tomwalters@32
|
87 "Process() in module %s."), module_identifier_.c_str());
|
tomwalters@32
|
88 return;
|
tomwalters@32
|
89 }
|
tomwalters@32
|
90
|
tomwalters@32
|
91 for (int ch = 0; ch < output_.channel_count(); ch++) {
|
tomwalters@32
|
92 output_.ResetStrobes(ch);
|
tomwalters@32
|
93 }
|
tomwalters@32
|
94 output_.set_start_time(input.start_time());
|
tomwalters@32
|
95 for (int i = 0; i < input.buffer_length(); i++) {
|
tomwalters@32
|
96 for (int ch = 0; ch < input.channel_count(); ++ch) {
|
tomwalters@32
|
97 // curr_sample is the sample at time (i - 1)
|
tomwalters@32
|
98 prev_sample_[ch] = curr_sample_[ch];
|
tomwalters@32
|
99 curr_sample_[ch] = next_sample_[ch];
|
tomwalters@32
|
100 next_sample_[ch] = input.sample(ch, i);
|
tomwalters@32
|
101 // Copy input signal to output signal
|
tomwalters@32
|
102 output_.set_sample(ch, i, input.sample(ch, i));
|
tomwalters@32
|
103
|
tomwalters@32
|
104 // If the current sample is above threshold, the threshold is raised to
|
tomwalters@32
|
105 // the level of the current sample, and decays from there.
|
tomwalters@32
|
106 if (curr_sample_[ch] >= threshold_[ch]) {
|
tomwalters@32
|
107 threshold_[ch] = curr_sample_[ch];
|
tomwalters@32
|
108 decay_constant_[ch] = threshold_[ch] / strobe_decay_samples_;
|
tomwalters@32
|
109
|
tomwalters@32
|
110 // If the current sample is also a peak, then it is a potential strobe
|
tomwalters@32
|
111 // point.
|
tomwalters@32
|
112 if (prev_sample_[ch] < curr_sample_[ch]
|
tomwalters@32
|
113 && next_sample_[ch] < curr_sample_[ch]) {
|
tomwalters@32
|
114 // If there are no strobes so far in this channel, then the sample
|
tomwalters@32
|
115 // is definitely a strobe (this means that the timeout is not
|
tomwalters@32
|
116 // respected across frame boundaries. This is a minor bug, but I
|
tomwalters@32
|
117 // don't believe that it's serious enough to warrant updating the
|
tomwalters@32
|
118 // samples since last strobe all the time.)
|
tomwalters@32
|
119 int count = output_.strobe_count(ch);
|
tomwalters@32
|
120 if (count > 0) {
|
tomwalters@32
|
121 // If there are previous strobes, then calculate the time since
|
tomwalters@32
|
122 // the last one. If it's long enough, then this is a strobe point,
|
tomwalters@32
|
123 // if not, then just move on.
|
tomwalters@32
|
124 int samples_since_last = (i - 1) - output_.strobe(ch, count - 1);
|
tomwalters@32
|
125 if (samples_since_last > strobe_timeout_samples_) {
|
tomwalters@32
|
126 output_.AddStrobe(ch, i - 1);
|
tomwalters@32
|
127 }
|
tomwalters@32
|
128 } else {
|
tomwalters@32
|
129 output_.AddStrobe(ch, i - 1);
|
tomwalters@32
|
130 }
|
tomwalters@32
|
131 }
|
tomwalters@32
|
132 }
|
tomwalters@32
|
133
|
tomwalters@32
|
134 // Update the threshold, decaying as necessary
|
tomwalters@32
|
135 if (threshold_[ch] > decay_constant_[ch])
|
tomwalters@32
|
136 threshold_[ch] -= decay_constant_[ch];
|
tomwalters@32
|
137 else
|
tomwalters@32
|
138 threshold_[ch] = 0.0f;
|
tomwalters@32
|
139 }
|
tomwalters@32
|
140 }
|
tomwalters@32
|
141 PushOutput();
|
tomwalters@32
|
142 }
|
tomwalters@32
|
143 } // namespace aimc
|
tomwalters@32
|
144
|