Skip to content

Commit 43df48f

Browse files
committed
Update dpf, start of filter plugin
Signed-off-by: falkTX <falktx@falktx.com>
1 parent af270b0 commit 43df48f

File tree

8 files changed

+608
-2
lines changed

8 files changed

+608
-2
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ plugins: dgl
2222
$(MAKE) all -C plugins/Compressor
2323
$(MAKE) all -C plugins/ConvolutionReverb
2424
$(MAKE) all -C plugins/DevilDistortion
25+
$(MAKE) all -C plugins/Filter
2526
# $(MAKE) all -C plugins/Sampler
2627

2728
ifneq ($(CROSS_COMPILING),true)
@@ -45,6 +46,7 @@ clean:
4546
$(MAKE) clean -C plugins/Compressor
4647
$(MAKE) clean -C plugins/ConvolutionReverb
4748
$(MAKE) clean -C plugins/DevilDistortion
49+
$(MAKE) clean -C plugins/Filter
4850
# $(MAKE) clean -C plugins/Sampler
4951
rm -rf bin build dpf-widgets/opengl/*.d dpf-widgets/opengl/*.o
5052
rm -f 3rd-party/FFTConvolver/*.d 3rd-party/FFTConvolver/*.o

plugins/Filter/Biquad.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//
2+
// Biquad.cpp
3+
//
4+
// Created by Nigel Redmon on 11/24/12
5+
// EarLevel Engineering: earlevel.com
6+
// Copyright 2012 Nigel Redmon
7+
//
8+
// For a complete explanation of the Biquad code:
9+
// http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
10+
//
11+
// License:
12+
//
13+
// This source code is provided as is, without warranty.
14+
// You may copy and distribute verbatim copies of this document.
15+
// You may modify and use this source code to create binary code
16+
// for your own purposes, free or commercial.
17+
//
18+
19+
#include <math.h>
20+
#include "Biquad.h"
21+
22+
Biquad::Biquad() {
23+
type = bq_type_lowpass;
24+
a0 = 1.0;
25+
a1 = a2 = b1 = b2 = 0.0;
26+
Fc = 0.50;
27+
Q = 0.707;
28+
peakGain = 0.0;
29+
z1 = z2 = 0.0;
30+
}
31+
32+
Biquad::Biquad(int type, double Fc, double Q, double peakGainDB) {
33+
setBiquad(type, Fc, Q, peakGainDB);
34+
z1 = z2 = 0.0;
35+
}
36+
37+
Biquad::~Biquad() {
38+
}
39+
40+
void Biquad::setType(int type) {
41+
this->type = type;
42+
calcBiquad();
43+
}
44+
45+
void Biquad::setQ(double Q) {
46+
this->Q = Q;
47+
calcBiquad();
48+
}
49+
50+
void Biquad::setFc(double Fc) {
51+
this->Fc = Fc;
52+
calcBiquad();
53+
}
54+
55+
void Biquad::setPeakGain(double peakGainDB) {
56+
this->peakGain = peakGainDB;
57+
calcBiquad();
58+
}
59+
60+
void Biquad::setBiquad(int type, double Fc, double Q, double peakGainDB) {
61+
this->type = type;
62+
this->Q = Q;
63+
this->Fc = Fc;
64+
setPeakGain(peakGainDB);
65+
}
66+
67+
void Biquad::calcBiquad(void) {
68+
double norm;
69+
double V = pow(10, fabs(peakGain) / 20.0);
70+
double K = tan(M_PI * Fc);
71+
switch (this->type) {
72+
case bq_type_lowpass:
73+
norm = 1 / (1 + K / Q + K * K);
74+
a0 = K * K * norm;
75+
a1 = 2 * a0;
76+
a2 = a0;
77+
b1 = 2 * (K * K - 1) * norm;
78+
b2 = (1 - K / Q + K * K) * norm;
79+
break;
80+
81+
case bq_type_highpass:
82+
norm = 1 / (1 + K / Q + K * K);
83+
a0 = 1 * norm;
84+
a1 = -2 * a0;
85+
a2 = a0;
86+
b1 = 2 * (K * K - 1) * norm;
87+
b2 = (1 - K / Q + K * K) * norm;
88+
break;
89+
90+
case bq_type_bandpass:
91+
norm = 1 / (1 + K / Q + K * K);
92+
a0 = K / Q * norm;
93+
a1 = 0;
94+
a2 = -a0;
95+
b1 = 2 * (K * K - 1) * norm;
96+
b2 = (1 - K / Q + K * K) * norm;
97+
break;
98+
99+
case bq_type_notch:
100+
norm = 1 / (1 + K / Q + K * K);
101+
a0 = (1 + K * K) * norm;
102+
a1 = 2 * (K * K - 1) * norm;
103+
a2 = a0;
104+
b1 = a1;
105+
b2 = (1 - K / Q + K * K) * norm;
106+
break;
107+
108+
case bq_type_peak:
109+
if (peakGain >= 0) { // boost
110+
norm = 1 / (1 + 1/Q * K + K * K);
111+
a0 = (1 + V/Q * K + K * K) * norm;
112+
a1 = 2 * (K * K - 1) * norm;
113+
a2 = (1 - V/Q * K + K * K) * norm;
114+
b1 = a1;
115+
b2 = (1 - 1/Q * K + K * K) * norm;
116+
}
117+
else { // cut
118+
norm = 1 / (1 + V/Q * K + K * K);
119+
a0 = (1 + 1/Q * K + K * K) * norm;
120+
a1 = 2 * (K * K - 1) * norm;
121+
a2 = (1 - 1/Q * K + K * K) * norm;
122+
b1 = a1;
123+
b2 = (1 - V/Q * K + K * K) * norm;
124+
}
125+
break;
126+
case bq_type_lowshelf:
127+
if (peakGain >= 0) { // boost
128+
norm = 1 / (1 + sqrt(2) * K + K * K);
129+
a0 = (1 + sqrt(2*V) * K + V * K * K) * norm;
130+
a1 = 2 * (V * K * K - 1) * norm;
131+
a2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
132+
b1 = 2 * (K * K - 1) * norm;
133+
b2 = (1 - sqrt(2) * K + K * K) * norm;
134+
}
135+
else { // cut
136+
norm = 1 / (1 + sqrt(2*V) * K + V * K * K);
137+
a0 = (1 + sqrt(2) * K + K * K) * norm;
138+
a1 = 2 * (K * K - 1) * norm;
139+
a2 = (1 - sqrt(2) * K + K * K) * norm;
140+
b1 = 2 * (V * K * K - 1) * norm;
141+
b2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
142+
}
143+
break;
144+
case bq_type_highshelf:
145+
if (peakGain >= 0) { // boost
146+
norm = 1 / (1 + sqrt(2) * K + K * K);
147+
a0 = (V + sqrt(2*V) * K + K * K) * norm;
148+
a1 = 2 * (K * K - V) * norm;
149+
a2 = (V - sqrt(2*V) * K + K * K) * norm;
150+
b1 = 2 * (K * K - 1) * norm;
151+
b2 = (1 - sqrt(2) * K + K * K) * norm;
152+
}
153+
else { // cut
154+
norm = 1 / (V + sqrt(2*V) * K + K * K);
155+
a0 = (1 + sqrt(2) * K + K * K) * norm;
156+
a1 = 2 * (K * K - 1) * norm;
157+
a2 = (1 - sqrt(2) * K + K * K) * norm;
158+
b1 = 2 * (K * K - V) * norm;
159+
b2 = (V - sqrt(2*V) * K + K * K) * norm;
160+
}
161+
break;
162+
}
163+
164+
return;
165+
}

plugins/Filter/Biquad.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Biquad.h
3+
//
4+
// Created by Nigel Redmon on 11/24/12
5+
// EarLevel Engineering: earlevel.com
6+
// Copyright 2012 Nigel Redmon
7+
//
8+
// For a complete explanation of the Biquad code:
9+
// http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
10+
//
11+
// License:
12+
//
13+
// This source code is provided as is, without warranty.
14+
// You may copy and distribute verbatim copies of this document.
15+
// You may modify and use this source code to create binary code
16+
// for your own purposes, free or commercial.
17+
//
18+
19+
#ifndef Biquad_h
20+
#define Biquad_h
21+
22+
enum {
23+
bq_type_lowpass = 0,
24+
bq_type_highpass,
25+
bq_type_bandpass,
26+
bq_type_notch,
27+
bq_type_peak,
28+
bq_type_lowshelf,
29+
bq_type_highshelf
30+
};
31+
32+
class Biquad {
33+
public:
34+
Biquad();
35+
Biquad(int type, double Fc, double Q, double peakGainDB);
36+
~Biquad();
37+
void setType(int type);
38+
void setQ(double Q);
39+
void setFc(double Fc);
40+
void setPeakGain(double peakGainDB);
41+
void setBiquad(int type, double Fc, double Q, double peakGainDB);
42+
float process(float in);
43+
44+
protected:
45+
void calcBiquad(void);
46+
47+
int type;
48+
double a0, a1, a2, b1, b2;
49+
double Fc, Q, peakGain;
50+
double z1, z2;
51+
};
52+
53+
inline float Biquad::process(float in) {
54+
double out = in * a0 + z1;
55+
z1 = in * a1 + z2 - b1 * out;
56+
z2 = in * a2 - b2 * out;
57+
return out;
58+
}
59+
60+
#endif // Biquad_h

plugins/Filter/DistrhoPluginInfo.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* DISTRHO OneKnob Filter
3+
* Copyright (C) 2023 Filipe Coelho <falktx@falktx.com>
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License as
7+
* published by the Free Software Foundation; either version 2 of
8+
* the License, or any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* For a full copy of the GNU General Public License see the LICENSE file.
16+
*/
17+
18+
#pragma once
19+
20+
#include "OneKnobPluginInfo.h"
21+
#include "Biquad.h"
22+
23+
#define DISTRHO_PLUGIN_NAME "OneKnob Filter"
24+
#define DISTRHO_PLUGIN_URI "https://distrho.kx.studio/plugins/oneknob#Filter"
25+
#define DISTRHO_PLUGIN_CLAP_ID "studio.kx.distrho.oneknob.Filter"
26+
27+
#define DISTRHO_PLUGIN_CLAP_FEATURES "audio-effect", "filter", "stereo"
28+
#define DISTRHO_PLUGIN_LV2_CATEGORY "lv2:FilterPlugin"
29+
#define DISTRHO_PLUGIN_VST3_CATEGORIES "Fx|Filter|Stereo"
30+
31+
#ifdef __MOD_DEVICES__
32+
#undef DISTRHO_PLUGIN_NUM_INPUTS
33+
#undef DISTRHO_PLUGIN_NUM_OUTPUTS
34+
#define DISTRHO_PLUGIN_NUM_INPUTS 1
35+
#define DISTRHO_PLUGIN_NUM_OUTPUTS 1
36+
#endif
37+
38+
enum Parameters {
39+
kParameterType = 0,
40+
kParameterFrequency,
41+
kParameterQ,
42+
kParameterGain,
43+
kParameterBypass,
44+
kParameterCount
45+
};
46+
47+
enum Programs {
48+
kProgramDefault,
49+
kProgramCount
50+
};
51+
52+
enum States {
53+
kStateCount
54+
};
55+
56+
static constexpr const struct OneKnobParameterRanges {
57+
float min, def, max;
58+
} kParameterRanges[kParameterCount] = {
59+
{ bq_type_lowpass, bq_type_lowpass, bq_type_highshelf },
60+
{ 20.f, 5000.f, 20000.f },
61+
{ 0.f, 0.707f, 1.f },
62+
{ -20.f, 0.f, 20.f },
63+
{}
64+
};

plugins/Filter/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/make -f
2+
# Makefile for DISTRHO Plugins #
3+
# ---------------------------- #
4+
# Created by falkTX
5+
#
6+
7+
# --------------------------------------------------------------
8+
# Project name, used for binaries
9+
10+
NAME = OK-Filter
11+
12+
# --------------------------------------------------------------
13+
# Files to build
14+
15+
FILES_DSP = \
16+
OneKnobPlugin.cpp \
17+
Biquad.cpp
18+
19+
# --------------------------------------------------------------
20+
# Do some magic
21+
22+
include ../../dpf/Makefile.plugins.mk
23+
24+
BUILD_CXX_FLAGS += -I../common
25+
BUILD_CXX_FLAGS += -I../../dpf-widgets/opengl
26+
LINK_FLAGS += $(SHARED_MEMORY_LIBS)
27+
28+
# --------------------------------------------------------------
29+
# Enable all possible plugin types
30+
31+
TARGETS += lv2_sep
32+
33+
all: $(TARGETS)
34+
35+
# --------------------------------------------------------------

0 commit comments

Comments
 (0)