Skip to content

Commit

Permalink
Add a Second Order All Pass Filter called Soap (#182)
Browse files Browse the repository at this point in the history
* Update CMakeLists.txt and Makefile

Update CMakeLists.txt and Makefile to include soap filter

* update daisysp.h to include soap

update daisysp.h to include soap

* add soap filter 

add soap filter

* update to cmath, floats, add output options

change to <cmath> instead of math.h for trig functions. replaced doubles with floats for speed. added bandpass and bandreject output modes similar to svf filter
  • Loading branch information
b-tice committed Jun 12, 2023
1 parent ed11433 commit 7364d0b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Source/Filters/moogladder.cpp
Source/Filters/nlfilt.cpp
Source/Filters/svf.cpp
Source/Filters/tone.cpp
Source/Filters/soap.cpp
Source/Noise/clockednoise.cpp
Source/Noise/grainlet.cpp
Source/Noise/particle.cpp
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ mode \
moogladder \
nlfilt \
svf \
tone
tone \
soap
#fir

NOISE_MOD_DIR = Noise
Expand Down
61 changes: 61 additions & 0 deletions Source/Filters/soap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "soap.h"
#include <cmath>

#define PI 3.141592653589793

void Soap::Init(float sample_rate)
{
soap_center_freq_ = 400.0;
soap_bandwidth_ = 50.0;
in_0_ = 0.0; // input x0
din_1_ = 0.0; // delayed input x1
din_2_ = 0.0; // delayed input x2
dout_1_ = 0.0; // delayed output y1
dout_2_ = 0.0; // delayed output y2
all_output_ = 0.0; // all pass output y0
out_bandpass_ = 0.0; // bandpass output
out_bandreject_ = 0.0; // bandreject output
sr_ = sample_rate;
return;
}

void Soap::Process(float in)
{
// recalculate the coefficients, later move this to a lookup table
float d = -std::cos(2.0 * PI * (soap_center_freq_/sr_));

// tangent bandwidth
float tf = std::tan(PI * (soap_bandwidth_/sr_));

// coefficient
float c = (tf - 1.0)/(tf + 1.0);

in_0_ = in;

all_output_ = -c*in_0_ + (d - d*c)*din_1_ + din_2_ - (d - d*c)*dout_1_ + c*dout_2_;

// move samples in delay for next sample
din_2_ = din_1_;
din_1_ = in_0_;
dout_2_ = dout_1_;
dout_1_ = all_output_;

// make factor -1.0 to create a bandpass
out_bandpass_ = (in_0_ + all_output_ * -1.0) * 0.5;

// make factor +1.0 to create a bandreject
out_bandreject_ = (in_0_ + all_output_ * 0.99) * 0.5;

return;
}

void Soap::SetCenterFreq(float f)
{
soap_center_freq_ = f;
return;
}

void Soap::SetFilterBandwidth(float b) {
soap_bandwidth_ = b;
return;
}
61 changes: 61 additions & 0 deletions Source/Filters/soap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

/** Second Order All Pass Filter
This is from Tom Erbe's synth notes. The filter
can be configured to be a band pass or band reject
Ported by: Brian Tice
*/
class Soap
{

public:
Soap() {}
~Soap() {}

/** Initializes the filter
float sample_rate - sample rate of the audio engine being run, and the frequency that the Process function will be called.
*/
void Init(float sample_rate);

/**
Process the input signal, updating all of the outputs
*/
void Process(float in);

/**
Sets the center frequency of the filter.
*/
void SetCenterFreq(float f);

/**
Sets the low frequency threshold of the filter.
*/
void SetFilterBandwidth(float b);

/** Bandpass output
\return bandpass output of the filter
*/
inline float Bandpass() { return out_bandpass_; }

/** Bandreject output
\return bandreject output of the filter
*/
inline float Bandreject() { return out_bandreject_; }

private:
float soap_center_freq_;
float soap_bandwidth_;
float in_0_;
float din_1_;
float din_2_;
float dout_1_;
float dout_2_;
float all_output_;
float out_bandpass_;
float out_bandreject_;
float sr_;


};
1 change: 1 addition & 0 deletions Source/daisysp.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "Filters/svf.h"
#include "Filters/tone.h"
#include "Filters/fir.h"
#include "Filters/soap.h"

/** Noise Modules */
#include "Noise/clockednoise.h"
Expand Down

0 comments on commit 7364d0b

Please sign in to comment.