@@ -14,11 +14,36 @@ Modified from https://en.wikipedia.org/wiki/Circular_buffer
1414Mirroring version
1515On 18 April 2014, the simplified version on the Wikipedia page for power of 2 sized buffers
1616doesn't work - cbIsEmpty() returns true whether the buffer is full or empty.
17+
18+ April 2025: modified for different buffer sizes under the suggestion
19+ of Meebleeps (https://github.com/sensorium/Mozzi/issues/281)
1720*/
1821
19- #define MOZZI_BUFFER_SIZE 256 // do not expect to change and it to work.
20- // just here for forward compatibility if one day
21- // the buffer size might be editable
22+ // TODO: remove this define from here, put a default value in config
23+ #define MOZZI_BUFFER_SIZE 256
24+
25+ // This is to get the correct cound for audioticks()
26+ #if (MOZZI_BUFFER_SIZE == 256)
27+ #define COUNT_LSHIFT 8
28+ #elif (MOZZI_BUFFER_SIZE == 128)
29+ #define COUNT_LSHIFT 7
30+ #elif (MOZZI_BUFFER_SIZE == 64)
31+ #define COUNT_LSHIFT 6
32+ #elif (MOZZI_BUFFER_SIZE == 32)
33+ #define COUNT_LSHIFT 5
34+ #elif (MOZZI_BUFFER_SIZE == 16)
35+ #define COUNT_LSHIFT 4
36+ #elif (MOZZI_BUFFER_SIZE == 8)
37+ #define COUNT_LSHIFT 3
38+ #elif (MOZZI_BUFFER_SIZE == 4)
39+ #define COUNT_LSHIFT 2
40+ #elif (MOZZI_BUFFER_SIZE == 2)
41+ #define COUNT_LSHIFT 1
42+ #elif (MOZZI_BUFFER_SIZE == 1)
43+ #define COUNT_LSHIFT 0
44+ #endif
45+
46+
2247
2348/* * Circular buffer object. Has a fixed number of cells, set to 256.
2449@tparam ITEM_TYPE the kind of data to store, eg. int, int8_t etc.
@@ -60,7 +85,7 @@ class CircularBuffer
6085
6186 inline
6287 unsigned long count () {
63- return (num_buffers_read << 8 ) + start;
88+ return (num_buffers_read << COUNT_LSHIFT ) + start;
6489 }
6590 inline
6691 ITEM_TYPE * address () {
@@ -75,7 +100,7 @@ class CircularBuffer
75100 uint8_t e_msb;
76101 unsigned long num_buffers_read;
77102
78-
103+ # if (CIRCULAR_BUFFER_SIZE == 256)
79104 inline
80105 void cbIncrStart () {
81106 start++;
@@ -90,5 +115,32 @@ class CircularBuffer
90115 end++;
91116 if (end == 0 ) e_msb ^= 1 ;
92117 }
118+ #else // if circular buffer length is != 256, use less efficient version for to manage start/end index buffer index
119+ inline
120+ void cbIncrStart () {
121+ start++;
122+ if (start == CIRCULAR_BUFFER_SIZE)
123+ {
124+ start = 0 ;
125+ s_msb ^= 1 ;
126+ num_buffers_read++;
127+ }
128+ }
129+
130+ inline
131+ void cbIncrEnd ()
132+ {
133+ end++;
134+ if (end == CIRCULAR_BUFFER_SIZE)
135+ {
136+ end = 0 ;
137+ e_msb ^= 1 ;
138+ }
139+ }
140+ #endif
141+
93142
94143};
144+
145+
146+ #UNDEF COUNT_LSHIFT // avoid macro spil
0 commit comments