annotate trunk/src/Support/SignalBank.cc @ 277:6b4921704eb1

- Ported over HTK file output - Added some more meat to the Slaney IIR gammatone implementation - Ported over the AIM-MAT sf2003 parabola strobe algorithm - Finished making the SAI implementation compile - Ported over the strobe list class (now uses STL deques internally)
author tomwalters
date Thu, 18 Feb 2010 16:55:40 +0000
parents c26222c51fb7
children ed91095d9240
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@277 87 centre_frequencies_[i] = input.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@277 92 strobes_[i].resize(0);
tomwalters@268 93 }
tomwalters@268 94 initialized_ = true;
tomwalters@268 95 return true;
tomwalters@268 96 }
tomwalters@268 97
tomwalters@268 98 bool SignalBank::Validate() const {
tomwalters@268 99 if (sample_rate_ <= 0.0f)
tomwalters@268 100 return false;
tomwalters@268 101
tomwalters@268 102 if (static_cast<int>(signals_.size()) < 1)
tomwalters@268 103 return false;
tomwalters@268 104
tomwalters@268 105 if (static_cast<int>(signals_.size()) != channel_count_)
tomwalters@268 106 return false;
tomwalters@268 107
tomwalters@268 108 if (static_cast<int>(strobes_.size()) != channel_count_)
tomwalters@268 109 return false;
tomwalters@268 110
tomwalters@268 111 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 112 if (static_cast<int>(signals_[i].size()) != buffer_length_)
tomwalters@268 113 return false;
tomwalters@268 114 }
tomwalters@268 115 return true;
tomwalters@268 116 }
tomwalters@268 117
tomwalters@273 118 float SignalBank::sample(int channel, int index) const {
tomwalters@268 119 return signals_[channel][index];
tomwalters@268 120 }
tomwalters@268 121
tomwalters@273 122 void SignalBank::set_sample(int channel, int index, float value) {
tomwalters@268 123 signals_[channel][index] = value;
tomwalters@268 124 }
tomwalters@268 125
tomwalters@277 126 int SignalBank::strobe(int channel, int index) const {
tomwalters@277 127 return strobes_[channel][index];
tomwalters@277 128 }
tomwalters@277 129
tomwalters@277 130 int SignalBank::strobe_count(int channel) const {
tomwalters@277 131 return strobes_[channel].size();
tomwalters@277 132 }
tomwalters@277 133
tomwalters@277 134 void SignalBank::AddStrobe(int channel, int time) {
tomwalters@277 135 strobes_[channel].push_back(time);
tomwalters@277 136 }
tomwalters@277 137
tomwalters@277 138 void SignalBank::ResetStrobes(int channel) {
tomwalters@277 139 strobes_[channel].resize(0);
tomwalters@268 140 }
tomwalters@268 141
tomwalters@268 142 float SignalBank::sample_rate() const {
tomwalters@268 143 return sample_rate_;
tomwalters@268 144 }
tomwalters@268 145
tomwalters@268 146 int SignalBank::buffer_length() const {
tomwalters@268 147 return buffer_length_;
tomwalters@268 148 }
tomwalters@268 149
tomwalters@268 150 int SignalBank::start_time() const {
tomwalters@268 151 return start_time_;
tomwalters@268 152 }
tomwalters@268 153
tomwalters@268 154 void SignalBank::set_start_time(int start_time) {
tomwalters@268 155 start_time_ = start_time;
tomwalters@268 156 }
tomwalters@268 157
tomwalters@277 158 float SignalBank::centre_frequency(int i) const {
tomwalters@268 159 if (i < channel_count_)
tomwalters@268 160 return centre_frequencies_[i];
tomwalters@268 161 else
tomwalters@268 162 return 0.0f;
tomwalters@268 163 }
tomwalters@268 164
tomwalters@268 165 void SignalBank::set_centre_frequency(int i, float cf) {
tomwalters@268 166 if (i < channel_count_)
tomwalters@268 167 centre_frequencies_[i] = cf;
tomwalters@268 168 }
tomwalters@268 169
tomwalters@268 170 bool SignalBank::initialized() const {
tomwalters@268 171 return initialized_;
tomwalters@268 172 }
tomwalters@268 173
tomwalters@268 174 int SignalBank::channel_count() const {
tomwalters@268 175 return channel_count_;
tomwalters@268 176 }
tomwalters@268 177 }