annotate trunk/src/Support/Module.cc @ 335:71c438f9daf7

- Scripts for running recognition experiments using AIM-C and HTK to compare MFCCs against features generated with AIM-C
author tomwalters
date Wed, 04 Aug 2010 06:41:56 +0000
parents 0f54006e91ea
children 7a573750b186
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 Base class for all AIM-C modules.
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/Module.h"
tomwalters@268 28
tomwalters@268 29 #include <utility>
tomwalters@268 30
tomwalters@268 31 namespace aimc {
tomwalters@268 32 using std::pair;
tomwalters@323 33 using std::ostream;
tomwalters@268 34 Module::Module(Parameters *parameters) {
tomwalters@268 35 initialized_ = false;
tomwalters@268 36 targets_.clear();
tomwalters@268 37 parameters_ = parameters;
tomwalters@268 38 module_identifier_ = "MODULE IDENTIFIER NOT SET";
tomwalters@268 39 module_type_ = "MODULE TYPE NOT SET";
tomwalters@268 40 module_description_ = "MODULE DESCRIPTION NOT SET";
tomwalters@268 41 module_version_ = "MODULE VERSION NOT SET";
tomwalters@268 42 };
tomwalters@268 43
tomwalters@268 44 Module::~Module() {
tomwalters@268 45 };
tomwalters@268 46
tomwalters@268 47 bool Module::Initialize(const SignalBank &input) {
tomwalters@296 48 // LOG_INFO_NN(_T("-> %s "), module_identifier_.c_str());
tomwalters@268 49 // Validate the input
tomwalters@268 50 if (!input.Validate()) {
tomwalters@278 51 LOG_ERROR(_T("Input SignalBank not valid"));
tomwalters@268 52 return false;
tomwalters@268 53 }
tomwalters@273 54 if (!InitializeInternal(input)) {
tomwalters@278 55 LOG_ERROR(_T("Initialization failed in module %s"),
tomwalters@278 56 module_identifier_.c_str());
tomwalters@268 57 return false;
tomwalters@268 58 }
tomwalters@268 59 // If the module has an output bank, then we can set up the targets
tomwalters@268 60 // of this module..
tomwalters@268 61 if (output_.initialized()) {
tomwalters@268 62 // Check that the output SignalBank has been set up correctly
tomwalters@268 63 if (!output_.Validate()) {
tomwalters@278 64 LOG_ERROR(_T("Output SignalBank not valid in module %s"),
tomwalters@278 65 module_identifier_.c_str());
tomwalters@268 66 return false;
tomwalters@268 67 }
tomwalters@268 68 // Iterate through all the targets of this module, initializing
tomwalters@268 69 // them if they have not already been initialized. If they have
tomwalters@268 70 // already been initialized, then they are assumed to have been
tomwalters@268 71 // set up to accept an input SignalBank of the correct form, but
tomwalters@268 72 // this is not checked.
tomwalters@268 73 set<Module*>::const_iterator it;
tomwalters@268 74 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 75 if (!(*it)->initialized())
tomwalters@268 76 if (!(*it)->Initialize(output_))
tomwalters@268 77 return false;
tomwalters@268 78 }
tomwalters@278 79 } else {
tomwalters@296 80 // LOG_INFO(_T("|"));
tomwalters@268 81 }
tomwalters@268 82 initialized_ = true;
tomwalters@268 83 return true;
tomwalters@268 84 }
tomwalters@268 85
tomwalters@275 86 void Module::Reset() {
tomwalters@275 87 if (!initialized_)
tomwalters@275 88 return;
tomwalters@275 89
tomwalters@296 90 // LOG_INFO("Resetting module %s", module_identifier_.c_str());
tomwalters@275 91 ResetInternal();
tomwalters@275 92
tomwalters@275 93 // Iterate through all the targets of this module, resetting
tomwalters@275 94 // them.
tomwalters@275 95 set<Module*>::const_iterator it;
tomwalters@275 96 for (it = targets_.begin(); it != targets_.end(); ++it)
tomwalters@275 97 (*it)->Reset();
tomwalters@275 98 }
tomwalters@275 99
tomwalters@268 100 bool Module::initialized() const {
tomwalters@268 101 return initialized_;
tomwalters@268 102 }
tomwalters@268 103
tomwalters@268 104 bool Module::AddTarget(Module* target_module) {
tomwalters@268 105 if (target_module) {
tomwalters@268 106 pair<set<Module*>::iterator, bool> ret;
tomwalters@268 107 ret = targets_.insert(target_module);
tomwalters@268 108 return ret.second;
tomwalters@268 109 }
tomwalters@268 110 return false;
tomwalters@268 111 }
tomwalters@268 112
tomwalters@281 113 bool Module::RemoveTarget(Module* target_module) {
tomwalters@268 114 if (targets_.erase(target_module) != 0)
tomwalters@268 115 return true;
tomwalters@268 116 return false;
tomwalters@268 117 }
tomwalters@268 118
tomwalters@281 119 void Module::RemoveAllTargets() {
tomwalters@268 120 targets_.clear();
tomwalters@268 121 }
tomwalters@268 122
tomwalters@268 123 const SignalBank* Module::GetOutputBank() const {
tomwalters@268 124 return &output_;
tomwalters@268 125 }
tomwalters@268 126
tomwalters@268 127 void Module::PushOutput() {
tomwalters@268 128 if (output_.initialized()) {
tomwalters@268 129 set<Module*>::const_iterator it;
tomwalters@268 130 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@268 131 (*it)->Process(output_);
tomwalters@268 132 }
tomwalters@268 133 }
tomwalters@268 134 }
tomwalters@323 135
tomwalters@323 136 void Module::PrintTargets(ostream &out) {
tomwalters@323 137 out << id();
tomwalters@323 138 if (targets_.size() > 0) {
tomwalters@323 139 out << "->(";
tomwalters@323 140 set<Module*>::const_iterator it;
tomwalters@323 141 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@323 142 (*it)->PrintTargets(out);
tomwalters@323 143 if (targets_.size() > 1) {
tomwalters@323 144 out << ",";
tomwalters@323 145 }
tomwalters@323 146 }
tomwalters@323 147 out << ")";
tomwalters@323 148 }
tomwalters@323 149 }
tomwalters@323 150
tomwalters@323 151 void Module::PrintVersions(ostream &out) {
tomwalters@323 152 out << version() << "\n";
tomwalters@323 153 set<Module*>::const_iterator it;
tomwalters@323 154 for (it = targets_.begin(); it != targets_.end(); ++it) {
tomwalters@323 155 (*it)->PrintVersions(out);
tomwalters@323 156 }
tomwalters@323 157 }
tomwalters@280 158 } // namespace aimc
tomwalters@268 159