Essentia  2.1-beta6-dev
multiratebuffer.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_MULTIRATEBUFFER_H
21 #define ESSENTIA_MULTIRATEBUFFER_H
22 
23 #include <vector>
24 #include "../types.h"
25 
26 namespace essentia {
27 namespace streaming {
28 
29 template <typename T>
31 
32  public:
33  virtual ~MultiRateBuffer() {}
34 
36  virtual BufferInfo bufferInfo() const = 0;
37  virtual void setBufferInfo(const BufferInfo& info) = 0;
38 
39  // add/remove readers to/from the buffer
40  // returns the id of the newly attached reader
41  virtual ReaderID addReader(bool startFromZero = false) = 0;
42  virtual void removeReader(ReaderID id) = 0;
43  virtual int numberReaders() const = 0;
44 
45  // get and release access to the items in the buffer
46  virtual bool acquireForRead(ReaderID id, int requested) = 0;
47  virtual void releaseForRead(ReaderID id, int released) = 0;
48  virtual bool acquireForWrite(int requested) = 0;
49  virtual void releaseForWrite(int released) = 0;
50 
51  virtual int availableForRead(ReaderID id) const = 0;
52  virtual int availableForWrite(bool contiguous=true) const = 0;
53 
54  virtual int totalTokensRead(ReaderID id) const = 0;
55  virtual int totalTokensWritten() const = 0;
56 
57  virtual const T& lastTokenProduced() const = 0;
58 
59  // some useful aliases, depending on the terminology used
60  void readerConsume(ReaderID id, int requested) { acquireForRead(id, requested); }
61  void readerProduce(ReaderID id, int released) { releaseForRead(id, released); }
62  void writerConsume(int requested) { acquireForWrite(requested); }
63  void writerProduce(int released) { releaseForWrite(released); }
64 
65  // get views of the data currently being accessed to
66  virtual const std::vector<T>& readView(ReaderID id) const = 0;
67  virtual std::vector<T>& writeView() = 0;
68 
69  virtual void reset() = 0;
70 
71  // @todo remove this, only here for debug
72  virtual void resize(int size, int phantomSize) = 0;
73 
74 };
75 
76 // it's the MultiRateBuffer that does all the allocation
77 // the source and sink do not allocate memory, they just take
78 // ownership of the memory zone in the buffer
79 
80 } // namespace streaming
81 } // namespace essentia
82 
83 #endif // ESSENTIA_MULTIRATEBUFFER_H
Definition: types.h:420
Definition: multiratebuffer.h:30
virtual BufferInfo bufferInfo() const =0
virtual const std::vector< T > & readView(ReaderID id) const =0
virtual void releaseForWrite(int released)=0
virtual int availableForWrite(bool contiguous=true) const =0
virtual void setBufferType(BufferUsage::BufferUsageType type)=0
void writerConsume(int requested)
Definition: multiratebuffer.h:62
virtual ~MultiRateBuffer()
Definition: multiratebuffer.h:33
virtual void releaseForRead(ReaderID id, int released)=0
virtual int availableForRead(ReaderID id) const =0
virtual int totalTokensWritten() const =0
virtual ReaderID addReader(bool startFromZero=false)=0
virtual std::vector< T > & writeView()=0
void writerProduce(int released)
Definition: multiratebuffer.h:63
virtual void removeReader(ReaderID id)=0
void readerConsume(ReaderID id, int requested)
Definition: multiratebuffer.h:60
void readerProduce(ReaderID id, int released)
Definition: multiratebuffer.h:61
virtual bool acquireForWrite(int requested)=0
virtual int totalTokensRead(ReaderID id) const =0
virtual void setBufferInfo(const BufferInfo &info)=0
virtual void resize(int size, int phantomSize)=0
virtual int numberReaders() const =0
virtual bool acquireForRead(ReaderID id, int requested)=0
virtual const T & lastTokenProduced() const =0
BufferUsageType
Definition: types.h:436
Definition: algorithm.h:28
int ReaderID
Definition: types.h:344