tomwalters@0: // Copyright 2010, Thomas Walters tomwalters@0: // tomwalters@0: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@0: // http://www.acousticscale.org/AIMC tomwalters@0: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@0: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@0: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@0: tomwalters@0: /*! \file tomwalters@0: * \brief Base class for all AIM-C modules. tomwalters@0: */ tomwalters@0: tomwalters@0: /*! \author: Thomas Walters tomwalters@0: * \date 2010/01/23 tomwalters@0: * \version \$Id$ tomwalters@0: */ tomwalters@0: tomwalters@0: #include "Support/Module.h" tomwalters@0: tomwalters@0: #include tomwalters@0: tomwalters@0: namespace aimc { tomwalters@0: using std::pair; tomwalters@0: Module::Module(Parameters *parameters) { tomwalters@0: initialized_ = false; tomwalters@0: targets_.clear(); tomwalters@0: parameters_ = parameters; tomwalters@0: module_identifier_ = "MODULE IDENTIFIER NOT SET"; tomwalters@0: module_type_ = "MODULE TYPE NOT SET"; tomwalters@0: module_description_ = "MODULE DESCRIPTION NOT SET"; tomwalters@0: module_version_ = "MODULE VERSION NOT SET"; tomwalters@0: }; tomwalters@0: tomwalters@0: Module::~Module() { tomwalters@0: }; tomwalters@0: tomwalters@0: bool Module::Initialize(const SignalBank &input) { tomwalters@23: // LOG_INFO_NN(_T("-> %s "), module_identifier_.c_str()); tomwalters@0: // Validate the input tomwalters@0: if (!input.Validate()) { tomwalters@6: LOG_ERROR(_T("Input SignalBank not valid")); tomwalters@0: return false; tomwalters@0: } tomwalters@1: if (!InitializeInternal(input)) { tomwalters@6: LOG_ERROR(_T("Initialization failed in module %s"), tomwalters@6: module_identifier_.c_str()); tomwalters@0: return false; tomwalters@0: } tomwalters@0: // If the module has an output bank, then we can set up the targets tomwalters@0: // of this module.. tomwalters@0: if (output_.initialized()) { tomwalters@0: // Check that the output SignalBank has been set up correctly tomwalters@0: if (!output_.Validate()) { tomwalters@6: LOG_ERROR(_T("Output SignalBank not valid in module %s"), tomwalters@6: module_identifier_.c_str()); tomwalters@0: return false; tomwalters@0: } tomwalters@0: // Iterate through all the targets of this module, initializing tomwalters@0: // them if they have not already been initialized. If they have tomwalters@0: // already been initialized, then they are assumed to have been tomwalters@0: // set up to accept an input SignalBank of the correct form, but tomwalters@0: // this is not checked. tomwalters@0: set::const_iterator it; tomwalters@0: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@0: if (!(*it)->initialized()) tomwalters@0: if (!(*it)->Initialize(output_)) tomwalters@0: return false; tomwalters@0: } tomwalters@6: } else { tomwalters@23: // LOG_INFO(_T("|")); tomwalters@0: } tomwalters@0: initialized_ = true; tomwalters@0: return true; tomwalters@0: } tomwalters@0: tomwalters@3: void Module::Reset() { tomwalters@3: if (!initialized_) tomwalters@3: return; tomwalters@3: tomwalters@23: // LOG_INFO("Resetting module %s", module_identifier_.c_str()); tomwalters@3: ResetInternal(); tomwalters@3: tomwalters@3: // Iterate through all the targets of this module, resetting tomwalters@3: // them. tomwalters@3: set::const_iterator it; tomwalters@3: for (it = targets_.begin(); it != targets_.end(); ++it) tomwalters@3: (*it)->Reset(); tomwalters@3: } tomwalters@3: tomwalters@0: bool Module::initialized() const { tomwalters@0: return initialized_; tomwalters@0: } tomwalters@0: tomwalters@0: bool Module::AddTarget(Module* target_module) { tomwalters@0: if (target_module) { tomwalters@0: pair::iterator, bool> ret; tomwalters@0: ret = targets_.insert(target_module); tomwalters@0: return ret.second; tomwalters@0: } tomwalters@0: return false; tomwalters@0: } tomwalters@0: tomwalters@9: bool Module::RemoveTarget(Module* target_module) { tomwalters@0: if (targets_.erase(target_module) != 0) tomwalters@0: return true; tomwalters@0: return false; tomwalters@0: } tomwalters@0: tomwalters@9: void Module::RemoveAllTargets() { tomwalters@0: targets_.clear(); tomwalters@0: } tomwalters@0: tomwalters@0: const SignalBank* Module::GetOutputBank() const { tomwalters@0: return &output_; tomwalters@0: } tomwalters@0: tomwalters@0: void Module::PushOutput() { tomwalters@0: if (output_.initialized()) { tomwalters@0: set::const_iterator it; tomwalters@0: for (it = targets_.begin(); it != targets_.end(); ++it) { tomwalters@0: (*it)->Process(output_); tomwalters@0: } tomwalters@0: } tomwalters@0: } tomwalters@8: } // namespace aimc tomwalters@0: