annotate trunk/src/Modules/SAI/ModuleSAI.cc @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents b58758854b88
children
rev   line source
tomwalters@268 1 // Copyright 2006-2010, Thomas Walters
tomwalters@268 2 //
tomwalters@268 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@268 4 // http://www.acousticscale.org/AIMC
tomwalters@268 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@268 9 //
tomwalters@318 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@268 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@268 17
tomwalters@268 18 /*! \file
tomwalters@268 19 * \brief SAI module
tomwalters@268 20 */
tomwalters@268 21
tomwalters@268 22 /*
tomwalters@268 23 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@268 24 * \date created 2007/08/29
tomwalters@296 25 * \version \$Id$
tomwalters@268 26 */
tomwalters@287 27 #include <cmath>
tomwalters@268 28
tomwalters@268 29 #include "Modules/SAI/ModuleSAI.h"
tomwalters@268 30
tomwalters@277 31 namespace aimc {
tomwalters@268 32 ModuleSAI::ModuleSAI(Parameters *parameters) : Module(parameters) {
tomwalters@277 33 module_identifier_ = "weighted_sai";
tomwalters@277 34 module_type_ = "sai";
tomwalters@268 35 module_description_ = "Stabilised auditory image";
tomwalters@296 36 module_version_ = "$Id$";
tomwalters@268 37
tomwalters@268 38 min_delay_ms_ = parameters_->DefaultFloat("sai.min_delay_ms", 0.0f);
tomwalters@268 39 max_delay_ms_ = parameters_->DefaultFloat("sai.max_delay_ms", 35.0f);
tomwalters@268 40 strobe_weight_alpha_ = parameters_->DefaultFloat("sai.strobe_weight_alpha",
tomwalters@268 41 0.5f);
tomwalters@268 42 buffer_memory_decay_ = parameters_->DefaultFloat("sai.buffer_memory_decay",
tomwalters@268 43 0.03f);
tomwalters@268 44 frame_period_ms_ = parameters_->DefaultFloat("sai.frame_period_ms", 20.0f);
tomwalters@268 45
tomwalters@277 46 max_concurrent_strobes_
tomwalters@277 47 = parameters_->DefaultInt("sai.max_concurrent_strobes", 50);
tomwalters@277 48
tomwalters@277 49 min_strobe_delay_idx_ = 0;
tomwalters@277 50 max_strobe_delay_idx_ = 0;
tomwalters@268 51 sai_decay_factor_ = 0.0f;
tomwalters@277 52 fire_counter_ = 0;
tomwalters@268 53 }
tomwalters@268 54
tomwalters@268 55 bool ModuleSAI::InitializeInternal(const SignalBank &input) {
tomwalters@268 56 // The SAI output bank must be as long as the SAI's Maximum delay.
tomwalters@268 57 // One sample is added to the SAI buffer length to account for the
tomwalters@268 58 // zero-lag point
tomwalters@278 59 int sai_buffer_length = 1 + floor(input.sample_rate() * max_delay_ms_
tomwalters@278 60 / 1000.0f);
tomwalters@277 61 channel_count_ = input.channel_count();
tomwalters@268 62
tomwalters@268 63 // Make an output SignalBank with the same number of channels and centre
tomwalters@268 64 // frequencies as the input, but with a different buffer length
tomwalters@268 65 if (!output_.Initialize(input.channel_count(),
tomwalters@268 66 sai_buffer_length,
tomwalters@277 67 input.sample_rate())) {
tomwalters@268 68 LOG_ERROR("Failed to create output buffer in SAI module");
tomwalters@268 69 return false;
tomwalters@268 70 }
tomwalters@280 71 for (int i = 0; i < input.channel_count(); ++i) {
tomwalters@277 72 output_.set_centre_frequency(i, input.centre_frequency(i));
tomwalters@268 73 }
tomwalters@268 74
tomwalters@268 75 // sai_temp_ will be initialized to zero
tomwalters@268 76 if (!sai_temp_.Initialize(output_)) {
tomwalters@268 77 LOG_ERROR("Failed to create temporary buffer in SAI module");
tomwalters@268 78 return false;
tomwalters@268 79 }
tomwalters@268 80
tomwalters@277 81 frame_period_samples_ = floor(input.sample_rate() * frame_period_ms_
tomwalters@277 82 / 1000.0f);
tomwalters@277 83 min_strobe_delay_idx_ = floor(input.sample_rate() * min_delay_ms_
tomwalters@277 84 / 1000.0f);
tomwalters@277 85 max_strobe_delay_idx_ = floor(input.sample_rate() * max_delay_ms_
tomwalters@277 86 / 1000.0f);
tomwalters@268 87
tomwalters@268 88 // Make sure we don't go past the output buffer's upper bound
tomwalters@277 89 if (max_strobe_delay_idx_ > output_.buffer_length()) {
tomwalters@268 90 max_strobe_delay_idx_ = output_.buffer_length();
tomwalters@277 91 }
tomwalters@268 92
tomwalters@268 93 // Define decay factor from time since last sample (see ti2003)
tomwalters@268 94 sai_decay_factor_ = pow(0.5f, 1.0f / (buffer_memory_decay_
tomwalters@268 95 * input.sample_rate()));
tomwalters@268 96
tomwalters@268 97 // Precompute strobe weights
tomwalters@268 98 strobe_weights_.resize(max_concurrent_strobes_);
tomwalters@268 99 for (int n = 0; n < max_concurrent_strobes_; ++n) {
tomwalters@277 100 strobe_weights_[n] = pow(1.0f / (n + 1), strobe_weight_alpha_);
tomwalters@268 101 }
tomwalters@268 102
tomwalters@277 103 ResetInternal();
tomwalters@268 104
tomwalters@268 105 return true;
tomwalters@268 106 }
tomwalters@268 107
tomwalters@275 108 void ModuleSAI::ResetInternal() {
tomwalters@277 109 // Active Strobes
tom@420 110 output_.Clear();
tom@420 111 sai_temp_.Clear();
tomwalters@277 112 active_strobes_.clear();
tomwalters@277 113 active_strobes_.resize(channel_count_);
tomwalters@277 114 fire_counter_ = frame_period_samples_ - 1;
tomwalters@268 115 }
tomwalters@268 116
tomwalters@268 117 void ModuleSAI::Process(const SignalBank &input) {
tomwalters@268 118 // Reset the next strobe times
tomwalters@268 119 next_strobes_.clear();
tomwalters@268 120 next_strobes_.resize(output_.channel_count(), 0);
tomwalters@268 121
tomwalters@268 122 // Offset the times on the strobes from the previous buffer
tomwalters@277 123 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@277 124 active_strobes_[ch].ShiftStrobes(input.buffer_length());
tomwalters@268 125 }
tomwalters@268 126
tomwalters@268 127 // Loop over samples to make the SAI
tomwalters@277 128 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@268 129 float decay_factor = pow(sai_decay_factor_, fire_counter_);
tomwalters@268 130 // Loop over channels
tomwalters@277 131 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@268 132 // Local convenience variables
tomwalters@277 133 StrobeList &active_strobes = active_strobes_[ch];
tomwalters@277 134 int next_strobe_index = next_strobes_[ch];
tomwalters@268 135
tomwalters@277 136 // Update strobes
tomwalters@268 137 // If we are up to or beyond the next strobe...
tomwalters@277 138 if (next_strobe_index < input.strobe_count(ch)) {
tomwalters@277 139 if (i == input.strobe(ch, next_strobe_index)) {
tomwalters@277 140 // A new strobe has arrived.
tomwalters@277 141 // If there are too many strobes active, then get rid of the
tomwalters@277 142 // earliest one
tomwalters@277 143 if (active_strobes.strobe_count() >= max_concurrent_strobes_) {
tomwalters@277 144 active_strobes.DeleteFirstStrobe();
tomwalters@268 145 }
tomwalters@268 146
tomwalters@277 147 // Add the active strobe to the list of current strobes and
tomwalters@277 148 // calculate the strobe weight
tomwalters@277 149 float weight = 1.0f;
tomwalters@277 150 if (active_strobes.strobe_count() > 0) {
tomwalters@277 151 int last_strobe_time = active_strobes.Strobe(
tomwalters@277 152 active_strobes.strobe_count() - 1).time;
tomwalters@277 153
tomwalters@277 154 // If the strobe occured within 10 impulse-response
tomwalters@277 155 // cycles of the previous strobe, then lower its weight
tomwalters@277 156 weight = (i - last_strobe_time) / input.sample_rate()
tomwalters@277 157 * input.centre_frequency(ch) / 10.0f;
tomwalters@277 158 if (weight > 1.0f)
tomwalters@277 159 weight = 1.0f;
tomwalters@277 160 }
tomwalters@277 161 active_strobes.AddStrobe(i, weight);
tomwalters@277 162 next_strobe_index++;
tomwalters@277 163
tomwalters@277 164
tomwalters@277 165 // Update the strobe weights
tomwalters@268 166 float total_strobe_weight = 0.0f;
tomwalters@277 167 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 168 total_strobe_weight += (active_strobes.Strobe(si).weight
tomwalters@277 169 * strobe_weights_[active_strobes.strobe_count() - si - 1]);
tomwalters@268 170 }
tomwalters@277 171 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 172 active_strobes.SetWorkingWeight(si,
tomwalters@277 173 (active_strobes.Strobe(si).weight
tomwalters@277 174 * strobe_weights_[active_strobes.strobe_count() - si - 1])
tomwalters@277 175 / total_strobe_weight);
tomwalters@268 176 }
tomwalters@268 177 }
tomwalters@268 178 }
tomwalters@268 179
tomwalters@277 180 // Remove inactive strobes
tomwalters@277 181 while (active_strobes.strobe_count() > 0) {
tomwalters@277 182 // Get the relative time of the first strobe, and see if it exceeds
tomwalters@277 183 // the maximum allowed time.
tomwalters@277 184 if ((i - active_strobes.Strobe(0).time) > max_strobe_delay_idx_)
tomwalters@277 185 active_strobes.DeleteFirstStrobe();
tomwalters@268 186 else
tomwalters@268 187 break;
tomwalters@268 188 }
tomwalters@268 189
tomwalters@277 190 // Update the SAI buffer with the weighted effect of all the active
tomwalters@277 191 // strobes at the current sample
tomwalters@277 192 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 193 // Add the effect of active strobe at correct place in the SAI buffer
tomwalters@277 194 // Calculate 'delay', the time from the strobe event to now
tomwalters@277 195 int delay = i - active_strobes.Strobe(si).time;
tomwalters@268 196
tomwalters@268 197 // If the delay is greater than the (user-set)
tomwalters@268 198 // minimum strobe delay, the strobe can be used
tomwalters@277 199 if (delay >= min_strobe_delay_idx_ && delay < max_strobe_delay_idx_) {
tomwalters@268 200 // The value at be added to the SAI
tomwalters@277 201 float sig = input.sample(ch, i);
tomwalters@268 202
tomwalters@268 203 // Weight the sample correctly
tomwalters@277 204 sig *= active_strobes.Strobe(si).working_weight;
tomwalters@268 205
tomwalters@268 206 // Adjust the weight acording to the number of samples until the
tomwalters@268 207 // next output frame
tomwalters@277 208 sig *= decay_factor;
tomwalters@268 209
tomwalters@268 210 // Update the temporary SAI buffer
tomwalters@596 211 sai_temp_.set_sample(ch, delay, sai_temp_.sample(ch, delay) + sig);
tomwalters@268 212 }
tomwalters@268 213 }
tomwalters@268 214
tomwalters@277 215 next_strobes_[ch] = next_strobe_index;
tomwalters@280 216 } // End loop over channels
tomwalters@268 217
tomwalters@277 218 fire_counter_--;
tomwalters@268 219
tomwalters@277 220 // Check to see if we need to output an SAI frame on this sample
tomwalters@277 221 if (fire_counter_ <= 0) {
tomwalters@268 222 // Decay the SAI by the correct amount and add the current output frame
tomwalters@277 223 float decay = pow(sai_decay_factor_, frame_period_samples_);
tomwalters@268 224
tomwalters@277 225 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@268 226 for (int i = 0; i < output_.buffer_length(); ++i) {
tomwalters@277 227 output_.set_sample(ch, i,
tomwalters@277 228 sai_temp_[ch][i] + output_[ch][i] * decay);
tomwalters@268 229 }
tomwalters@268 230 }
tomwalters@268 231
tomwalters@268 232 // Zero the temporary signal
tomwalters@268 233 for (int ch = 0; ch < sai_temp_.channel_count(); ++ch) {
tomwalters@268 234 for (int i = 0; i < sai_temp_.buffer_length(); ++i) {
tomwalters@268 235 sai_temp_.set_sample(ch, i, 0.0f);
tomwalters@268 236 }
tomwalters@268 237 }
tomwalters@268 238
tomwalters@277 239 fire_counter_ = frame_period_samples_ - 1;
tomwalters@268 240
tomwalters@277 241 // Transfer the current time to the output buffer
tomwalters@277 242 output_.set_start_time(input.start_time() + i);
tomwalters@268 243 PushOutput();
tomwalters@268 244 }
tomwalters@280 245 } // End loop over samples
tomwalters@268 246 }
tomwalters@268 247
tomwalters@268 248 ModuleSAI::~ModuleSAI() {
tomwalters@268 249 }
tomwalters@277 250 } // namespace aimc