Vital
Loading...
Searching...
No Matches
filter_module.cpp
Go to the documentation of this file.
1#include "filter_module.h"
2
3#include "comb_module.h"
4#include "digital_svf.h"
5#include "diode_filter.h"
6#include "dirty_filter.h"
7#include "formant_module.h"
8#include "ladder_filter.h"
9#include "phaser_filter.h"
10#include "sallen_key_filter.h"
11#include "synth_constants.h"
12
13namespace vital {
14
15 FilterModule::FilterModule(std::string prefix) :
16 SynthModule(kNumInputs, 1),
17 last_model_(-1),
18 was_on_(false),
19 prefix_(std::move(prefix)),
20 create_on_value_(true),
21 mono_(false),
22 on_(nullptr),
23 filter_model_(nullptr),
24 mix_(0.0f),
25 filter_mix_(nullptr),
26 comb_filter_(nullptr),
27 digital_svf_(nullptr),
28 dirty_filter_(nullptr),
29 formant_filter_(nullptr),
30 ladder_filter_(nullptr),
31 phaser_filter_(nullptr),
32 sallen_key_filter_(nullptr) {
33
34 // Create submodules for each filter type.
40 phaser_filter_ = new PhaserFilter(false);
43
44 // Register submodules so they can be managed by the framework.
47
56 }
57
65 Output* keytrack_amount = createModControl(prefix_ + "_keytrack");
66 cr::Multiply* current_keytrack = new cr::Multiply();
67 current_keytrack->useInput(input(kKeytrack), 0);
68 current_keytrack->plug(keytrack_amount, 1);
69
70 Output* midi_cutoff = createModControl(prefix_ + "_cutoff", true, true, current_keytrack->output());
71 Output* resonance = createModControl(prefix_ + "_resonance");
72 Output* drive = createModControl(prefix_ + "_drive");
73 Output* blend = createModControl(prefix_ + "_blend");
74 Output* blend_transpose = createModControl(prefix_ + "_blend_transpose");
76 on_ = createBaseControl(prefix_ + "_on");
77 Value* filter_style = createBaseControl(prefix_ + "_style");
79
81
82 // Wire up the comb filter:
84 comb_filter_->plug(filter_style, CombModule::kStyle);
92
93 // Digital SVF filter:
95 digital_svf_->plug(filter_style, DigitalSvf::kStyle);
102
103 // Diode filter:
107 diode_filter_->plug(filter_style, DiodeFilter::kStyle);
112
113 // Dirty filter:
117 dirty_filter_->plug(filter_style, DirtyFilter::kStyle);
122
123 // Formant filter:
129
130 // Ladder filter:
139
140 // Phaser filter:
150
151 // Sallen-Key filter:
160
161 // Disable all but dynamically chosen filter models initially:
162 comb_filter_->enable(false);
163 digital_svf_->enable(false);
164 diode_filter_->enable(false);
165 dirty_filter_->enable(false);
166 formant_filter_->enable(false);
167 ladder_filter_->enable(false);
169
170 addProcessor(current_keytrack);
171
173 }
174
188
189 Output* FilterModule::createModControl(std::string name, bool audio_rate, bool smooth_value,
190 Output* internal_modulation) {
194 if (mono_)
195 return createMonoModControl(name, audio_rate, smooth_value, internal_modulation);
196 return createPolyModControl(name, audio_rate, smooth_value, internal_modulation, input(kReset));
197 }
198
203 comb_filter_->enable(new_model == constants::kComb);
211
212 if (new_model == last_model_)
213 return;
214
215 Processor* to_reset = nullptr;
216 if (new_model == constants::kAnalog) to_reset = sallen_key_filter_;
217 else if (new_model == constants::kComb) to_reset = comb_filter_;
218 else if (new_model == constants::kDigital) to_reset = digital_svf_;
219 else if (new_model == constants::kDiode) to_reset = diode_filter_;
220 else if (new_model == constants::kDirty) to_reset = dirty_filter_;
221 else if (new_model == constants::kFormant) to_reset = formant_filter_;
222 else if (new_model == constants::kLadder) to_reset = ladder_filter_;
223 else if (new_model == constants::kPhase) to_reset = phaser_filter_;
224
225 if (to_reset)
226 getLocalProcessor(to_reset)->hardReset();
227
228 last_model_ = new_model;
229 }
230
231 void FilterModule::process(int num_samples) {
235 bool on = on_ == nullptr || on_->value() > 0.5f;
236 setModel(static_cast<int>(roundf(filter_model_->value())));
237
238 if (on) {
239 SynthModule::process(num_samples);
240
241 poly_float current_mix = mix_;
242 mix_ = utils::clamp(filter_mix_->buffer[0], 0.0f, 1.0f);
243 current_mix = utils::maskLoad(current_mix, mix_, getResetMask(kReset));
244 poly_float delta_mix = (mix_ - current_mix) * (1.0f / num_samples);
245
246 poly_float* audio_out = output()->buffer;
247 const poly_float* audio_in = input(kAudio)->source->buffer;
248 for (int i = 0; i < num_samples; ++i) {
249 current_mix += delta_mix;
250 // Interpolate between dry (audio_in) and wet (audio_out) based on current_mix
251 audio_out[i] = utils::interpolate(audio_in[i], audio_out[i], current_mix);
252 }
253 }
254 else {
255 // Filter is off: output silence
256 utils::zeroBuffer(output()->buffer, num_samples);
257 }
258 }
259
260 void FilterModule::setMono(bool mono) {
264 mono_ = mono;
266 }
267
268} // namespace vital
A module that implements a comb filter effect.
Definition comb_module.h:17
void hardReset() override
Performs a hard reset of the comb filter, returning it to its initial state.
Definition comb_module.cpp:43
@ kAudio
Definition comb_module.h:35
@ kFilterCutoffBlend
Definition comb_module.h:39
@ kMidiBlendTranspose
Definition comb_module.h:38
@ kResonance
Definition comb_module.h:41
@ kReset
Definition comb_module.h:36
@ kStyle
Definition comb_module.h:40
@ kMidiCutoff
Definition comb_module.h:37
@ kMidi
Definition comb_module.h:42
A state-variable filter (SVF) implementation, supporting multiple filter types (12/24 dB,...
Definition digital_svf.h:17
void hardReset() override
Performs a complete reset of the filter states for all voices.
Definition digital_svf.cpp:497
A diode ladder filter implementation for the Vital synthesizer.
Definition diode_filter.h:20
void hardReset() override
Performs a complete hard reset of all internal states (for all voices).
Definition diode_filter.cpp:32
A nonlinear filter that produces a "dirty" and saturated sound, ideal for adding character to the sig...
Definition dirty_filter.h:30
void hardReset() override
Hard resets the filter, clearing all internal states.
Definition dirty_filter.cpp:32
SallenKeyFilter * sallen_key_filter_
Sallen-Key filter submodule (analog-inspired).
Definition filter_module.h:147
@ kMidi
Definition filter_module.h:39
@ kReset
Definition filter_module.h:37
@ kKeytrack
Definition filter_module.h:38
@ kAudio
Definition filter_module.h:36
std::string prefix_
A prefix for naming parameters, enabling multiple instances.
Definition filter_module.h:130
Value * filter_model_
A parameter selecting which filter model to use.
Definition filter_module.h:135
void setMono(bool mono)
Configures the FilterModule to be mono or polyphonic.
Definition filter_module.cpp:260
void setModel(int new_model)
Sets the current filter model, enabling the corresponding submodule and disabling others.
Definition filter_module.cpp:199
DirtyFilter * dirty_filter_
"Dirty" filter submodule (non-linear, characterful).
Definition filter_module.h:143
DigitalSvf * digital_svf_
Digital state-variable filter submodule.
Definition filter_module.h:141
Output * filter_mix_
The parameter output controlling the wet/dry mix.
Definition filter_module.h:138
int last_model_
Tracks the last active filter model to detect changes.
Definition filter_module.h:128
bool create_on_value_
Whether an "on" parameter should be created.
Definition filter_module.h:131
FilterModule(std::string prefix="")
Constructs a FilterModule, optionally with a prefix for parameter naming.
Definition filter_module.cpp:15
poly_float mix_
Current wet/dry mix value for blending filtered audio with the original.
Definition filter_module.h:136
bool mono_
True if the filter is in mono mode, false for polyphonic.
Definition filter_module.h:132
void hardReset() override
Performs a hard reset of all filter submodules, clearing their states and buffers.
Definition filter_module.cpp:175
void process(int num_samples) override
Processes a block of samples, applying the selected filter model and mixing the result.
Definition filter_module.cpp:231
Output * createModControl(std::string name, bool audio_rate=false, bool smooth_value=false, Output *internal_modulation=nullptr)
Creates a modulation control parameter for the filter.
Definition filter_module.cpp:189
FormantModule * formant_filter_
Formant filter submodule (for vowel/formant filtering).
Definition filter_module.h:144
CombModule * comb_filter_
Comb filter submodule.
Definition filter_module.h:140
void init() override
Initializes the filter module, creating parameters and linking them to submodules.
Definition filter_module.cpp:58
PhaserFilter * phaser_filter_
Phaser filter submodule (phase-based filtering).
Definition filter_module.h:146
LadderFilter * ladder_filter_
Ladder filter submodule (classic analog-style filter).
Definition filter_module.h:145
DiodeFilter * diode_filter_
Diode filter submodule.
Definition filter_module.h:142
Value * on_
Optional "on" control parameter.
Definition filter_module.h:134
A module that applies various formant filtering styles to an incoming audio signal.
Definition formant_module.h:16
@ kReset
Definition formant_module.h:29
@ kStyle
Definition formant_module.h:32
@ kBlend
Definition formant_module.h:31
@ kAudio
Definition formant_module.h:28
void setMono(bool mono)
Sets whether the module is operating in mono mode or not.
Definition formant_module.h:83
void hardReset() override
Performs a hard reset of the currently active formant filter, clearing its internal state.
Definition formant_module.cpp:89
A classic transistor ladder-style filter for the Vital synthesizer.
Definition ladder_filter.h:19
void hardReset() override
Performs a hard reset of all filter states.
Definition ladder_filter.cpp:30
A multi-stage phaser filter for the Vital synthesizer.
Definition phaser_filter.h:19
void hardReset() override
Performs a full reset of the filter states (for all voices).
Definition phaser_filter.cpp:34
Base class for all signal-processing units in Vital.
Definition processor.h:212
void useOutput(Output *output)
Uses an existing Output object as this Processor's first output.
Definition processor.cpp:138
force_inline Input * input(unsigned int index=0) const
Retrieves the Input pointer at a given index.
Definition processor.h:587
void useInput(Input *input)
Uses an existing Input object as this Processor's first input.
Definition processor.cpp:126
force_inline poly_mask getResetMask(int input_index) const
Retrieves a mask indicating which voices triggered a note-on event. Compares the input's trigger_valu...
Definition processor.h:360
virtual void hardReset()
Called to perform a "hard" reset for all voices.
Definition processor.h:272
void plug(const Output *source)
Connects an external Output to this Processor's first input.
Definition processor.cpp:79
virtual void process(int num_samples)=0
Main processing function. Called by the ProcessorRouter.
force_inline Output * output(unsigned int index=0) const
Retrieves the Output pointer at a given index.
Definition processor.h:616
virtual void enable(bool enable)
Enables or disables this Processor.
Definition processor.h:318
virtual void init()
Called after constructor, used for any additional initialization. Subclasses can override....
Definition processor.h:258
Processor * getLocalProcessor(const Processor *global_processor)
Retrieves the local instance of a globally defined Processor.
Definition processor_router.cpp:376
virtual void addProcessor(Processor *processor)
Adds a Processor to be managed by this router.
Definition processor_router.cpp:121
A Sallen-Key style filter capable of multiple modes (12dB, 24dB, dual modes) with nonlinear drive and...
Definition sallen_key_filter.h:28
void hardReset() override
Performs a hard reset of all internal states.
Definition sallen_key_filter.cpp:29
@ kPassBlend
Blending parameter for low-pass, high-pass, band-pass.
Definition synth_filter.h:62
@ kResonance
Resonance parameter.
Definition synth_filter.h:58
@ kTranspose
MIDI transpose in semitones.
Definition synth_filter.h:65
@ kMidiCutoff
MIDI-based cutoff parameter.
Definition synth_filter.h:57
@ kReset
Reset signal.
Definition synth_filter.h:56
@ kStyle
Filter style (12 dB, 24 dB, etc.)
Definition synth_filter.h:61
@ kDriveGain
Drive amount in dB.
Definition synth_filter.h:59
@ kAudio
Audio input index.
Definition synth_filter.h:55
A ProcessorRouter that encapsulates a cohesive unit of functionality in the synthesizer.
Definition synth_module.h:129
Output * createPolyModControl(std::string name, bool audio_rate=false, bool smooth_value=false, Output *internal_modulation=nullptr, Input *reset=nullptr)
Creates a polyphonic mod control, including applying parameter scaling.
Definition synth_module.cpp:173
Value * createBaseControl(std::string name, bool audio_rate=false, bool smooth_value=false)
Creates a simple control processor for a given parameter name.
Definition synth_module.cpp:22
void addSubmodule(SynthModule *module)
Adds a submodule to this SynthModule.
Definition synth_module.h:289
virtual void enable(bool enable) override
Enables or disables this SynthModule and its owned processors.
Definition synth_module.cpp:516
Output * createMonoModControl(std::string name, bool audio_rate=false, bool smooth_value=false, Output *internal_modulation=nullptr)
Creates a monophonic mod control, including applying parameter scaling.
Definition synth_module.cpp:104
A Processor that maintains and outputs a constant poly_float value.
Definition value.h:24
force_inline mono_float value() const
Returns the current mono_float value of the first lane.
Definition value.h:60
Control-rate multiplication of two values.
Definition operators.h:719
#define force_inline
Definition common.h:23
cr::Value resonance
Resonance factor for this formant.
Definition formant_filter.cpp:18
cr::Value midi_cutoff
MIDI note for the filter's cutoff.
Definition formant_filter.cpp:19
@ kDirty
Definition synth_constants.h:196
@ kDigital
Definition synth_constants.h:198
@ kAnalog
Definition synth_constants.h:195
@ kPhase
Definition synth_constants.h:202
@ kFormant
Definition synth_constants.h:200
@ kDiode
Definition synth_constants.h:199
@ kLadder
Definition synth_constants.h:197
@ kComb
Definition synth_constants.h:201
force_inline poly_float clamp(poly_float value, mono_float min, mono_float max)
Clamps each lane of a vector to [min, max].
Definition poly_utils.h:306
force_inline void zeroBuffer(mono_float *buffer, int size)
Zeros a mono buffer (standard array).
Definition poly_utils.h:570
force_inline poly_float maskLoad(poly_float zero_value, poly_float one_value, poly_mask reset_mask)
Selects between two values (zero_value or one_value) based on a mask in each lane.
Definition poly_utils.h:351
force_inline poly_float interpolate(poly_float from, poly_float to, mono_float t)
Performs a linear interpolation between two poly_floats using a scalar t in [0..1].
Definition poly_utils.h:182
Contains classes and functions used within the Vital synthesizer framework.
const Output * source
The output from which this input reads samples.
Definition processor.h:134
Holds and manages a buffer of samples (poly_float) for a Processor's output.
Definition processor.h:35
poly_float * buffer
Pointer to the output buffer.
Definition processor.h:110
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600