Skip to content

Commit ccd340e

Browse files
committed
Fix event overlay alignment issues
1 parent 0877866 commit ccd340e

6 files changed

Lines changed: 138 additions & 143 deletions

File tree

Source/Plugins/LfpDisplayNode/LfpDisplayNode.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ void LfpDisplayNode::handleEvent(const EventChannel* eventInfo, const MidiMessag
225225
block2Size, // numSamples
226226
float (ttlState[eventSourceNodeId])); // gain
227227
}
228-
// std::cout << "ttlState: " << ttlState << std::endl;
229-
230-
// std::cout << "Received event from " << eventNodeId <<
231-
// " on channel " << eventChannel <<
232-
// " with value " << eventId <<
233-
// " at timestamp " << event.getTimeStamp() << std::endl;
228+
// std::cout << "ttlState: " << ttlState[eventSourceNodeId] << std::endl;
229+
//
230+
// std::cout << "Received event from " << eventSourceNodeId <<
231+
// " on channel " << eventChannel <<
232+
// " with value " << eventId <<
233+
// " at timestamp " << event.getTimeStamp() << std::endl;
234234
}
235235
}
236236

Source/Plugins/LfpDisplayNodeAlpha/LfpDisplayCanvas.cpp

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,15 @@ void LfpDisplayCanvas::updateScreenBuffer()
362362
dbi %= displayBufferSize; // make sure we're not overshooting
363363
int nextPos = (dbi + 1) % displayBufferSize; // position next to displayBufferIndex in display buffer to copy from
364364

365-
// if (channel == 0)
366-
// std::cout << "Channel "
367-
// << channel << " : "
368-
// << sbi << " : "
369-
// << index << " : "
370-
// << dbi << " : "
371-
// << valuesNeeded << " : "
372-
// << ratio
373-
// << std::endl;
365+
// if (channel == 0)
366+
// std::cout << "Channel "
367+
// << channel << " : "
368+
// << sbi << " : "
369+
// << index << " : "
370+
// << dbi << " : "
371+
// << valuesNeeded << " : "
372+
// << ratio
373+
// << std::endl;
374374

375375
if (valuesNeeded > 0 && valuesNeeded < 1000000)
376376
{
@@ -387,21 +387,25 @@ void LfpDisplayCanvas::updateScreenBuffer()
387387
screenBufferMin->clear(channel, sbi, 1);
388388
screenBufferMax->clear(channel, sbi, 1);
389389

390-
dbi %= displayBufferSize; // just to be sure
391-
392-
// interpolate between two samples with invAlpha and alpha
393-
screenBuffer->addFrom(channel, // destChannel
394-
sbi, // destStartSample
395-
displayBuffer->getReadPointer(channel, dbi), // source
396-
1, // numSamples
397-
invAlpha*gain); // gain
398-
399-
400-
screenBuffer->addFrom(channel, // destChannel
401-
sbi, // destStartSample
402-
displayBuffer->getReadPointer(channel, nextPos), // source
403-
1, // numSamples
404-
alpha*gain); // gain
390+
dbi %= displayBufferSize; // just to be sure
391+
392+
// update continuous data channels
393+
if (channel != nChans)
394+
{
395+
// interpolate between two samples with invAlpha and alpha
396+
screenBuffer->addFrom(channel, // destChannel
397+
sbi, // destStartSample
398+
displayBuffer->getReadPointer(channel, dbi), // source
399+
1, // numSamples
400+
invAlpha*gain); // gain
401+
402+
403+
screenBuffer->addFrom(channel, // destChannel
404+
sbi, // destStartSample
405+
displayBuffer->getReadPointer(channel, nextPos), // source
406+
1, // numSamples
407+
alpha*gain); // gain
408+
}
405409

406410
// same thing again, but this time add the min,mean, and max of all samples in current pixel
407411
float sample_min = 10000000;
@@ -432,13 +436,19 @@ void LfpDisplayCanvas::updateScreenBuffer()
432436

433437
}
434438

439+
// update event channel
440+
if (channel == nChans)
441+
{
442+
screenBuffer->setSample(channel, sbi, sample_max);
443+
}
444+
435445
// similarly, for each pixel on the screen, we want a list of all values so we can draw a histogram later
436446
// for simplicity, we'll just do this as 2d array, samplesPerPixel[px][samples]
437447
// with an additional array sampleCountPerPixel[px] that holds the N samples per pixel
438448
if (channel < nChans) // we're looping over one 'extra' channel for events above, so make sure not to loop over that one here
439449
{
440450
int c = 0;
441-
for (int j = dbi; j < nextpix & c < MAX_N_SAMP_PER_PIXEL; j++)
451+
for (int j = dbi; j < nextpix && c < MAX_N_SAMP_PER_PIXEL; j++)
442452
{
443453
float sample_current = displayBuffer->getSample(channel, j);
444454
samplesPerPixel[channel][sbi][c]=sample_current;
@@ -454,26 +464,26 @@ void LfpDisplayCanvas::updateScreenBuffer()
454464

455465
screenBufferMin->addSample(channel, sbi, sample_min*gain);
456466
screenBufferMax->addSample(channel, sbi, sample_max*gain);
457-
}
458-
sbi++;
467+
}
468+
sbi++;
459469
}
460470

461-
subSampleOffset += ratio;
462-
463-
while (subSampleOffset >= 1.0)
464-
{
465-
if (++dbi > displayBufferSize)
466-
dbi = 0;
467-
468-
nextPos = (dbi + 1) % displayBufferSize;
469-
subSampleOffset -= 1.0;
471+
subSampleOffset += ratio;
472+
473+
while (subSampleOffset >= 1.0)
474+
{
475+
if (++dbi > displayBufferSize)
476+
dbi = 0;
477+
478+
nextPos = (dbi + 1) % displayBufferSize;
479+
subSampleOffset -= 1.0;
480+
}
481+
470482
}
471-
472-
}
473-
474-
// update values after we're done
475-
screenBufferIndex.set(channel, sbi);
476-
displayBufferIndex.set(channel, dbi);
483+
484+
// update values after we're done
485+
screenBufferIndex.set(channel, sbi);
486+
displayBufferIndex.set(channel, dbi);
477487
}
478488

479489
}
@@ -1931,11 +1941,20 @@ void LfpTimescale::paint(Graphics& g)
19311941
3 * getHeight()/4,
19321942
2.0f);
19331943
}
1944+
19341945

19351946
if (i != 0 && i % 2 == 0)
19361947
g.drawText(labels[i-1],getWidth()/steps*i+3,0,100,getHeight(),Justification::left, false);
1948+
19371949
}
1950+
}
19381951

1952+
void LfpTimescale::mouseUp(const MouseEvent &e)
1953+
{
1954+
if (e.mods.isLeftButtonDown())
1955+
{
1956+
lfpDisplay->trackZoomInfo.isScrollingX = false;
1957+
}
19391958
}
19401959

19411960
void LfpTimescale::resized()
@@ -1999,14 +2018,6 @@ void LfpTimescale::mouseDrag(const juce::MouseEvent &e)
19992018
}
20002019
}
20012020

2002-
void LfpTimescale::mouseUp(const MouseEvent &e)
2003-
{
2004-
if (e.mods.isLeftButtonDown())
2005-
{
2006-
lfpDisplay->trackZoomInfo.isScrollingX = false;
2007-
}
2008-
}
2009-
20102021
void LfpTimescale::setTimebase(float t)
20112022
{
20122023
timebase = t;
@@ -3121,9 +3132,6 @@ void LfpChannelDisplay::pxPaint()
31213132
// draw event markers
31223133
int rawEventState = canvas->getYCoord(canvas->getNumChannels(), i);// get last channel+1 in buffer (represents events)
31233134

3124-
//if (i == ifrom)
3125-
// std::cout << rawEventState << std::endl;
3126-
31273135
for (int ev_ch = 0; ev_ch < 8 ; ev_ch++) // for all event channels
31283136
{
31293137
if (display->getEventDisplayState(ev_ch)) // check if plotting for this channel is enabled
@@ -3281,8 +3289,19 @@ void LfpChannelDisplay::pxPaint()
32813289
plotterInfo.samp = i;
32823290
plotterInfo.lineColour = lineColour;
32833291

3292+
// if (plotterInfo.samp == canvas->lastScreenBufferIndex[chan])
3293+
// {
3294+
// for (int j = jfrom_wholechannel; j <= jto_wholechannel; j++)
3295+
// {
3296+
// bdLfpChannelBitmap.setPixelColour(i,j,lineColour);
3297+
// }
3298+
// }
3299+
3300+
// if (getChannelNumber() == 0)
3301+
// std::cout << "plotting " << i << std::endl;
3302+
32843303
// TODO: (kelly) complete transition toward plotter class encapsulation
3285-
display->getPlotterPtr()->plot(bdLfpChannelBitmap, plotterInfo); // plotterInfo is prepared above
3304+
display->getPlotterPtr()->plot(bdLfpChannelBitmap, plotterInfo); // plotterInfo is prepared above
32863305

32873306
// int jfrom=from+getY();
32883307
// int jto=to+getY();
@@ -3772,6 +3791,7 @@ bool LfpChannelDisplayInfo::isChannelNumberHidden()
37723791

37733792

37743793

3794+
37753795
#pragma mark - EventDisplayInterface -
37763796
// Event display Options --------------------------------------------------------------------
37773797

Source/Plugins/LfpDisplayNodeAlpha/LfpDisplayNode.cpp

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,22 @@ void LfpDisplayNode::setParameter (int parameterIndex, float newValue)
171171

172172
void LfpDisplayNode::handleEvent(const EventChannel* eventInfo, const MidiMessage& event, int samplePosition)
173173
{
174-
if (Event::getEventType(event) == EventChannel::TTL)
174+
if (Event::getEventType(event) == EventChannel::TTL)
175175
{
176-
TTLEventPtr ttl = TTLEvent::deserializeFromMessage(event, eventInfo);
177-
178-
//int eventNodeId = *(dataptr+1);
179-
const int eventId = ttl->getState() ? 1 : 0;
180-
const int eventChannel = ttl->getChannel();
181-
const int eventTime = samplePosition;
182-
const uint32 eventSourceNodeId = getChannelSourceID(eventInfo);
183-
const int nSamples = getNumSourceSamples(eventSourceNodeId);
184-
int samplesToFill = nSamples - eventTime;
185-
if (samplesToFill < 0) samplesToFill = 0;
186-
187-
// std::cout << "Received event from " << eventSourceNode << ", channel "
188-
// << eventChannel << ", with ID " << eventId << ", copying to "
189-
// << channelForEventSource[eventSourceNode] << std::endl;
190-
////
191-
int bufferIndex = (displayBufferIndex[channelForEventSource[eventSourceNodeId]] + eventTime - nSamples)
192-
% displayBuffer->getNumSamples();
193-
194-
bufferIndex = bufferIndex >= 0
195-
? bufferIndex
196-
: displayBuffer->getNumSamples() + bufferIndex;
197-
176+
TTLEventPtr ttl = TTLEvent::deserializeFromMessage(event, eventInfo);
177+
178+
//int eventNodeId = *(dataptr+1);
179+
const int eventId = ttl->getState() ? 1 : 0;
180+
const int eventChannel = ttl->getChannel();
181+
const int eventTime = samplePosition;
182+
const uint32 eventSourceNodeId = getChannelSourceID(eventInfo);
183+
184+
185+
const int chan = channelForEventSource[eventSourceNodeId];
186+
const int index = (displayBufferIndex[chan] + eventTime) % displayBuffer->getNumSamples();
187+
const int samplesLeft = displayBuffer->getNumSamples() - index;
188+
const int nSamples = getNumSourceSamples(eventSourceNodeId) - eventTime;
189+
198190
if (eventId == 1)
199191
{
200192
ttlState[eventSourceNodeId] |= (1L << eventChannel);
@@ -203,39 +195,36 @@ void LfpDisplayNode::handleEvent(const EventChannel* eventInfo, const MidiMessag
203195
{
204196
ttlState[eventSourceNodeId] &= ~(1L << eventChannel);
205197
}
206-
207-
if (samplesToFill + bufferIndex < displayBuffer->getNumSamples())
198+
199+
if (nSamples < samplesLeft)
208200
{
209-
//std::cout << bufferIndex << " " << samplesToFill << " " << ttlState[eventSourceNode] << std::endl;
210-
211201
displayBuffer->copyFrom (channelForEventSource[eventSourceNodeId], // destChannel
212-
bufferIndex, // destStartSample
202+
index, // destStartSample
213203
arrayOfOnes, // source
214-
samplesToFill, // numSamples
204+
nSamples, // numSamples
215205
float (ttlState[eventSourceNodeId])); // gain
216206
}
217207
else
218208
{
219-
const int block2Size = (samplesToFill + bufferIndex) % displayBuffer->getNumSamples();
220-
const int block1Size = samplesToFill - block2Size;
221-
209+
int extraSamples = nSamples - samplesLeft;
210+
222211
displayBuffer->copyFrom (channelForEventSource[eventSourceNodeId], // destChannel
223-
bufferIndex, // destStartSample
212+
index, // destStartSample
224213
arrayOfOnes, // source
225-
block1Size, // numSamples
214+
samplesLeft, // numSamples
226215
float (ttlState[eventSourceNodeId])); // gain
227-
216+
228217
displayBuffer->copyFrom (channelForEventSource[eventSourceNodeId], // destChannel
229218
0, // destStartSample
230219
arrayOfOnes, // source
231-
block2Size, // numSamples
220+
extraSamples, // numSamples
232221
float (ttlState[eventSourceNodeId])); // gain
233222
}
234-
235-
// std::cout << "Received event from " << eventNodeId
236-
// << " on channel " << eventChannel
237-
// << " with value " << eventId
238-
// << " at timestamp " << event.getTimeStamp() << std::endl;
223+
224+
// std::cout << "Received event from " << eventSourceNodeId
225+
// << " on channel " << eventChannel
226+
// << " with value " << eventId
227+
// << " at timestamp " << event.getTimeStamp() << std::endl;
239228
}
240229
}
241230

@@ -248,20 +237,15 @@ void LfpDisplayNode::initializeEventChannels()
248237
const int index = displayBufferIndex[chan];
249238
const int samplesLeft = displayBuffer->getNumSamples() - index;
250239
const int nSamples = getNumSourceSamples(eventSourceNodes[i]);
251-
252-
//std::cout << "Event source node " << i << ", channel " << chan << std::endl;
253-
240+
254241
if (nSamples < samplesLeft)
255242
{
256-
// std::cout << getNumInputs()+1 << " " << displayBufferIndex << " " << totalSamples << " " << ttlState << std::endl;
257243

258244
displayBuffer->copyFrom (chan, // destChannel
259245
index, // destStartSample
260246
arrayOfOnes, // source
261247
nSamples, // numSamples
262248
float (ttlState[eventSourceNodes[i]])); // gain
263-
264-
displayBufferIndex.set (chan, index + nSamples);
265249
}
266250
else
267251
{
@@ -278,9 +262,31 @@ void LfpDisplayNode::initializeEventChannels()
278262
arrayOfOnes, // source
279263
extraSamples, // numSamples
280264
float (ttlState[eventSourceNodes[i]])); // gain
265+
}
266+
}
267+
}
281268

282-
displayBufferIndex.set (chan, extraSamples);
269+
void LfpDisplayNode::finalizeEventChannels()
270+
{
271+
for (int i = 0; i < eventSourceNodes.size(); ++i)
272+
{
273+
const int chan = channelForEventSource[eventSourceNodes[i]];
274+
const int index = displayBufferIndex[chan];
275+
const int samplesLeft = displayBuffer->getNumSamples() - index;
276+
const int nSamples = getNumSourceSamples(eventSourceNodes[i]);
277+
278+
int newIdx = 0;
279+
280+
if (nSamples < samplesLeft)
281+
{
282+
newIdx = index + nSamples;
283283
}
284+
else
285+
{
286+
newIdx = nSamples - samplesLeft;
287+
}
288+
289+
displayBufferIndex.set(chan, newIdx);
284290
}
285291
}
286292

@@ -290,11 +296,12 @@ void LfpDisplayNode::process (AudioSampleBuffer& buffer)
290296
// 1. place any new samples into the displayBuffer
291297
//std::cout << "Display node sample count: " << nSamples << std::endl; ///buffer.getNumSamples() << std::endl;
292298

299+
ScopedLock displayLock (displayMutex);
300+
293301
initializeEventChannels();
294-
295302
checkForEvents (); // see if we got any TTL events
303+
finalizeEventChannels();
296304

297-
ScopedLock displayLock (displayMutex);
298305

299306
for (int chan = 0; chan < buffer.getNumChannels(); ++chan)
300307
{

0 commit comments

Comments
 (0)