-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCircularBuffer.cs
292 lines (256 loc) · 7.91 KB
/
CircularBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace opentuner
{
public class CircularBuffer
{
private byte[] buffer;
private int head;
private int tail;
private readonly int capacity;
private readonly object syncRoot = new object();
public int Count { get; private set; }
public CircularBuffer(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentException("Buffer capacity must be positive.", nameof(capacity));
}
this.capacity = capacity;
buffer = new byte[capacity];
}
public void Enqueue(byte item)
{
lock (syncRoot)
{
if (Count == capacity)
{
Dequeue();
}
buffer[head] = item;
head = (head + 1) % capacity;
Count++;
}
}
public void Enqueue(byte[] items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
lock (syncRoot)
{
int bytesToAdd = items.Length;
int sourceIndex = 0;
while (bytesToAdd > 0)
{
int bytesToCopy = Math.Min(capacity - Count, bytesToAdd);
Array.Copy(items, sourceIndex, buffer, head, bytesToCopy);
head = (head + bytesToCopy) % capacity;
Count += bytesToCopy;
sourceIndex += bytesToCopy;
bytesToAdd -= bytesToCopy;
}
}
}
public byte Dequeue()
{
lock (syncRoot)
{
if (Count == 0)
{
// throw new InvalidOperationException("Buffer is empty.");
/* remark DL1RF: I observed this exception a few times.
* There seem to be a glitch between threads.
* Log an Error instead and return 0 as fake data here
* This happend while stop playing or leaving the program at all
* Occured only a very times.
*/
Log.Error("Dequeue(): Buffer is empty.");
return 0;
}
byte item = buffer[tail];
// buffer[tail] = 0; // mod dl1rf: I think no need to do this.
tail = (tail + 1) % capacity;
Count--;
return item;
}
}
public byte Peek()
{
lock (syncRoot)
{
if (Count == 0)
{
// throw new InvalidOperationException("Buffer is empty.");
/* remark DL1RF: I observed this exception a few times.
* There seem to be a glitch between threads.
* Log an Error instead and return 0 as fake data here
* This happend while stop playing or leaving the program at all
* Occured only a very few times.
*/
Log.Error("Peek(): Buffer is empty.");
return 0;
}
return buffer[tail];
}
}
public byte TryPeek()
{
lock (syncRoot)
{
if (Count == 0)
{
return 0;
}
return buffer[tail];
}
}
public byte[] DequeueBytes(int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
lock (syncRoot)
{
if (Count == 0)
{
throw new InvalidOperationException("Buffer is empty.");
}
int bytesToRead = Math.Min(count, Count);
byte[] result = new byte[bytesToRead];
int sourceIndex = tail;
int destIndex = 0;
while (bytesToRead > 0)
{
int bytesToCopy = Math.Min(bytesToRead, capacity - sourceIndex);
Array.Copy(buffer, sourceIndex, result, destIndex, bytesToCopy);
sourceIndex = (sourceIndex + bytesToCopy) % capacity;
destIndex += bytesToCopy;
bytesToRead -= bytesToCopy;
}
Count -= result.Length;
tail = sourceIndex;
return result;
}
}
public void Clear()
{
lock (syncRoot)
{
Array.Clear(buffer, 0, buffer.Length);
head = tail = Count = 0;
}
}
}
/*
public class CircularBuffer<T>
{
private T[] buffer;
private int capacity;
private int head;
private int tail;
public CircularBuffer(int initialCapacity)
{
if (initialCapacity < 1)
{
throw new ArgumentException("Initial capacity must be greater than zero.");
}
capacity = initialCapacity;
buffer = new T[capacity];
head = 0;
tail = 0;
}
public int Count
{
get
{
lock (buffer)
{
if (head >= tail)
{
return head - tail;
}
else
{
return capacity - (tail - head);
}
}
}
}
public void Enqueue(T item)
{
lock (buffer)
{
buffer[head] = item;
head = (head + 1) % capacity;
if (head == tail)
{
ResizeBuffer();
}
}
}
public bool TryDequeue(out T result)
{
lock (buffer)
{
if (head == tail)
{
result = default(T);
return false;
}
else
{
result = buffer[tail];
tail = (tail + 1) % capacity;
return true;
}
}
}
public bool TryPeek(out T result)
{
lock (buffer)
{
if (head == tail)
{
result = default(T);
return false;
}
else
{
result = buffer[tail];
return true;
}
}
}
public void Clear()
{
lock (buffer)
{
head = 0;
tail = 0;
buffer = new T[capacity];
}
}
private void ResizeBuffer()
{
T[] newBuffer = new T[capacity * 2];
int i = 0;
for (int j = tail; j != head; j = (j + 1) % capacity)
{
newBuffer[i++] = buffer[j];
}
buffer = newBuffer;
capacity *= 2;
head = i;
tail = 0;
//Log.Information("Need to resize buffer: " + capacity.ToString());
}
}
*/
}