Essentia  2.1-beta6-dev
source.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_SOURCE_H
21 #define ESSENTIA_SOURCE_H
22 
23 #include "sourcebase.h"
24 #include "multiratebuffer.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 
30 // also known as Output-port, OutputDataStream
31 template<typename TokenType>
32 class Source : public SourceBase {
33  USE_TYPE_INFO(TokenType);
34 
35  protected:
37 
38  public:
39 
41  Source(const std::string& name);
42 
43  ~Source() {
44  delete _buffer;
45  }
46 
47  const void* buffer() const { return _buffer; }
48  void* buffer() { return _buffer; }
49 
50  const MultiRateBuffer<TokenType>& typedBuffer() const { return *_buffer; }
52 
54  _buffer->setBufferType(type);
55  }
56 
57  virtual BufferInfo bufferInfo() const {
58  return _buffer->bufferInfo();
59  }
60 
61  virtual void setBufferInfo(const BufferInfo& info) {
62  _buffer->setBufferInfo(info);
63  }
64 
65  int totalProduced() const { return _buffer->totalTokensWritten(); }
66 
68  return _buffer->addReader();
69  }
70 
72  _buffer->removeReader(id);
73  }
74 
75 
76  std::vector<TokenType>& tokens() { return _buffer->writeView(); }
77  TokenType& firstToken() { return _buffer->writeView()[0]; }
78  const TokenType& lastTokenProduced() const { return _buffer->lastTokenProduced(); }
79 
80  virtual void* getTokens() { return &tokens(); }
81  virtual void* getFirstToken() { return &firstToken(); }
82 
83  inline void acquire() { StreamConnector::acquire(); }
84 
85  virtual bool acquire(int n) {
86  return _buffer->acquireForWrite(n);
87  }
88 
89  inline void release() { StreamConnector::release(); }
90 
91  virtual void release(int n) {
93  }
94 
95  virtual int available() const {
96  return _buffer->availableForWrite(false);
97  }
98 
99  virtual void reset() {
100  _buffer->reset();
101  }
102 
103 };
104 
109 template<typename TokenType>
110 class AbsoluteSource : public Source<TokenType> {
111  public:
113  return this->_buffer->addReader(true);
114  }
115 };
116 
117 
118 
122 template <typename T>
123 const T& lastTokenProduced(const SourceBase& source) {
124  const Source<T>* src = dynamic_cast<const Source<T>*>(&source);
125  if (!src)
126  throw EssentiaException(source.fullName(), " does not produce ", nameOfType(typeid(T)), " tokens");
127 
128  return src->lastTokenProduced();
129 }
130 
131 } // namespace essentia
132 } // namespace streaming
133 
134 
136 // NB: Implementation needs to go into the header as it is a template class we are defining
137 
138 #include "phantombuffer.h"
139 
140 
141 namespace essentia {
142 namespace streaming {
143 
144 // We need to have a specific MultiRateBuffer implementation (PhantomBuffer, here)
145 // before we can define the constructors
146 template <typename TokenType>
148  SourceBase(parent),
149  _buffer(new PhantomBuffer<TokenType>(this, BufferUsage::forSingleFrames)) {}
150 
151 template <typename TokenType>
152 Source<TokenType>::Source(const std::string& name) :
153  SourceBase(name),
154  _buffer(new PhantomBuffer<TokenType>(this, BufferUsage::forSingleFrames)) {}
155 
156 } // namespace streaming
157 } // namespace essentia
158 
159 
160 #endif // ESSENTIA_SOURCE_H
Definition: types.h:77
const std::string & name() const
Definition: types.h:284
Definition: source.h:110
ReaderID addReader()
Definition: source.h:112
Definition: streamingalgorithm.h:140
Definition: types.h:420
std::string fullName() const
const Algorithm * parent() const
Definition: connector.h:53
virtual BufferInfo bufferInfo() const =0
virtual void releaseForWrite(int released)=0
virtual int availableForWrite(bool contiguous=true) const =0
virtual void setBufferType(BufferUsage::BufferUsageType type)=0
virtual int totalTokensWritten() const =0
virtual ReaderID addReader(bool startFromZero=false)=0
virtual std::vector< T > & writeView()=0
virtual void removeReader(ReaderID id)=0
virtual bool acquireForWrite(int requested)=0
virtual void setBufferInfo(const BufferInfo &info)=0
virtual const T & lastTokenProduced() const =0
Definition: phantombuffer.h:60
Definition: sourcebase.h:52
Definition: source.h:32
virtual bool acquire(int n)
Definition: source.h:85
void removeReader(ReaderID id)
Definition: source.h:71
std::vector< TokenType > & tokens()
Definition: source.h:76
void release()
Definition: source.h:89
virtual void setBufferType(BufferUsage::BufferUsageType type)
Definition: source.h:53
MultiRateBuffer< TokenType > & typedBuffer()
Definition: source.h:51
virtual int available() const
Definition: source.h:95
const MultiRateBuffer< TokenType > & typedBuffer() const
Definition: source.h:50
virtual BufferInfo bufferInfo() const
Definition: source.h:57
virtual void setBufferInfo(const BufferInfo &info)
Definition: source.h:61
virtual void reset()
Definition: source.h:99
int totalProduced() const
Definition: source.h:65
ReaderID addReader()
Definition: source.h:67
virtual void * getFirstToken()
Definition: source.h:81
const TokenType & lastTokenProduced() const
Definition: source.h:78
Source(Algorithm *parent=0)
Definition: source.h:147
MultiRateBuffer< TokenType > * _buffer
Definition: source.h:33
const void * buffer() const
Definition: source.h:47
~Source()
Definition: source.h:43
void * buffer()
Definition: source.h:48
virtual void * getTokens()
Definition: source.h:80
virtual void release(int n)
Definition: source.h:91
TokenType & firstToken()
Definition: source.h:77
Source(const std::string &name)
Definition: source.h:152
void acquire()
Definition: source.h:83
void release()
Definition: streamconnector.h:59
bool acquire()
Definition: streamconnector.h:49
BufferUsageType
Definition: types.h:436
@ forSingleFrames
Definition: types.h:437
const T & lastTokenProduced(const SourceBase &source)
Definition: source.h:123
Definition: algorithm.h:28
std::string nameOfType(const std::type_info &type)
int ReaderID
Definition: types.h:344
#define USE_TYPE_INFO(TokenType)
Definition: types.h:332