From 95072833a226ec5f9187dc3857a84879056d30ca Mon Sep 17 00:00:00 2001
From: Aaron Cuevas Lopez <aacuelo@teleco.upv.es>
Date: Thu, 12 Jan 2017 05:05:45 +0100
Subject: [PATCH] Make Lfp Viewer able to detect source channel from event
 metadata

---
 .../Plugins/LfpDisplayNode/LfpDisplayNode.cpp | 17 +++++++++---
 .../Plugins/LfpDisplayNode/LfpDisplayNode.h   |  2 ++
 .../Plugins/PhaseDetector/PhaseDetector.cpp   | 27 ++++++++++---------
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/Source/Plugins/LfpDisplayNode/LfpDisplayNode.cpp b/Source/Plugins/LfpDisplayNode/LfpDisplayNode.cpp
index dcf2d80a8..757ea362e 100644
--- a/Source/Plugins/LfpDisplayNode/LfpDisplayNode.cpp
+++ b/Source/Plugins/LfpDisplayNode/LfpDisplayNode.cpp
@@ -67,7 +67,7 @@ void LfpDisplayNode::updateSettings()
 
     for (int i = 0; i < eventChannelArray.size(); ++i)
     {
-		uint32 sourceID = getProcessorFullId(eventChannelArray[i]->getSourceNodeID(), eventChannelArray[i]->getSubProcessorIdx());
+		uint32 sourceID = getChannelSourceID(eventChannelArray[i]);
         if (! eventSourceNodes.contains (sourceID ))
         {
             eventSourceNodes.add (sourceID);
@@ -93,6 +93,17 @@ void LfpDisplayNode::updateSettings()
     displayBufferIndex.insertMultiple (0, 0, getNumInputs() + numEventChannels);
 }
 
+uint32 LfpDisplayNode::getChannelSourceID(const EventChannel* event) const
+{
+	int metaDataIndex = event->findMetaData(MetaDataDescriptor::UINT16, 3, "source.channel.identifier.full");
+	if (metaDataIndex < 0)
+	{
+		return getProcessorFullId(event->getSourceNodeID(), event->getSubProcessorIdx());
+	}
+	uint16 values[3];
+	event->getMetaDataValue(metaDataIndex)->getValue(static_cast<uint16*>(values));
+	return getProcessorFullId(values[1], values[2]);
+}
 
 bool LfpDisplayNode::resizeBuffer()
 {
@@ -162,8 +173,8 @@ void LfpDisplayNode::handleEvent(const EventChannel* eventInfo, const MidiMessag
         const int eventId           = ttl->getState() ? 1 : 0;
         const int eventChannel      = ttl->getChannel();
         const int eventTime         = samplePosition;
-        const uint32 eventSourceNodeId = getProcessorFullId(ttl->getSourceID(), ttl->getSubProcessorIdx());
-        const int nSamples          = getNumSourceSamples (eventSourceNodeId);
+		const uint32 eventSourceNodeId = getChannelSourceID(eventInfo);
+		const int nSamples = getNumSourceSamples(eventSourceNodeId);
         int samplesToFill     = nSamples - eventTime;
 		if (samplesToFill < 0) samplesToFill = 0;
 
diff --git a/Source/Plugins/LfpDisplayNode/LfpDisplayNode.h b/Source/Plugins/LfpDisplayNode/LfpDisplayNode.h
index 6b863ca2f..cf0dc82ab 100644
--- a/Source/Plugins/LfpDisplayNode/LfpDisplayNode.h
+++ b/Source/Plugins/LfpDisplayNode/LfpDisplayNode.h
@@ -89,6 +89,8 @@ private:
 
     CriticalSection displayMutex;
 
+	uint32 getChannelSourceID(const EventChannel* event) const;
+
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LfpDisplayNode);
 };
 
diff --git a/Source/Plugins/PhaseDetector/PhaseDetector.cpp b/Source/Plugins/PhaseDetector/PhaseDetector.cpp
index c1612d792..a0d29130b 100644
--- a/Source/Plugins/PhaseDetector/PhaseDetector.cpp
+++ b/Source/Plugins/PhaseDetector/PhaseDetector.cpp
@@ -167,13 +167,14 @@ void PhaseDetector::updateSettings()
 			uint16 sourceInfo[3];
 			sourceInfo[0] = in->getSourceIndex();
 			sourceInfo[1] = in->getSourceNodeID();
-			sourceInfo[3] = in->getSubProcessorIdx();
+			sourceInfo[2] = in->getSubProcessorIdx();
 			mv.setValue(static_cast<const uint16*>(sourceInfo));
 			ev->addMetaData(md, mv);
 		}
 		eventChannelArray.add(ev);
 		moduleEventChannels.add(ev);
 	}
+	lastNumInputs = getNumInputs();
 }
 
 
@@ -219,9 +220,9 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
     checkForEvents ();
 
     // loop through the modules
-    for (int i = 0; i < modules.size(); ++i)
+    for (int m = 0; m < modules.size(); ++m)
     {
-        DetectorModule& module = modules.getReference (i);
+        DetectorModule& module = modules.getReference (m);
 
         // check to see if it's active and has a channel
         if (module.isActive && module.outputChan >= 0
@@ -239,8 +240,8 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
                     if (module.type == PEAK)
                     {
 						uint8 ttlData = 1 << module.outputChan;
-						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[i], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
-						addEvent(moduleEventChannels[i], event, i);
+						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[m], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
+						addEvent(moduleEventChannels[m], event, i);
                         module.samplesSinceTrigger = 0;
                         module.wasTriggered = true;
                     }
@@ -254,8 +255,8 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
                     if (module.type == FALLING_ZERO)
                     {
 						uint8 ttlData = 1 << module.outputChan;
-						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[i], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
-						addEvent(moduleEventChannels[i], event, i);
+						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[m], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
+						addEvent(moduleEventChannels[m], event, i);
                         module.samplesSinceTrigger = 0;
                         module.wasTriggered = true;
                     }
@@ -267,8 +268,8 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
                     if (module.type == TROUGH)
                     {
 						uint8 ttlData = 1 << module.outputChan;
-						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[i], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
-						addEvent(moduleEventChannels[i], event, i);
+						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[m], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
+						addEvent(moduleEventChannels[m], event, i);
                         module.samplesSinceTrigger = 0;
                         module.wasTriggered = true;
                     }
@@ -282,8 +283,8 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
                     if (module.type == RISING_ZERO)
                     {
 						uint8 ttlData = 1 << module.outputChan;
-						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[i], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
-						addEvent(moduleEventChannels[i], event, i);
+						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[m], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
+						addEvent(moduleEventChannels[m], event, i);
                         module.samplesSinceTrigger = 0;
                         module.wasTriggered = true;
                     }
@@ -298,8 +299,8 @@ void PhaseDetector::process (AudioSampleBuffer& buffer)
                     if (module.samplesSinceTrigger > 1000)
                     {
 						uint8 ttlData = 0;
-						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[i], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
-						addEvent(moduleEventChannels[i], event, i);
+						TTLEventPtr event = TTLEvent::createTTLEvent(moduleEventChannels[m], getTimestamp(module.inputChan) + i, &ttlData, sizeof(uint8), module.outputChan);
+						addEvent(moduleEventChannels[m], event, i);
                         module.wasTriggered = false;
                     }
                     else
-- 
GitLab