annotate trunk/src/Support/Module.h @ 284:fb52ca0e6339

-Profile module for taking slices of an SAI or SSI (or anything else for that matter) -Stub SSI module - not yet complete -Fixes to the module template
author tomwalters
date Fri, 19 Feb 2010 13:07:54 +0000
parents ef14c9f2c1d2
children fe5ce00a64f5
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 Base class for all AIM-C modules.
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: Module.h 4 2010-02-03 18:44:58Z tcw $
tomwalters@268 26 */
tomwalters@268 27
tomwalters@283 28 #ifndef AIMC_SUPPORT_MODULE_H_
tomwalters@283 29 #define AIMC_SUPPORT_MODULE_H_
tomwalters@268 30
tomwalters@268 31 #include <set>
tomwalters@268 32 #include <string>
tomwalters@268 33
tomwalters@268 34 #include "Support/Common.h"
tomwalters@268 35 #include "Support/Parameters.h"
tomwalters@268 36 #include "Support/SignalBank.h"
tomwalters@268 37
tomwalters@268 38 namespace aimc {
tomwalters@268 39 using std::set;
tomwalters@268 40 using std::string;
tomwalters@281 41
tomwalters@281 42 /*! \brief Base class for all AIM-C modules.
tomwalters@281 43 *
tomwalters@281 44 * Module() is a base class, from which all AIM-C modules are derived.
tomwalters@281 45 * Classes deriving from module need to implement, at minimum, the pure
tomwalters@281 46 * virtual public function Module::Process() and the pure virtual private
tomwalters@281 47 * functions Module::InitializeInternal() and Module::ResetInternal().
tomwalters@281 48 * (Note: this is in contravention of
tomwalters@281 49 * <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Inheritance#Inheritance">
tomwalters@281 50 * this rule on inheritance</a> in the Google style guide, but it is designed
tomwalters@281 51 * to make the implementation of modules as simple as possible.)
tomwalters@281 52 *
tomwalters@281 53 * The module constructor is called with a pointer to a set of Parameters.
tomwalters@281 54 * In the constructor, the module sets the defaults for its various
tomwalters@281 55 * parameters.
tomwalters@281 56 * A module is initialized with a reference to a valid SignalBank. After the
tomwalters@281 57 * Initialize(SignalBank*) function has been called, a call to GetOutputBank()
tomwalters@281 58 * returns a pointer to a SignalBank in which the results
tomwalters@281 59 * of the module's processing will be placed. Modules can use the output_
tomwalters@281 60 * SignalBank to store their output, or leave it uninitialized if they do not
tomwalters@281 61 * produce an output.
tomwalters@281 62 * At each call to Process(input), the module takes the
tomwalters@281 63 * SignalBank 'input' (which must, unless otherwise specified, have the same
tomwalters@281 64 * number of channels, sample rate, buffer size and centre frequencies as the
tomwalters@281 65 * SignalBank which was passed to Initialize()), processes it, and places the
tomwalters@281 66 * output in the internal SignalBank output_.
tomwalters@281 67 * Modules can have an arbitrary number of unique targets. Each
tomwalters@281 68 * completed output frame is 'pushed' to all of the targets of the module
tomwalters@281 69 * in turn when PushOutput() is called. To achieve this, after each complete
tomwalters@281 70 * output SignalBank is filled, the module calls the Process() function of
tomwalters@281 71 * each of its targets in turn.
tomwalters@281 72 * When Initialize() is first called. The module Initialize()s all of its
tomwalters@281 73 * targets with its ouptut_ SignalBank, if its output bank has been set up.
tomwalters@281 74 *
tomwalters@281 75 */
tomwalters@268 76 class Module {
tomwalters@268 77 public:
tomwalters@268 78 explicit Module(Parameters *parameters);
tomwalters@268 79
tomwalters@268 80 virtual ~Module();
tomwalters@268 81
tomwalters@281 82 /* \brief Initialize the module, including calling InitializeInternal().
tomwalters@281 83 * Validate this module's output SignalBank, and initialize
tomwalters@268 84 * any targets of the module if necessary.
tomwalters@268 85 * \param input Input SignalBank.
tomwalters@281 86 * \return true on success, false on failure.
tomwalters@275 87 *
tomwalters@281 88 * A call to Initialize() will first validate the input SignalBank passed to
tomwalters@281 89 * it. If this SignalBank is valid, then it will call the
tomwalters@281 90 * InitializeInternal() function of the child class; this will set up the
tomwalters@281 91 * child class, and may, if the module produces an output, initialize the
tomwalters@281 92 * member variable SignalBank output_. If output_ is initialized after the
tomwalters@281 93 * call to InitializeInternal(), the module will Initialize its targets with
tomwalters@281 94 * the output. In this way, it is possible to initialize an entire module
tomwalters@281 95 * tree with a single call to the Initialize() function of the root.
tomwalters@281 96 *
tomwalters@281 97 * This function is declared virtual in order to deal with the edge case of
tomwalters@281 98 * input modules which do not take a SignalBank as input, but rather
tomwalters@281 99 * generate their own input. In this case, it is better to be able to
tomwalters@281 100 * override the default Initialize function. When creating a new module, do
tomwalters@281 101 * not create a new version of Initialize uness you're sure you know what
tomwalters@281 102 * you're doing!
tomwalters@268 103 */
tomwalters@275 104 virtual bool Initialize(const SignalBank &input);
tomwalters@268 105
tomwalters@281 106 /*! \brief Returns true if the module has been correctly initialized
tomwalters@281 107 * \return true if module has been initialized, false otherwise
tomwalters@268 108 */
tomwalters@268 109 bool initialized() const;
tomwalters@268 110
tomwalters@268 111 /* \brief Add a target to this module. Whenever it generates a new
tomwalters@268 112 * output, this module will push its output to all its targets.
tomwalters@281 113 * \param target_module Pointer to a target Module to add.
tomwalters@281 114 * \return true on success, false on failure.
tomwalters@281 115 *
tomwalters@281 116 * When a pointer is passed as a target to the module, the caller retains
tomwalters@281 117 * ownership of the module that it points to. The pointed-to module must
tomwalters@281 118 * continue to exist until it is deleted from the target list by a call
tomwalters@281 119 * to DeleteTarget() or DeleteAllTargets(), or the current module is
tomwalters@281 120 * destroyed. Bad things will happen if the Module pointed to is deleted
tomwalters@281 121 * and Initialize(), Reset() or Process() is subsequently called.
tomwalters@268 122 */
tomwalters@268 123 bool AddTarget(Module* target_module);
tomwalters@268 124
tomwalters@281 125 /*! \brief Remove a previously added target module from the list of targets
tomwalters@281 126 * for this module.
tomwalters@281 127 * \param target_module Pointer to the module to remove. This must be a
tomwalters@281 128 * pointer that was previously passed to AddTarget()
tomwalters@281 129 * \return true on success, false on failure.
tomwalters@268 130 */
tomwalters@281 131 bool RemoveTarget(Module* target_module);
tomwalters@268 132
tomwalters@281 133 /*! \brief Remove all previously added target modules from the list of
tomwalters@281 134 * targets for this module.
tomwalters@268 135 */
tomwalters@281 136 void RemoveAllTargets();
tomwalters@268 137
tomwalters@281 138 /*! \brief Process a buffer.
tomwalters@281 139 * \param input SignalBank of the form which was passed to Initialize()
tomwalters@281 140 *
tomwalters@281 141 * Process is called once for each input SignalBank. When implemented in
tomwalters@281 142 * classes inheriting from aimc::Module, P
tomwalters@281 143 * this SignalBank contains the (read-only) input to the module. It is
tomwalters@281 144 * expected that the input SignalBank will have the same number of channels,
tomwalters@281 145 * buffer length and sample rate as the SignalBank passed to Initialize.
tomwalters@268 146 */
tomwalters@268 147 virtual void Process(const SignalBank &input) = 0;
tomwalters@268 148
tomwalters@275 149 /*! \brief Reset the internal state of this module and all its children to
tomwalters@275 150 * their initial state.
tomwalters@281 151 *
tomwalters@281 152 * Like a call to Initialize() will cause all target modules to be
tomwalters@281 153 * initialized, a call to Reset() will cause all target modules to be reset.
tomwalters@268 154 */
tomwalters@280 155 void Reset();
tomwalters@268 156
tomwalters@281 157 /*! \brief Return a pointer to the output SignalBank_ for this class.
tomwalters@281 158 * \return pointer to the SignalBank that this module uses to store its
tomwalters@281 159 * output.
tomwalters@268 160 */
tomwalters@268 161 const SignalBank* GetOutputBank() const;
tomwalters@268 162
tomwalters@268 163 protected:
tomwalters@268 164 void PushOutput();
tomwalters@268 165
tomwalters@275 166 virtual void ResetInternal() = 0;
tomwalters@275 167
tomwalters@268 168 virtual bool InitializeInternal(const SignalBank &input) = 0;
tomwalters@268 169
tomwalters@268 170 bool initialized_;
tomwalters@268 171 set<Module*> targets_;
tomwalters@268 172 SignalBank output_;
tomwalters@268 173 Parameters* parameters_;
tomwalters@268 174
tomwalters@268 175 string module_identifier_;
tomwalters@268 176 string module_type_;
tomwalters@268 177 string module_description_;
tomwalters@268 178 string module_version_;
tomwalters@268 179
tomwalters@268 180 private:
tomwalters@268 181 DISALLOW_COPY_AND_ASSIGN(Module);
tomwalters@268 182 };
tomwalters@268 183 }
tomwalters@268 184
tomwalters@283 185 #endif // AIMC_SUPPORT_MODULE_H_