-
Notifications
You must be signed in to change notification settings - Fork 686
/
Copy pathFilterNode.cpp
221 lines (172 loc) · 6.64 KB
/
FilterNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2016 Open Ephys
------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "FilterNode.h"
#include "FilterEditor.h"
void BandpassFilterSettings::createFilters(int numChannels, float sampleRate_, double lowCut, double highCut)
{
sampleRate = sampleRate_;
filters.clear();
for (int n = 0; n < numChannels; ++n)
{
filters.add(new Dsp::SmoothedFilterDesign
<Dsp::Butterworth::Design::BandPass // design type
<2>, // order
1, // number of channels (must be const)
Dsp::DirectFormII>(1)); // realization
}
updateFilters(lowCut, highCut);
}
void BandpassFilterSettings::updateFilters(double lowCut, double highCut)
{
for (int n = 0; n < filters.size(); n++)
{
setFilterParameters(lowCut, highCut, n);
}
}
void BandpassFilterSettings::setFilterParameters(double lowCut, double highCut, int channel)
{
Dsp::Params params;
params[0] = sampleRate; // sample rate
params[1] = 2; // order
params[2] = (highCut + lowCut) / 2; // center frequency
params[3] = highCut - lowCut; // bandwidth
filters[channel]->setParams(params);
}
FilterNode::FilterNode()
: GenericProcessor ("Bandpass Filter")
{
addFloatParameter(Parameter::STREAM_SCOPE, "high_cut", "Filter high cut", 6000, 0.1, 15000, false);
addFloatParameter(Parameter::STREAM_SCOPE, "low_cut", "Filter low cut", 300, 0.1, 15000, false);
addMaskChannelsParameter(Parameter::STREAM_SCOPE, "Channels", "Channels to filter for this stream");
}
AudioProcessorEditor* FilterNode::createEditor()
{
editor = std::make_unique<FilterEditor> (this);
return editor.get();
}
// ----------------------------------------------------
// From the filter library documentation:
// ----------------------------------------------------
//
// each family of filters is given its own namespace
// RBJ: filters from the RBJ cookbook
// Butterworth
// ChebyshevI: ripple in the passband
// ChebyshevII: ripple in the stop band
// Elliptic: ripple in both the passband and stopband
// Bessel: theoretically with linear phase
// Legendre: "Optimum-L" filters with steepest transition and monotonic passband
// Custom: Simple filters that allow poles and zeros to be specified directly
// within each namespace exists a set of "raw filters"
// Butterworth::LowPass
// HighPass
// BandPass
// BandStop
// LowShelf
// HighShelf
// BandShelf
//
// class templates (such as SimpleFilter) which require FilterClasses
// expect an identifier of a raw filter
// raw filters do not support introspection, or the Params style of changing
// filter settings; they only offer a setup() function for updating the IIR
// coefficients to a given set of parameters
//
// each filter family namespace also has the nested namespace "Design"
// here we have all of the raw filter names repeated, except these classes
// also provide the Design interface, which adds introspection, polymorphism,
// the Params style of changing filter settings, and in general all fo the features
// necessary to interoperate with the Filter virtual base class and its derived classes
// available methods:
//
// filter->getKind()
// filter->getName()
// filter->getNumParams()
// filter->getParamInfo()
// filter->getDefaultParams()
// filter->getParams()
// filter->getParam()
// filter->setParam()
// filter->findParamId()
// filter->setParamById()
// filter->setParams()
// filter->copyParamsFrom()
// filter->getPoleZeros()
// filter->response()
// filter->getNumChannels()
// filter->reset()
// filter->process()
void FilterNode::updateSettings()
{
settings.update(getDataStreams());
for (auto stream : getDataStreams())
{
settings[stream->getStreamId()]->createFilters(
stream->getChannelCount(),
stream->getSampleRate(),
(*stream)["low_cut"],
(*stream)["high_cut"]
);
}
}
void FilterNode::parameterValueChanged(Parameter* param)
{
uint16 currentStream = param->getStreamId();
if (param->getName().equalsIgnoreCase("low_cut"))
{
if ((*getDataStream(currentStream))["low_cut"] >= (*getDataStream(currentStream))["high_cut"])
{
getDataStream(currentStream)->getParameter("low_cut")->restorePreviousValue();
return;
}
settings[currentStream]->updateFilters(
(*getDataStream(currentStream))["low_cut"],
(*getDataStream(currentStream))["high_cut"]
);
}
else if (param->getName().equalsIgnoreCase("high_cut"))
{
if ((*getDataStream(currentStream))["high_cut"] <= (*getDataStream(currentStream))["low_cut"])
{
getDataStream(currentStream)->getParameter("high_cut")->restorePreviousValue();
return;
}
settings[currentStream]->updateFilters(
(*getDataStream(currentStream))["low_cut"],
(*getDataStream(currentStream))["high_cut"]
);
}
}
void FilterNode::process (AudioBuffer<float>& buffer)
{
for (auto stream : getDataStreams())
{
if ((*stream)["enable_stream"])
{
BandpassFilterSettings* streamSettings = settings[stream->getStreamId()];
const uint16 streamId = stream->getStreamId();
const uint32 numSamples = getNumSamplesInBlock(streamId);
for (auto localChannelIndex : *((*stream)["Channels"].getArray()))
{
int globalChannelIndex = getGlobalChannelIndex(stream->getStreamId(), (int) localChannelIndex);
float* ptr = buffer.getWritePointer(globalChannelIndex);
streamSettings->filters[localChannelIndex]->process(numSamples, &ptr);
}
}
}
}