annotate trunk/src/Support/SignalBank.cc @ 268:e14c70d1b171

- Initial add of support code and modules. Not everything is working yet.
author tomwalters
date Fri, 12 Feb 2010 12:31:23 +0000
parents
children c26222c51fb7
rev   line source
tomwalters@268 1 // Copyright 2010, Thomas Walters
tomwalters@268 2 //
tomwalters@268 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@268 4 // http://www.acousticscale.org/AIMC
tomwalters@268 5 //
tomwalters@268 6 // This program is free software: you can redistribute it and/or modify
tomwalters@268 7 // it under the terms of the GNU General Public License as published by
tomwalters@268 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@268 9 // (at your option) any later version.
tomwalters@268 10 //
tomwalters@268 11 // This program is distributed in the hope that it will be useful,
tomwalters@268 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@268 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@268 14 // GNU General Public License for more details.
tomwalters@268 15 //
tomwalters@268 16 // You should have received a copy of the GNU General Public License
tomwalters@268 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@268 18
tomwalters@268 19 /*! \file
tomwalters@268 20 * \brief
tomwalters@268 21 */
tomwalters@268 22
tomwalters@268 23 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@268 24 * \date 2010/01/23
tomwalters@268 25 * \version \$Id$
tomwalters@268 26 */
tomwalters@268 27
tomwalters@268 28 #include "Support/SignalBank.h"
tomwalters@268 29
tomwalters@268 30 namespace aimc {
tomwalters@268 31 using std::deque;
tomwalters@268 32 using std::vector;
tomwalters@268 33
tomwalters@268 34 SignalBank::SignalBank() {
tomwalters@268 35 sample_rate_ = 0.0f;
tomwalters@268 36 start_time_ = 0;
tomwalters@268 37 channel_count_ = 0;
tomwalters@268 38 buffer_length_ = 0;
tomwalters@268 39 initialized_ = false;
tomwalters@268 40 }
tomwalters@268 41
tomwalters@268 42 SignalBank::~SignalBank() {
tomwalters@268 43 }
tomwalters@268 44
tomwalters@268 45 bool SignalBank::Initialize(int channel_count,
tomwalters@268 46 int signal_length,
tomwalters@268 47 float sample_rate) {
tomwalters@268 48 if (channel_count < 1)
tomwalters@268 49 return false;
tomwalters@268 50 if (signal_length < 1)
tomwalters@268 51 return false;
tomwalters@268 52 if (sample_rate < 0.0f)
tomwalters@268 53 return false;
tomwalters@268 54
tomwalters@268 55 start_time_ = 0;
tomwalters@268 56 sample_rate_ = sample_rate;
tomwalters@268 57 buffer_length_ = signal_length;
tomwalters@268 58 channel_count_ = channel_count;
tomwalters@268 59 signals_.resize(channel_count_);
tomwalters@268 60 strobes_.resize(channel_count_);
tomwalters@268 61 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 62 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 63 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@268 64 }
tomwalters@268 65 initialized_ = true;
tomwalters@268 66 return true;
tomwalters@268 67 }
tomwalters@268 68
tomwalters@268 69 bool SignalBank::Initialize(const SignalBank &input) {
tomwalters@268 70 if (input.channel_count() < 1)
tomwalters@268 71 return false;
tomwalters@268 72 if (input.buffer_length() < 1)
tomwalters@268 73 return false;
tomwalters@268 74 if (input.sample_rate() < 0.0f)
tomwalters@268 75 return false;
tomwalters@268 76
tomwalters@268 77 start_time_ = input.start_time();
tomwalters@268 78 sample_rate_ = input.sample_rate();
tomwalters@268 79 buffer_length_ = input.buffer_length();
tomwalters@268 80 channel_count_ = input.channel_count();
tomwalters@268 81
tomwalters@268 82 signals_.resize(channel_count_);
tomwalters@268 83 strobes_.resize(channel_count_);
tomwalters@268 84
tomwalters@268 85 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 86 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 87 centre_frequencies_[i] = input.get_centre_frequency(i);
tomwalters@268 88 }
tomwalters@268 89
tomwalters@268 90 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 91 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@268 92 }
tomwalters@268 93 initialized_ = true;
tomwalters@268 94 return true;
tomwalters@268 95 }
tomwalters@268 96
tomwalters@268 97 bool SignalBank::Validate() const {
tomwalters@268 98 if (sample_rate_ <= 0.0f)
tomwalters@268 99 return false;
tomwalters@268 100
tomwalters@268 101 if (static_cast<int>(signals_.size()) < 1)
tomwalters@268 102 return false;
tomwalters@268 103
tomwalters@268 104 if (static_cast<int>(signals_.size()) != channel_count_)
tomwalters@268 105 return false;
tomwalters@268 106
tomwalters@268 107 if (static_cast<int>(strobes_.size()) != channel_count_)
tomwalters@268 108 return false;
tomwalters@268 109
tomwalters@268 110 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 111 if (static_cast<int>(signals_[i].size()) != buffer_length_)
tomwalters@268 112 return false;
tomwalters@268 113 }
tomwalters@268 114 return true;
tomwalters@268 115 }
tomwalters@268 116
tomwalters@268 117 inline const vector<float> &SignalBank::operator[](int channel) const {
tomwalters@268 118 return signals_[channel];
tomwalters@268 119 }
tomwalters@268 120
tomwalters@268 121 inline float SignalBank::sample(int channel, int index) const {
tomwalters@268 122 return signals_[channel][index];
tomwalters@268 123 }
tomwalters@268 124
tomwalters@268 125 inline void SignalBank::set_sample(int channel, int index, float value) {
tomwalters@268 126 signals_[channel][index] = value;
tomwalters@268 127 }
tomwalters@268 128
tomwalters@268 129 const deque<int> &SignalBank::strobes(int channel) const {
tomwalters@268 130 return strobes_[channel];
tomwalters@268 131 }
tomwalters@268 132
tomwalters@268 133 float SignalBank::sample_rate() const {
tomwalters@268 134 return sample_rate_;
tomwalters@268 135 }
tomwalters@268 136
tomwalters@268 137 int SignalBank::buffer_length() const {
tomwalters@268 138 return buffer_length_;
tomwalters@268 139 }
tomwalters@268 140
tomwalters@268 141 int SignalBank::start_time() const {
tomwalters@268 142 return start_time_;
tomwalters@268 143 }
tomwalters@268 144
tomwalters@268 145 void SignalBank::set_start_time(int start_time) {
tomwalters@268 146 start_time_ = start_time;
tomwalters@268 147 }
tomwalters@268 148
tomwalters@268 149 float SignalBank::get_centre_frequency(int i) const {
tomwalters@268 150 if (i < channel_count_)
tomwalters@268 151 return centre_frequencies_[i];
tomwalters@268 152 else
tomwalters@268 153 return 0.0f;
tomwalters@268 154 }
tomwalters@268 155
tomwalters@268 156 void SignalBank::set_centre_frequency(int i, float cf) {
tomwalters@268 157 if (i < channel_count_)
tomwalters@268 158 centre_frequencies_[i] = cf;
tomwalters@268 159 }
tomwalters@268 160
tomwalters@268 161 bool SignalBank::initialized() const {
tomwalters@268 162 return initialized_;
tomwalters@268 163 }
tomwalters@268 164
tomwalters@268 165 int SignalBank::channel_count() const {
tomwalters@268 166 return channel_count_;
tomwalters@268 167 }
tomwalters@268 168 }