annotate src/Modules/SSI/ModuleSSI.cc @ 237:af02b6addf7a

- Added support for movies!
author tomwalters
date Thu, 21 Oct 2010 01:46:39 +0000
parents 73c6d61440ad
children 0a3342606855
rev   line source
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@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@12 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@12 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@12 17
tomwalters@12 18 /*!
tomwalters@12 19 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@12 20 * \date created 2010/02/19
tomwalters@12 21 * \version \$Id$
tomwalters@12 22 */
tomwalters@12 23
tomwalters@15 24 #include <cmath>
tomwalters@15 25
tomwalters@12 26 #include "Modules/SSI/ModuleSSI.h"
tomwalters@12 27
tomwalters@12 28 namespace aimc {
tomwalters@163 29 #ifdef _MSC_VER
tomwalters@163 30 // MSVC doesn't define log2()
tomwalters@163 31 float log2(float n) {
tomwalters@163 32 return log(n) / log(2.0);
tomwalters@163 33 }
tomwalters@163 34 #endif
tomwalters@163 35
tomwalters@12 36 ModuleSSI::ModuleSSI(Parameters *params) : Module(params) {
tomwalters@12 37 module_description_ = "Size-shape image (aka the 'sscAI')";
tomwalters@12 38 module_identifier_ = "ssi";
tomwalters@12 39 module_type_ = "ssi";
tomwalters@12 40 module_version_ = "$Id$";
tomwalters@12 41
tomwalters@32 42 // Cut off the SSI at the end of the first cycle
tomwalters@32 43 do_pitch_cutoff_ = parameters_->DefaultBool("ssi.pitch_cutoff", false);
tomwalters@32 44
tomwalters@32 45 // Weight the values in each channel more strongly if the channel was
tomwalters@32 46 // truncated due to the pitch cutoff. This ensures that the same amount of
tomwalters@32 47 // energy remains in the SSI spectral profile
tomwalters@32 48 weight_by_cutoff_ = parameters_->DefaultBool("ssi.weight_by_cutoff", false);
tomwalters@32 49
tomwalters@32 50 // Weight the values in each channel more strongly if the channel was
tomwalters@32 51 // scaled such that the end goes off the edge of the computed SSI.
tomwalters@32 52 // Again, this ensures that the overall energy of the spectral profile
tomwalters@32 53 // remains the same.
tomwalters@32 54 weight_by_scaling_ = parameters_->DefaultBool("ssi.weight_by_scaling",
tomwalters@32 55 false);
tomwalters@32 56
tomwalters@32 57 // Time from the zero-lag line of the SAI from which to start searching
tomwalters@32 58 // for a maximum in the input SAI's temporal profile.
tomwalters@32 59 pitch_search_start_ms_ = parameters_->DefaultFloat(
tomwalters@237 60 "ssi.pitch_search_start_ms", 2.0f);
tomwalters@32 61
tomwalters@32 62 // Total width in cycles of the whole SSI
tomwalters@32 63 ssi_width_cycles_ = parameters_->DefaultFloat("ssi.width_cycles", 10.0f);
tomwalters@32 64
tomwalters@32 65 // Set to true to make the cycles axis logarithmic (ie indexing by gamma
tomwalters@32 66 // rather than by cycles)
tomwalters@32 67 log_cycles_axis_ = parameters_->DefaultBool("ssi.log_cycles_axis", true);
tomwalters@32 68
tomwalters@32 69 // The centre frequency of the channel which will just fill the complete
tomwalters@32 70 // width of the SSI buffer
tomwalters@32 71 pivot_cf_ = parameters_->DefaultFloat("ssi.pivot_cf", 1000.0f);
tomwalters@227 72
tomwalters@227 73 // Whether or not to do smooth offset when the pitch cutoff is active.
tomwalters@227 74 do_smooth_offset_ = parameters_->DefaultBool("ssi.do_smooth_offset", false);
tomwalters@227 75
tomwalters@227 76 // The number of cycles, centered on the pitch line, over which the SSI is taken
tomwalters@227 77 // to zero when doing the pitch cutoff.
tomwalters@227 78 smooth_offset_cycles_ = parameters_->DefaultFloat("ssi.smooth_offset_cycles", 3.0f);
tomwalters@12 79 }
tomwalters@12 80
tomwalters@12 81 ModuleSSI::~ModuleSSI() {
tomwalters@12 82 }
tomwalters@12 83
tomwalters@12 84 bool ModuleSSI::InitializeInternal(const SignalBank &input) {
tomwalters@12 85 // Copy the parameters of the input signal bank into internal variables, so
tomwalters@12 86 // that they can be checked later.
tomwalters@12 87 sample_rate_ = input.sample_rate();
tomwalters@12 88 buffer_length_ = input.buffer_length();
tomwalters@12 89 channel_count_ = input.channel_count();
tomwalters@12 90
tomwalters@32 91 ssi_width_samples_ = sample_rate_ * ssi_width_cycles_ / pivot_cf_;
tomwalters@15 92 if (ssi_width_samples_ > buffer_length_) {
tomwalters@15 93 ssi_width_samples_ = buffer_length_;
tomwalters@32 94 float cycles = ssi_width_samples_ * pivot_cf_ / sample_rate_;
tomwalters@15 95 LOG_INFO(_T("Requested SSI width of %f cycles is too long for the "
tomwalters@15 96 "input buffer length of %d samples. The SSI will be "
tomwalters@15 97 "truncated at %d samples wide. This corresponds to a width "
tomwalters@15 98 "of %f cycles."), ssi_width_cycles_, buffer_length_,
tomwalters@15 99 ssi_width_samples_, cycles);
tomwalters@15 100 ssi_width_cycles_ = cycles;
tomwalters@15 101 }
tomwalters@237 102 for (int i = 0; i < input.channel_count(); ++i) {
tomwalters@237 103 output_.set_centre_frequency(i, input.centre_frequency(i));
tomwalters@237 104 }
tomwalters@237 105
tomwalters@15 106 output_.Initialize(channel_count_, ssi_width_samples_, sample_rate_);
tomwalters@12 107 return true;
tomwalters@12 108 }
tomwalters@12 109
tomwalters@12 110 void ModuleSSI::ResetInternal() {
tomwalters@12 111 }
tomwalters@12 112
tomwalters@32 113 int ModuleSSI::ExtractPitchIndex(const SignalBank &input) const {
tomwalters@32 114 // Generate temporal profile of the SAI
tomwalters@32 115 vector<float> sai_temporal_profile(buffer_length_, 0.0f);
tomwalters@32 116 for (int i = 0; i < buffer_length_; ++i) {
tomwalters@32 117 float val = 0.0f;
tomwalters@32 118 for (int ch = 0; ch < channel_count_; ++ch) {
tomwalters@32 119 val += input.sample(ch, i);
tomwalters@32 120 }
tomwalters@32 121 sai_temporal_profile[i] = val;
tomwalters@32 122 }
tomwalters@32 123
tomwalters@32 124 // Find pitch value
tomwalters@32 125 int start_sample = floor(pitch_search_start_ms_ * sample_rate_ / 1000.0f);
tomwalters@32 126 int max_idx = 0;
tomwalters@32 127 float max_val = 0.0f;
tomwalters@32 128 for (int i = start_sample; i < buffer_length_; ++i) {
tomwalters@32 129 if (sai_temporal_profile[i] > max_val) {
tomwalters@32 130 max_idx = i;
tomwalters@32 131 max_val = sai_temporal_profile[i];
tomwalters@32 132 }
tomwalters@32 133 }
tomwalters@32 134 return max_idx;
tomwalters@32 135 }
tomwalters@32 136
tomwalters@12 137 void ModuleSSI::Process(const SignalBank &input) {
tomwalters@12 138 // Check to see if the module has been initialized. If not, processing
tomwalters@12 139 // should not continue.
tomwalters@12 140 if (!initialized_) {
tomwalters@13 141 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tomwalters@12 142 return;
tomwalters@12 143 }
tomwalters@12 144
tomwalters@12 145 // Check that ths input this time is the same as the input passed to
tomwalters@12 146 // Initialize()
tomwalters@12 147 if (buffer_length_ != input.buffer_length()
tomwalters@12 148 || channel_count_ != input.channel_count()) {
tomwalters@12 149 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tomwalters@13 150 "Process() in module %s."), module_identifier_.c_str());
tomwalters@12 151 return;
tomwalters@12 152 }
tomwalters@12 153
tomwalters@15 154 output_.set_start_time(input.start_time());
tomwalters@12 155
tomwalters@32 156 int pitch_index = buffer_length_ - 1;
tomwalters@32 157 if (do_pitch_cutoff_) {
tomwalters@32 158 pitch_index = ExtractPitchIndex(input);
tomwalters@32 159 }
tomwalters@227 160
tomwalters@227 161 float gamma_min = -1.0f;
tomwalters@227 162 float gamma_max = log2(ssi_width_cycles_);
tomwalters@32 163
tomwalters@15 164 for (int ch = 0; ch < channel_count_; ++ch) {
tomwalters@32 165 float centre_frequency = input.centre_frequency(ch);
tomwalters@237 166 float cycle_samples = sample_rate_ / centre_frequency;
tomwalters@227 167
tomwalters@227 168 float channel_weight = 1.0f;
tomwalters@227 169 int cutoff_index = buffer_length_ - 1;
tomwalters@227 170 if (do_pitch_cutoff_) {
tomwalters@227 171 if (pitch_index < cutoff_index) {
tomwalters@227 172 if (weight_by_cutoff_) {
tomwalters@227 173 channel_weight = static_cast<float>(buffer_length_)
tomwalters@237 174 / static_cast<float>(pitch_index);
tomwalters@227 175 }
tomwalters@227 176 cutoff_index = pitch_index;
tomwalters@227 177 }
tomwalters@227 178 }
tomwalters@227 179
tomwalters@227 180 // tanh(3) is about 0.995. Seems reasonable.
tomwalters@237 181 float smooth_pitch_constant = 3.0f / smooth_offset_cycles_;
tomwalters@227 182 float pitch_h = 0.0f;
tomwalters@227 183 if (do_smooth_offset_) {
tomwalters@237 184 pitch_h = static_cast<float>(pitch_index) / cycle_samples;
tomwalters@227 185 }
tomwalters@227 186
tomwalters@237 187 // Copy the buffer from input to output, addressing by h-value.
tomwalters@162 188 for (int i = 0; i < ssi_width_samples_; ++i) {
tomwalters@162 189 float h;
tomwalters@162 190 if (log_cycles_axis_) {
tomwalters@162 191 float gamma = gamma_min + (gamma_max - gamma_min)
tomwalters@162 192 * static_cast<float>(i)
tomwalters@162 193 / static_cast<float>(ssi_width_samples_);
tomwalters@162 194 h = pow(2.0f, gamma);
tomwalters@162 195 } else {
tomwalters@162 196 h = static_cast<float>(i) * ssi_width_cycles_
tomwalters@162 197 / static_cast<float>(ssi_width_samples_);
tomwalters@116 198 }
tomwalters@162 199
tomwalters@15 200 // The index into the input array is a floating-point number, which is
tomwalters@15 201 // split into a whole part and a fractional part. The whole part and
tomwalters@15 202 // fractional part are found, and are used to linearly interpolate
tomwalters@15 203 // between input samples to yield an output sample.
tomwalters@15 204 double whole_part;
tomwalters@162 205 float frac_part = modf(h * cycle_samples, &whole_part);
tomwalters@32 206 int sample = floor(whole_part);
tomwalters@32 207
tomwalters@227 208 float weight = channel_weight;
tomwalters@227 209
tomwalters@227 210 if (do_smooth_offset_ && do_pitch_cutoff_) {
tomwalters@227 211 // Smoothing around the pitch cutoff line.
tomwalters@237 212 float pitch_weight = (1.0f + tanh((pitch_h - h)
tomwalters@237 213 * smooth_pitch_constant)) / 2.0f;
tomwalters@237 214 weight *= pitch_weight;
tomwalters@237 215 //LOG_INFO("Channel %d, Sample %d. Pitch weight: %f", ch, i, pitch_weight);
tomwalters@32 216 }
tomwalters@32 217
tomwalters@32 218 if (weight_by_scaling_) {
tomwalters@32 219 if (centre_frequency > pivot_cf_) {
tomwalters@32 220 weight *= (centre_frequency / pivot_cf_);
tomwalters@32 221 }
tomwalters@32 222 }
tomwalters@15 223
tomwalters@15 224 float val;
tomwalters@227 225 if (sample < cutoff_index || do_smooth_offset_) {
tomwalters@15 226 float curr_sample = input.sample(ch, sample);
tomwalters@15 227 float next_sample = input.sample(ch, sample + 1);
tomwalters@32 228 val = weight * (curr_sample
tomwalters@32 229 + frac_part * (next_sample - curr_sample));
tomwalters@15 230 } else {
tomwalters@36 231 val = 0.0f;
tomwalters@15 232 }
tomwalters@15 233 output_.set_sample(ch, i, val);
tomwalters@15 234 }
tomwalters@15 235 }
tomwalters@12 236 PushOutput();
tomwalters@12 237 }
tomwalters@12 238 } // namespace aimc
tomwalters@12 239