annotate src/Support/SignalBank.cc @ 0:582cbe817f2c

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