CSL  6.0
RingBuffer.h
Go to the documentation of this file.
1 //
2 // RingBuffer.h -- the ring buffer class specification
3 // RingBufferTap, RingBuffer and BufferStream
4 //
5 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
6 //
7 // This is a typical circular buffer with one writer and multiple readers (tap instances)
8 //
9 
10 #ifndef CSL_RingBuffer_H
11 #define CSL_RingBuffer_H
12 
13 #include "CSL_Core.h"
14 
15 namespace csl {
16 
17 ///
18 /// RingBufferTap is a reader that loops over a buffer
19 ///
20 
21 class RingBufferTap: public UnitGenerator, public Scalable, public Seekable {
22 
23 public:
24  friend class RingBuffer; ///< Allow RingBuffer to access private members of RingBufferTap
25 
26  /// Create a tap on a ring buffer, optionally offset relative
27  /// to the current write position.
28  RingBufferTap(RingBuffer *parent = 0, int offset = 0);
29 
30  unsigned mLoopStartFrame; ///< Number of frames from the beginning to start a loop at
31  unsigned mLoopEndFrame; ///< Number of frames away from buffer end.
32 
33  void setOffset(int offset);
34  unsigned duration();
35  unsigned seekTo(int position, SeekPosition whence) throw(CException);
36  void setLoopStart(unsigned frame) { mLoopStartFrame = frame; }
37  void setLoopEnd(unsigned frame) { mLoopEndFrame = frame; }
38  void setBuffer(RingBuffer *parent) { mParentBuffer = parent; }
39 
40  void nextBuffer(Buffer &outputBuffer) throw(CException); ///< nextBuffer method
41  void nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw(CException);
42  void destructiveNextBuffer(Buffer&outputBuffer) throw(CException); ///< zeroing as it goes.
43  void destructiveNextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw(CException);
44 
45 protected:
46  unsigned mTempCurrentFrame; // A hack necessary to track info.
47  RingBuffer *mParentBuffer; // A reference to the buffer from which i'm reading.
48 
49 };
50 
51 ///
52 /// RingBuffer is the storage + a default reader
53 ///
54 // NOTE: Doesn't delete the memory allocated for the buffer. It should! Jorge, June 2006.
55 
56 class RingBuffer : public Effect, public Scalable, public Writeable {
57 
58 public:
59  friend class RingBufferTap; ///< Allow the RingBufferTap to access private members of this class.
60 
61  RingBuffer(); ///< Constructors
62  RingBuffer(unsigned int nmChannels, unsigned int nmFrames);
63  RingBuffer(UnitGenerator & input, unsigned int nmChannels, unsigned int nmFrames);
64 
65  unsigned mCurrentWriteFrame; ///< state -- users can manipulate my internal tap and buffer
67  RingBufferTap mTap; ///< internal tap so a RingBuffer can also be a a UnitGenerator
68 
69  unsigned seekTo(int position) throw(CException);
70 
71  /// These loop setters allow for variable buffer lengths by varying the points where the buffer
72  /// writes.
73  void setLoopStart(unsigned frame) { mTap.setLoopStart(frame); }; ///< Calls the setLoopStart of it's tap.
74  void setLoopEnd(unsigned frame) { mTap.setLoopEnd(frame); }; ///< Calls the setLoopEnd of it's tap.
75 
76  /// Various flavours of next buffer. Also the nextBuffer(Buffer &) has to be implemented to be able to
77  /// hold state of the currentWriteFrame. Otherwise every time it's called by the super nextBuffer
78  /// the currentWriteFrame would be incremented more and more, leading to erroneous results.
79  void nextBuffer(Buffer &outputBuffer) throw(CException); ///< Read a buffer from the ring buffer
80  void writeBuffer(Buffer &inputBuffer) throw(CException); ///< Write a buffer of data into the ring buffer
81  void sumIntoBuffer(Buffer &inputBuffer) throw(CException); ///< Do an adding write of data into the ring buffer
82  void destructiveNextBuffer(Buffer &outputBuffer) throw(CException); ///< Read a buffer zeroing as you go
83  void writeBuffer(Buffer &inputBuffer, unsigned bufNum) throw(CException);
84  void sumIntoBuffer(Buffer &inputBuffer, unsigned bufNum) throw(CException);
85 
86 protected:
87  unsigned mTempCurrentWriteFrame; ///< Used in next buffer to save state between calls in the same block.
88 };
89 
90 /// Class to simplify writing into and playing back buffers.
91 /// Think of this as a simple buffer that has a seek, read and write calls built-in.
92 
93 class BufferStream : public UnitGenerator, public Seekable, public Writeable {
94 public:
95  BufferStream(Buffer &buffer) : UnitGenerator(), Seekable(), Writeable(), mBuffer(&buffer),
96  mCurrentWriteFrame(0), mTempCurrentFrame(0), mTempCurrentWriteFrame(0) { };
98  void dump() { };
99 
100  void setBuffer(Buffer &buffer) { mBuffer = &buffer; }
101 
102  void nextBuffer(Buffer &outputBuffer) throw(CException); ///< Read a buffer from the buffer stream
103  void nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw(CException);
104 
105  void writeBuffer(Buffer &inputBuffer) throw(CException); ///< Write a buffer of data into the ring buffer
106  void writeBuffer(Buffer &inputBuffer, unsigned bufNum) throw(CException);
107 
108  unsigned seekTo(int position, SeekPosition whence) throw(CException);
109  unsigned duration() { return mBuffer->mNumFrames; };
110 
111 protected:
112  Buffer *mBuffer;
114  unsigned mTempCurrentFrame; ///< a little hack necessary to track info
115  unsigned mTempCurrentWriteFrame; ///< a little hack necessary to track info
116 };
117 
118 }
119 
120 #endif
SeekPosition
Enumeration for seek flags.
Definition: CSL_Core.h:560
unsigned mLoopEndFrame
Number of frames away from buffer end.
Definition: RingBuffer.h:31
RingBuffer is the storage + a default reader.
Definition: RingBuffer.h:56
unsigned mNumFrames
num frames used in each buffer
Definition: CSL_Core.h:113
void setOffset(int offset)
Definition: RingBuffer.cpp:27
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
Effect – mix-in for classes that have unit generators as inputs (like filters).
Definition: CSL_Core.h:466
void setLoopEnd(unsigned frame)
Definition: RingBuffer.h:37
void sumIntoBuffer(Buffer &inputBuffer)
Do an adding write of data into the ring buffer.
Definition: RingBuffer.cpp:207
unsigned mTempCurrentWriteFrame
Used in next buffer to save state between calls in the same block.
Definition: RingBuffer.h:87
unsigned mTempCurrentFrame
a little hack necessary to track info
Definition: RingBuffer.h:114
RingBuffer()
Constructors.
Definition: RingBuffer.cpp:165
void setLoopStart(unsigned frame)
Definition: RingBuffer.h:36
void setBuffer(RingBuffer *parent)
Definition: RingBuffer.h:38
RingBufferTap mTap
internal tap so a RingBuffer can also be a a UnitGenerator
Definition: RingBuffer.h:67
BufferStream(Buffer &buffer)
Definition: RingBuffer.h:95
void setLoopStart(unsigned frame)
These loop setters allow for variable buffer lengths by varying the points where the buffer writes...
Definition: RingBuffer.h:73
unsigned mCurrentWriteFrame
Definition: RingBuffer.h:113
Class to simplify writing into and playing back buffers. Think of this as a simple buffer that has a ...
Definition: RingBuffer.h:93
unsigned duration()
Definition: RingBuffer.cpp:23
void nextBuffer(Buffer &outputBuffer)
nextBuffer method
Definition: RingBuffer.cpp:42
void writeBuffer(Buffer &inputBuffer)
Write a buffer of data into the ring buffer.
Definition: RingBuffer.cpp:326
Scalable – mix-in class with scale and offset control inputs (may be constants or generators)...
Definition: CSL_Core.h:403
unsigned mLoopStartFrame
Number of frames from the beginning to start a loop at.
Definition: RingBuffer.h:30
void setLoopEnd(unsigned frame)
Calls the setLoopStart of it's tap.
Definition: RingBuffer.h:74
Buffer mBuffer
Definition: RingBuffer.h:66
RingBuffer * mParentBuffer
Definition: RingBuffer.h:47
void writeBuffer(Buffer &inputBuffer)
Write a buffer of data into the ring buffer.
Definition: RingBuffer.cpp:201
Writeable – a mix-in for buffers and streams that one can write to.
Definition: CSL_Core.h:546
unsigned seekTo(int position, SeekPosition whence)
general-purpose seek on a stream
Definition: RingBuffer.cpp:138
unsigned seekTo(int position)
Definition: RingBuffer.cpp:215
unsigned duration()
Definition: RingBuffer.h:109
void nextBuffer(Buffer &outputBuffer)
Calls the setLoopEnd of it's tap.
Definition: RingBuffer.cpp:189
RingBufferTap is a reader that loops over a buffer.
Definition: RingBuffer.h:21
void nextBuffer(Buffer &outputBuffer)
Read a buffer from the buffer stream.
Definition: RingBuffer.cpp:320
unsigned seekTo(int position, SeekPosition whence)
general-purpose seek on a stream
Definition: RingBuffer.cpp:305
void dump()
pretty-print the receiver
Definition: RingBuffer.h:98
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
void destructiveNextBuffer(Buffer &outputBuffer)
zeroing as it goes.
Definition: RingBuffer.cpp:89
RingBufferTap(RingBuffer *parent=0, int offset=0)
Create a tap on a ring buffer, optionally offset relative to the current write position.
Definition: RingBuffer.cpp:16
void destructiveNextBuffer(Buffer &outputBuffer)
Read a buffer zeroing as you go.
Definition: RingBuffer.cpp:197
void setBuffer(Buffer &buffer)
Definition: RingBuffer.h:100
unsigned mTempCurrentFrame
Definition: RingBuffer.h:46
forward declaration
Definition: CSL_Core.h:241
Seekable – a mix-in for positionable streams.
Definition: CSL_Core.h:577
unsigned mCurrentWriteFrame
state – users can manipulate my internal tap and buffer
Definition: RingBuffer.h:65
Base class of CSL exceptions (written upper-case). Has a string message.
unsigned mTempCurrentWriteFrame
a little hack necessary to track info
Definition: RingBuffer.h:115
Buffer * mBuffer
Definition: RingBuffer.h:109