Essentia  2.1-beta5-dev
streamingalgorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_STREAMINGALGORITHM_H
21 #define ESSENTIA_STREAMINGALGORITHM_H
22 
23 #include "../configurable.h"
24 #include "../threading.h"
25 #include "sourcebase.h"
26 #include "sinkbase.h"
27 
28 namespace essentia {
29 namespace scheduler {
30 
31  class Network;
32 
33 } // namespace scheduler
34 } // namespace essentia
35 
36 namespace essentia {
37 namespace streaming {
38 
39 
40 class ProcessTask;
41 
42 
46 void connect(SourceBase& source, SinkBase& sink);
47 
48 inline void operator>>(SourceBase& source, SinkBase& sink) {
49  connect(source, sink);
50 }
51 
56 inline void connect(SinkBase& sink, SourceBase& source) {
57  connect(source, sink);
58 }
59 
63 void disconnect(SourceBase& source, SinkBase& sink);
64 
69 void connect(Algorithm* sourceAlgo, const std::string& sourcePort,
70  Algorithm* sinkAlgo, const std::string& sinkPort);
71 
72 
73 
107  OK = 0,
108  CONTINUE = 0,
113 };
114 
140 class ESSENTIA_API Algorithm : public Configurable {
141 
142  public:
143  static const std::string processingMode;
144 
147 
150 
151 
152  public:
153 
154  Algorithm() : _shouldStop(false)
156  , nProcess(0)
157 #endif
158  {}
159 
160  virtual ~Algorithm() {
161  // Note: no need to do anything here wrt to sources and sinks, as they
162  // disconnect themselves when destroyed.
163  }
164 
165 
166  SinkBase& input(const std::string& name);
167  SourceBase& output(const std::string& name);
168 
169  SinkBase& input(int idx);
170  SourceBase& output(int idx);
171 
172  const InputMap& inputs() const { return _inputs; }
173  const OutputMap& outputs() const { return _outputs; }
174 
175 
179  std::vector<std::string> inputNames() const { return _inputs.keys(); }
180 
184  std::vector<std::string> outputNames() const { return _outputs.keys(); }
185 
186 
192  virtual void shouldStop(bool stop);
193 
198  virtual bool shouldStop() const { return _shouldStop; }
199 
200 
201 
205  void disconnectAll();
206 
210  AlgorithmStatus acquireData();
211  void releaseData();
212 
213  virtual AlgorithmStatus process() = 0;
214 
223  virtual void reset();
224 
225  protected:
227  void declareInput(SinkBase& sink, const std::string& name, const std::string& desc);
228 
230  void declareInput(SinkBase& sink, int n, const std::string& name, const std::string& desc);
231 
233  void declareInput(SinkBase& sink, int acquireSize, int releaseSize, const std::string& name, const std::string& desc);
234 
235 
237  void declareOutput(SourceBase& source, const std::string& name, const std::string& desc);
238 
240  void declareOutput(SourceBase& source, int n, const std::string& name, const std::string& desc);
241 
243  void declareOutput(SourceBase& source, int acquireSize, int releaseSize, const std::string& name, const std::string& desc);
244 
245 
246 
248 
249  OutputMap _outputs;
250  InputMap _inputs;
251 
252 #if DEBUGGING_ENABLED
254 
256  int nProcess;
257 #endif
258 
259 };
260 
261 
262 } // namespace streaming
263 } // namespace essentia
264 
265 
266 // these #include have to go here, and not on top, because they need to have
267 // Algorithm defined first. Indeed, Source implementation (templated, so
268 // implementation goes into header) depends on PhantomBuffer (templated), which
269 // in turn depends on Algorithm for the synchronization for acquiring
270 // access to the buffer resources
271 #include "source.h"
272 #include "sink.h"
273 
274 // also include template algorithms which are not in the factory, but are still
275 // useful most of the time
276 #include "algorithms/devnull.h"
277 
278 
279 #endif // ESSENTIA_STREAMINGALGORITHM_H
DescriptionMap outputDescription
Definition: streamingalgorithm.h:149
virtual bool shouldStop() const
Definition: streamingalgorithm.h:198
Definition: sourcebase.h:52
Definition: configurable.h:38
std::vector< std::string > inputNames() const
Definition: streamingalgorithm.h:179
std::istream & operator>>(std::istream &is, MTRand &mtrand)
Definition: MersenneTwister.h:376
Algorithm()
Definition: streamingalgorithm.h:154
Definition: streamingalgorithm.h:110
InputMap _inputs
Definition: streamingalgorithm.h:250
OutputMap _outputs
Definition: streamingalgorithm.h:249
Definition: sinkbase.h:52
OrderedMap< SourceBase > OutputMap
Definition: streamingalgorithm.h:146
Definition: streamingalgorithm.h:108
Definition: streamingalgorithm.h:111
Definition: streamingalgorithm.h:112
Definition: network.h:98
Definition: streamingalgorithm.h:107
AlgorithmStatus
Definition: streamingalgorithm.h:106
OrderedMap< SinkBase > InputMap
Definition: streamingalgorithm.h:145
void connect(Algorithm *sourceAlgo, const std::string &sourcePort, Algorithm *sinkAlgo, const std::string &sinkPort)
Definition: algorithm.h:28
bool _shouldStop
Definition: streamingalgorithm.h:247
int nProcess
Definition: streamingalgorithm.h:256
virtual ~Algorithm()
Definition: streamingalgorithm.h:160
Definition: streamingalgorithm.h:140
void disconnect(SourceBase &source, DevNullConnector devnull)
#define DEBUGGING_ENABLED
Definition: config.h:50
DescriptionMap inputDescription
Definition: streamingalgorithm.h:148
static const std::string processingMode
Definition: streamingalgorithm.h:143
const InputMap & inputs() const
Definition: streamingalgorithm.h:172
std::vector< std::string > outputNames() const
Definition: streamingalgorithm.h:184
const OutputMap & outputs() const
Definition: streamingalgorithm.h:173
Definition: streamingalgorithm.h:109