Essentia  2.1-beta5-dev
copy.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_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(),
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
int maxContiguousElements
Definition: types.h:384
void acquire()
Definition: source.h:83
void declareInput(SinkBase &sink, const std::string &name, const std::string &desc)
const TokenType & firstToken() const
Definition: sink.h:61
void declareParameters()
Definition: copy.h:50
const MultiRateBuffer< TokenType > & buffer() const
Definition: sink.h:45
std::string nameOfType(const std::type_info &type)
const std::string & name() const
Definition: configurable.h:48
void declareOutput(SourceBase &source, const std::string &name, const std::string &desc)
void release()
Definition: sink.h:79
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:172
void setName(const std::string &name)
Definition: configurable.h:53
TokenType & firstToken()
Definition: source.h:77
std::string _name
Definition: configurable.h:171
void release()
Definition: source.h:89
Definition: streamingalgorithm.h:111
Definition: streamingalgorithm.h:112
AlgorithmStatus process()
Definition: copy.h:52
Definition: streamingalgorithm.h:107
AlgorithmStatus
Definition: streamingalgorithm.h:106
Definition: sink.h:31
Sink< TokenType > _framesIn
Definition: copy.h:31
Definition: algorithm.h:28
Source< TokenType > _framesOut
Definition: copy.h:32
Definition: streamingalgorithm.h:140
void acquire()
Definition: sink.h:67
Definition: threading.h:95
Copy()
Definition: copy.h:35
Definition: sink.h:35
Definition: threading.h:80
virtual BufferInfo bufferInfo() const =0
#define EXEC_DEBUG(msg)
Definition: debugging.h:161
Definition: copy.h:29
virtual int available() const
Definition: sink.h:88
#define E_DEBUG(module, msg)
Definition: debugging.h:157
Definition: debugging.h:44