diff --git a/Source/Plugins/SpikeRaster/SpikeRaster.cpp b/Source/Plugins/SpikeRaster/SpikeRaster.cpp index b3ef325c222313293355aa4eb3889b277e9f1d5d..d9e6020df302cd8ffd7e1f93b667833c30b7c7be 100644 --- a/Source/Plugins/SpikeRaster/SpikeRaster.cpp +++ b/Source/Plugins/SpikeRaster/SpikeRaster.cpp @@ -63,30 +63,25 @@ void SpikeRaster::updateSettings() electrodes.clear(); - for (int i = 0; i < eventChannels.size(); i++) - { - ChannelType type = eventChannels[i]->getType(); - - if (type == ELECTRODE_CHANNEL) - { + for (int i = 0; i < spikeChannelArray.size(); i++) + { - Electrode elec; - elec.numChannels = static_cast<SpikeChannel*>(eventChannels[i]->extraData.get())->numChannels; + Electrode* elec = new Electrode(); + elec->numChannels = spikeChannelArray[i]->getNumChannels(); - elec.name = eventChannels[i]->getName(); - elec.currentSpikeIndex = 0; - elec.mostRecentSpikes.ensureStorageAllocated(displayBufferSize); + elec->name = spikeChannelArray[i]->getName(); + elec->currentSpikeIndex = 0; + elec->mostRecentSpikes.ensureStorageAllocated(displayBufferSize); - for (int j = 0; j < elec.numChannels; j++) - { - elec.displayThresholds.add(0); - elec.detectorThresholds.add(0); - } + for (int j = 0; j < elec->numChannels; j++) + { + elec->displayThresholds.add(0); + elec->detectorThresholds.add(0); + } - electrodes.add(elec); + electrodes.add(elec); - } - } + } } @@ -128,13 +123,14 @@ int SpikeRaster::getNumElectrodes() void SpikeRaster::setRasterPlot(RasterPlot* r) { canvas = r; - r->setSampleRate(settings.sampleRate); + //A bit of a hack since a processor doesn't actually have a sample rate unless it is a source one + r->setSampleRate(dataChannelArray[0]->getSampleRate()); } -void SpikeRaster::process(AudioSampleBuffer& buffer, MidiBuffer& events) +void SpikeRaster::process(AudioSampleBuffer& buffer) { - checkForEvents(events); // automatically calls 'handleEvent + checkForEvents(true); // automatically calls 'handleEvent if (redrawRequested) { @@ -144,14 +140,14 @@ void SpikeRaster::process(AudioSampleBuffer& buffer, MidiBuffer& events) for (int i = 0; i < getNumElectrodes(); i++) { - Electrode& e = electrodes.getReference(i); + Electrode* e = electrodes[i]; // transfer buffered spikes to spike plot - for (int j = 0; j < e.currentSpikeIndex; j++) + for (int j = 0; j < e->currentSpikeIndex; j++) { //std::cout << "Transferring spikes." << std::endl; - canvas->processSpikeObject(e.mostRecentSpikes[j]); - e.currentSpikeIndex = 0; + canvas->processSpikeObject(e->mostRecentSpikes[j]); + e->currentSpikeIndex = 0; } } @@ -163,58 +159,47 @@ void SpikeRaster::process(AudioSampleBuffer& buffer, MidiBuffer& events) } -void SpikeRaster::handleEvent(int eventType, MidiMessage& event, int samplePosition) +void SpikeRaster::handleSpike(const SpikeChannel* channelInfo, const MidiMessage& event, int samplePosition) { - //std::cout << "Received event of type " << eventType << std::endl; - - if (eventType == SPIKE) - { + //std::cout << "Received event of type " << eventType << std::endl; - const uint8_t* dataptr = event.getRawData(); - int bufferSize = event.getRawDataSize(); - if (bufferSize > 0) - { + SpikeEventPtr newSpike = SpikeEvent::deserializeFromMessage(event, channelInfo); - SpikeObject newSpike; + if (newSpike > 0) + { - bool isValid = unpackSpike(&newSpike, dataptr, bufferSize); - if (isValid) - { - int electrodeNum = newSpike.source; + int electrodeNum = spikeChannelArray.indexOf(channelInfo); + if (electrodeNum < 0) return; - Electrode& e = electrodes.getReference(electrodeNum); - // std::cout << electrodeNum << std::endl; + Electrode* e = electrodes[electrodeNum]; + // std::cout << electrodeNum << std::endl; - // add to buffer - if (e.currentSpikeIndex < displayBufferSize) - { - // std::cout << "Adding spike " << e.currentSpikeIndex + 1 << std::endl; - e.mostRecentSpikes.set(e.currentSpikeIndex, newSpike); - e.currentSpikeIndex++; - } + // add to buffer + if (e->currentSpikeIndex < displayBufferSize) + { + // std::cout << "Adding spike " << e.currentSpikeIndex + 1 << std::endl; + e->mostRecentSpikes.set(e->currentSpikeIndex, newSpike.release()); + e->currentSpikeIndex++; + } - } + } - } - - } else if (eventType == TTL) - { - const uint8* dataptr = event.getRawData(); - - //int eventNodeId = *(dataptr+1); - int eventId = *(dataptr+2); - int eventChannel = *(dataptr+3); - uint8 sourceNodeId = event.getNoteNumber(); +} - int64 timestamp = timestamps[sourceNodeId] + samplePosition; - if (eventId == 1) - { - canvas->processEvent(eventChannel, timestamp); - } - } - -} +void SpikeRaster::handleEvent(const EventChannel* channelInfo, const MidiMessage& event, int samplePosition) +{ + if (Event::getEventType(event) == EventChannel::TTL) + { + TTLEventPtr ttl = TTLEvent::deserializeFromMessage(event, channelInfo); + int64 timestamp = getSourceTimestamp(ttl->getSourceID(), ttl->getSubProcessorIdx()) + samplePosition; + + if (ttl->getState()) + { + canvas->processEvent(ttl->getChannel(), timestamp); + } + } +} \ No newline at end of file diff --git a/Source/Plugins/SpikeRaster/SpikeRaster.h b/Source/Plugins/SpikeRaster/SpikeRaster.h index 41758a92cd549ed4ae7033c838f300ae5dea1bb6..e3fe6976c2e3f4a5d953a098829709b5b161774f 100644 --- a/Source/Plugins/SpikeRaster/SpikeRaster.h +++ b/Source/Plugins/SpikeRaster/SpikeRaster.h @@ -29,7 +29,6 @@ #endif #include <ProcessorHeaders.h> -#include <SpikeLib.h> class RasterPlot; @@ -53,37 +52,38 @@ public: ~SpikeRaster(); /** Determines whether the processor is treated as a source. */ - bool isSource() + bool isSource() const override { return false; } /** Determines whether the processor is treated as a sink. */ - bool isSink() + bool isSink() const override { return false; } /** Indicates if the processor has a custom editor. Defaults to false */ - bool hasEditor() const + bool hasEditor() const override { return true; } - void updateSettings(); + void updateSettings() override; int getNumElectrodes(); - AudioProcessorEditor* createEditor(); + AudioProcessorEditor* createEditor() override; - void process(AudioSampleBuffer& buffer, MidiBuffer& events); + void process(AudioSampleBuffer& buffer) override; - void handleEvent(int, MidiMessage&, int); + void handleSpike(const SpikeChannel*, const MidiMessage&, int) override; + void handleEvent(const EventChannel*, const MidiMessage&, int) override; - void setParameter(int parameterIndex, float newValue); + void setParameter(int parameterIndex, float newValue) override; - bool enable(); - bool disable(); + bool enable() override; + bool disable() override; void setRasterPlot(RasterPlot*); @@ -98,7 +98,7 @@ private: Array<float> displayThresholds; Array<float> detectorThresholds; - Array<SpikeObject> mostRecentSpikes; + OwnedArray<SpikeEvent> mostRecentSpikes; int currentSpikeIndex; int recordIndex; @@ -108,7 +108,7 @@ private: RasterPlot* canvas; - Array<Electrode> electrodes; + OwnedArray<Electrode> electrodes; int displayBufferSize; bool redrawRequested; diff --git a/Source/Plugins/SpikeRaster/SpikeRasterEditor.cpp b/Source/Plugins/SpikeRaster/SpikeRasterEditor.cpp index 7180e5357336109160fc64e6f38c244a1709dc81..a436b6453d9b0481cbbcc7f1fb501124559f9d13 100644 --- a/Source/Plugins/SpikeRaster/SpikeRasterEditor.cpp +++ b/Source/Plugins/SpikeRaster/SpikeRasterEditor.cpp @@ -293,7 +293,8 @@ void SpikeRasterCanvas::buttonClicked(Button* b) -RasterPlot::RasterPlot(SpikeRasterCanvas*) +RasterPlot::RasterPlot(SpikeRasterCanvas* c) + :processor(c->getProcessor()) { rasterWidth = 500; @@ -558,11 +559,12 @@ void RasterPlot::setSampleRate(float sr) sampleRate = sr; } -void RasterPlot::processSpikeObject(const SpikeObject& s) +void RasterPlot::processSpikeObject(const SpikeEvent* s) { - int electrode = s.source; + + int electrode = processor->getSpikeChannelIndex(s); //int unit = s.sortedId; - int timestamp = s.timestamp; // absolute time + int timestamp = s->getTimestamp(); // absolute time float bufferPos = float(timestamp - rasterStartTimestamp) / (sampleRate * rasterTimebase); diff --git a/Source/Plugins/SpikeRaster/SpikeRasterEditor.h b/Source/Plugins/SpikeRaster/SpikeRasterEditor.h index 050f6672b3f7d492af5c83afbe854433eeb89b1c..7e85e94454be146ee3f49707ef467f90b4587569 100644 --- a/Source/Plugins/SpikeRaster/SpikeRasterEditor.h +++ b/Source/Plugins/SpikeRaster/SpikeRasterEditor.h @@ -26,7 +26,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <VisualizerEditorHeaders.h> #include <VisualizerWindowHeaders.h> -#include <SpikeLib.h> #include "SpikeRaster.h" @@ -181,7 +180,7 @@ public: void resized(); RasterPlot* getRasterPlot() {return rasterPlot.get();} - + SpikeRaster* getProcessor() { return processor; } private: SpikeRaster* processor; ScopedPointer<RasterPlot> rasterPlot; @@ -227,7 +226,7 @@ public: void resized(); void reset(); - void processSpikeObject(const SpikeObject& s); + void processSpikeObject(const SpikeEvent* s); void processEvent(int eventChan, int64 ts); Random random; @@ -272,6 +271,8 @@ public: float sampleRate; Colour getColourForChannel(int ch); +private: + const SpikeRaster* const processor; };