tomwalters@268: // Copyright 2010, Thomas Walters tomwalters@268: // tomwalters@268: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@268: // http://www.acousticscale.org/AIMC tomwalters@268: // tomwalters@268: // This program is free software: you can redistribute it and/or modify tomwalters@268: // it under the terms of the GNU General Public License as published by tomwalters@268: // the Free Software Foundation, either version 3 of the License, or tomwalters@268: // (at your option) any later version. tomwalters@268: // tomwalters@268: // This program is distributed in the hope that it will be useful, tomwalters@268: // but WITHOUT ANY WARRANTY; without even the implied warranty of tomwalters@268: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tomwalters@268: // GNU General Public License for more details. tomwalters@268: // tomwalters@268: // You should have received a copy of the GNU General Public License tomwalters@268: // along with this program. If not, see . tomwalters@268: tomwalters@268: /*! \file tomwalters@268: * \brief tomwalters@268: */ tomwalters@268: tomwalters@268: /*! \author: Thomas Walters tomwalters@268: * \date 2010/01/23 tomwalters@268: * \version \$Id$ tomwalters@268: */ tomwalters@268: tomwalters@268: #include "Support/SignalBank.h" tomwalters@268: tomwalters@268: namespace aimc { tomwalters@268: using std::deque; tomwalters@268: using std::vector; tomwalters@268: tomwalters@268: SignalBank::SignalBank() { tomwalters@268: sample_rate_ = 0.0f; tomwalters@268: start_time_ = 0; tomwalters@268: channel_count_ = 0; tomwalters@268: buffer_length_ = 0; tomwalters@268: initialized_ = false; tomwalters@268: } tomwalters@268: tomwalters@268: SignalBank::~SignalBank() { tomwalters@268: } tomwalters@268: tomwalters@268: bool SignalBank::Initialize(int channel_count, tomwalters@268: int signal_length, tomwalters@268: float sample_rate) { tomwalters@268: if (channel_count < 1) tomwalters@268: return false; tomwalters@268: if (signal_length < 1) tomwalters@268: return false; tomwalters@268: if (sample_rate < 0.0f) tomwalters@268: return false; tomwalters@268: tomwalters@268: start_time_ = 0; tomwalters@268: sample_rate_ = sample_rate; tomwalters@268: buffer_length_ = signal_length; tomwalters@268: channel_count_ = channel_count; tomwalters@268: signals_.resize(channel_count_); tomwalters@268: strobes_.resize(channel_count_); tomwalters@268: centre_frequencies_.resize(channel_count_, 0.0f); tomwalters@268: for (int i = 0; i < channel_count_; ++i) { tomwalters@268: signals_[i].resize(buffer_length_, 0.0f); tomwalters@268: } tomwalters@268: initialized_ = true; tomwalters@268: return true; tomwalters@268: } tomwalters@268: tomwalters@268: bool SignalBank::Initialize(const SignalBank &input) { tomwalters@268: if (input.channel_count() < 1) tomwalters@268: return false; tomwalters@268: if (input.buffer_length() < 1) tomwalters@268: return false; tomwalters@268: if (input.sample_rate() < 0.0f) tomwalters@268: return false; tomwalters@268: tomwalters@268: start_time_ = input.start_time(); tomwalters@268: sample_rate_ = input.sample_rate(); tomwalters@268: buffer_length_ = input.buffer_length(); tomwalters@268: channel_count_ = input.channel_count(); tomwalters@268: tomwalters@268: signals_.resize(channel_count_); tomwalters@268: strobes_.resize(channel_count_); tomwalters@268: tomwalters@268: centre_frequencies_.resize(channel_count_, 0.0f); tomwalters@268: for (int i = 0; i < channel_count_; ++i) { tomwalters@277: centre_frequencies_[i] = input.centre_frequency(i); tomwalters@268: } tomwalters@268: tomwalters@268: for (int i = 0; i < channel_count_; ++i) { tomwalters@268: signals_[i].resize(buffer_length_, 0.0f); tomwalters@277: strobes_[i].resize(0); tomwalters@268: } tomwalters@268: initialized_ = true; tomwalters@268: return true; tomwalters@268: } tomwalters@268: tomwalters@268: bool SignalBank::Validate() const { tomwalters@268: if (sample_rate_ <= 0.0f) tomwalters@268: return false; tomwalters@268: tomwalters@268: if (static_cast(signals_.size()) < 1) tomwalters@268: return false; tomwalters@268: tomwalters@268: if (static_cast(signals_.size()) != channel_count_) tomwalters@268: return false; tomwalters@268: tomwalters@268: if (static_cast(strobes_.size()) != channel_count_) tomwalters@268: return false; tomwalters@268: tomwalters@268: for (int i = 0; i < channel_count_; ++i) { tomwalters@268: if (static_cast(signals_[i].size()) != buffer_length_) tomwalters@268: return false; tomwalters@268: } tomwalters@268: return true; tomwalters@268: } tomwalters@268: tomwalters@273: float SignalBank::sample(int channel, int index) const { tomwalters@268: return signals_[channel][index]; tomwalters@268: } tomwalters@268: tomwalters@273: void SignalBank::set_sample(int channel, int index, float value) { tomwalters@268: signals_[channel][index] = value; tomwalters@268: } tomwalters@268: tomwalters@277: int SignalBank::strobe(int channel, int index) const { tomwalters@277: return strobes_[channel][index]; tomwalters@277: } tomwalters@277: tomwalters@277: int SignalBank::strobe_count(int channel) const { tomwalters@277: return strobes_[channel].size(); tomwalters@277: } tomwalters@277: tomwalters@277: void SignalBank::AddStrobe(int channel, int time) { tomwalters@277: strobes_[channel].push_back(time); tomwalters@277: } tomwalters@277: tomwalters@277: void SignalBank::ResetStrobes(int channel) { tomwalters@277: strobes_[channel].resize(0); tomwalters@268: } tomwalters@268: tomwalters@268: float SignalBank::sample_rate() const { tomwalters@268: return sample_rate_; tomwalters@268: } tomwalters@268: tomwalters@268: int SignalBank::buffer_length() const { tomwalters@268: return buffer_length_; tomwalters@268: } tomwalters@268: tomwalters@268: int SignalBank::start_time() const { tomwalters@268: return start_time_; tomwalters@268: } tomwalters@268: tomwalters@268: void SignalBank::set_start_time(int start_time) { tomwalters@268: start_time_ = start_time; tomwalters@268: } tomwalters@268: tomwalters@277: float SignalBank::centre_frequency(int i) const { tomwalters@268: if (i < channel_count_) tomwalters@268: return centre_frequencies_[i]; tomwalters@268: else tomwalters@268: return 0.0f; tomwalters@268: } tomwalters@268: tomwalters@268: void SignalBank::set_centre_frequency(int i, float cf) { tomwalters@268: if (i < channel_count_) tomwalters@268: centre_frequencies_[i] = cf; tomwalters@268: } tomwalters@268: tomwalters@268: bool SignalBank::initialized() const { tomwalters@268: return initialized_; tomwalters@268: } tomwalters@268: tomwalters@268: int SignalBank::channel_count() const { tomwalters@268: return channel_count_; tomwalters@268: } tomwalters@268: }