Skip to content

Commit

Permalink
Add DCFilter node
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Nov 12, 2023
1 parent 5109f1c commit 1636106
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions source/include/signalflow/node/processors/filters/dc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "signalflow/core/constants.h"
#include "signalflow/node/node.h"

namespace signalflow
{
/**--------------------------------------------------------------------------------*
* Remove DC offset.
*---------------------------------------------------------------------------------*/
class DCFilter : public UnaryOpNode
{
public:
DCFilter(NodeRef input = 0.0);

virtual void process(Buffer &out, int num_frames) override;
virtual void alloc() override;

private:
float R;
std::vector<float> previous_input;
std::vector<float> previous_output;
};

REGISTER(DCFilter, "dc-filter")
}
1 change: 1 addition & 0 deletions source/include/signalflow/signalflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
#include <signalflow/node/processors/dynamics/maximiser.h>
#include <signalflow/node/processors/dynamics/rms.h>
#include <signalflow/node/processors/filters/biquad.h>
#include <signalflow/node/processors/filters/dc.h>
#include <signalflow/node/processors/filters/eq.h>
#include <signalflow/node/processors/filters/moog.h>
#include <signalflow/node/processors/filters/svf.h>
Expand Down
1 change: 1 addition & 0 deletions source/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(SRC ${SRC}
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/filters/svf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/filters/eq.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/filters/moog.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/filters/dc.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/panning/stereo-panner.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/panning/stereo-balance.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/panning/stereo-width.cpp
Expand Down
45 changes: 45 additions & 0 deletions source/src/node/processors/filters/dc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "signalflow/core/graph.h"
#include "signalflow/node/processors/filters/dc.h"

#include <stdlib.h>

namespace signalflow
{

DCFilter::DCFilter(NodeRef input)
: UnaryOpNode(input)
{
this->name = "dc-filter";

// "R" depends on sampling rate and the low frequency point. Do not set "R" to a fixed value
// (e.g. 0.99) if you don't know the sample rate. Instead set R to:
// (-3dB @ 40Hz): R = 1-(250/samplerate)
// (-3dB @ 30Hz): R = 1-(190/samplerate)
// (-3dB @ 20Hz): R = 1-(126/samplerate)
// this->R = 1.0 - (30.0 / this->get_graph()->get_sample_rate());

// this->alloc();
}

void DCFilter::alloc()
{
this->previous_input.resize(this->num_output_channels_allocated);
this->previous_output.resize(this->num_output_channels_allocated);
}

void DCFilter::process(Buffer &out, int num_frames)
{
this->R = 1.0 - (30.0 / this->graph->get_sample_rate());
for (int channel = 0; channel < num_output_channels; channel++)
{
for (int frame = 0; frame < num_frames; frame++)
{
float output = this->input->out[channel][frame] - this->previous_input[channel] + R * this->previous_output[channel];
this->previous_input[channel] = this->input->out[channel][frame];
this->previous_output[channel] = output;
out[channel][frame] = output;
}
}
}

}
3 changes: 3 additions & 0 deletions source/src/python/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ void init_python_nodes(py::module &m)
.def(py::init<NodeRef, signalflow_filter_type_t, NodeRef, NodeRef, NodeRef>(), "input"_a = 0.0, "filter_type"_a = SIGNALFLOW_FILTER_TYPE_LOW_PASS, "cutoff"_a = 440, "resonance"_a = 0.0, "peak_gain"_a = 0.0)
.def(py::init<NodeRef, std::string, NodeRef, NodeRef, NodeRef>(), "input"_a, "filter_type"_a, "cutoff"_a = 440, "resonance"_a = 0.0, "peak_gain"_a = 0.0);

py::class_<DCFilter, Node, NodeRefTemplate<DCFilter>>(m, "DCFilter", "Remove DC offset.")
.def(py::init<NodeRef>(), "input"_a = 0.0);

py::class_<EQ, Node, NodeRefTemplate<EQ>>(m, "EQ", "Three-band EQ.")
.def(py::init<NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef>(), "input"_a = 0.0, "low_gain"_a = 1.0, "mid_gain"_a = 1.0, "high_gain"_a = 1.0, "low_freq"_a = 500, "high_freq"_a = 5000);

Expand Down

0 comments on commit 1636106

Please sign in to comment.