Open Ephys GUI
 All Classes Functions Variables Pages
SpikeDetector.h
1 /*
2  ------------------------------------------------------------------
3 
4  This file is part of the Open Ephys GUI
5  Copyright (C) 2012 Open Ephys
6 
7  ------------------------------------------------------------------
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifndef __SPIKEDETECTOR_H_3F920F95__
25 #define __SPIKEDETECTOR_H_3F920F95__
26 
27 #include "../../JuceLibraryCode/JuceHeader.h"
28 
29 #include "GenericProcessor.h"
30 #include "Editors/SpikeDetectorEditor.h"
31 
32 #include "Visualization/SpikeObject.h"
33 
34 /**
35 
36  == UNDER CONSTRUCTION ==
37 
38  Detects spikes in a continuous signal and outputs events containing the spike data.
39 
40  @see GenericProcessor, SpikeDetectorEditor
41 
42 */
43 
45 
47 
48 {
49 public:
50 
51  // CONSTRUCTOR AND DESTRUCTOR //
52 
53  /** constructor */
54  SpikeDetector();
55 
56  /** destructor */
58 
59 
60  // PROCESSOR METHODS //
61 
62  /** Processes an incoming continuous buffer and places new
63  spikes into the event buffer. */
64  void process(AudioSampleBuffer &buffer, MidiBuffer &events, int& nSamples);
65 
66  /** Used to alter parameters of data acquisition. */
67  void setParameter (int parameterIndex, float newValue);
68 
69  /** Called whenever the signal chain is altered. */
70  void updateSettings();
71 
72  /** Called prior to start of acquisition. */
73  bool enable();
74 
75  /** Called after acquisition is finished. */
76  bool disable();
77 
78  /** Creates the SpikeDetectorEditor. */
79  AudioProcessorEditor* createEditor();
80 
81 
82  // INTERNAL BUFFERS //
83 
84  /** Extra samples are placed in this buffer to allow seamless
85  transitions between callbacks. */
86  AudioSampleBuffer overflowBuffer;
87 
88  /** Reference to a continuous buffer (for internal use only). */
89  AudioSampleBuffer& dataBuffer;
90 
91 
92  // CREATE AND DELETE ELECTRODES //
93 
94  /** Adds an electrode with n channels to be processed. */
95  bool addElectrode(int nChans);
96 
97  /** Removes an electrode with a given index. */
98  bool removeElectrode(int index);
99 
100 
101  // EDIT AND QUERY ELECTRODE SETTINGS //
102 
103  /** Returns the number of channels for a given electrode. */
104  int getNumChannels(int index);
105 
106  /** Edits the mapping between input channels and electrode channels. */
107  bool setChannel(int electrodeIndex, int channelNum, int newChannel);
108 
109  /** Returns the continuous channel that maps to a given
110  electrode channel. */
111  int getChannel(int index, int chan);
112 
113  /** Sets the name of a given electrode. */
114  bool setElectrodeName(int index, String newName);
115 
116 
117  // RETURN STRING ARRAYS //
118 
119  /** Returns a StringArray containing the names of all electrodes */
120  StringArray getElectrodeNames();
121 
122  /** Returns a list of possible electrode types (e.g., stereotrode, tetrode). */
123  StringArray electrodeTypes;
124 
125  void setChannelThreshold(int electrodeNum, int channelNum, float threshold);
126 
127  double getChannelThreshold(int electrodeNum, int channelNum);
128 
129 private:
130 
131  float getDefaultThreshold();
132 
133  int overflowBufferSize;
134 
135  int sampleIndex;
136 
137  Array<int> electrodeCounter;
138 
139  float getNextSample(int& chan);
140  float getCurrentSample(int& chan);
141  bool samplesAvailable(int& nSamples);
142 
143  bool useOverflowBuffer;
144 
145  int currentElectrode;
146  int currentChannelIndex;
147  int currentIndex;
148 
149  struct Electrode {
150 
151  String name;
152 
153  int numChannels;
154  int prePeakSamples, postPeakSamples;
155  int lastBufferIndex;
156 
157  int* channels;
158  double* thresholds;
159  bool* isActive;
160 
161  };
162 
163  uint8_t* spikeBuffer;///[256];
164 
165  Array<Electrode*> electrodes;
166 
167  // void createSpikeEvent(int& peakIndex,
168  // int& electrodeNumber,
169  // int& currentChannel,
170  // MidiBuffer& eventBuffer);
171 
172  void addSpikeEvent(SpikeObject* s, MidiBuffer& eventBuffer, int peakIndex);
173  void addWaveformToSpikeObject(SpikeObject* s,
174  int& peakIndex,
175  int& electrodeNumber,
176  int& currentChannel);
177 
178  void resetElectrode(Electrode*);
179 
180  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpikeDetector);
181 
182 };
183 
184 
185 
186 #endif // __SPIKEDETECTOR_H_3F920F95__