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@318: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@318: // you may not use this file except in compliance with the License. tomwalters@318: // You may obtain a copy of the License at tomwalters@268: // tomwalters@318: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@268: // tomwalters@318: // Unless required by applicable law or agreed to in writing, software tomwalters@318: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@318: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@318: // See the License for the specific language governing permissions and tomwalters@318: // limitations under the License. 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: tom@420: void SignalBank::Clear() { tom@420: for (int i = 0; i < channel_count_; ++i) { tom@420: signals_[i].assign(buffer_length_, 0.0f); tom@420: strobes_[i].resize(0); tom@420: } tom@420: } tom@420: 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@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: }