Essentia  2.1-beta6-dev
streamingalgorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 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,
112  NO_OUTPUT
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 
206 
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 
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
Definition: configurable.h:38
Definition: network.h:98
Definition: streamingalgorithm.h:140
SourceBase & output(const std::string &name)
OutputMap _outputs
Definition: streamingalgorithm.h:249
const InputMap & inputs() const
Definition: streamingalgorithm.h:172
std::vector< std::string > inputNames() const
Definition: streamingalgorithm.h:179
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
DescriptionMap outputDescription
Definition: streamingalgorithm.h:149
OrderedMap< SourceBase > OutputMap
Definition: streamingalgorithm.h:146
virtual void shouldStop(bool stop)
void declareInput(SinkBase &sink, int n, const std::string &name, const std::string &desc)
AlgorithmStatus acquireData()
void declareOutput(SourceBase &source, int n, const std::string &name, const std::string &desc)
SourceBase & output(int idx)
int nProcess
Definition: streamingalgorithm.h:256
virtual ~Algorithm()
Definition: streamingalgorithm.h:160
SinkBase & input(int idx)
OrderedMap< SinkBase > InputMap
Definition: streamingalgorithm.h:145
bool _shouldStop
Definition: streamingalgorithm.h:247
SinkBase & input(const std::string &name)
InputMap _inputs
Definition: streamingalgorithm.h:250
virtual bool shouldStop() const
Definition: streamingalgorithm.h:198
static const std::string processingMode
Definition: streamingalgorithm.h:143
DescriptionMap inputDescription
Definition: streamingalgorithm.h:148
void declareInput(SinkBase &sink, int acquireSize, int releaseSize, const std::string &name, const std::string &desc)
void declareOutput(SourceBase &source, int acquireSize, int releaseSize, const std::string &name, const std::string &desc)
virtual AlgorithmStatus process()=0
void declareOutput(SourceBase &source, const std::string &name, const std::string &desc)
std::vector< std::string > outputNames() const
Definition: streamingalgorithm.h:184
Algorithm()
Definition: streamingalgorithm.h:154
const OutputMap & outputs() const
Definition: streamingalgorithm.h:173
Definition: sinkbase.h:52
Definition: sourcebase.h:52
#define DEBUGGING_ENABLED
Definition: config.h:50
void disconnect(SourceBase &source, DevNullConnector devnull)
void operator>>(SourceBase &source, DevNullConnector devnull)
Definition: devnull.h:81
void connect(SourceBase &source, DevNullConnector devnull)
AlgorithmStatus
Definition: streamingalgorithm.h:106
@ PASS
Definition: streamingalgorithm.h:109
@ OK
Definition: streamingalgorithm.h:107
@ CONTINUE
Definition: streamingalgorithm.h:108
@ NO_INPUT
Definition: streamingalgorithm.h:111
@ NO_OUTPUT
Definition: streamingalgorithm.h:112
@ FINISHED
Definition: streamingalgorithm.h:110
Definition: algorithm.h:28