tomwalters@305: // Copyright 2010, Thomas Walters
tomwalters@305: //
tomwalters@305: // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@305: // http://www.acousticscale.org/AIMC
tomwalters@305: //
tomwalters@305: // This program is free software: you can redistribute it and/or modify
tomwalters@305: // it under the terms of the GNU General Public License as published by
tomwalters@305: // the Free Software Foundation, either version 3 of the License, or
tomwalters@305: // (at your option) any later version.
tomwalters@305: //
tomwalters@305: // This program is distributed in the hope that it will be useful,
tomwalters@305: // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@305: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@305: // GNU General Public License for more details.
tomwalters@305: //
tomwalters@305: // You should have received a copy of the GNU General Public License
tomwalters@305: // along with this program. If not, see .
tomwalters@305:
tomwalters@305: /*!
tomwalters@305: * \file
tomwalters@305: * \brief
tomwalters@305: *
tomwalters@305: * \author Thomas Walters
tomwalters@305: * \date created 2010/02/23
tomwalters@305: * \version \$Id$
tomwalters@305: */
tomwalters@305:
tomwalters@305: #include
tomwalters@305: #include "Modules/Strobes/ModuleLocalMax.h"
tomwalters@305:
tomwalters@305: namespace aimc {
tomwalters@305: ModuleLocalMax::ModuleLocalMax(Parameters *params) : Module(params) {
tomwalters@305: module_description_ = "Local maximum strobe criterion: decaying threshold "
tomwalters@305: "with timeout";
tomwalters@305: module_identifier_ = "local_max";
tomwalters@305: module_type_ = "strobes";
tomwalters@305: module_version_ = "$Id$";
tomwalters@305:
tomwalters@305: decay_time_ms_ = parameters_->DefaultFloat("strobes.decay_time_ms", 20.0f);
tomwalters@305: timeout_ms_ = parameters_->DefaultFloat("strobes.timeout_ms", 3.0f);
tomwalters@305: }
tomwalters@305:
tomwalters@305: ModuleLocalMax::~ModuleLocalMax() {
tomwalters@305: }
tomwalters@305:
tomwalters@305: bool ModuleLocalMax::InitializeInternal(const SignalBank &input) {
tomwalters@305: // Copy the parameters of the input signal bank into internal variables, so
tomwalters@305: // that they can be checked later.
tomwalters@305: sample_rate_ = input.sample_rate();
tomwalters@305: buffer_length_ = input.buffer_length();
tomwalters@305: channel_count_ = input.channel_count();
tomwalters@305: output_.Initialize(input);
tomwalters@305: strobe_timeout_samples_ = floor(timeout_ms_ * sample_rate_ / 1000.0f);
tomwalters@305: strobe_decay_samples_ = floor(decay_time_ms_ * sample_rate_ / 1000.0f);
tomwalters@305: ResetInternal();
tomwalters@305: return true;
tomwalters@305: }
tomwalters@305:
tomwalters@305: void ModuleLocalMax::ResetInternal() {
tomwalters@305: threshold_.clear();
tomwalters@305: threshold_.resize(channel_count_, 0.0f);
tomwalters@305:
tomwalters@305: decay_constant_.clear();
tomwalters@305: decay_constant_.resize(channel_count_, 1.0f);
tomwalters@305:
tomwalters@305: prev_sample_.clear();
tomwalters@305: prev_sample_.resize(channel_count_, 10000.0f);
tomwalters@305: curr_sample_.clear();
tomwalters@305: curr_sample_.resize(channel_count_, 5000.0f);
tomwalters@305: next_sample_.clear();
tomwalters@305: next_sample_.resize(channel_count_, 0.0f);
tomwalters@305: }
tomwalters@305:
tomwalters@305: void ModuleLocalMax::Process(const SignalBank &input) {
tomwalters@305: // Check to see if the module has been initialized. If not, processing
tomwalters@305: // should not continue.
tomwalters@305: if (!initialized_) {
tomwalters@305: LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tomwalters@305: return;
tomwalters@305: }
tomwalters@305:
tomwalters@305: // Check that ths input this time is the same as the input passed to
tomwalters@305: // Initialize()
tomwalters@305: if (buffer_length_ != input.buffer_length()
tomwalters@305: || channel_count_ != input.channel_count()) {
tomwalters@305: LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tomwalters@305: "Process() in module %s."), module_identifier_.c_str());
tomwalters@305: return;
tomwalters@305: }
tomwalters@305:
tomwalters@305: for (int ch = 0; ch < output_.channel_count(); ch++) {
tomwalters@305: output_.ResetStrobes(ch);
tomwalters@305: }
tomwalters@305: output_.set_start_time(input.start_time());
tomwalters@305: for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@305: for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@305: // curr_sample is the sample at time (i - 1)
tomwalters@305: prev_sample_[ch] = curr_sample_[ch];
tomwalters@305: curr_sample_[ch] = next_sample_[ch];
tomwalters@305: next_sample_[ch] = input.sample(ch, i);
tomwalters@305: // Copy input signal to output signal
tomwalters@305: output_.set_sample(ch, i, input.sample(ch, i));
tomwalters@305:
tomwalters@305: // If the current sample is above threshold, the threshold is raised to
tomwalters@305: // the level of the current sample, and decays from there.
tomwalters@305: if (curr_sample_[ch] >= threshold_[ch]) {
tomwalters@305: threshold_[ch] = curr_sample_[ch];
tomwalters@305: decay_constant_[ch] = threshold_[ch] / strobe_decay_samples_;
tomwalters@305:
tomwalters@305: // If the current sample is also a peak, then it is a potential strobe
tomwalters@305: // point.
tomwalters@305: if (prev_sample_[ch] < curr_sample_[ch]
tomwalters@305: && next_sample_[ch] < curr_sample_[ch]) {
tomwalters@305: // If there are no strobes so far in this channel, then the sample
tomwalters@305: // is definitely a strobe (this means that the timeout is not
tomwalters@305: // respected across frame boundaries. This is a minor bug, but I
tomwalters@305: // don't believe that it's serious enough to warrant updating the
tomwalters@305: // samples since last strobe all the time.)
tomwalters@305: int count = output_.strobe_count(ch);
tomwalters@305: if (count > 0) {
tomwalters@305: // If there are previous strobes, then calculate the time since
tomwalters@305: // the last one. If it's long enough, then this is a strobe point,
tomwalters@305: // if not, then just move on.
tomwalters@305: int samples_since_last = (i - 1) - output_.strobe(ch, count - 1);
tomwalters@305: if (samples_since_last > strobe_timeout_samples_) {
tomwalters@305: output_.AddStrobe(ch, i - 1);
tomwalters@305: }
tomwalters@305: } else {
tomwalters@305: output_.AddStrobe(ch, i - 1);
tomwalters@305: }
tomwalters@305: }
tomwalters@305: }
tomwalters@305:
tomwalters@305: // Update the threshold, decaying as necessary
tomwalters@305: if (threshold_[ch] > decay_constant_[ch])
tomwalters@305: threshold_[ch] -= decay_constant_[ch];
tomwalters@305: else
tomwalters@305: threshold_[ch] = 0.0f;
tomwalters@305: }
tomwalters@305: }
tomwalters@305: PushOutput();
tomwalters@305: }
tomwalters@305: } // namespace aimc
tomwalters@305: