annotate trunk/src/Support/SignalBank.cc @ 318:30dde71d0230

- Modified licence from GPL 3 to Apache v2
author tomwalters
date Tue, 30 Mar 2010 22:06:24 +0000
parents ed91095d9240
children 733a11a65f3d
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@318 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@318 7 // you may not use this file except in compliance with the License.
tomwalters@318 8 // You may obtain a copy of the License at
tomwalters@268 9 //
tomwalters@318 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@268 11 //
tomwalters@318 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@318 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@318 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@318 15 // See the License for the specific language governing permissions and
tomwalters@318 16 // limitations under the License.
tomwalters@268 17
tomwalters@268 18 /*! \file
tomwalters@268 19 * \brief
tomwalters@268 20 */
tomwalters@268 21
tomwalters@268 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@268 23 * \date 2010/01/23
tomwalters@268 24 * \version \$Id$
tomwalters@268 25 */
tomwalters@268 26
tomwalters@268 27 #include "Support/SignalBank.h"
tomwalters@268 28
tomwalters@268 29 namespace aimc {
tomwalters@268 30 using std::deque;
tomwalters@268 31 using std::vector;
tomwalters@268 32
tomwalters@268 33 SignalBank::SignalBank() {
tomwalters@268 34 sample_rate_ = 0.0f;
tomwalters@268 35 start_time_ = 0;
tomwalters@268 36 channel_count_ = 0;
tomwalters@268 37 buffer_length_ = 0;
tomwalters@268 38 initialized_ = false;
tomwalters@268 39 }
tomwalters@268 40
tomwalters@268 41 SignalBank::~SignalBank() {
tomwalters@268 42 }
tomwalters@268 43
tomwalters@268 44 bool SignalBank::Initialize(int channel_count,
tomwalters@268 45 int signal_length,
tomwalters@268 46 float sample_rate) {
tomwalters@268 47 if (channel_count < 1)
tomwalters@268 48 return false;
tomwalters@268 49 if (signal_length < 1)
tomwalters@268 50 return false;
tomwalters@268 51 if (sample_rate < 0.0f)
tomwalters@268 52 return false;
tomwalters@268 53
tomwalters@268 54 start_time_ = 0;
tomwalters@268 55 sample_rate_ = sample_rate;
tomwalters@268 56 buffer_length_ = signal_length;
tomwalters@268 57 channel_count_ = channel_count;
tomwalters@268 58 signals_.resize(channel_count_);
tomwalters@268 59 strobes_.resize(channel_count_);
tomwalters@268 60 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 61 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 62 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@268 63 }
tomwalters@268 64 initialized_ = true;
tomwalters@268 65 return true;
tomwalters@268 66 }
tomwalters@268 67
tomwalters@268 68 bool SignalBank::Initialize(const SignalBank &input) {
tomwalters@268 69 if (input.channel_count() < 1)
tomwalters@268 70 return false;
tomwalters@268 71 if (input.buffer_length() < 1)
tomwalters@268 72 return false;
tomwalters@268 73 if (input.sample_rate() < 0.0f)
tomwalters@268 74 return false;
tomwalters@268 75
tomwalters@268 76 start_time_ = input.start_time();
tomwalters@268 77 sample_rate_ = input.sample_rate();
tomwalters@268 78 buffer_length_ = input.buffer_length();
tomwalters@268 79 channel_count_ = input.channel_count();
tomwalters@268 80
tomwalters@268 81 signals_.resize(channel_count_);
tomwalters@268 82 strobes_.resize(channel_count_);
tomwalters@268 83
tomwalters@268 84 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 85 for (int i = 0; i < channel_count_; ++i) {
tomwalters@277 86 centre_frequencies_[i] = input.centre_frequency(i);
tomwalters@268 87 }
tomwalters@268 88
tomwalters@268 89 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 90 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@277 91 strobes_[i].resize(0);
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@277 117 int SignalBank::strobe(int channel, int index) const {
tomwalters@277 118 return strobes_[channel][index];
tomwalters@277 119 }
tomwalters@277 120
tomwalters@277 121 int SignalBank::strobe_count(int channel) const {
tomwalters@277 122 return strobes_[channel].size();
tomwalters@277 123 }
tomwalters@277 124
tomwalters@277 125 void SignalBank::AddStrobe(int channel, int time) {
tomwalters@277 126 strobes_[channel].push_back(time);
tomwalters@277 127 }
tomwalters@277 128
tomwalters@277 129 void SignalBank::ResetStrobes(int channel) {
tomwalters@277 130 strobes_[channel].resize(0);
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@277 149 float SignalBank::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 }