forked from open-ephys/plugin-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBuffer.cpp
More file actions
executable file
·176 lines (139 loc) · 5.8 KB
/
Copy pathDataBuffer.cpp
File metadata and controls
executable file
·176 lines (139 loc) · 5.8 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
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2024 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 "DataBuffer.h"
DataBuffer::DataBuffer (int chans, int size)
: abstractFifo (size), buffer (chans, size), numChans (chans)
{
sampleNumberBuffer.malloc (size);
timestampBuffer.malloc (size);
eventCodeBuffer.malloc (size);
lastSampleNumber = 0;
lastTimestamp = -1.0;
}
DataBuffer::~DataBuffer()
{
}
void DataBuffer::clear()
{
buffer.clear();
abstractFifo.reset();
lastSampleNumber = 0;
lastTimestamp = -1.0;
}
void DataBuffer::resize (int chans, int size)
{
buffer.setSize (chans, size);
abstractFifo.setTotalSize (size);
sampleNumberBuffer.malloc (size);
timestampBuffer.malloc (size);
eventCodeBuffer.malloc (size);
lastSampleNumber = 0;
lastTimestamp = -1.0;
numChans = chans;
}
int DataBuffer::addToBuffer (float* data,
int64* sampleNumbers,
double* timestamps,
uint64* eventCodes,
int numItems)
{
int startIndex1, blockSize1, startIndex2, blockSize2;
abstractFifo.prepareToWrite (numItems, startIndex1, blockSize1, startIndex2, blockSize2);
int bs[3] = { blockSize1, blockSize2, 0 };
int si[2] = { startIndex1, startIndex2 };
int cSize = 0;
int idx = 0;
// for each of the dest blocks we can write to...
for (int i = 0; bs[i] != 0; ++i)
{
cSize = bs[i];
for (int chan = 0; chan < numChans; ++chan) // write that much, per channel
{
buffer.copyFrom (chan, // (int destChannel)
si[i], // (int destStartSample)
data + (chan * numItems) + idx, // (const float* source)
cSize); // (int num samples)
}
memcpy (sampleNumberBuffer + si[i], sampleNumbers + idx, (size_t) cSize * sizeof (int64));
memcpy (timestampBuffer + si[i], timestamps + idx, (size_t) cSize * sizeof (double));
memcpy (eventCodeBuffer + si[i], eventCodes + idx, (size_t) cSize * sizeof (uint64));
idx += cSize;
}
// finish write
abstractFifo.finishedWrite (idx);
return idx;
}
int DataBuffer::getNumSamples() const { return abstractFifo.getNumReady(); }
int DataBuffer::readAllFromBuffer (AudioBuffer<float>& data,
int64* blockSampleNumber,
double* blockTimestamps,
uint64* eventCodes,
int maxSize,
int dstStartChannel,
int numChannels)
{
// check to see if the maximum size is smaller than the total number of available ints
int numReady = abstractFifo.getNumReady();
int numItems = (maxSize < numReady) ? maxSize : numReady;
int startIndex1, blockSize1, startIndex2, blockSize2;
abstractFifo.prepareToRead (numItems, startIndex1, blockSize1, startIndex2, blockSize2);
int channelsToCopy = numChannels < 0 ? data.getNumChannels() : numChannels;
if (blockSize1 > 0)
{
for (int chan = 0; chan < channelsToCopy; ++chan)
{
data.copyFrom (dstStartChannel + chan, // destChan
0, // destStartSample
buffer, // source
chan, // sourceChannel
startIndex1, // sourceStartSample
blockSize1); // numSamples
}
memcpy (blockSampleNumber, sampleNumberBuffer + startIndex1, sizeof (int64));
memcpy (blockTimestamps, timestampBuffer + startIndex1, (size_t) blockSize1 * sizeof (double));
memcpy (eventCodes, eventCodeBuffer + startIndex1, (size_t) blockSize1 * sizeof (uint64));
}
else
{
// std::cout << "NO SAMPLES" << std::endl;
memcpy (blockSampleNumber, &lastSampleNumber, sizeof (int64));
memcpy (blockTimestamps, &lastTimestamp, sizeof (double));
}
if (blockSize2 > 0)
{
for (int chan = 0; chan < channelsToCopy; ++chan)
{
data.copyFrom (dstStartChannel + chan, // destChan
blockSize1, // destStartSample
buffer, // source
chan, // sourceChannel
startIndex2, // sourceStartSample
blockSize2); // numSamples
}
memcpy (blockTimestamps + blockSize1, timestampBuffer + startIndex2, (size_t) blockSize2 * sizeof (double));
memcpy (eventCodes + blockSize1, eventCodeBuffer + startIndex2, (size_t) blockSize2 * sizeof (uint64));
}
// std::cout << "START SAMPLE FOR READ: " << *blockSampleNumber << std::endl;
if (numItems > 0)
{
lastSampleNumber = *blockSampleNumber;
lastTimestamp = *blockTimestamps;
// std::cout << "Updating last sample number: " << lastSampleNumber << std::endl;
}
abstractFifo.finishedRead (numItems);
return numItems;
}