Essentia  2.1-beta6-dev
sourcebase.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_SOURCEBASE_H
21 #define ESSENTIA_SOURCEBASE_H
22 
23 #include "../types.h"
24 #include "../connector.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 class SourceBase;
30 class SourceProxyBase;
31  //template <typename T> class SourceProxy;
32 class SinkBase;
33 class Algorithm;
34 
35 void connect(SourceBase& source, SinkBase& sink);
36 void disconnect(SourceBase& source, SinkBase& sink);
37 
38 void attach(SourceBase& innerSource, SourceProxyBase& proxy);
39 void detach(SourceBase& innerSource, SourceProxyBase& proxy);
40 
41 
52 class SourceBase : public Connector {
53  protected:
54  std::vector<SinkBase*> _sinks;
55 
56  // we only allow for 1 proxy to be connected at the moment
57  // (although multiple ones would be theoretically correct, too)
59 
60  public:
61  // TODO: are those still useful?
62  SourceBase(Algorithm* parent = 0, const std::string& name = "unnamed") :
63  Connector(parent, name), _sproxy(0) {}
64 
65  SourceBase(const std::string& name) :
66  Connector(name), _sproxy(0) {}
67 
69 
70  // this function should probably be protected, with friend = SinkBase, Sink
71  virtual void* buffer() = 0;
72 
73  virtual int totalProduced() const = 0;
74 
75  const std::vector<SinkBase*>& sinks() const { return _sinks; }
76 
77  // TODO: remove me to avoid people doing stuff like src.sinks().clear()
78  std::vector<SinkBase*>& sinks() { return _sinks; }
79 
80  // should return a vector<TokenType>*
81  virtual void* getTokens() = 0;
82 
83  // should return a TokenType*
84  virtual void* getFirstToken() = 0;
85 
86  bool isProxied() const { return _sproxy != 0; }
87 
92  const std::vector<SinkBase*>& proxiedSinks() const;
93 
94  template <typename TokenType>
95  void push(const TokenType& value) {
96  try {
97  checkType<TokenType>();
98  if (!acquire(1))
99  throw EssentiaException(fullName(), ": Could not push 1 value, output buffer is full");
100 
101  *(TokenType*)getFirstToken() = value;
102 
103  release(1);
104  }
105  catch (EssentiaException& e) {
106  throw EssentiaException("While trying to push item into source ", fullName(), ":\n", e.what());
107  }
108  }
109 
110  // function to resize the buffer given the type of tokens we want to convey
112 
113  virtual BufferInfo bufferInfo() const = 0;
114  virtual void setBufferInfo(const BufferInfo& info) = 0;
115 
116  protected:
117  // made those protected so that only our friend streaming::{dis}connect() functions can access these
118  // @todo this function should probably be protected by a mutex (?)
119  virtual void connect(SinkBase& sink);
120  virtual void disconnect(SinkBase& sink);
121 
122  friend void connect(SourceBase& source, SinkBase& sink);
123  friend void disconnect(SourceBase& source, SinkBase& sink);
124 
125 
126  virtual ReaderID addReader() = 0;
127  virtual void removeReader(ReaderID id) = 0;
128 
129 
130  friend void attach(SourceBase& innerSource, SourceProxyBase& proxy);
131  friend void detach(SourceBase& innerSource, SourceProxyBase& proxy);
132 
133  // Note: these can't be called attach because they would be shadowed by SourceProxyBase::attach(SourceBase)
136 
137  friend class SourceProxyBase;
138  // TODO: still needed?
139  template <typename T> friend class SourceProxy;
140  template <typename T> friend class SinkProxy;
141 
142 };
143 
144 } // namespace streaming
145 } // namespace essentia
146 
147 #endif // ESSENTIA_SOURCEBASE_H
Definition: types.h:77
virtual const char * what() const
Definition: types.h:100
const std::string & name() const
Definition: types.h:284
Definition: streamingalgorithm.h:140
Definition: types.h:420
Definition: connector.h:40
std::string fullName() const
const Algorithm * parent() const
Definition: connector.h:53
Definition: sinkbase.h:52
Definition: sinkproxy.h:179
Definition: sourcebase.h:52
virtual BufferInfo bufferInfo() const =0
const std::vector< SinkBase * > & proxiedSinks() const
virtual void * getTokens()=0
std::vector< SinkBase * > _sinks
Definition: sourcebase.h:54
virtual void * getFirstToken()=0
friend void attach(SourceBase &innerSource, SourceProxyBase &proxy)
Definition: sourceproxy.h:243
bool isProxied() const
Definition: sourcebase.h:86
friend void connect(SourceBase &source, SinkBase &sink)
virtual void setBufferType(BufferUsage::BufferUsageType type)=0
virtual int totalProduced() const =0
SourceProxyBase * _sproxy
Definition: sourcebase.h:58
friend void disconnect(SourceBase &source, SinkBase &sink)
virtual void removeReader(ReaderID id)=0
const std::vector< SinkBase * > & sinks() const
Definition: sourcebase.h:75
void push(const TokenType &value)
Definition: sourcebase.h:95
virtual void setBufferInfo(const BufferInfo &info)=0
std::vector< SinkBase * > & sinks()
Definition: sourcebase.h:78
void attachProxy(SourceProxyBase *sproxy)
virtual void disconnect(SinkBase &sink)
SourceBase(const std::string &name)
Definition: sourcebase.h:65
void detachProxy(SourceProxyBase *sproxy)
SourceBase(Algorithm *parent=0, const std::string &name="unnamed")
Definition: sourcebase.h:62
virtual ReaderID addReader()=0
friend void detach(SourceBase &innerSource, SourceProxyBase &proxy)
Definition: sourceproxy.h:260
virtual void connect(SinkBase &sink)
Definition: sourceproxy.h:37
Definition: sourceproxy.h:175
void release()
Definition: streamconnector.h:59
bool acquire()
Definition: streamconnector.h:49
BufferUsageType
Definition: types.h:436
void detach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:257
void disconnect(SourceBase &source, DevNullConnector devnull)
void connect(SourceBase &source, DevNullConnector devnull)
void attach(SinkProxyBase &proxy, SinkBase &innerSink)
Definition: sinkproxy.h:239
Definition: algorithm.h:28
int ReaderID
Definition: types.h:344