Essentia  2.1-beta5-dev
source.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_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 
40  Source(Algorithm* parent = 0);
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) {
92  _buffer->releaseForWrite(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
virtual BufferInfo bufferInfo() const
Definition: source.h:57
virtual void setBufferType(BufferUsage::BufferUsageType type)=0
MultiRateBuffer< TokenType > & typedBuffer()
Definition: source.h:51
std::vector< TokenType > & tokens()
Definition: source.h:76
const TokenType & lastTokenProduced() const
Definition: source.h:78
const Algorithm * parent() const
Definition: connector.h:53
void acquire()
Definition: source.h:83
int ReaderID
Definition: types.h:343
virtual void setBufferInfo(const BufferInfo &info)
Definition: source.h:61
virtual std::vector< T > & writeView()=0
Definition: sourcebase.h:52
virtual void removeReader(ReaderID id)=0
virtual const T & lastTokenProduced() const =0
ReaderID addReader()
Definition: source.h:67
std::string nameOfType(const std::type_info &type)
virtual void reset()
Definition: source.h:99
bool acquire()
Definition: streamconnector.h:49
TokenType & firstToken()
Definition: source.h:77
BufferUsageType
Definition: types.h:397
Definition: phantombuffer.h:60
void * buffer()
Definition: source.h:48
Definition: source.h:110
virtual void release(int n)
Definition: source.h:91
virtual void setBufferType(BufferUsage::BufferUsageType type)
Definition: source.h:53
~Source()
Definition: source.h:43
void release()
Definition: streamconnector.h:59
void release()
Definition: source.h:89
void removeReader(ReaderID id)
Definition: source.h:71
virtual bool acquireForWrite(int requested)=0
virtual void setBufferInfo(const BufferInfo &info)=0
std::string fullName() const
virtual ReaderID addReader(bool startFromZero=false)=0
Source(Algorithm *parent=0)
Definition: source.h:147
MultiRateBuffer< TokenType > * _buffer
Definition: source.h:33
#define USE_TYPE_INFO(TokenType)
Definition: types.h:331
ReaderID addReader()
Definition: source.h:112
virtual void releaseForWrite(int released)=0
Definition: types.h:381
virtual void * getFirstToken()
Definition: source.h:81
Definition: sink.h:31
Definition: algorithm.h:28
virtual int totalTokensWritten() const =0
virtual bool acquire(int n)
Definition: source.h:85
int totalProduced() const
Definition: source.h:65
Definition: types.h:76
Definition: streamingalgorithm.h:140
virtual int availableForWrite(bool contiguous=true) const =0
const void * buffer() const
Definition: source.h:47
virtual void * getTokens()
Definition: source.h:80
virtual int available() const
Definition: source.h:95
virtual BufferInfo bufferInfo() const =0
const std::string & name() const
Definition: types.h:283
const MultiRateBuffer< TokenType > & typedBuffer() const
Definition: source.h:50