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@0: // This program is free software: you can redistribute it and/or modify
tomwalters@0: // it under the terms of the GNU General Public License as published by
tomwalters@0: // the Free Software Foundation, either version 3 of the License, or
tomwalters@0: // (at your option) any later version.
tomwalters@0: //
tomwalters@0: // This program is distributed in the hope that it will be useful,
tomwalters@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@0: // GNU General Public License for more details.
tomwalters@0: //
tomwalters@0: // You should have received a copy of the GNU General Public License
tomwalters@0: // along with this program. If not, see .
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@6: 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@6: 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@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: