annotate src/Support/Module.h @ 611:0fbaf443ec82

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