-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathDisplayBuffer.cpp
More file actions
289 lines (233 loc) · 7.62 KB
/
DisplayBuffer.cpp
File metadata and controls
289 lines (233 loc) · 7.62 KB
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
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2021 Open Ephys
------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DisplayBuffer.h"
namespace LfpViewer
{
#define BUFFER_LENGTH_S 1.0f
DisplayBuffer::DisplayBuffer (int id_, String name_, float sampleRate_) : id (id_), name (name_), sampleRate (sampleRate_), isNeeded (true)
{
previousSize = 0;
numChannels = 0;
const int heapSize = 5000;
arrayOfOnes = new float[heapSize];
for (int n = 0; n < heapSize; ++n)
{
arrayOfOnes[n] = 1;
}
ttlState = 0;
}
DisplayBuffer::~DisplayBuffer()
{
delete[] arrayOfOnes;
}
void DisplayBuffer::prepareToUpdate()
{
previousSize = numChannels;
channelMetadata.clear();
channelMap.clear();
numChannels = 0;
isNeeded = false;
}
void DisplayBuffer::addChannel (
String name,
int channelNum,
ContinuousChannel::Type type,
bool isRecorded,
int group,
float xpos,
float ypos,
String description,
String structure,
float inputRangeMin,
float inputRangeMax,
String units,
bool hasGroupMetadata,
bool hasYposMetadata,
bool hasXposMetadata)
{
ChannelMetadata metadata = ChannelMetadata();
metadata.name = name;
metadata.type = type;
metadata.group = group;
metadata.xpos = xpos;
metadata.ypos = ypos;
metadata.structure = structure;
metadata.type = type;
metadata.isRecorded = isRecorded;
metadata.description = description;
metadata.inputRangeMin = inputRangeMin;
metadata.inputRangeMax = inputRangeMax;
metadata.units = units;
metadata.hasGroupMetadata = hasGroupMetadata;
metadata.hasYposMetadata = hasYposMetadata;
metadata.hasXposMetadata = hasXposMetadata;
channelMetadata.add (metadata);
channelMap[channelNum] = numChannels;
numChannels++;
isNeeded = true;
// std::cout << "Adding channel " << name << " with index " << numChannels << "; ";
}
void DisplayBuffer::update()
{
if (numChannels != previousSize)
setSize (numChannels + 1, int (sampleRate * BUFFER_LENGTH_S));
clear();
displayBufferIndices.clear();
for (int i = 0; i <= numChannels; i++)
displayBufferIndices.set (i, 0);
}
void DisplayBuffer::resetIndices()
{
for (int i = 0; i <= numChannels; i++)
displayBufferIndices.set (i, 0);
}
void DisplayBuffer::addDisplay (int splitID)
{
if (! displays.contains (splitID))
displays.add (splitID);
}
void DisplayBuffer::removeDisplay (int splitID)
{
if (displays.contains (splitID))
displays.remove (displays.indexOf (splitID));
}
void DisplayBuffer::initializeEventChannel (int nSamples)
{
if (displays.size() == 0)
return;
const int samplesLeft = getNumSamples() - displayBufferIndices[numChannels];
if (nSamples < samplesLeft)
{
copyFrom (numChannels, // destChannel
displayBufferIndices[numChannels], // destStartSample
arrayOfOnes, // source
nSamples, // numSamples
float (ttlState)); // gain
}
else
{
int extraSamples = nSamples - samplesLeft;
copyFrom (numChannels, // destChannel
displayBufferIndices[numChannels], // destStartSample
arrayOfOnes, // source
samplesLeft, // numSamples
float (ttlState)); // gain
copyFrom (numChannels, // destChannel
0, // destStartSample
arrayOfOnes, // source
extraSamples, // numSamples
float (ttlState)); // gain
}
}
void DisplayBuffer::finalizeEventChannel (int nSamples)
{
if (displays.size() == 0)
return;
const int index = displayBufferIndices[numChannels];
const int samplesLeft = getNumSamples() - index;
int newIdx = 0;
if (nSamples < samplesLeft)
{
newIdx = displayBufferIndices[numChannels] + nSamples;
}
else
{
newIdx = nSamples - samplesLeft;
}
displayBufferIndices.set (numChannels, newIdx);
}
void DisplayBuffer::addEvent (int eventTime, int eventChannel, int eventId, int numSourceSamples)
{
if (displays.size() == 0)
return;
if (eventTime > numSourceSamples)
eventTime = numSourceSamples;
const int index = (displayBufferIndices[numChannels] + eventTime) % getNumSamples();
const int samplesLeft = getNumSamples() - index;
const int nSamples = numSourceSamples - eventTime;
if (index < 0)
return;
if (eventId == 1)
{
ttlState |= (1LL << eventChannel);
}
else
{
ttlState &= ~(1LL << eventChannel);
}
if (nSamples < samplesLeft)
{
copyFrom (numChannels, // destChannel
index, // destStartSample
arrayOfOnes, // source
nSamples, // numSamples
float (ttlState)); // gain
}
else
{
int extraSamples = nSamples - samplesLeft;
copyFrom (numChannels, // destChannel
index, // destStartSample
arrayOfOnes, // source
samplesLeft, // numSamples
float (ttlState)); // gain
copyFrom (numChannels, // destChannel
0, // destStartSample
arrayOfOnes, // source
extraSamples, // numSamples
float (ttlState)); // gain
}
}
void DisplayBuffer::addData (AudioBuffer<float>& buffer, int chan, int nSamples)
{
if (displays.size() == 0)
return;
int previousIndex = displayBufferIndices[channelMap[chan]];
int channelIndex = channelMap[chan];
const int samplesLeft = getNumSamples() - displayBufferIndices[channelMap[chan]];
int newIndex;
if (nSamples < samplesLeft)
{
copyFrom (channelMap[chan], // destChannel
displayBufferIndices[channelMap[chan]], // destStartSample
buffer, // source
chan, // source channel
0, // source start sample
nSamples); // numSamples
int lastIndex = displayBufferIndices[channelMap[chan]];
newIndex = lastIndex + nSamples;
}
else
{
const int extraSamples = nSamples - samplesLeft;
copyFrom (channelMap[chan], // destChannel
displayBufferIndices[channelMap[chan]], // destStartSample
buffer, // source
chan, // source channel
0, // source start sample
samplesLeft); // numSamples
copyFrom (channelMap[chan], // destChannel
0, // destStartSample
buffer, // source
chan, // source channel
samplesLeft, // source start sample
extraSamples); // numSamples
newIndex = extraSamples;
}
displayBufferIndices.set (channelMap[chan], newIndex);
}
}; // namespace LfpViewer