Essentia  2.1-beta5-dev
vectorinput.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_VECTORINPUT_H
21 #define ESSENTIA_VECTORINPUT_H
22 
23 #include "../streamingalgorithm.h"
24 
25 namespace essentia {
26 namespace streaming {
27 
28 
29 template <typename TokenType, int acquireSize = 1>
30 class VectorInput : public Algorithm {
31  protected:
33 
34  const std::vector<TokenType>* _inputVector;
35  bool _ownVector;
36  int _idx;
38 
39  public:
40 
41  VectorInput(const std::vector<TokenType>* input=0, bool own = false)
42  : _inputVector(input), _ownVector(own) {
43  setName("VectorInput");
44  setAcquireSize(acquireSize);
45  declareOutput(_output, _acquireSize, "data", "the values read from the vector");
46  reset();
47  }
48 
49  VectorInput(std::vector<TokenType>* input, bool own = false)
50  : _inputVector(input), _ownVector(own) {
51  setName("VectorInput");
52  setAcquireSize(acquireSize);
53  declareOutput(_output, _acquireSize, "data", "the values read from the vector");
54  reset();
55  }
56 
57  template <typename Array>
58  VectorInput(const Array& inputArray, bool own = true) {
59  setName("VectorInput");
60  _inputVector = new std::vector<TokenType>(arrayToVector<TokenType>(inputArray));
61  _ownVector = true;
62  setAcquireSize(acquireSize);
63  declareOutput(_output, _acquireSize, "data", "the values read from the vector");
64  reset();
65  }
66 
67  // TODO: This constructor takes in an Array2D but it converts it to a
68  // vector-vector to work with the existing code. Ideally, we would keep the
69  // Array2D (don't forget to turn off _ownVector) and read from it directly.
70 
72  setName("VectorInput");
73 
74  // convert TNT array to vector-vector
75  std::vector<TokenType>* inputVector = new std::vector<TokenType>();
76  inputVector->resize(input.dim1());
77 
78  for (int i=0; i<input.dim1(); ++i) {
79  (*inputVector)[i].resize(input.dim2());
80  for (int j=0; j<input.dim2(); ++j) {
81  (*inputVector)[i][j] = input[i][j];
82  }
83  }
84 
85  _inputVector = inputVector;
86  _ownVector = true;
87  setAcquireSize(acquireSize);
88  declareOutput(_output, _acquireSize, "data", "the values read from the vector");
89  reset();
90  }
91 
93  clear();
94  }
95 
96  void clear() {
97  if (_ownVector) delete _inputVector;
98  _inputVector = 0;
99  }
100 
104  void setVector(const std::vector<TokenType>* input, bool own=false) {
105  clear();
106  _inputVector = input;
107  _ownVector = own;
108  }
109 
110  void setAcquireSize(const int size) {
111  _acquireSize = size;
112 
113  _output.setAcquireSize(_acquireSize);
114  _output.setReleaseSize(_acquireSize);
115  }
116 
117  void reset() {
119  _idx = 0;
120  _output.setAcquireSize(_acquireSize);
121  _output.setReleaseSize(_acquireSize);
122  }
123 
124  bool shouldStop() const {
125  return _idx >= (int)_inputVector->size();
126  }
127 
129  // no more data available in vector. shouldn't be necessary to check,
130  // but it doesn't cost us anything to be sure
131  EXEC_DEBUG("process()");
132  if (shouldStop()) {
133  return PASS;
134  }
135 
136  // if we're at the end of the vector, just acquire the necessary amount of
137  // tokens on the output source
138  if (_idx + _output.acquireSize() > (int)_inputVector->size()) {
139  int howmuch = (int)_inputVector->size() - _idx;
140  _output.setAcquireSize(howmuch);
141  _output.setReleaseSize(howmuch);
142  }
143 
144  EXEC_DEBUG("acquiring " << _output.acquireSize() << " tokens");
145  AlgorithmStatus status = acquireData();
146 
147  if (status != OK) {
148  if (status == NO_OUTPUT) {
149  throw EssentiaException("VectorInput: internal error: output buffer full");
150  }
151  // should never get there, right?
152  return NO_INPUT;
153  }
154 
155  TokenType* dest = (TokenType*)_output.getFirstToken();
156  const TokenType* src = &((*_inputVector)[_idx]);
157  int howmuch = _output.acquireSize();
158  fastcopy(dest, src, howmuch);
159  _idx += howmuch;
160 
161  releaseData();
162  EXEC_DEBUG("released " << _output.releaseSize() << " tokens");
163 
164  return OK;
165  }
166 
168 
169 };
170 
171 template <typename T>
172 void connect(VectorInput<T>& v, SinkBase& sink) {
173  // optimization: if the sink we're connected to requires a lot of samples at once,
174  // we might as well wait to have them all instead of feeding it them one by one
175  int size = sink.acquireSize();
176  SourceBase& visource = v.output("data");
177  if (visource.acquireSize() < size) {
178  v.setAcquireSize(size);
179  }
180  connect(v.output("data"), sink);
181 
182 
183 }
184 
185 template <typename T>
187  connect(v, sink);
188 }
189 
190 // TODO: in order to use this function runGenerator should be able to be called
191 // with a vector
192 template <typename T>
193 void connect(std::vector<T>& v, SinkBase& sink) {
194  VectorInput<T>* vectorInput = new VectorInput<T>(&v);
195 
196  // optimize acquire/release sizes to seldom sink's sizes
197  int size = sink.acquireSize();
198  SourceBase& source = vectorInput->output("data");
199  if (source.acquireSize() < size) {
200  source.setAcquireSize(size);
201  source.setReleaseSize(size);
202  }
203 
204  connect(vectorInput->output("data"), sink);
205 }
206 
207 template <typename T>
208 void operator>>(std::vector<T>& v, SinkBase& sink) {
209  connect(v, sink);
210 }
211 
212 } // namespace streaming
213 } // namespace essentia
214 
215 #endif // ESSENTIA_VECTORINPUT_H
int dim1() const
Definition: tnt_array2d.h:231
~VectorInput()
Definition: vectorinput.h:92
int _idx
Definition: vectorinput.h:36
void clear()
Definition: vectorinput.h:96
AlgorithmStatus acquireData()
Definition: sourcebase.h:52
SinkBase & input(const std::string &name)
void connect(SourceBase &source, DevNullConnector devnull)
virtual int acquireSize() const
Definition: streamconnector.h:69
void declareOutput(SourceBase &source, const std::string &name, const std::string &desc)
SourceBase & output(const std::string &name)
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:172
void setName(const std::string &name)
Definition: configurable.h:53
VectorInput(const TNT::Array2D< Real > &input)
Definition: vectorinput.h:71
void setVector(const std::vector< TokenType > *input, bool own=false)
Definition: vectorinput.h:104
bool shouldStop() const
Definition: vectorinput.h:124
Definition: vectorinput.h:30
Definition: sinkbase.h:52
int dim2() const
Definition: tnt_array2d.h:234
void operator>>(SourceBase &source, DevNullConnector devnull)
Definition: devnull.h:81
Definition: streamingalgorithm.h:111
void declareParameters()
Definition: vectorinput.h:167
Definition: streamingalgorithm.h:112
VectorInput(const Array &inputArray, bool own=true)
Definition: vectorinput.h:58
bool _ownVector
Definition: vectorinput.h:35
const std::vector< TokenType > * _inputVector
Definition: vectorinput.h:34
Definition: streamingalgorithm.h:107
AlgorithmStatus
Definition: streamingalgorithm.h:106
virtual void * getFirstToken()
Definition: source.h:81
Definition: sink.h:31
Definition: algorithm.h:28
Definition: types.h:76
virtual void setReleaseSize(int n)
Definition: streamconnector.h:84
virtual void setAcquireSize(int n)
Definition: streamconnector.h:79
Definition: streamingalgorithm.h:140
VectorInput(const std::vector< TokenType > *input=0, bool own=false)
Definition: vectorinput.h:41
int _acquireSize
Definition: vectorinput.h:37
virtual int releaseSize() const
Definition: streamconnector.h:74
AlgorithmStatus process()
Definition: vectorinput.h:128
void reset()
Definition: vectorinput.h:117
VectorInput(std::vector< TokenType > *input, bool own=false)
Definition: vectorinput.h:49
Source< TokenType > _output
Definition: vectorinput.h:32
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
Definition: streamingalgorithm.h:109
void setAcquireSize(const int size)
Definition: vectorinput.h:110