Essentia  2.1-beta6-dev
vectoroutput.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_VECTOROUTPUT_H
21 #define ESSENTIA_VECTOROUTPUT_H
22 
23 #include "../streamingalgorithm.h"
24 
25 namespace essentia {
26 namespace streaming {
27 
34 template <typename TokenType, typename StorageType = TokenType>
35 class VectorOutput : public Algorithm {
36  protected:
38  std::vector<TokenType>* _v;
39 
40  public:
41  VectorOutput(std::vector<TokenType>* v = 0) : Algorithm(), _v(v) {
42  setName("VectorOutput");
43  declareInput(_data, 1, "data", "the input data");
44  }
45 
47  }
48 
50 
51  void setVector(std::vector<TokenType>* v) {
52  _v = v;
53  }
54 
56  if (!_v) {
57  throw EssentiaException("VectorOutput algorithm has no output vector set...");
58  }
59 
60  EXEC_DEBUG("process()");
61 
62  int ntokens = std::min(_data.available(), _data.buffer().bufferInfo().maxContiguousElements);
63  ntokens = std::max(1, ntokens);
64 
65  EXEC_DEBUG("acquiring " << ntokens << " tokens");
66  if (!_data.acquire(ntokens)) {
67  return NO_INPUT;
68  }
69 
70  // copy tokens in the vector
71  int curSize = _v->size();
72  _v->resize(curSize + ntokens);
73 
74  TokenType* dest = &_v->front() + curSize;
75  const TokenType* src = &_data.firstToken();
76 
77  fastcopy(dest, src, ntokens);
78  _data.release(ntokens);
79 
80  return OK;
81  }
82 
83  void reset() {
84  //_acquireSize = acquireSize;
85  }
86 };
87 
88 template <typename TokenType, typename StorageType>
90  connect(source, v.input("data"));
91 }
92 
93 template <typename TokenType, typename StorageType>
95  connect(source, v);
96 }
97 
102 template <typename T>
103 void connect(SourceBase& source, std::vector<T>& v) {
104  VectorOutput<T> * vectorOutput = new VectorOutput<T>(&v);
105  connect(source, vectorOutput->input("data"));
106 }
107 
108 template <typename T>
109 void operator>>(SourceBase& source, std::vector<T>& v) {
110  connect(source, v);
111 }
112 
113 
114 } // namespace streaming
115 } // namespace essentia
116 
117 #endif // ESSENTIA_VECTOROUTPUT_H
void setName(const std::string &name)
Definition: configurable.h:53
Definition: types.h:77
Definition: streamingalgorithm.h:140
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
SinkBase & input(const std::string &name)
Definition: sink.h:35
Definition: sourcebase.h:52
Definition: vectoroutput.h:35
std::vector< TokenType > * _v
Definition: vectoroutput.h:38
VectorOutput(std::vector< TokenType > *v=0)
Definition: vectoroutput.h:41
Sink< TokenType > _data
Definition: vectoroutput.h:37
AlgorithmStatus process()
Definition: vectoroutput.h:55
void setVector(std::vector< TokenType > *v)
Definition: vectoroutput.h:51
~VectorOutput()
Definition: vectoroutput.h:46
void declareParameters()
Definition: vectoroutput.h:49
void reset()
Definition: vectoroutput.h:83
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
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
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:180