tomwalters@0: // Copyright 2010, Thomas Walters tomwalters@0: // tomwalters@0: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@0: // http://www.acousticscale.org/AIMC tomwalters@0: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@0: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@0: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@0: tomwalters@0: /*! \file tomwalters@0: * \brief tomwalters@0: */ tomwalters@0: tomwalters@0: /*! \author: Thomas Walters tomwalters@0: * \date 2010/01/23 tomwalters@0: * \version \$Id$ tomwalters@0: */ tomwalters@0: tomwalters@0: #include "Support/SignalBank.h" tomwalters@0: tomwalters@0: namespace aimc { tomwalters@0: using std::deque; tomwalters@0: using std::vector; tomwalters@0: tomwalters@0: SignalBank::SignalBank() { tomwalters@0: sample_rate_ = 0.0f; tomwalters@0: start_time_ = 0; tomwalters@0: channel_count_ = 0; tomwalters@0: buffer_length_ = 0; tomwalters@0: initialized_ = false; tomwalters@0: } tomwalters@0: tomwalters@0: SignalBank::~SignalBank() { tomwalters@0: } tomwalters@0: tomwalters@0: bool SignalBank::Initialize(int channel_count, tomwalters@0: int signal_length, tomwalters@0: float sample_rate) { tomwalters@0: if (channel_count < 1) tomwalters@0: return false; tomwalters@0: if (signal_length < 1) tomwalters@0: return false; tomwalters@0: if (sample_rate < 0.0f) tomwalters@0: return false; tomwalters@0: tomwalters@0: start_time_ = 0; tomwalters@0: sample_rate_ = sample_rate; tomwalters@0: buffer_length_ = signal_length; tomwalters@0: channel_count_ = channel_count; tomwalters@0: signals_.resize(channel_count_); tomwalters@0: strobes_.resize(channel_count_); tomwalters@0: centre_frequencies_.resize(channel_count_, 0.0f); tomwalters@0: for (int i = 0; i < channel_count_; ++i) { tomwalters@0: signals_[i].resize(buffer_length_, 0.0f); tomwalters@0: } tomwalters@0: initialized_ = true; tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@0: bool SignalBank::Initialize(const SignalBank &input) { tomwalters@0: if (input.channel_count() < 1) tomwalters@0: return false; tomwalters@0: if (input.buffer_length() < 1) tomwalters@0: return false; tomwalters@0: if (input.sample_rate() < 0.0f) tomwalters@0: return false; tomwalters@0: tomwalters@0: start_time_ = input.start_time(); tomwalters@0: sample_rate_ = input.sample_rate(); tomwalters@0: buffer_length_ = input.buffer_length(); tomwalters@0: channel_count_ = input.channel_count(); tomwalters@0: tomwalters@0: signals_.resize(channel_count_); tomwalters@0: strobes_.resize(channel_count_); tomwalters@0: tomwalters@0: centre_frequencies_.resize(channel_count_, 0.0f); tomwalters@0: for (int i = 0; i < channel_count_; ++i) { tomwalters@5: centre_frequencies_[i] = input.centre_frequency(i); tomwalters@0: } tomwalters@0: tomwalters@0: for (int i = 0; i < channel_count_; ++i) { tomwalters@0: signals_[i].resize(buffer_length_, 0.0f); tomwalters@5: strobes_[i].resize(0); tomwalters@0: } tomwalters@0: initialized_ = true; tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@0: bool SignalBank::Validate() const { tomwalters@0: if (sample_rate_ <= 0.0f) tomwalters@0: return false; tomwalters@0: tomwalters@0: if (static_cast(signals_.size()) < 1) tomwalters@0: return false; tomwalters@0: tomwalters@0: if (static_cast(signals_.size()) != channel_count_) tomwalters@0: return false; tomwalters@0: tomwalters@0: if (static_cast(strobes_.size()) != channel_count_) tomwalters@0: return false; tomwalters@0: tomwalters@0: for (int i = 0; i < channel_count_; ++i) { tomwalters@0: if (static_cast(signals_[i].size()) != buffer_length_) tomwalters@0: return false; tomwalters@0: } tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@5: int SignalBank::strobe(int channel, int index) const { tomwalters@5: return strobes_[channel][index]; tomwalters@5: } tomwalters@5: tomwalters@5: int SignalBank::strobe_count(int channel) const { tomwalters@5: return strobes_[channel].size(); tomwalters@5: } tomwalters@5: tomwalters@5: void SignalBank::AddStrobe(int channel, int time) { tomwalters@5: strobes_[channel].push_back(time); tomwalters@5: } tomwalters@5: tomwalters@5: void SignalBank::ResetStrobes(int channel) { tomwalters@5: strobes_[channel].resize(0); tomwalters@0: } tomwalters@0: tomwalters@0: float SignalBank::sample_rate() const { tomwalters@0: return sample_rate_; tomwalters@0: } tomwalters@0: tomwalters@0: int SignalBank::buffer_length() const { tomwalters@0: return buffer_length_; tomwalters@0: } tomwalters@0: tomwalters@0: int SignalBank::start_time() const { tomwalters@0: return start_time_; tomwalters@0: } tomwalters@0: tomwalters@0: void SignalBank::set_start_time(int start_time) { tomwalters@0: start_time_ = start_time; tomwalters@0: } tomwalters@0: tomwalters@5: float SignalBank::centre_frequency(int i) const { tomwalters@0: if (i < channel_count_) tomwalters@0: return centre_frequencies_[i]; tomwalters@0: else tomwalters@0: return 0.0f; tomwalters@0: } tomwalters@0: tomwalters@0: void SignalBank::set_centre_frequency(int i, float cf) { tomwalters@0: if (i < channel_count_) tomwalters@0: centre_frequencies_[i] = cf; tomwalters@0: } tomwalters@0: tomwalters@0: bool SignalBank::initialized() const { tomwalters@0: return initialized_; tomwalters@0: } tomwalters@0: tomwalters@0: int SignalBank::channel_count() const { tomwalters@0: return channel_count_; tomwalters@0: } tomwalters@0: }