Jamoma API  0.6.0.a19
TTDelayBuffer.h
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspLibrary
4  *
5  * @brief Audio delay buffer object.
6  *
7  * @authors Timothy Place
8  *
9  * @copyright Copyright © 2010, Timothy Place @n
10  * This code is licensed under the terms of the "New BSD License" @n
11  * http://creativecommons.org/licenses/BSD/
12  */
13 
14 #ifndef __TT_DELAYBUFFER_H__
15 #define __TT_DELAYBUFFER_H__
16 
17 #include "TTDSP.h"
18 
19 
20 /** TTDelayBuffer is a container object that holds some audio in a chunk of memory,
21  with accessors for using it as a circular delay line.
22 */
23 
24 
25 // class TTDSP_EXPORT TTDelayBuffer {
26 // This class is designed to be inlined, so there is nothing to export
28 
29 public: // NW: Should these be protected?
30 
31  TTSampleVector mBuffer;
32  TTSampleValuePtr mWritePointer; ///< "record" pointer for buffer
33  TTSampleValuePtr mReadPointer; ///< "playback" pointer
34  TTSampleValuePtr mEndPointer; ///< points to last sample in buffer (for speed)
35 
36 public:
37 
38  TTDelayBuffer() :
39  mWritePointer(NULL),
40  mReadPointer(NULL),
41  mEndPointer(NULL)
42  {
43  ;
44  }
45 
46  TTUInt32 size()
47  {
48  return mBuffer.size();
49  }
50 
51  void resize(TTUInt32 newSize)
52  {
53  mBuffer.resize(newSize);
54  mReadPointer = mWritePointer = &mBuffer[0];
55  mEndPointer = (&mBuffer[0]) + size();
56  }
57 
58  void clear()
59  {
60  mBuffer.assign(size(), 0.0);
61  }
62 
63  void setDelay(TTUInt32 delayInSamples)
64  {
65  mReadPointer = mWritePointer - delayInSamples;
66  if (mReadPointer < (&mBuffer[0]))
67  mReadPointer = mEndPointer + (mReadPointer - (&mBuffer[0])) + 1;
68  else if (mReadPointer > mEndPointer)
69  mReadPointer = (&mBuffer[0]) + (mReadPointer - mEndPointer);
70  }
71 
72  TTSampleValuePtr head()
73  {
74  return &mBuffer[0];
75  }
76 
77  TTSampleValuePtr tail()
78  {
79  return mEndPointer;
80  }
81 
82  // check pointers (both directions) to ensure they are in-bounds
83  TTSampleValuePtr wrapPointer(TTSampleValuePtr ptr)
84  {
85  if (ptr > tail())
86  ptr = head() + (ptr - tail()) - 1;
87  else if (ptr < head())
88  ptr = tail() + (ptr - head()) + 1;
89  return ptr;
90  }
91 
92  // check pointers (only at the end) to ensure they are in-bounds
93  TTSampleValuePtr wrapPointerForward(TTSampleValuePtr ptr)
94  {
95  if (ptr > tail())
96  ptr = head() + (ptr - tail()) - 1;
97  return ptr;
98  }
99 
100  void wrap()
101  {
102  mReadPointer = wrapPointer(mReadPointer);
103  mWritePointer = wrapPointer(mWritePointer);
104  }
105 
106  void wrapForward()
107  {
108  mReadPointer = wrapPointerForward(mReadPointer);
109  mWritePointer = wrapPointerForward(mWritePointer);
110  }
111 
112 };
114 typedef std::vector<TTDelayBuffer> TTDelayBufferVector;
115 typedef TTDelayBufferVector::iterator TTDelayBufferIter;
116 
117 
118 #endif // __TT_DELAYBUFFER_H__
TTSampleValuePtr mWritePointer
"record" pointer for buffer
Definition: TTDelayBuffer.h:32
Jamoma DSP Library.
std::vector< TTSampleValue > TTSampleVector
A TTSampleVector is simply a pointer to the first of an array of TTSampleValues.
Definition: TTBase.h:233
TTSampleValuePtr mReadPointer
"playback" pointer
Definition: TTDelayBuffer.h:33
TTSampleValuePtr mEndPointer
points to last sample in buffer (for speed)
Definition: TTDelayBuffer.h:34
std::uint32_t TTUInt32
32 bit unsigned integer
Definition: TTBase.h:178
TTDelayBuffer is a container object that holds some audio in a chunk of memory, with accessors for us...
Definition: TTDelayBuffer.h:27