forked from alexbw/novocaine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRingBuffer.mm
248 lines (192 loc) · 7.87 KB
/
RingBuffer.mm
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
// Copyright (c) 2012 Alex Wiltschko
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#include "RingBuffer.h"
// TODO: teardown function to release mData
RingBuffer::RingBuffer(SInt64 bufferLength, SInt64 numChannels) :
mSizeOfBuffer(bufferLength)
{
if (numChannels > kMaxNumChannels)
mNumChannels = kMaxNumChannels;
else if (numChannels <= 0)
mNumChannels = 1;
else
mNumChannels = numChannels;
mData = (float **)calloc(numChannels, sizeof(float *));
for (int i=0; i < numChannels; ++i) {
mData[i] = (float *)calloc(bufferLength, sizeof(float));
mLastWrittenIndex[i] = 0;
mLastReadIndex[i] = 0;
mNumUnreadFrames[i] = 0;
}
}
void RingBuffer::AddNewSInt16AudioBuffer(const AudioBuffer aBuffer)
{
int numChannelsHere = aBuffer.mNumberChannels;
int numFrames = aBuffer.mDataByteSize/(numChannelsHere*sizeof(SInt16));
SInt16 *newData = (SInt16 *)aBuffer.mData;
SInt64 idx;
for (int iChannel = 0; iChannel < mNumChannels; ++iChannel) {
for (int i=0; i < numFrames; ++i) {
idx = (i + mLastWrittenIndex[iChannel]) % (mSizeOfBuffer);
mData[iChannel][idx] = (float)newData[i*numChannelsHere + iChannel];
}
mLastWrittenIndex[iChannel] = (mLastWrittenIndex[iChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[iChannel] = mNumUnreadFrames[iChannel] + numFrames;
if (mNumUnreadFrames[iChannel] >= mSizeOfBuffer) mNumUnreadFrames[iChannel] = mSizeOfBuffer;
}
}
void RingBuffer::AddNewSInt16Data(const SInt16 *newData, const SInt64 numFrames, const SInt64 whichChannel)
{
SInt64 idx;
for (int i=0; i < numFrames; ++i) {
idx = (i + mLastWrittenIndex[whichChannel]) % (mSizeOfBuffer);
mData[whichChannel][idx] = (float)newData[i];
}
mLastWrittenIndex[whichChannel] = (mLastWrittenIndex[whichChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[whichChannel] = mNumUnreadFrames[whichChannel] + numFrames;
if (mNumUnreadFrames[whichChannel] >= mSizeOfBuffer) mNumUnreadFrames[whichChannel] = mSizeOfBuffer;
}
void RingBuffer::AddNewFloatData(const float *newData, const SInt64 numFrames, const SInt64 whichChannel)
{
SInt64 idx;
for (int i=0; i < numFrames; ++i) {
idx = (i + mLastWrittenIndex[whichChannel]) % (mSizeOfBuffer);
mData[whichChannel][idx] = newData[i];
}
mLastWrittenIndex[whichChannel] = (mLastWrittenIndex[whichChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[whichChannel] = mNumUnreadFrames[whichChannel] + numFrames;
if (mNumUnreadFrames[whichChannel] >= mSizeOfBuffer) mNumUnreadFrames[whichChannel] = mSizeOfBuffer;
}
void RingBuffer::AddNewDoubleData(const double *newData, const SInt64 numFrames, const SInt64 whichChannel)
{
SInt64 idx;
for (int i=0; i < numFrames; ++i) {
idx = (i + mLastWrittenIndex[whichChannel]) % (mSizeOfBuffer);
mData[whichChannel][idx] = (float)newData[i];
}
mLastWrittenIndex[whichChannel] = (mLastWrittenIndex[whichChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[whichChannel] = mNumUnreadFrames[whichChannel] + numFrames;
if (mNumUnreadFrames[whichChannel] >= mSizeOfBuffer) mNumUnreadFrames[whichChannel] = mSizeOfBuffer;
}
void RingBuffer::AddNewInterleavedFloatData(const float *newData, const SInt64 numFrames, const SInt64 numChannelsHere)
{
int numChannelsToCopy = (numChannelsHere <= mNumChannels) ? numChannelsHere : mNumChannels;
float zero = 0.0f;
for (int iChannel = 0; iChannel < numChannelsToCopy; ++iChannel) {
if (numFrames + mLastWrittenIndex[iChannel] < mSizeOfBuffer) { // if our new set of samples won't overrun the edge of the buffer
vDSP_vsadd((float *)&newData[iChannel],
numChannelsHere,
&zero,
&mData[iChannel][mLastWrittenIndex[iChannel]],
1,
numFrames);
}
else { // if we will overrun, then we need to do two separate copies.
int numSamplesInFirstCopy = mSizeOfBuffer - mLastWrittenIndex[iChannel];
int numSamplesInSecondCopy = numFrames - numSamplesInFirstCopy;
vDSP_vsadd((float *)&newData[iChannel],
numChannelsHere,
&zero,
&mData[iChannel][mLastWrittenIndex[iChannel]],
1,
numSamplesInFirstCopy);
vDSP_vsadd((float *)&newData[numSamplesInFirstCopy*numChannelsHere + iChannel],
numChannelsHere,
&zero,
&mData[iChannel][0],
1,
numSamplesInSecondCopy);
}
mLastWrittenIndex[iChannel] = (mLastWrittenIndex[iChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[iChannel] = (mNumUnreadFrames[iChannel] + numFrames);
if (mNumUnreadFrames[iChannel] >= mSizeOfBuffer) mNumUnreadFrames[iChannel] = mSizeOfBuffer;
}
}
void RingBuffer::FetchData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride)
{
int idx;
for (int i=0; i < numFrames; ++i) {
idx = (mLastReadIndex[whichChannel] + i) % (mSizeOfBuffer);
outData[i*stride] = mData[whichChannel][idx];
}
mLastReadIndex[whichChannel] = (mLastReadIndex[whichChannel] + numFrames) % (mSizeOfBuffer);
mNumUnreadFrames[whichChannel] -= numFrames;
if (mNumUnreadFrames[whichChannel] <= 0) mNumUnreadFrames[whichChannel] = 0;
}
void RingBuffer::FetchInterleavedData(float *outData, SInt64 numFrames, SInt64 numChannels)
{
for (int iChannel=0; iChannel < numChannels; ++iChannel) {
FetchData(&outData[iChannel], numFrames, iChannel, numChannels);
}
}
void RingBuffer::FetchFreshData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride)
{
int idx;
for (int i=0; i < numFrames; ++i) {
idx = (mLastWrittenIndex[whichChannel] - numFrames + i) % (mSizeOfBuffer);
outData[i*stride] = mData[whichChannel][idx];
}
mLastReadIndex[whichChannel] = mLastWrittenIndex[whichChannel];
mNumUnreadFrames[whichChannel] = 0; // Reading at the front of the buffer resets old data
}
void RingBuffer::SeekWriteHeadPosition(SInt64 offset, int iChannel)
{
mLastWrittenIndex[iChannel] = (mLastWrittenIndex[iChannel] + offset) % (mSizeOfBuffer);
}
void RingBuffer::SeekReadHeadPosition(SInt64 offset, int iChannel)
{
mLastReadIndex[iChannel] = (mLastReadIndex[iChannel] + offset) % (mSizeOfBuffer);
}
SInt64 RingBuffer::NumNewFrames(SInt64 lastReadFrame, int iChannel)
{
int numNewFrames = mLastWrittenIndex[iChannel] - lastReadFrame;
if (numNewFrames < 0) numNewFrames += mSizeOfBuffer;
return (SInt64)numNewFrames;
}
#pragma mark - Analytics
float RingBuffer::Mean(const SInt64 whichChannel)
{
float mean;
vDSP_meanv(mData[whichChannel],1,&mean,mSizeOfBuffer);
return mean;
}
float RingBuffer::Max(const SInt64 whichChannel)
{
float max;
vDSP_maxv(mData[whichChannel],1,&max,mSizeOfBuffer);
return max;
}
float RingBuffer::Min(const SInt64 whichChannel)
{
float min;
vDSP_minv(mData[whichChannel],1,&min,mSizeOfBuffer);
return min;
}
void RingBuffer::Clear()
{
for (int i=0; i < mNumChannels; ++i) {
memset(mData[i], 0, sizeof(float)*mSizeOfBuffer);
mLastWrittenIndex[i] = 0;
mLastReadIndex[i] = 0;
}
}