@@ -14,11 +14,36 @@ Modified from https://en.wikipedia.org/wiki/Circular_buffer
14
14
Mirroring version
15
15
On 18 April 2014, the simplified version on the Wikipedia page for power of 2 sized buffers
16
16
doesn'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)
17
20
*/
18
21
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
+
22
47
23
48
/* * Circular buffer object. Has a fixed number of cells, set to 256.
24
49
@tparam ITEM_TYPE the kind of data to store, eg. int, int8_t etc.
@@ -60,7 +85,7 @@ class CircularBuffer
60
85
61
86
inline
62
87
unsigned long count () {
63
- return (num_buffers_read << 8 ) + start;
88
+ return (num_buffers_read << COUNT_LSHIFT ) + start;
64
89
}
65
90
inline
66
91
ITEM_TYPE * address () {
@@ -75,7 +100,7 @@ class CircularBuffer
75
100
uint8_t e_msb;
76
101
unsigned long num_buffers_read;
77
102
78
-
103
+ # if (CIRCULAR_BUFFER_SIZE == 256)
79
104
inline
80
105
void cbIncrStart () {
81
106
start++;
@@ -90,5 +115,32 @@ class CircularBuffer
90
115
end++;
91
116
if (end == 0 ) e_msb ^= 1 ;
92
117
}
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
+
93
142
94
143
};
144
+
145
+
146
+ #undef COUNT_LSHIFT // avoid macro spil
0 commit comments