Skip to content

Commit 0aea578

Browse files
committed
Fix indexing issue in Open Ephys format
1 parent 7289059 commit 0aea578

2 files changed

Lines changed: 26 additions & 40 deletions

File tree

Source/Processors/RecordNode/OriginalRecording.cpp

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ String OriginalRecording::getEngineID() const
6767
return "OPENEPHYS";
6868
}
6969

70-
void OriginalRecording::addChannel(int index, const Channel* chan)
71-
{
72-
//Just populate the file array with null so we can address it by index afterwards
73-
fileArray.add(nullptr);
74-
blockIndex.add(0);
75-
samplesSinceLastTimestamp.add(0);
76-
}
77-
7870
void OriginalRecording::addSpikeElectrode(int index, const SpikeRecordInfo* elec)
7971
{
8072
spikeFileArray.add(nullptr);
@@ -100,16 +92,15 @@ void OriginalRecording::openFiles(File rootFolder, int experimentNumber, int rec
10092
openFile(rootFolder,nullptr);
10193
openMessageFile(rootFolder);
10294

103-
for (int i = 0; i < fileArray.size(); i++)
104-
{
105-
if (getChannel(i)->getRecordState())
106-
{
107-
openFile(rootFolder,getChannel(i));
108-
blockIndex.set(i,0);
109-
samplesSinceLastTimestamp.set(i,0);
110-
}
95+
int nChannels = getNumRecordedChannels();
11196

112-
}
97+
for (int i = 0; i < nChannels; i++)
98+
{
99+
Channel* ch = getChannel(getRealChannel(i));
100+
openFile(rootFolder, ch);
101+
blockIndex.add(0);
102+
samplesSinceLastTimestamp.add(0);
103+
}
113104
for (int i = 0; i < spikeFileArray.size(); i++)
114105
{
115106
openSpikeFile(rootFolder,getSpikeElectrode(i));
@@ -175,7 +166,7 @@ void OriginalRecording::openFile(File rootFolder, Channel* ch)
175166
eventFile = chFile;
176167
else
177168
{
178-
fileArray.set(ch->recordIndex,chFile);
169+
fileArray.add(chFile);
179170
if (ch->nodeId != lastProcId)
180171
{
181172
lastProcId = ch->nodeId;
@@ -450,18 +441,15 @@ void OriginalRecording::writeData(int writeChannel, int realChannel, const float
450441
{
451442
int samplesWritten = 0;
452443

453-
//int sourceNodeId = getChannel(realChannel)->sourceNodeId;
454-
455-
//TODO: optimize. Now we use realchannel, we should optimize the whole thing to only use recorded channels
456-
samplesSinceLastTimestamp.set(realChannel, 0);
444+
samplesSinceLastTimestamp.set(writeChannel, 0);
457445

458446
int nSamples = size;
459447

460448
while (samplesWritten < nSamples) // there are still unwritten samples in this buffer
461449
{
462450
int numSamplesToWrite = nSamples - samplesWritten;
463451

464-
if (blockIndex[realChannel] + numSamplesToWrite < BLOCK_LENGTH) // we still have space in this block
452+
if (blockIndex[writeChannel] + numSamplesToWrite < BLOCK_LENGTH) // we still have space in this block
465453
{
466454

467455
// write buffer to disk!
@@ -470,15 +458,15 @@ void OriginalRecording::writeData(int writeChannel, int realChannel, const float
470458
writeChannel);
471459

472460
//timestamp += numSamplesToWrite;
473-
samplesSinceLastTimestamp.set(realChannel, samplesSinceLastTimestamp[realChannel] + numSamplesToWrite);
474-
blockIndex.set(realChannel, blockIndex[realChannel] + numSamplesToWrite);
461+
samplesSinceLastTimestamp.set(writeChannel, samplesSinceLastTimestamp[writeChannel] + numSamplesToWrite);
462+
blockIndex.set(writeChannel, blockIndex[writeChannel] + numSamplesToWrite);
475463
samplesWritten += numSamplesToWrite;
476464

477465
}
478466
else // there's not enough space left in this block for all remaining samples
479467
{
480468

481-
numSamplesToWrite = BLOCK_LENGTH - blockIndex[realChannel];
469+
numSamplesToWrite = BLOCK_LENGTH - blockIndex[writeChannel];
482470

483471
// write buffer to disk!
484472
writeContinuousBuffer(buffer + samplesWritten,
@@ -488,8 +476,8 @@ void OriginalRecording::writeData(int writeChannel, int realChannel, const float
488476
// update our variables
489477
samplesWritten += numSamplesToWrite;
490478
//timestamp += numSamplesToWrite;
491-
samplesSinceLastTimestamp.set(realChannel, samplesSinceLastTimestamp[realChannel] + numSamplesToWrite);
492-
blockIndex.set(realChannel,0); // back to the beginning of the block
479+
samplesSinceLastTimestamp.set(writeChannel, samplesSinceLastTimestamp[writeChannel] + numSamplesToWrite);
480+
blockIndex.set(writeChannel, 0); // back to the beginning of the block
493481
}
494482
}
495483

@@ -498,31 +486,30 @@ void OriginalRecording::writeData(int writeChannel, int realChannel, const float
498486

499487
void OriginalRecording::writeContinuousBuffer(const float* data, int nSamples, int writeChannel)
500488
{
501-
int channel = getRealChannel(writeChannel);
502489
// check to see if the file exists
503-
if (fileArray[channel] == nullptr)
490+
if (fileArray[writeChannel] == nullptr)
504491
return;
505492

506493
// scale the data back into the range of int16
507-
float scaleFactor = float(0x7fff) * getChannel(channel)->bitVolts;
494+
float scaleFactor = float(0x7fff) * getChannel(getRealChannel(writeChannel))->bitVolts;
508495

509496
for (int n = 0; n < nSamples; n++)
510497
{
511498
*(continuousDataFloatBuffer+n) = *(data+n) / scaleFactor;
512499
}
513500
AudioDataConverters::convertFloatToInt16BE(continuousDataFloatBuffer, continuousDataIntegerBuffer, nSamples);
514501

515-
if (blockIndex[channel] == 0)
502+
if (blockIndex[writeChannel] == 0)
516503
{
517-
writeTimestampAndSampleCount(fileArray[channel], writeChannel);
504+
writeTimestampAndSampleCount(fileArray[writeChannel], writeChannel);
518505
}
519506

520507
diskWriteLock.enter();
521508

522509
size_t count = fwrite(continuousDataIntegerBuffer, // ptr
523510
2, // size of each element
524511
nSamples, // count
525-
fileArray[channel]); // ptr to FILE object
512+
fileArray[writeChannel]); // ptr to FILE object
526513

527514
//std::cout << channel << " : " << nSamples << " : " << count << std::endl;
528515

@@ -531,9 +518,9 @@ void OriginalRecording::writeContinuousBuffer(const float* data, int nSamples, i
531518

532519
diskWriteLock.exit();
533520

534-
if (blockIndex[channel] + nSamples == BLOCK_LENGTH)
521+
if (blockIndex[writeChannel] + nSamples == BLOCK_LENGTH)
535522
{
536-
writeRecordMarker(fileArray[channel]);
523+
writeRecordMarker(fileArray[writeChannel]);
537524
}
538525
}
539526

@@ -590,13 +577,13 @@ void OriginalRecording::closeFiles()
590577
writeContinuousBuffer(zeroBuffer.getReadPointer(0), BLOCK_LENGTH - blockIndex[i], i);
591578
diskWriteLock.enter();
592579
fclose(fileArray[i]);
593-
fileArray.set(i,nullptr);
594580
diskWriteLock.exit();
595581
}
596582
}
597-
598-
blockIndex.set(i,0);
599583
}
584+
fileArray.clear();
585+
blockIndex.clear();
586+
samplesSinceLastTimestamp.clear();
600587
for (int i = 0; i < spikeFileArray.size(); i++)
601588
{
602589
if (spikeFileArray[i] != nullptr)

Source/Processors/RecordNode/OriginalRecording.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class OriginalRecording : public RecordEngine
5050
void closeFiles() override;
5151
void writeData(int writeChannel, int realChannel, const float* buffer, int size) override;
5252
void writeEvent(int eventType, const MidiMessage& event, int64 timestamp) override;
53-
void addChannel(int index, const Channel* chan) override;
5453
void resetChannels() override;
5554
void addSpikeElectrode(int index, const SpikeRecordInfo* elec) override;
5655
void writeSpike(int electrodeIndex, const SpikeObject& spike, int64 timestamp) override;

0 commit comments

Comments
 (0)