Vital
Loading...
Searching...
No Matches
flanger_module.cpp
Go to the documentation of this file.
1#include "flanger_module.h"
2#include "delay.h"
3#include "memory.h"
4#include "synth_constants.h"
5
6namespace vital {
7
8 FlangerModule::FlangerModule(const Output* beats_per_second) :
9 SynthModule(0, kNumOutputs),
10 beats_per_second_(beats_per_second),
11 frequency_(nullptr), phase_offset_(nullptr), mod_depth_(nullptr),
12 phase_(0.0f), delay_(nullptr) { }
13
15
23 static constexpr int kMaxSamples = 40000;
24 static const cr::Value kDelayStyle(StereoDelay::kClampedUnfiltered);
25
26 delay_ = new StereoDelay(kMaxSamples);
28 phase_ = 0.0f;
30
31 Output* free_frequency = createMonoModControl("flanger_frequency");
32 frequency_ = createTempoSyncSwitch("flanger", free_frequency->owner, beats_per_second_, false);
33 center_ = createMonoModControl("flanger_center");
34 Output* feedback = createMonoModControl("flanger_feedback");
35 Output* wet = createMonoModControl("flanger_dry_wet");
36 mod_depth_ = createMonoModControl("flanger_mod_depth");
37
38 phase_offset_ = createMonoModControl("flanger_phase_offset");
39
40 // Plugging parameters into the StereoDelay
44 delay_->plug(&kDelayStyle, StereoDelay::kStyle);
45
47 }
48
49 void FlangerModule::processWithInput(const poly_float* audio_in, int num_samples) {
56 static constexpr float kMaxFrequency = 20000.0f;
57
58 SynthModule::process(num_samples);
59 poly_float frequency = frequency_->buffer[0];
60 poly_float delta_phase = (frequency * num_samples) / getSampleRate();
61 phase_ = utils::mod(phase_ + delta_phase);
62
63 poly_float phase_offset = phase_offset_->buffer[0];
64 poly_float right_offset = (phase_offset & constants::kRightMask);
65 poly_float phase_total = phase_ - phase_offset / 2.0f + right_offset;
66
67 // Compute modulation as a triangle waveform varying from -1 to 1, then map it with depth
68 poly_float mod = mod_depth_->buffer[0] * (utils::triangleWave(phase_total) * 2.0f - 1.0f) + 1.0f;
69
70 // Calculate the effective delay time from center (converted from MIDI note to frequency)
72 delay = (delay - kModulationDelayBuffer) * mod + kModulationDelayBuffer;
73 poly_float delay_frequency = poly_float(1.0f) / utils::max(delay, 1.0f / kMaxFrequency);
74
75 output(kFrequencyOutput)->buffer[0] = delay_frequency;
76 delay_frequency_.set(delay_frequency);
77 delay_->processWithInput(audio_in, num_samples);
78 }
79
86} // namespace vital
@ kClampedUnfiltered
Definition delay.h:88
virtual void processWithInput(const poly_float *audio_in, int num_samples) override
Processes a block of audio from a given input buffer.
Definition delay.cpp:83
@ kFrequency
Base delay frequency.
Definition delay.h:70
@ kFeedback
Feedback amount.
Definition delay.h:72
@ kWet
Wet mix amount.
Definition delay.h:69
@ kStyle
Delay style selection.
Definition delay.h:74
void processWithInput(const poly_float *audio_in, int num_samples) override
Processes audio through the flanger effect using the input buffer and number of samples.
Definition flanger_module.cpp:49
Output * frequency_
The flanger frequency parameter (possibly tempo-synced).
Definition flanger_module.h:97
FlangerModule(const Output *beats_per_second)
Constructs a FlangerModule linked to a beats-per-second output for tempo syncing.
Definition flanger_module.cpp:8
@ kAudioOutput
Definition flanger_module.h:30
@ kFrequencyOutput
Definition flanger_module.h:31
const Output * beats_per_second_
Reference output for tempo sync.
Definition flanger_module.h:96
StereoDelay * delay_
The stereo delay line used to create the flanging effect.
Definition flanger_module.h:104
cr::Value delay_frequency_
Internal parameter representing the current delay frequency set in StereoDelay.
Definition flanger_module.h:103
Output * phase_offset_
Controls stereo phase offset for left/right channels.
Definition flanger_module.h:98
Output * mod_depth_
Controls the depth (amount) of modulation applied to the delay time.
Definition flanger_module.h:100
virtual ~FlangerModule()
Destroys the FlangerModule and releases associated resources.
Definition flanger_module.cpp:14
void correctToTime(double seconds) override
Adjusts the internal modulation phase to align with a given time, useful for syncing to host time.
Definition flanger_module.cpp:80
void init() override
Initializes the FlangerModule by creating parameters and setting up the internal delay line.
Definition flanger_module.cpp:16
Output * center_
Controls the central delay time around which modulation occurs.
Definition flanger_module.h:99
static constexpr mono_float kModulationDelayBuffer
Definition flanger_module.h:21
poly_float phase_
Current modulation phase.
Definition flanger_module.h:101
void useOutput(Output *output)
Uses an existing Output object as this Processor's first output.
Definition processor.cpp:138
force_inline int getSampleRate() const
Retrieves the current (effective) sample rate.
Definition processor.h:326
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 init()
Called after constructor, used for any additional initialization. Subclasses can override....
Definition processor.h:258
virtual void addIdleProcessor(Processor *processor)
Adds a Processor that should remain idle (not processed) in the router.
Definition processor_router.cpp:146
A ProcessorRouter that encapsulates a cohesive unit of functionality in the synthesizer.
Definition synth_module.h:129
Output * createTempoSyncSwitch(std::string name, Processor *frequency, const Output *beats_per_second, bool poly, Input *midi=nullptr)
Creates a tempo sync switch that toggles between tempo-based frequency and free-running frequency.
Definition synth_module.cpp:289
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
virtual void set(poly_float value)
Sets the internal value to a new poly_float.
Definition value.cpp:17
A control-rate variant of the Value processor.
Definition value.h:82
Declares classes for time-domain memory storage and retrieval with cubic interpolation.
const poly_mask kRightMask
A mask identifying the right channel when comparing to kRightOne.
Definition synth_constants.h:263
force_inline poly_float mod(poly_float value)
Returns the fractional part of each lane by subtracting the floored value.
Definition poly_utils.h:814
force_inline poly_float triangleWave(poly_float t)
Generates a simple triangle wave [0..1] from a fraction t in [0..1].
Definition poly_utils.h:873
force_inline poly_float getCycleOffsetFromSeconds(double seconds, poly_float frequency)
Computes a cycle offset given a time in seconds and a frequency.
Definition poly_utils.h:885
force_inline poly_float max(poly_float left, poly_float right)
Returns the maximum of two poly_floats lane-by-lane.
Definition poly_utils.h:327
force_inline poly_float midiNoteToFrequency(poly_float value)
Converts a MIDI note to a frequency (vectorized).
Definition poly_utils.h:123
Contains classes and functions used within the Vital synthesizer framework.
Delay< StereoMemory > StereoDelay
StereoDelay is a Delay processor specialized with StereoMemory.
Definition delay.h:307
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
Processor * owner
Owning processor.
Definition processor.h:112
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600