-
Notifications
You must be signed in to change notification settings - Fork 2
/
RingBuffer.cs
221 lines (181 loc) · 7.5 KB
/
RingBuffer.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
using CSIntel.Ipp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VE3NEA.HamCockpit.PluginAPI;
namespace VE3NEA.HamCockpit.DspFun
{
/// <summary>A thread-safe ring buffer for the floating point values.</summary>
public unsafe class RingBuffer
{
private readonly object lockObject = new object();
private float[] ringBuffer;
private float[] buffer;
/// <summary>Gets the count of the floating point values in the buffer.</summary>
/// <value>The count.</value>
public int Count { get; private set; }
private int readPos, writePos;
/// <summary>Gets or sets a value indicating whether the output is padded with zeros if there is not enough data to read.</summary>
/// <value>
/// <c>true</c> if padded with zeros; otherwise, <c>false</c>.</value>
public bool FillWithZeros { get; set; }
/// <summary>Occurs when filtered samples are available.</summary>
public event EventHandler<SamplesAvailableEventArgs> SamplesAvailable;
/// <summary>Initializes a new instance of the <see cref="RingBuffer" /> class.</summary>
/// <param name="capacity">The capacity of the ring buffer.</param>
public RingBuffer(int capacity)
{
Resize(capacity);
FillWithZeros = true;
}
/// <summary> Changes the capacity of the ring buffer.</summary>
/// <param name="capacity">The capacity.</param>
public void Resize(int capacity)
{
ringBuffer = new float[capacity];
Count = readPos = writePos = 0;
}
/// <summary>Writes floating point values to the specified buffer.</summary>
/// <param name="buffer">The buffer.</param>
/// <param name="offset">The offset to the first value.</param>
/// <param name="count">The number of values.</param>
public void Write(float[] buffer, int offset, int count)
{
if (count > ringBuffer.Length)
//throw new ArgumentException($"Too many values to write: {count}.");
Resize(count);
lock (lockObject)
{
int spaceAvailable = ringBuffer.Length - Count;
int count1 = Math.Min(count, ringBuffer.Length - writePos);
int count2 = count - count1;
if (count1 > 0)
{
Array.Copy(buffer, offset, ringBuffer, writePos, count1);
writePos += count1;
if (writePos == ringBuffer.Length) writePos = 0;
}
if (count2 > 0)
{
Array.Copy(buffer, offset + count1, ringBuffer, 0, count2);
writePos = count2;
}
Count += count;
//buffer overflow
if (count > spaceAvailable)
{
Count = ringBuffer.Length;
readPos = writePos;
}
}
SamplesAvailable?.Invoke(this, new SamplesAvailableEventArgs((float[])buffer.Clone(), offset, count));
}
public void WriteInt16(Int16[] buffer, int wordCount)
{
if (this.buffer == null || this.buffer.Length < wordCount)
this.buffer = new float[wordCount];
fixed (Int16* pInBuffer = buffer)
fixed (float* pOutBuffer = this.buffer)
sp.ippsConvert_16s32f_Sfs(pInBuffer, pOutBuffer, wordCount, 15);
Write(this.buffer, 0, wordCount);
}
//todo: avoid double buffering
/// <summary> Converts 16-bit integers to the floating point values and writes them to the buffer.</summary>
/// <param name="buffer">The buffer.</param>
/// <param name="byteCount">The number of bytes to convert and write.</param>
public void WriteInt16(byte[] buffer, int byteCount)
{
int sampleCount = byteCount / sizeof(Int16);
if (this.buffer == null || this.buffer.Length < sampleCount)
this.buffer = new float[sampleCount];
fixed (byte* pInBuffer = buffer)
fixed (float* pOutBuffer = this.buffer)
sp.ippsConvert_16s32f_Sfs((Int16*)pInBuffer, pOutBuffer, sampleCount, 15);
Write(this.buffer, 0, sampleCount);
}
/// <summary>Writes one of the channels of the strided data to the ring buffer.</summary>
/// <param name="data">The data.</param>
/// <param name="offset">The offset to the first value.</param>
/// <param name="inFloatCount">The number of the input floating point values.</param>
/// <param name="format">The format of the data.</param>
public void WriteStrided(float[] data, int offset, int inFloatCount, SignalFormat format)
{
//single channel, just copy
if (format.Channels == 1)
Write(data, offset, inFloatCount);
//multiple complex channels
else if (format.IsComplex)
{
int stride = format.Channels * Dsp.COMPONENTS_IN_COMPLEX;
int floatCount = inFloatCount / format.Channels;
int complexCount = inFloatCount / stride;
if (buffer == null || this.buffer.Length < floatCount) buffer = new float[floatCount];
Dsp.StridedToComplex(data, offset, stride, buffer, 0, complexCount);
Write(buffer, 0, floatCount);
}
//multiple real channels
else
{
int floatCount = inFloatCount / format.Channels;
if (buffer == null || this.buffer.Length < floatCount) buffer = new float[floatCount];
Dsp.StridedToFloat(data, offset, format.Channels, buffer, 0, floatCount);
Write(buffer, 0, floatCount);
}
}
/// <summary>Reads the data from the ring buffer to the provided buffer.</summary>
/// <param name="buffer">The output buffer.</param>
/// <param name="offset">The offset to the first value.</param>
/// <param name="count">The number of the values to read.</param>
/// <returns>The number of read values.</returns>
public int Read(float[] buffer, int offset, int count)
{
if (count > ringBuffer.Length)
//throw new ArgumentException($"Too many values to read: {count}.");
Resize(count);
lock (lockObject)
{
int readCount = Math.Min(Count, count);
int count1 = Math.Min(readCount, ringBuffer.Length - readPos);
int count2 = readCount - count1;
if (count1 > 0)
{
Array.Copy(ringBuffer, readPos, buffer, offset, count1);
readPos += count1;
if (readPos == ringBuffer.Length) readPos = 0;
}
if (count2 > 0)
{
Array.Copy(ringBuffer, 0, buffer, offset + count1, count2);
readPos = count2;
}
Count -= readCount;
if (readCount < count && FillWithZeros)
Array.Clear(buffer, offset + readCount, count - readCount);
return FillWithZeros ? count : readCount;
}
}
/// <summary>Removes the values from the ring buffer.</summary>
/// <param name="count">The number of values to remove.</param>
/// <exception cref="ArgumentException">Too many values to dump: {count}.</exception>
public void Dump(int count)
{
if (count > ringBuffer.Length)
throw new ArgumentException($"Too many values to dump: {count}.");
lock (lockObject)
{
int readCount = Math.Min(Count, count);
int count1 = Math.Min(readCount, ringBuffer.Length - readPos);
int count2 = readCount - count1;
if (count1 > 0)
{
readPos += count1;
if (readPos == ringBuffer.Length) readPos = 0;
}
if (count2 > 0) readPos = count2;
Count -= readCount;
}
}
}
}