From 010534b12d1a43a2f70255444c74f3e0d37491bb Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Wed, 11 Jan 2017 03:01:50 +0100
Subject: [PATCH] Update kwik plugin

---
 .../KWIKFormat/RecordEngine/HDF5Recording.cpp | 54 ++++++++++++-------
 .../KWIKFormat/RecordEngine/HDF5Recording.h   |  9 ++--
 .../KWIKFormat/RecordEngine/KWIKFormat.cpp    |  4 +-
 .../KWIKFormat/RecordEngine/KWIKFormat.h      |  2 +-
 4 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.cpp b/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.cpp
index 473038db6..87fe24ce0 100644
--- a/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.cpp
+++ b/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.cpp
@@ -82,7 +82,7 @@ void HDF5Recording::resetChannels()
         spikesFile->resetChannels();
 }
 
-void HDF5Recording::addChannel(int index,const Channel* chan)
+void HDF5Recording::addDataChannel(int index,const DataChannel* chan)
 {
     processorMap.add(processorIndex);
 }
@@ -120,14 +120,14 @@ void HDF5Recording::openFiles(File rootFolder, int experimentNumber, int recordi
 		int index = processorMap[getRealChannel(i)];
 		if (!fileArray[index]->isOpen())
 		{
-			fileArray[index]->initFile(getChannel(getRealChannel(i))->nodeId, basepath);
+			fileArray[index]->initFile(getDataChannel(getRealChannel(i))->getCurrentNodeID(), basepath);
 			infoArray[index]->start_time = getTimestamp(i);
 		}
 
 		channelsPerProcessor.set(index, channelsPerProcessor[index] + 1);
-		bitVoltsArray[index]->add(getChannel(getRealChannel(i))->bitVolts);
-		sampleRatesArray[index]->add(getChannel(getRealChannel(i))->sampleRate);
-		if (getChannel(getRealChannel(i))->sampleRate != infoArray[index]->sample_rate)
+		bitVoltsArray[index]->add(getDataChannel(getRealChannel(i))->getBitVolts());
+		sampleRatesArray[index]->add(getDataChannel(getRealChannel(i))->getSampleRate());
+		if (getDataChannel(getRealChannel(i))->getSampleRate() != infoArray[index]->sample_rate)
 		{
 			infoArray[index]->multiSample = true;
 		}
@@ -199,8 +199,8 @@ void HDF5Recording::writeData(int writeChannel, int realChannel, const float* bu
 		scaledBuffer.malloc(size);
 		intBuffer.malloc(size);
 	}
-	double multFactor = 1 / (float(0x7fff) * getChannel(realChannel)->bitVolts);
-	int index = processorMap[getChannel(realChannel)->recordIndex];
+	double multFactor = 1 / (float(0x7fff) * getDataChannel(realChannel)->getBitVolts());
+	int index = processorMap[realChannel]; //CHECK
 	FloatVectorOperations::copyWithMultiply(scaledBuffer.getData(), buffer, multFactor, size);
 	AudioDataConverters::convertFloatToInt16LE(scaledBuffer.getData(), intBuffer.getData(), size);
 	fileArray[index]->writeRowData(intBuffer.getData(), size, recordedChanToKWDChan[writeChannel]);
@@ -235,29 +235,47 @@ void HDF5Recording::endChannelBlock(bool lastBlock)
 		if ((tsSize > 0) && ((tsSize > CHANNEL_TIMESTAMP_MIN_WRITE) || lastBlock))
 		{
 			int realChan = getRealChannel(ch);
-			int index = processorMap[getChannel(realChan)->recordIndex];
+			int index = processorMap[realChan]; //CHECK
 			fileArray[index]->writeTimestamps(channelTimestampArray[ch]->getRawDataPointer(), tsSize, recordedChanToKWDChan[ch]);
 			channelTimestampArray[ch]->clearQuick();
 		}
 	}
 }
 
-void HDF5Recording::writeEvent(int eventType, const MidiMessage& event, int64 timestamp)
+void HDF5Recording::writeEvent(int eventChannel, const MidiMessage& event)
 {
-    const uint8* dataptr = event.getRawData();
-    if (eventType == GenericProcessor::TTL)
-        eventFile->writeEvent(0,*(dataptr+2),*(dataptr+1),(void*)(dataptr+3),timestamp);
-    else if (eventType == GenericProcessor::MESSAGE)
-        eventFile->writeEvent(1,*(dataptr+2),*(dataptr+1),(void*)(dataptr+6),timestamp);
+	if (Event::getEventType(event) == EventChannel::TTL)
+	{
+		TTLEventPtr ttl = TTLEvent::deserializeFromMessage(event, getEventChannel(eventChannel));
+		if (ttl == nullptr) return;
+		uint8 channel = ttl->getChannel();
+		eventFile->writeEvent(0, (ttl->getState() ? 1 : 0), ttl->getSourceID(), &channel, ttl->getTimestamp());
+	}
+	else if (Event::getEventType(event) == EventChannel::TEXT)
+	{
+		TextEventPtr text = TextEvent::deserializeFromMessage(event, getEventChannel(eventChannel));
+		if (text == nullptr) return;
+		String textMsg = text->getText();
+		eventFile->writeEvent(1, 0, text->getSourceID(), textMsg.toUTF8().getAddress(), text->getTimestamp());
+	}
+}
+
+void HDF5Recording::writeTimestampSyncText(uint16 sourceID, uint16 sourceIdx, uint64 timestamp, String text)
+{
+	eventFile->writeEvent(1, 0xFF, sourceID, text.toUTF8().getAddress(), timestamp);
 }
 
-void HDF5Recording::addSpikeElectrode(int index, const SpikeRecordInfo* elec)
+void HDF5Recording::addSpikeElectrode(int index, const SpikeChannel* elec)
 {
-    spikesFile->addChannelGroup(elec->numChannels);
+    spikesFile->addChannelGroup(elec->getNumChannels());
 }
-void HDF5Recording::writeSpike(int electrodeIndex, const SpikeObject& spike, int64 /*timestamp*/)
+void HDF5Recording::writeSpike(int electrodeIndex, const SpikeEvent* spike)
 {
-    spikesFile->writeSpike(electrodeIndex,spike.nSamples,spike.data,spike.timestamp);
+	const SpikeChannel* spikeInfo = getSpikeChannel(electrodeIndex);
+	Array<float> bitVolts;
+	for (int i = 0; i < spikeInfo->getNumChannels(); i++)
+		bitVolts.add(spikeInfo->getChannelBitVolts(i));
+    spikesFile->writeSpike(electrodeIndex,spikeInfo->getTotalSamples(),spike->getDataPointer(), bitVolts, spike->getTimestamp());
 }
 
 void HDF5Recording::startAcquisition()
diff --git a/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.h b/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.h
index fbd37efd9..100e00b40 100644
--- a/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.h
+++ b/Source/Plugins/KWIKFormat/RecordEngine/HDF5Recording.h
@@ -36,10 +36,11 @@ public:
     void openFiles(File rootFolder, int experimentNumber, int recordingNumber) override;
 	void closeFiles() override;
 	void writeData(int writeChannel, int realChannel, const float* buffer, int size) override;
-	void writeEvent(int eventType, const MidiMessage& event, int64 timestamp) override;
-	void addChannel(int index, const Channel* chan) override;
-	void addSpikeElectrode(int index,const  SpikeRecordInfo* elec) override;
-	void writeSpike(int electrodeIndex, const SpikeObject& spike, int64 timestamp) override;
+	void writeEvent(int eventType, const MidiMessage& event) override;
+	void writeTimestampSyncText(uint16 sourceID, uint16 sourceIdx, uint64 timestamp, String text) override;
+	void addDataChannel(int index, const DataChannel* chan) override;
+	void addSpikeElectrode(int index,const  SpikeChannel* elec) override;
+	void writeSpike(int electrodeIndex, const SpikeEvent* spike) override;
 	void registerProcessor(const GenericProcessor* processor) override;
 	void resetChannels() override;
 	void startAcquisition() override;
diff --git a/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.cpp b/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.cpp
index 7b4743f18..d1390a43e 100644
--- a/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.cpp
+++ b/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.cpp
@@ -392,7 +392,7 @@ void KWXFile::resetChannels()
     channelArray.clear();
 }
 
-void KWXFile::writeSpike(int groupIndex, int nSamples, const uint16* data, uint64 timestamp)
+void KWXFile::writeSpike(int groupIndex, int nSamples, const float* data, Array<float>& bitVolts, uint64 timestamp)
 {
     if ((groupIndex < 0) || (groupIndex >= numElectrodes))
     {
@@ -408,7 +408,7 @@ void KWXFile::writeSpike(int groupIndex, int nSamples, const uint16* data, uint6
     {
         for (int j = 0; j < nChans; j++)
         {
-            *(dst++) = *(data+j*nSamples+i)-32768;
+            *(dst++) = static_cast<int16>((*(data+j*nSamples+i))/bitVolts[j]);
         }
     }
 
diff --git a/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.h b/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.h
index 324d637d9..e9597e720 100644
--- a/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.h
+++ b/Source/Plugins/KWIKFormat/RecordEngine/KWIKFormat.h
@@ -113,7 +113,7 @@ public:
     void stopRecording();
     void addChannelGroup(int nChannels);
     void resetChannels();
-    void writeSpike(int groupIndex, int nSamples, const uint16* data, uint64 timestamp);
+    void writeSpike(int groupIndex, int nSamples, const float* data, Array<float>& bitVolts, uint64 timestamp);
     String getFileName();
 
 protected:
-- 
GitLab