annotate trunk/src/Modules/SAI/ModuleSAI.cc @ 349:89b49df39ce6

- AWS
author tomwalters
date Wed, 11 Aug 2010 11:27:07 +0000
parents 30dde71d0230
children 733a11a65f3d
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
tomwalters@277 110 active_strobes_.clear();
tomwalters@277 111 active_strobes_.resize(channel_count_);
tomwalters@277 112 fire_counter_ = frame_period_samples_ - 1;
tomwalters@268 113 }
tomwalters@268 114
tomwalters@268 115 void ModuleSAI::Process(const SignalBank &input) {
tomwalters@268 116 // Reset the next strobe times
tomwalters@268 117 next_strobes_.clear();
tomwalters@268 118 next_strobes_.resize(output_.channel_count(), 0);
tomwalters@268 119
tomwalters@268 120 // Offset the times on the strobes from the previous buffer
tomwalters@277 121 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@277 122 active_strobes_[ch].ShiftStrobes(input.buffer_length());
tomwalters@268 123 }
tomwalters@268 124
tomwalters@268 125 // Loop over samples to make the SAI
tomwalters@277 126 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@268 127 float decay_factor = pow(sai_decay_factor_, fire_counter_);
tomwalters@268 128 // Loop over channels
tomwalters@277 129 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@268 130 // Local convenience variables
tomwalters@277 131 StrobeList &active_strobes = active_strobes_[ch];
tomwalters@277 132 int next_strobe_index = next_strobes_[ch];
tomwalters@268 133
tomwalters@277 134 // Update strobes
tomwalters@268 135 // If we are up to or beyond the next strobe...
tomwalters@277 136 if (next_strobe_index < input.strobe_count(ch)) {
tomwalters@277 137 if (i == input.strobe(ch, next_strobe_index)) {
tomwalters@277 138 // A new strobe has arrived.
tomwalters@277 139 // If there are too many strobes active, then get rid of the
tomwalters@277 140 // earliest one
tomwalters@277 141 if (active_strobes.strobe_count() >= max_concurrent_strobes_) {
tomwalters@277 142 active_strobes.DeleteFirstStrobe();
tomwalters@268 143 }
tomwalters@268 144
tomwalters@277 145 // Add the active strobe to the list of current strobes and
tomwalters@277 146 // calculate the strobe weight
tomwalters@277 147 float weight = 1.0f;
tomwalters@277 148 if (active_strobes.strobe_count() > 0) {
tomwalters@277 149 int last_strobe_time = active_strobes.Strobe(
tomwalters@277 150 active_strobes.strobe_count() - 1).time;
tomwalters@277 151
tomwalters@277 152 // If the strobe occured within 10 impulse-response
tomwalters@277 153 // cycles of the previous strobe, then lower its weight
tomwalters@277 154 weight = (i - last_strobe_time) / input.sample_rate()
tomwalters@277 155 * input.centre_frequency(ch) / 10.0f;
tomwalters@277 156 if (weight > 1.0f)
tomwalters@277 157 weight = 1.0f;
tomwalters@277 158 }
tomwalters@277 159 active_strobes.AddStrobe(i, weight);
tomwalters@277 160 next_strobe_index++;
tomwalters@277 161
tomwalters@277 162
tomwalters@277 163 // Update the strobe weights
tomwalters@268 164 float total_strobe_weight = 0.0f;
tomwalters@277 165 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 166 total_strobe_weight += (active_strobes.Strobe(si).weight
tomwalters@277 167 * strobe_weights_[active_strobes.strobe_count() - si - 1]);
tomwalters@268 168 }
tomwalters@277 169 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 170 active_strobes.SetWorkingWeight(si,
tomwalters@277 171 (active_strobes.Strobe(si).weight
tomwalters@277 172 * strobe_weights_[active_strobes.strobe_count() - si - 1])
tomwalters@277 173 / total_strobe_weight);
tomwalters@268 174 }
tomwalters@268 175 }
tomwalters@268 176 }
tomwalters@268 177
tomwalters@277 178 // Remove inactive strobes
tomwalters@277 179 while (active_strobes.strobe_count() > 0) {
tomwalters@277 180 // Get the relative time of the first strobe, and see if it exceeds
tomwalters@277 181 // the maximum allowed time.
tomwalters@277 182 if ((i - active_strobes.Strobe(0).time) > max_strobe_delay_idx_)
tomwalters@277 183 active_strobes.DeleteFirstStrobe();
tomwalters@268 184 else
tomwalters@268 185 break;
tomwalters@268 186 }
tomwalters@268 187
tomwalters@277 188 // Update the SAI buffer with the weighted effect of all the active
tomwalters@277 189 // strobes at the current sample
tomwalters@277 190 for (int si = 0; si < active_strobes.strobe_count(); ++si) {
tomwalters@277 191 // Add the effect of active strobe at correct place in the SAI buffer
tomwalters@277 192 // Calculate 'delay', the time from the strobe event to now
tomwalters@277 193 int delay = i - active_strobes.Strobe(si).time;
tomwalters@268 194
tomwalters@268 195 // If the delay is greater than the (user-set)
tomwalters@268 196 // minimum strobe delay, the strobe can be used
tomwalters@277 197 if (delay >= min_strobe_delay_idx_ && delay < max_strobe_delay_idx_) {
tomwalters@268 198 // The value at be added to the SAI
tomwalters@277 199 float sig = input.sample(ch, i);
tomwalters@268 200
tomwalters@268 201 // Weight the sample correctly
tomwalters@277 202 sig *= active_strobes.Strobe(si).working_weight;
tomwalters@268 203
tomwalters@268 204 // Adjust the weight acording to the number of samples until the
tomwalters@268 205 // next output frame
tomwalters@277 206 sig *= decay_factor;
tomwalters@268 207
tomwalters@268 208 // Update the temporary SAI buffer
tomwalters@277 209 output_.set_sample(ch, delay, output_.sample(ch, delay) + sig);
tomwalters@268 210 }
tomwalters@268 211 }
tomwalters@268 212
tomwalters@277 213 next_strobes_[ch] = next_strobe_index;
tomwalters@280 214 } // End loop over channels
tomwalters@268 215
tomwalters@277 216 fire_counter_--;
tomwalters@268 217
tomwalters@277 218 // Check to see if we need to output an SAI frame on this sample
tomwalters@277 219 if (fire_counter_ <= 0) {
tomwalters@268 220 // Decay the SAI by the correct amount and add the current output frame
tomwalters@277 221 float decay = pow(sai_decay_factor_, frame_period_samples_);
tomwalters@268 222
tomwalters@277 223 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@268 224 for (int i = 0; i < output_.buffer_length(); ++i) {
tomwalters@277 225 output_.set_sample(ch, i,
tomwalters@277 226 sai_temp_[ch][i] + output_[ch][i] * decay);
tomwalters@268 227 }
tomwalters@268 228 }
tomwalters@268 229
tomwalters@268 230 // Zero the temporary signal
tomwalters@268 231 for (int ch = 0; ch < sai_temp_.channel_count(); ++ch) {
tomwalters@268 232 for (int i = 0; i < sai_temp_.buffer_length(); ++i) {
tomwalters@268 233 sai_temp_.set_sample(ch, i, 0.0f);
tomwalters@268 234 }
tomwalters@268 235 }
tomwalters@268 236
tomwalters@277 237 fire_counter_ = frame_period_samples_ - 1;
tomwalters@268 238
tomwalters@277 239 // Transfer the current time to the output buffer
tomwalters@277 240 output_.set_start_time(input.start_time() + i);
tomwalters@268 241 PushOutput();
tomwalters@268 242 }
tomwalters@280 243 } // End loop over samples
tomwalters@268 244 }
tomwalters@268 245
tomwalters@268 246 ModuleSAI::~ModuleSAI() {
tomwalters@268 247 }
tomwalters@277 248 } // namespace aimc