annotate src/Support/SignalBank.cc @ 45:c5f5e9569863

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