From 619424e5f81a7fe56719ba7931f371009b2c4c23 Mon Sep 17 00:00:00 2001
From: jsiegle <jsiegle@mit.edu>
Date: Tue, 21 Feb 2012 21:48:44 -0500
Subject: [PATCH] Fixed blips in LfpDisplay

It was a float vs. int problem!
---
 Source/Processors/LfpDisplayNode.cpp          | 228 +++---------------
 Source/Processors/LfpDisplayNode.h            |   2 +
 .../Visualization/LfpDisplayCanvas.cpp        |  16 +-
 .../Processors/Visualization/OpenGLCanvas.cpp |   2 +-
 4 files changed, 42 insertions(+), 206 deletions(-)

diff --git a/Source/Processors/LfpDisplayNode.cpp b/Source/Processors/LfpDisplayNode.cpp
index d434da1ea..f0bea7a45 100644
--- a/Source/Processors/LfpDisplayNode.cpp
+++ b/Source/Processors/LfpDisplayNode.cpp
@@ -27,7 +27,7 @@
 LfpDisplayNode::LfpDisplayNode()
 	: GenericProcessor("LFP Viewer"),
 	  bufferLength(5.0f), displayGain(1),
-	  displayBufferIndex(0)
+	  displayBufferIndex(0), abstractFifo(100)
 
 {
 
@@ -94,7 +94,7 @@ bool LfpDisplayNode::resizeBuffer()
 
 	if (nSamples > 0 && nInputs > 0)
 	{
-		
+		abstractFifo.setTotalSize(nSamples);
 		displayBuffer->setSize(nInputs, nSamples);
 		return true;
 	} else {
@@ -133,8 +133,6 @@ void LfpDisplayNode::process(AudioSampleBuffer &buffer, MidiBuffer &midiMessages
 {
 	// 1. place any new samples into the displayBuffer
 	int samplesLeft = displayBuffer->getNumSamples() - displayBufferIndex;
-	
-	//lock->enterWrite();
 
 	if (nSamples < samplesLeft)
 	{
@@ -172,212 +170,52 @@ void LfpDisplayNode::process(AudioSampleBuffer &buffer, MidiBuffer &midiMessages
 								extraSamples);
 		}
 
-		displayBufferIndex = extraSamples+1;
+		displayBufferIndex = extraSamples;
 	}
 
-	//lock->exitWrite();
-
-	//std::cout << displayBufferIndex << std::endl;
-
-	// 2. update the screenBuffer
-	// if (true)
-	// {
-	// 	float nPixels = (float) getWidth();
-	// 	float ratio = sampleRate * timebase / 1000.0f / nPixels;
-	// 	float subSampleOffset = 0.0f;
 
-	// 	int valuesNeeded = nSamples / (int) ratio;
+	///// failed attempt to use abstractFifo:
 
-	//     for (int index = screenBufferIndex; index < valuesNeeded; index++)
-	//     {
+	// int start1, size1, start2, size2;
 
-	//         for (int channel = 0; channel < buffer.getNumChannels(); ++channel) {
+	// abstractFifo.prepareToWrite(nSamples, start1, size1, start2, size2);
 
-	//         	screenBuffer->copyFrom(channel, 		// destChannel
-	//         						index,  			// destSampleOffset
-	//         						buffer,				// source
-	//         						channel,			// sourceChannel
-	//         						(int) subSampleOffset,// sourceSampleOffset
-	//         						1);					// number of samples
-	        	
-	//         	subSampleOffset += ratio;
-	//         	subSampleOffset = jmin(subSampleOffset, (float) buffer.getNumSamples());
-	//        	}
+	// if (size1 > 0)
+	// {
+	// 	for (int chan = 0; chan < buffer.getNumChannels(); chan++)
+	// 	{	
+	// 		displayBuffer->copyFrom(chan,  			// destChannel
+	// 						    start1, 			// destStartSample
+	// 						    buffer, 			// source
+	// 						    chan, 				// source channel
+	// 						    0,					// source start sample
+	// 						    size1); 			// numSamples
 
 	// 	}
-	// }
-
-	// repaintCounter++;
-	// // 3. if it's time, repaint the display
-	// if (false && repaintCounter >= repaintInterval)
-	// {
-	// 	repaint();
 
+	// 	displayBufferIndex += size1;
 	// }
-}
-
-
-// void LfpDisplayNode::newOpenGLContextCreated()
-// {
-
-// 	setUp2DCanvas();
-// 	activateAntiAliasing();
-
-// 	glClearColor (0.8, 0.4, 0.9, 1.0);
-// 	resized();
-
-// }
-
-// void LfpDisplayNode::renderOpenGL()
-// {
-
-// 	repaintCounter = 0;
-	
-// 	glClear(GL_COLOR_BUFFER_BIT); // clear buffers to preset values
-
-// 	for (int i = 0; i < getNumInputs(); i++)
-// 	{
-// 		bool isSelected = false;
-
-// 		if (selectedChan == i)
-// 			isSelected = true;
-
-// 		if (checkBounds(i)) {
-// 			setViewport(i);
-// 			drawBorder(isSelected);
-// 			drawChannelInfo(i,isSelected);
-// 			//drawWaveform(i,isSelected);
-// 		}	
-// 	}
-// 	drawScrollBars();
-// }
-
-// void LfpDisplayNode::drawWaveform(int chan, bool isSelected)
-// {
-// 	// draw the screen buffer for a given channel
-
-// 	glBegin(GL_LINE_STRIP);
 
-// 	for (int i = 0; i < getWidth(); i++)
-// 	{
-// 		glVertex2f(i,0.5);//*screenBuffer->getSampleData(chan, i)+0.5);
-// 	}
-
-// 	glEnd();
-// }
-
-
-// void LfpDisplayNode::drawTicks()
-// {
-	
-// 	glViewport(0,0,getWidth(),getHeight());
-
-// 	glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
-
-// 	for (int i = 0; i < 10; i++)
-// 	{
-// 		if (i == 5)
-// 			glLineWidth(3.0);
-// 		else if (i == 1 || i == 3 || i == 7 || i == 9)
-// 			glLineWidth(2.0);
-// 		else
-// 			glLineWidth(1.0);
-
-// 		glBegin(GL_LINE_STRIP);
-// 		glVertex2f(0.1*i,0);
-// 		glVertex2f(0.1*i,1);
-// 		glEnd();
-// 	}
-// }
-
-
-// bool LfpDisplayNode::checkBounds(int chan)
-// {
-// 	bool isVisible;
-
-// 	int lowerBound = (chan+1)*(plotHeight+yBuffer);
-// 	int upperBound = chan*(plotHeight+yBuffer);
-
-// 	if (getScrollAmount() < lowerBound && getScrollAmount() + getHeight() > upperBound)
-// 		isVisible = true;
-// 	else
-// 		isVisible = false;
-	
-// 	return isVisible;
-
-// }
-
-// void LfpDisplayNode::setViewport(int chan)
-// {
-// 	glViewport(xBuffer,
-// 			   getHeight()-(chan+1)*(plotHeight+yBuffer)+getScrollAmount(),
-// 	           getWidth()-2*xBuffer,
-// 	           plotHeight);
-// }
-
-// void LfpDisplayNode::drawBorder(bool isSelected)
-// {
-// 	float alpha = 0.5f;
-
-// 	if (isSelected)
-// 		alpha = 1.0f;
-
-// 	glColor4f(0.0f, 0.0f, 0.0f, alpha);
-// 	glBegin(GL_LINE_STRIP);
-//  	glVertex2f(0.0f, 0.0f);
-//  	glVertex2f(1.0f, 0.0f);
-//  	glVertex2f(1.0f, 1.0f);
-//  	glVertex2f(0.0f, 1.0f);
-//  	glVertex2f(0.0f, 0.0f);
-//  	glEnd();
-
-// }
-
-// void LfpDisplayNode::drawChannelInfo(int chan, bool isSelected)
-// {
-// 	float alpha = 0.5f;
-
-// 	if (isSelected)
-// 		alpha = 1.0f;
-
-// 	glColor4f(0.0f,0.0f,0.0f,alpha);
-// 	glRasterPos2f(5.0f/getWidth(),0.3);
-// 	String s = String("Channel ");
-// 	s += (chan+1);
-
-// 	getFont(String("miso-regular"))->FaceSize(16);
-// 	getFont(String("miso-regular"))->Render(s);
-// }
-
-// int LfpDisplayNode::getTotalHeight() 
-// {
-// 	return (plotHeight+yBuffer)*getNumInputs() + yBuffer;
-// }
-
-
-// void LfpDisplayNode::resized()
+	// if (size2 > 0)
+	// {
+	// 	for (int chan = 0; chan < buffer.getNumChannels(); chan++)
+	// 	{	
+	// 		displayBuffer->copyFrom(chan,  			// destChannel
+	// 						    start2, 			// destStartSample
+	// 						    buffer, 			// source
+	// 						    chan, 				// source channel
+	// 						    size1,				// source start sample
+	// 						    size2); 			// numSamples
 
-// {
+	// 	}
 
-// 	canvasWasResized();
-// 	// glClear(GL_COLOR_BUFFER_BIT);
+	// 	displayBufferIndex = size2;
+	// }
 
-// 	// int h, w;
+	// std::cout << displayBufferIndex << std::endl;
 
-// 	// if (inWindow)
-// 	// {
-// 	// 	h = getParentComponent()->getHeight();
-// 	// 	w = getParentComponent()->getWidth();
-// 	// } else {
-// 	// 	h = getHeight();
-// 	// 	w = getWidth();
-// 	// }
+	// abstractFifo.finishedWrite(size1 + size2);
 
-// 	// if (getScrollAmount() + h > getTotalHeight() && getTotalHeight() > h)
-// 	// 	setScrollAmount(getTotalHeight() - h);
-// 	// else
-// 	// 	setScrollAmount(0);
 
-// 	// showScrollBars();
+}
 
-// }
diff --git a/Source/Processors/LfpDisplayNode.h b/Source/Processors/LfpDisplayNode.h
index f0e0fb5b7..a0555542c 100644
--- a/Source/Processors/LfpDisplayNode.h
+++ b/Source/Processors/LfpDisplayNode.h
@@ -77,6 +77,8 @@ private:
 	float displayGain; // 
 	float bufferLength; // s
 
+	AbstractFifo abstractFifo;
+
 	bool resizeBuffer();
 
 	JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LfpDisplayNode);
diff --git a/Source/Processors/Visualization/LfpDisplayCanvas.cpp b/Source/Processors/Visualization/LfpDisplayCanvas.cpp
index c12ef470a..7123ef5bb 100644
--- a/Source/Processors/Visualization/LfpDisplayCanvas.cpp
+++ b/Source/Processors/Visualization/LfpDisplayCanvas.cpp
@@ -137,8 +137,8 @@ void LfpDisplayCanvas::updateScreenBuffer()
 
 	float ratio = sampleRate * timebase / float(getWidth());
 
-	// this number is crucial: this method should be updated:
-	int valuesNeeded = nSamples / int(ratio);
+	// this number is crucial:
+	int valuesNeeded = (int) float(nSamples) / ratio;
 
 	//lock->enterRead();
 	float subSampleOffset = 0.0;
@@ -147,13 +147,6 @@ void LfpDisplayCanvas::updateScreenBuffer()
 	//int screenBufferPos; 
 
 	if (valuesNeeded > 0 && valuesNeeded < 1000) {
-//
-		//int sourceSampleNumber;
-		//int destSampleNumber;
-
-		//int valuesTaken = 0;
-
-		//float subSampleOffset = 0.0;
 
 		int maxVal = screenBufferIndex + valuesNeeded;
 		int overflow = maxVal - maxSamples;
@@ -183,7 +176,7 @@ void LfpDisplayCanvas::updateScreenBuffer()
 	        						  screenBufferIndex,
 	        						  *displayBuffer,
 	        						  channel,
-	        						  displayBufferIndex,
+	        						  nextPos,
 	        						  1,
 	        						  alpha*gain*displayGain);
 	       	}
@@ -203,6 +196,9 @@ void LfpDisplayCanvas::updateScreenBuffer()
 	       	screenBufferIndex %= maxSamples;
 
 	    }
+
+	} else {
+		//std::cout << "Skip." << std::endl;
 	}
 }
 
diff --git a/Source/Processors/Visualization/OpenGLCanvas.cpp b/Source/Processors/Visualization/OpenGLCanvas.cpp
index 246cf6ccb..4353220dc 100644
--- a/Source/Processors/Visualization/OpenGLCanvas.cpp
+++ b/Source/Processors/Visualization/OpenGLCanvas.cpp
@@ -28,7 +28,7 @@
 OpenGLCanvas::OpenGLCanvas() : //OpenGLComponent(OpenGLComponent::OpenGLType::openGLDefault, true),
 	scrollPix(0), scrollTime(0), scrollDiff(0), originalScrollPix(0), 
 	scrollBarWidth(15), PI(3.1415926), showScrollTrack(true),
-	animationIsActive(false), refreshMs(200)
+	animationIsActive(false), refreshMs(100)
 {
 
 	loadFonts();
-- 
GitLab