annotate trunk/src/Support/Module.h @ 319:566a8543a6f1

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