2323
2424#include " HDF5Recording.h"
2525#define MAX_BUFFER_SIZE 10000
26+ #define CHANNEL_TIMESTAMP_PREALLOC_SIZE 16
27+ #define TIMESTAMP_EACH_NSAMPLES 1024
2628
2729HDF5Recording::HDF5Recording () : processorIndex(-1 ), hasAcquired(false )
2830{
@@ -37,7 +39,7 @@ HDF5Recording::~HDF5Recording()
3739 delete intBuffer;
3840}
3941
40- String HDF5Recording::getEngineID ()
42+ String HDF5Recording::getEngineID () const
4143{
4244 return " KWIK" ;
4345}
@@ -73,6 +75,8 @@ void HDF5Recording::resetChannels()
7375 processorMap.clear ();
7476 infoArray.clear ();
7577 recordedChanToKWDChan.clear ();
78+ channelLeftOverSamples.clear ();
79+ channelTimestampArray.clear ();
7680 if (spikesFile)
7781 spikesFile->resetChannels ();
7882}
@@ -129,31 +133,11 @@ void HDF5Recording::openFiles(File rootFolder, int experimentNumber, int recordi
129133 int procPos = processorRecPos[index];
130134 recordedChanToKWDChan.add (procPos);
131135 processorRecPos.set (index, procPos+1 );
136+ channelTimestampArray.add (new Array<int64>);
137+ channelTimestampArray.getLast ()->ensureStorageAllocated (CHANNEL_TIMESTAMP_PREALLOC_SIZE);
138+ channelLeftOverSamples.add (0 );
132139 }
133- #if 0
134- for (int i = 0; i < processorMap.size(); i++)
135- {
136- int index = processorMap[i];
137- if (getChannel(i)->getRecordState())
138- {
139- if (!fileArray[index]->isOpen())
140- {
141- fileArray[index]->initFile(getChannel(i)->nodeId,basepath);
142- if (hasAcquired)
143- infoArray[index]->start_time = (*timestamps)[getChannel(i)->sourceNodeId]; //the timestamps of the first channel
144- else
145- infoArray[index]->start_time = 0;
146- }
147- channelsPerProcessor.set(index, channelsPerProcessor[index] + 1);
148- bitVoltsArray[index]->add(getChannel(i)->bitVolts);
149- sampleRatesArray[index]->add(getChannel(i)->sampleRate);
150- if (getChannel(i)->sampleRate != infoArray[index]->sample_rate)
151- {
152- infoArray[index]->multiSample = true;
153- }
154- }
155- }
156- #endif
140+
157141 for (int i = 0 ; i < fileArray.size (); i++)
158142 {
159143 if ((!fileArray[i]->isOpen ()) && (fileArray[i]->isReadyToOpen ()))
@@ -196,6 +180,18 @@ void HDF5Recording::closeFiles()
196180 }
197181 channelsPerProcessor.set (i, 0 );
198182 }
183+ recordedChanToKWDChan.clear ();
184+ channelTimestampArray.clear ();
185+ channelLeftOverSamples.clear ();
186+ }
187+
188+ void HDF5Recording::startChannelBlock ()
189+ {
190+ int nCh = channelTimestampArray.size ();
191+ for (int i = 0 ; i < nCh; ++i)
192+ {
193+ channelTimestampArray[i]->clearQuick ();
194+ }
199195}
200196
201197void HDF5Recording::writeData (int writeChannel, int realChannel, const float * buffer, int size)
@@ -208,6 +204,48 @@ void HDF5Recording::writeData(int writeChannel, int realChannel, const float* bu
208204 fileArray[index]->writeRowData (intBuffer, size, recordedChanToKWDChan[writeChannel]);
209205// int64 t2 = Time::getHighResolutionTicks();
210206// std::cout << "record time: " << float(t2 - t1) / float(Time::getHighResolutionTicksPerSecond()) << std::endl;
207+ int64 sampleOffset = channelLeftOverSamples[writeChannel];
208+ if (writeChannel == 0 )
209+ std::cout << " Write " << size << " off " << sampleOffset << " ts " << getTimestamp (realChannel) << " - " ;
210+ if (sampleOffset + size >= TIMESTAMP_EACH_NSAMPLES)
211+ {
212+ int64 currentTimestamp = getTimestamp (realChannel);
213+ if (sampleOffset > 0 )
214+ {
215+ currentTimestamp = getTimestamp (realChannel) + TIMESTAMP_EACH_NSAMPLES - sampleOffset;
216+ }
217+
218+ for (int samp = 0 ; samp < size; samp += TIMESTAMP_EACH_NSAMPLES)
219+ {
220+ if (writeChannel == 0 )
221+ std::cout << " w: " << currentTimestamp << " " ;
222+ channelTimestampArray[writeChannel]->add (currentTimestamp);
223+ currentTimestamp += TIMESTAMP_EACH_NSAMPLES;
224+ }
225+
226+ channelLeftOverSamples.set (writeChannel, (size + sampleOffset) % TIMESTAMP_EACH_NSAMPLES);
227+ }
228+ else
229+ {
230+ channelLeftOverSamples.set (writeChannel, sampleOffset + size);
231+ }
232+ if (writeChannel == 0 )
233+ std::cout << std::endl;
234+ }
235+
236+ void HDF5Recording::endChannelBlock ()
237+ {
238+ int nCh = channelTimestampArray.size ();
239+ for (int ch = 0 ; ch < nCh; ++ch)
240+ {
241+ int tsSize = channelTimestampArray[ch]->size ();
242+ if (tsSize > 0 )
243+ {
244+ int realChan = getRealChannel (ch);
245+ int index = processorMap[getChannel (realChan)->recordIndex ];
246+ fileArray[index]->writeTimestamps (channelTimestampArray[ch]->getRawDataPointer (), tsSize, ch);
247+ }
248+ }
211249}
212250
213251void HDF5Recording::writeEvent (int eventType, const MidiMessage& event, int64 timestamp)
0 commit comments