Essentia  2.1-beta5-dev
poolstorage.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_POOLSTORAGE_H
21 #define ESSENTIA_POOLSTORAGE_H
22 
23 #include "../streamingalgorithm.h"
24 #include "../../pool.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 class PoolStorageBase : public Algorithm {
30  protected:
32  std::string _descriptorName;
33  bool _setSingle;
34 
35  public:
36  PoolStorageBase(Pool* pool, const std::string& descriptorName, bool setSingle = false) :
37  _pool(pool), _descriptorName(descriptorName), _setSingle(setSingle) {}
38 
40 
41  const std::string& descriptorName() const {
42  return _descriptorName;
43  }
44 
45  Pool* pool() const {
46  return _pool;
47  }
48 
49 };
50 
51 template <typename TokenType, typename StorageType = TokenType>
52 class PoolStorage : public PoolStorageBase {
53  protected:
55 
56  public:
57  PoolStorage(Pool* pool, const std::string& descriptorName, bool setSingle = false) :
58  PoolStorageBase(pool, descriptorName, setSingle) {
59 
60  setName("PoolStorage");
61  declareInput(_descriptor, 1, "data", "the input data");
62  }
63 
65 
67 
69  EXEC_DEBUG("process(), for desc: " << _descriptorName);
70 
71  int ntokens = std::min(_descriptor.available(),
72  _descriptor.buffer().bufferInfo().maxContiguousElements);
73  ntokens = std::max(ntokens, 1);
74 
75  // for singleFrames buffer usage, the size of the phantom zone may be zero,
76  // thus need to +1. And we're still on the safe side, see acquireForRead (phantombuffer_impl.cpp)
77  EXEC_DEBUG("trying to acquire " << ntokens << " tokens");
78  if (!_descriptor.acquire(ntokens)) {
79  return NO_INPUT;
80  }
81 
82  EXEC_DEBUG("appending tokens to pool");
83  if (ntokens > 1) {
84  _pool->append(_descriptorName, _descriptor.tokens());
85  }
86  else {
87  addToPool((StorageType)_descriptor.firstToken());
88  }
89 
90  EXEC_DEBUG("releasing");
91  _descriptor.release(ntokens);
92 
93  return OK;
94  }
95 
96  template <typename T>
97  void addToPool(const std::vector<T>& value) {
98  if (_setSingle) {
99  for (int i=0; i<(int)value.size();++i)
100  _pool->add(_descriptorName, value[i]);
101  }
102  else _pool->add(_descriptorName, value);
103  }
104 
105  void addToPool(const std::vector<Real>& value) {
106  if (_setSingle) _pool->set(_descriptorName, value);
107  else _pool->add(_descriptorName, value);
108  }
109 
110  template <typename T>
111  void addToPool(const T& value) {
112  if (_setSingle) _pool->set(_descriptorName, value);
113  else _pool->add(_descriptorName, value);
114  }
115 
116  template <typename T>
117  void addToPool(const TNT::Array2D<T>& value) {
118  _pool->add(_descriptorName, value);
119  /*
120  if (_setSingle) {
121  throw EssentiaException("PoolStorage::addToPool, setting Array2D as single value"
122  " is not supported by Pool.");
123  }
124  else _pool->add(_descriptorName, value);
125  */
126  }
127 
128  void addToPool(const StereoSample& value) {
129  if (_setSingle) {
130  throw EssentiaException("PoolStorage::addToPool, setting StereoSample as single value"
131  " is not supported by Pool.");
132  }
133  else {
134  _pool->add(_descriptorName, value);
135  }
136  }
137 
138 };
139 
140 
145 void connect(SourceBase& source, Pool& pool,
146  const std::string& descriptorName);
147 
149  protected:
151  std::string name;
152 
153  public:
154  PoolConnector(Pool& p, const std::string& descName) : pool(p), name(descName) {}
155 
156  friend void operator>>(SourceBase& source, const PoolConnector& pc);
157 };
158 
159 #define PC essentia::streaming::PoolConnector
160 
161 // The reason why this function is defined with a const PC& as argument is described here:
162 // http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/
163 inline void operator>>(SourceBase& source, const PoolConnector& pc) {
164  connect(source, pc.pool, pc.name);
165 }
166 
172 void connectSingleValue(SourceBase& source, Pool& pool,
173  const std::string& descriptorName);
174 
178 void disconnect(SourceBase& source, Pool& pool,
179  const std::string& descriptorName);
180 
181 
182 } // namespace streaming
183 } // namespace essentia
184 
185 #endif // ESSENTIA_POOLSTORAGE_H
void addToPool(const std::vector< Real > &value)
Definition: poolstorage.h:105
void connectSingleValue(SourceBase &source, Pool &pool, const std::string &descriptorName)
void append(const std::string &name, const std::vector< T > &values)
int maxContiguousElements
Definition: types.h:384
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
const TokenType & firstToken() const
Definition: sink.h:61
void addToPool(const T &value)
Definition: poolstorage.h:111
Definition: sourcebase.h:52
Definition: poolstorage.h:29
const MultiRateBuffer< TokenType > & buffer() const
Definition: sink.h:45
void connect(SourceBase &source, DevNullConnector devnull)
PoolStorageBase(Pool *pool, const std::string &descriptorName, bool setSingle=false)
Definition: poolstorage.h:36
void addToPool(const StereoSample &value)
Definition: poolstorage.h:128
void release()
Definition: sink.h:79
~PoolStorageBase()
Definition: poolstorage.h:39
void setName(const std::string &name)
Definition: configurable.h:53
void set(const std::string &name, const Real &value, bool validityCheck=false)
Sets the value of a descriptor name.
const std::vector< TokenType > & tokens() const
Definition: sink.h:60
Definition: pool.h:96
void add(const std::string &name, const Real &value, bool validityCheck=false)
Definition: tnt_array2d.h:37
Pool * _pool
Definition: poolstorage.h:31
bool _setSingle
Definition: poolstorage.h:33
~PoolStorage()
Definition: poolstorage.h:64
void operator>>(SourceBase &source, DevNullConnector devnull)
Definition: devnull.h:81
Definition: streamingalgorithm.h:111
void declareParameters()
Definition: poolstorage.h:66
AlgorithmStatus process()
Definition: poolstorage.h:68
Definition: poolstorage.h:52
Definition: streamingalgorithm.h:107
AlgorithmStatus
Definition: streamingalgorithm.h:106
Definition: algorithm.h:28
Definition: types.h:76
PoolConnector(Pool &p, const std::string &descName)
Definition: poolstorage.h:154
void addToPool(const TNT::Array2D< T > &value)
Definition: poolstorage.h:117
Definition: streamingalgorithm.h:140
void acquire()
Definition: sink.h:67
Definition: poolstorage.h:148
void disconnect(SourceBase &source, DevNullConnector devnull)
Definition: sink.h:35
PoolStorage(Pool *pool, const std::string &descriptorName, bool setSingle=false)
Definition: poolstorage.h:57
Sink< TokenType > _descriptor
Definition: poolstorage.h:54
virtual BufferInfo bufferInfo() const =0
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
std::string _descriptorName
Definition: poolstorage.h:32
virtual int available() const
Definition: sink.h:88
void addToPool(const std::vector< T > &value)
Definition: poolstorage.h:97
Pool * pool() const
Definition: poolstorage.h:45
Pool & pool
Definition: poolstorage.h:150
const std::string & descriptorName() const
Definition: poolstorage.h:41
std::string name
Definition: poolstorage.h:151