| Mozzi version v1.1.0 sound synthesis library for Arduino |

CircularBuffer.h
1 /*
2 Modified from https://en.wikipedia.org/wiki/Circular_buffer
3 Mirroring version
4 On 18 April 2014, the simplified version on the Wikipedia page for power of 2 sized buffers
5 doesn't work - cbIsEmpty() returns true whether the buffer is full or empty.
6 */
7
8 /** Circular buffer object. Has a fixed number of cells, set to 256.
9 @tparam ITEM_TYPE the kind of data to store, eg. int, int8_t etc.
10 */
11 template <class ITEM_TYPE>
12 class CircularBuffer
13 {
14
15 public:
16 /** Constructor
17 */
18 CircularBuffer(): start(0),end(0),s_msb(0),e_msb(0)
19 {
20 }
21
22 inline
23 bool isFull() {
24 return end == start && e_msb != s_msb;
25 }
26
27 inline
28 bool isEmpty() {
29 return end == start && e_msb == s_msb;
30 }
31
32 inline
33 void write(ITEM_TYPE in) {
34 items[end] = in;
35 //if (isFull()) cbIncrStart(); /* full, overwrite moves start pointer */
36 cbIncrEnd();
37 }
38
39 inline
40 ITEM_TYPE read() {
41 ITEM_TYPE out = items[start];
42 cbIncrStart();
43 return out;
44 }
45
46 inline
47 unsigned long count() {
48 return (num_buffers_read << 8) + start;
49 }
50
51 private:
52 ITEM_TYPE items[256];
53 uint8_t start; /* index of oldest itement */
54 uint8_t end; /* index at which to write new itement */
55 uint8_t s_msb;
56 uint8_t e_msb;
57 unsigned long num_buffers_read;
58
59
60 inline
61 void cbIncrStart() {
62 start++;
63 if (start == 0) {
64 s_msb ^= 1;
65 num_buffers_read++;
66 }
67 }
68
69 inline
70 void cbIncrEnd() {
71 end++;
72 if (end == 0) e_msb ^= 1;
73 }
74
75 };
CircularBuffer::CircularBuffer
CircularBuffer()
Constructor.
Definition: CircularBuffer.h:18
CircularBuffer
Circular buffer object.
Definition: CircularBuffer.h:12