Essentia  2.1-beta6-dev
copy.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_STREAMING_COPY_H
21 #define ESSENTIA_STREAMING_COPY_H
22 
23 #include "streamingalgorithm.h"
24 
25 namespace essentia {
26 namespace streaming {
27 
28 template <typename TokenType>
29 class Copy : public Algorithm {
30  protected:
33 
34  public:
35  Copy() : Algorithm() {
36  static ForcedMutex _copyInitMutex;
37  static int _copyId = 0;
38 
39  ForcedMutexLocker lock(_copyInitMutex);
40 
41  int copyId = _copyId++;
42  std::ostringstream name;
43  name << "Copy<" << nameOfType(typeid(TokenType)) << ">[" << copyId << "]";
44  setName(name.str());
45  declareInput(_framesIn, 1, "data", "the input data");
46  declareOutput(_framesOut, 1, "data", "the output data");
47  E_DEBUG(EFactory, "Created " << _name);
48  }
49 
51 
53  int nframes = std::min(_framesIn.available(),
54  _framesIn.buffer().bufferInfo().maxContiguousElements);
55  nframes = std::max(nframes, 1); // in case phantomsize == 0
56 
57  EXEC_DEBUG("Consuming " << nframes << " tokens");
58 
59  if (!_framesIn.acquire(nframes)) {
60  EXEC_DEBUG("Could not consume because not enough input tokens");
61  return NO_INPUT;
62  }
63 
64  if (!_framesOut.acquire(nframes)) {
65  EXEC_DEBUG("Could not consume because not enough output tokens");
66  return NO_OUTPUT;
67  }
68 
69  fastcopy(&_framesOut.firstToken(), &_framesIn.firstToken(), nframes);
70 
71  // release the tokens we just copied
72  _framesIn.release(nframes);
73  _framesOut.release(nframes);
74 
75  return OK;
76  }
77 };
78 
79 } // namespace streaming
80 } // namespace essentia
81 
82 #endif // ESSENTIA_STREAMING_COPY_H
void setName(const std::string &name)
Definition: configurable.h:53
std::string _name
Definition: configurable.h:171
const std::string & name() const
Definition: configurable.h:48
Definition: threading.h:95
Definition: threading.h:80
Definition: streamingalgorithm.h:140
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
void declareOutput(SourceBase &source, const std::string &name, const std::string &desc)
Definition: copy.h:29
Copy()
Definition: copy.h:35
AlgorithmStatus process()
Definition: copy.h:52
void declareParameters()
Definition: copy.h:50
Source< TokenType > _framesOut
Definition: copy.h:32
Sink< TokenType > _framesIn
Definition: copy.h:31
Definition: sink.h:35
Definition: source.h:32
#define E_DEBUG(module, msg)
Definition: debugging.h:157
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
AlgorithmStatus
Definition: streamingalgorithm.h:106
@ OK
Definition: streamingalgorithm.h:107
@ NO_INPUT
Definition: streamingalgorithm.h:111
@ NO_OUTPUT
Definition: streamingalgorithm.h:112
Definition: algorithm.h:28
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:180
std::string nameOfType(const std::type_info &type)
@ EFactory
Definition: debugging.h:44