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@0
|
104 */
|
tomwalters@160
|
105 virtual bool Initialize(const SignalBank &input);
|
tomwalters@0
|
106
|
tomwalters@9
|
107 /*! \brief Returns true if the module has been correctly initialized
|
tomwalters@9
|
108 * \return true if module has been initialized, false otherwise
|
tomwalters@0
|
109 */
|
tomwalters@0
|
110 bool initialized() const;
|
tomwalters@0
|
111
|
tomwalters@0
|
112 /* \brief Add a target to this module. Whenever it generates a new
|
tomwalters@0
|
113 * output, this module will push its output to all its targets.
|
tomwalters@9
|
114 * \param target_module Pointer to a target Module to add.
|
tomwalters@9
|
115 * \return true on success, false on failure.
|
tomwalters@9
|
116 *
|
tomwalters@9
|
117 * When a pointer is passed as a target to the module, the caller retains
|
tomwalters@9
|
118 * ownership of the module that it points to. The pointed-to module must
|
tomwalters@9
|
119 * continue to exist until it is deleted from the target list by a call
|
tomwalters@9
|
120 * to DeleteTarget() or DeleteAllTargets(), or the current module is
|
tomwalters@9
|
121 * destroyed. Bad things will happen if the Module pointed to is deleted
|
tomwalters@9
|
122 * and Initialize(), Reset() or Process() is subsequently called.
|
tomwalters@0
|
123 */
|
tomwalters@0
|
124 bool AddTarget(Module* target_module);
|
tomwalters@0
|
125
|
tomwalters@9
|
126 /*! \brief Remove a previously added target module from the list of targets
|
tomwalters@9
|
127 * for this module.
|
tomwalters@9
|
128 * \param target_module Pointer to the module to remove. This must be a
|
tomwalters@9
|
129 * pointer that was previously passed to AddTarget()
|
tomwalters@9
|
130 * \return true on success, false on failure.
|
tomwalters@0
|
131 */
|
tomwalters@9
|
132 bool RemoveTarget(Module* target_module);
|
tomwalters@0
|
133
|
tomwalters@9
|
134 /*! \brief Remove all previously added target modules from the list of
|
tomwalters@9
|
135 * targets for this module.
|
tomwalters@0
|
136 */
|
tomwalters@9
|
137 void RemoveAllTargets();
|
tomwalters@0
|
138
|
tomwalters@9
|
139 /*! \brief Process a buffer.
|
tomwalters@9
|
140 * \param input SignalBank of the form which was passed to Initialize()
|
tomwalters@9
|
141 *
|
tomwalters@9
|
142 * Process is called once for each input SignalBank. When implemented in
|
tomwalters@9
|
143 * classes inheriting from aimc::Module, P
|
tomwalters@9
|
144 * this SignalBank contains the (read-only) input to the module. It is
|
tomwalters@9
|
145 * expected that the input SignalBank will have the same number of channels,
|
tomwalters@9
|
146 * buffer length and sample rate as the SignalBank passed to Initialize.
|
tomwalters@0
|
147 */
|
tomwalters@0
|
148 virtual void Process(const SignalBank &input) = 0;
|
tomwalters@0
|
149
|
tomwalters@3
|
150 /*! \brief Reset the internal state of this module and all its children to
|
tomwalters@3
|
151 * their initial state.
|
tomwalters@9
|
152 *
|
tomwalters@9
|
153 * Like a call to Initialize() will cause all target modules to be
|
tomwalters@9
|
154 * initialized, a call to Reset() will cause all target modules to be reset.
|
tomwalters@0
|
155 */
|
tomwalters@8
|
156 void Reset();
|
tomwalters@0
|
157
|
tomwalters@9
|
158 /*! \brief Return a pointer to the output SignalBank_ for this class.
|
tomwalters@9
|
159 * \return pointer to the SignalBank that this module uses to store its
|
tomwalters@9
|
160 * output.
|
tomwalters@0
|
161 */
|
tomwalters@0
|
162 const SignalBank* GetOutputBank() const;
|
tomwalters@0
|
163
|
tomwalters@49
|
164 void PrintTargets(ostream &out);
|
tomwalters@160
|
165
|
tomwalters@160
|
166 void PrintVersions(ostream &out);
|
tomwalters@49
|
167
|
tomwalters@23
|
168 string version() const {
|
tomwalters@23
|
169 return module_version_;
|
tomwalters@23
|
170 }
|
tomwalters@23
|
171
|
tomwalters@23
|
172 string id() const {
|
tomwalters@23
|
173 return module_identifier_;
|
tomwalters@23
|
174 }
|
tomwalters@23
|
175
|
tomwalters@23
|
176 string description() const {
|
tomwalters@23
|
177 return module_description_;
|
tomwalters@23
|
178 }
|
tomwalters@23
|
179
|
tomwalters@23
|
180 string type() const {
|
tomwalters@23
|
181 return module_type_;
|
tomwalters@23
|
182 }
|
tomwalters@23
|
183
|
tomwalters@0
|
184 protected:
|
tomwalters@0
|
185 void PushOutput();
|
tomwalters@0
|
186
|
tomwalters@3
|
187 virtual void ResetInternal() = 0;
|
tomwalters@3
|
188
|
tomwalters@0
|
189 virtual bool InitializeInternal(const SignalBank &input) = 0;
|
tomwalters@0
|
190
|
tomwalters@0
|
191 bool initialized_;
|
tomwalters@0
|
192 set<Module*> targets_;
|
tomwalters@0
|
193 SignalBank output_;
|
tomwalters@0
|
194 Parameters* parameters_;
|
tomwalters@0
|
195
|
tomwalters@0
|
196 string module_identifier_;
|
tomwalters@0
|
197 string module_type_;
|
tomwalters@0
|
198 string module_description_;
|
tomwalters@0
|
199 string module_version_;
|
tomwalters@0
|
200
|
tomwalters@0
|
201 private:
|
tomwalters@0
|
202 DISALLOW_COPY_AND_ASSIGN(Module);
|
tomwalters@0
|
203 };
|
tomwalters@0
|
204 }
|
tomwalters@0
|
205
|
tomwalters@11
|
206 #endif // AIMC_SUPPORT_MODULE_H_
|