annotate src/Support/Module.h @ 46:c8024714e13e

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