Essentia  2.1-beta6-dev
poolstorage.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_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) :
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) {
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  template <typename T>
129  void addToPool(const Tensor<T>& value) {
130  _pool->add(_descriptorName, value);
131  }
132 
133  void addToPool(const StereoSample& value) {
134  if (_setSingle) {
135  throw EssentiaException("PoolStorage::addToPool, setting StereoSample as single value"
136  " is not supported by Pool.");
137  }
138  else {
139  _pool->add(_descriptorName, value);
140  }
141  }
142 
143 };
144 
145 
150 void connect(SourceBase& source, Pool& pool,
151  const std::string& descriptorName);
152 
154  protected:
156  std::string name;
157 
158  public:
159  PoolConnector(Pool& p, const std::string& descName) : pool(p), name(descName) {}
160 
161  friend void operator>>(SourceBase& source, const PoolConnector& pc);
162 };
163 
164 #define PC essentia::streaming::PoolConnector
165 
166 // The reason why this function is defined with a const PC& as argument is described here:
167 // http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/
168 inline void operator>>(SourceBase& source, const PoolConnector& pc) {
169  connect(source, pc.pool, pc.name);
170 }
171 
177 void connectSingleValue(SourceBase& source, Pool& pool,
178  const std::string& descriptorName);
179 
183 void disconnect(SourceBase& source, Pool& pool,
184  const std::string& descriptorName);
185 
186 
187 } // namespace streaming
188 } // namespace essentia
189 
190 #endif // ESSENTIA_POOLSTORAGE_H
Definition: tnt_array2d.h:38
void setName(const std::string &name)
Definition: configurable.h:53
Definition: types.h:77
Definition: pool.h:96
void append(const std::string &name, const std::vector< T > &values)
void add(const std::string &name, const Real &value, bool validityCheck=false)
void set(const std::string &name, const Real &value, bool validityCheck=false)
Sets the value of a descriptor name.
Definition: streamingalgorithm.h:140
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
Definition: poolstorage.h:153
PoolConnector(Pool &p, const std::string &descName)
Definition: poolstorage.h:159
friend void operator>>(SourceBase &source, const PoolConnector &pc)
Definition: poolstorage.h:168
std::string name
Definition: poolstorage.h:156
Pool & pool
Definition: poolstorage.h:155
Definition: poolstorage.h:29
~PoolStorageBase()
Definition: poolstorage.h:39
PoolStorageBase(Pool *pool, const std::string &descriptorName, bool setSingle=false)
Definition: poolstorage.h:36
bool _setSingle
Definition: poolstorage.h:33
Pool * _pool
Definition: poolstorage.h:31
std::string _descriptorName
Definition: poolstorage.h:32
const std::string & descriptorName() const
Definition: poolstorage.h:41
Pool * pool() const
Definition: poolstorage.h:45
Definition: poolstorage.h:52
void addToPool(const Tensor< T > &value)
Definition: poolstorage.h:129
~PoolStorage()
Definition: poolstorage.h:64
void addToPool(const std::vector< Real > &value)
Definition: poolstorage.h:105
void addToPool(const TNT::Array2D< T > &value)
Definition: poolstorage.h:117
AlgorithmStatus process()
Definition: poolstorage.h:68
PoolStorage(Pool *pool, const std::string &descriptorName, bool setSingle=false)
Definition: poolstorage.h:57
void addToPool(const T &value)
Definition: poolstorage.h:111
Sink< TokenType > _descriptor
Definition: poolstorage.h:54
void addToPool(const std::vector< T > &value)
Definition: poolstorage.h:97
void declareParameters()
Definition: poolstorage.h:66
void addToPool(const StereoSample &value)
Definition: poolstorage.h:133
Definition: sink.h:35
Definition: sourcebase.h:52
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
void connectSingleValue(SourceBase &source, Pool &pool, const std::string &descriptorName)
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
@ OK
Definition: streamingalgorithm.h:107
@ NO_INPUT
Definition: streamingalgorithm.h:111
Definition: algorithm.h:28
Eigen::Tensor< T, 4, Eigen::RowMajor > Tensor
Definition: types.h:384