Skip to content

Commit

Permalink
Update dpf, start of filter plugin
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed Dec 11, 2023
1 parent af270b0 commit 43df48f
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins: dgl
$(MAKE) all -C plugins/Compressor
$(MAKE) all -C plugins/ConvolutionReverb
$(MAKE) all -C plugins/DevilDistortion
$(MAKE) all -C plugins/Filter
# $(MAKE) all -C plugins/Sampler

ifneq ($(CROSS_COMPILING),true)
Expand All @@ -45,6 +46,7 @@ clean:
$(MAKE) clean -C plugins/Compressor
$(MAKE) clean -C plugins/ConvolutionReverb
$(MAKE) clean -C plugins/DevilDistortion
$(MAKE) clean -C plugins/Filter
# $(MAKE) clean -C plugins/Sampler
rm -rf bin build dpf-widgets/opengl/*.d dpf-widgets/opengl/*.o
rm -f 3rd-party/FFTConvolver/*.d 3rd-party/FFTConvolver/*.o
Expand Down
165 changes: 165 additions & 0 deletions plugins/Filter/Biquad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// Biquad.cpp
//
// Created by Nigel Redmon on 11/24/12
// EarLevel Engineering: earlevel.com
// Copyright 2012 Nigel Redmon
//
// For a complete explanation of the Biquad code:
// http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
//
// License:
//
// This source code is provided as is, without warranty.
// You may copy and distribute verbatim copies of this document.
// You may modify and use this source code to create binary code
// for your own purposes, free or commercial.
//

#include <math.h>
#include "Biquad.h"

Biquad::Biquad() {
type = bq_type_lowpass;
a0 = 1.0;
a1 = a2 = b1 = b2 = 0.0;
Fc = 0.50;
Q = 0.707;
peakGain = 0.0;
z1 = z2 = 0.0;
}

Biquad::Biquad(int type, double Fc, double Q, double peakGainDB) {
setBiquad(type, Fc, Q, peakGainDB);
z1 = z2 = 0.0;
}

Biquad::~Biquad() {
}

void Biquad::setType(int type) {
this->type = type;
calcBiquad();
}

void Biquad::setQ(double Q) {
this->Q = Q;
calcBiquad();
}

void Biquad::setFc(double Fc) {
this->Fc = Fc;
calcBiquad();
}

void Biquad::setPeakGain(double peakGainDB) {
this->peakGain = peakGainDB;
calcBiquad();
}

void Biquad::setBiquad(int type, double Fc, double Q, double peakGainDB) {
this->type = type;
this->Q = Q;
this->Fc = Fc;
setPeakGain(peakGainDB);
}

void Biquad::calcBiquad(void) {
double norm;
double V = pow(10, fabs(peakGain) / 20.0);
double K = tan(M_PI * Fc);
switch (this->type) {
case bq_type_lowpass:
norm = 1 / (1 + K / Q + K * K);
a0 = K * K * norm;
a1 = 2 * a0;
a2 = a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;

case bq_type_highpass:
norm = 1 / (1 + K / Q + K * K);
a0 = 1 * norm;
a1 = -2 * a0;
a2 = a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;

case bq_type_bandpass:
norm = 1 / (1 + K / Q + K * K);
a0 = K / Q * norm;
a1 = 0;
a2 = -a0;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - K / Q + K * K) * norm;
break;

case bq_type_notch:
norm = 1 / (1 + K / Q + K * K);
a0 = (1 + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = a0;
b1 = a1;
b2 = (1 - K / Q + K * K) * norm;
break;

case bq_type_peak:
if (peakGain >= 0) { // boost
norm = 1 / (1 + 1/Q * K + K * K);
a0 = (1 + V/Q * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - V/Q * K + K * K) * norm;
b1 = a1;
b2 = (1 - 1/Q * K + K * K) * norm;
}
else { // cut
norm = 1 / (1 + V/Q * K + K * K);
a0 = (1 + 1/Q * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - 1/Q * K + K * K) * norm;
b1 = a1;
b2 = (1 - V/Q * K + K * K) * norm;
}
break;
case bq_type_lowshelf:
if (peakGain >= 0) { // boost
norm = 1 / (1 + sqrt(2) * K + K * K);
a0 = (1 + sqrt(2*V) * K + V * K * K) * norm;
a1 = 2 * (V * K * K - 1) * norm;
a2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - sqrt(2) * K + K * K) * norm;
}
else { // cut
norm = 1 / (1 + sqrt(2*V) * K + V * K * K);
a0 = (1 + sqrt(2) * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - sqrt(2) * K + K * K) * norm;
b1 = 2 * (V * K * K - 1) * norm;
b2 = (1 - sqrt(2*V) * K + V * K * K) * norm;
}
break;
case bq_type_highshelf:
if (peakGain >= 0) { // boost
norm = 1 / (1 + sqrt(2) * K + K * K);
a0 = (V + sqrt(2*V) * K + K * K) * norm;
a1 = 2 * (K * K - V) * norm;
a2 = (V - sqrt(2*V) * K + K * K) * norm;
b1 = 2 * (K * K - 1) * norm;
b2 = (1 - sqrt(2) * K + K * K) * norm;
}
else { // cut
norm = 1 / (V + sqrt(2*V) * K + K * K);
a0 = (1 + sqrt(2) * K + K * K) * norm;
a1 = 2 * (K * K - 1) * norm;
a2 = (1 - sqrt(2) * K + K * K) * norm;
b1 = 2 * (K * K - V) * norm;
b2 = (V - sqrt(2*V) * K + K * K) * norm;
}
break;
}

return;
}
60 changes: 60 additions & 0 deletions plugins/Filter/Biquad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Biquad.h
//
// Created by Nigel Redmon on 11/24/12
// EarLevel Engineering: earlevel.com
// Copyright 2012 Nigel Redmon
//
// For a complete explanation of the Biquad code:
// http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
//
// License:
//
// This source code is provided as is, without warranty.
// You may copy and distribute verbatim copies of this document.
// You may modify and use this source code to create binary code
// for your own purposes, free or commercial.
//

#ifndef Biquad_h
#define Biquad_h

enum {
bq_type_lowpass = 0,
bq_type_highpass,
bq_type_bandpass,
bq_type_notch,
bq_type_peak,
bq_type_lowshelf,
bq_type_highshelf
};

class Biquad {
public:
Biquad();
Biquad(int type, double Fc, double Q, double peakGainDB);
~Biquad();
void setType(int type);
void setQ(double Q);
void setFc(double Fc);
void setPeakGain(double peakGainDB);
void setBiquad(int type, double Fc, double Q, double peakGainDB);
float process(float in);

protected:
void calcBiquad(void);

int type;
double a0, a1, a2, b1, b2;
double Fc, Q, peakGain;
double z1, z2;
};

inline float Biquad::process(float in) {
double out = in * a0 + z1;
z1 = in * a1 + z2 - b1 * out;
z2 = in * a2 - b2 * out;
return out;
}

#endif // Biquad_h
64 changes: 64 additions & 0 deletions plugins/Filter/DistrhoPluginInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* DISTRHO OneKnob Filter
* Copyright (C) 2023 Filipe Coelho <[email protected]>
*
* 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 2 of
* the License, or 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.
*
* For a full copy of the GNU General Public License see the LICENSE file.
*/

#pragma once

#include "OneKnobPluginInfo.h"
#include "Biquad.h"

#define DISTRHO_PLUGIN_NAME "OneKnob Filter"
#define DISTRHO_PLUGIN_URI "https://distrho.kx.studio/plugins/oneknob#Filter"
#define DISTRHO_PLUGIN_CLAP_ID "studio.kx.distrho.oneknob.Filter"

#define DISTRHO_PLUGIN_CLAP_FEATURES "audio-effect", "filter", "stereo"
#define DISTRHO_PLUGIN_LV2_CATEGORY "lv2:FilterPlugin"
#define DISTRHO_PLUGIN_VST3_CATEGORIES "Fx|Filter|Stereo"

#ifdef __MOD_DEVICES__
#undef DISTRHO_PLUGIN_NUM_INPUTS
#undef DISTRHO_PLUGIN_NUM_OUTPUTS
#define DISTRHO_PLUGIN_NUM_INPUTS 1
#define DISTRHO_PLUGIN_NUM_OUTPUTS 1
#endif

enum Parameters {
kParameterType = 0,
kParameterFrequency,
kParameterQ,
kParameterGain,
kParameterBypass,
kParameterCount
};

enum Programs {
kProgramDefault,
kProgramCount
};

enum States {
kStateCount
};

static constexpr const struct OneKnobParameterRanges {
float min, def, max;
} kParameterRanges[kParameterCount] = {
{ bq_type_lowpass, bq_type_lowpass, bq_type_highshelf },
{ 20.f, 5000.f, 20000.f },
{ 0.f, 0.707f, 1.f },
{ -20.f, 0.f, 20.f },
{}
};
35 changes: 35 additions & 0 deletions plugins/Filter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/make -f
# Makefile for DISTRHO Plugins #
# ---------------------------- #
# Created by falkTX
#

# --------------------------------------------------------------
# Project name, used for binaries

NAME = OK-Filter

# --------------------------------------------------------------
# Files to build

FILES_DSP = \
OneKnobPlugin.cpp \
Biquad.cpp

# --------------------------------------------------------------
# Do some magic

include ../../dpf/Makefile.plugins.mk

BUILD_CXX_FLAGS += -I../common
BUILD_CXX_FLAGS += -I../../dpf-widgets/opengl
LINK_FLAGS += $(SHARED_MEMORY_LIBS)

# --------------------------------------------------------------
# Enable all possible plugin types

TARGETS += lv2_sep

all: $(TARGETS)

# --------------------------------------------------------------
Loading

0 comments on commit 43df48f

Please sign in to comment.