Vital
Loading...
Searching...
No Matches
distortion.h
Go to the documentation of this file.
1#pragma once
2
3#include "processor.h"
4#include "futils.h"
5
6namespace vital {
7
16 class Distortion : public Processor {
17 public:
21 static constexpr mono_float kMaxDrive = 30.0f;
22
26 static constexpr mono_float kMinDrive = -30.0f;
27
31 static constexpr float kPeriodScale = 1.0f / 88200.0f;
32
36 static constexpr mono_float kMinDistortionMult = 32.0f / INT_MAX;
37
42 enum {
47 };
48
53 enum {
57 };
58
72
82
90 constexpr mono_float kDriveScale = 1.0f / (Distortion::kMaxDrive - Distortion::kMinDrive);
91
92 poly_float drive = utils::max(db - kMinDrive, 0.0f) * kDriveScale;
93 return utils::clamp(drive * drive, kMinDistortionMult, 1.0f);
94 }
95
105 constexpr mono_float kDriveScale = 1.0f / (Distortion::kMaxDrive - Distortion::kMinDrive);
106
107 // Normalize drive to [0..1], then invert and square it
108 poly_float drive = utils::max(db - Distortion::kMinDrive, 0.0f) * kDriveScale;
109 drive = -drive + 1.0f;
110 drive = poly_float(1.0f) / utils::clamp(drive * drive, Distortion::kMinDistortionMult, 1.0f);
111 return utils::max(drive * 0.99f, 1.0f) * Distortion::kPeriodScale;
112 }
113
121 static poly_float getDriveValue(int type, poly_float input_drive) {
122 if (type == kBitCrush)
123 return bitCrushScale(input_drive);
124 if (type == kDownSample)
125 return downSampleScale(input_drive);
126 return driveDbScale(input_drive);
127 }
128
137 static poly_float getDrivenValue(int type, poly_float value, poly_float drive);
138
142 Distortion();
143
145 virtual ~Distortion() { }
146
151 virtual Processor* clone() const override {
152 VITAL_ASSERT(false);
153 return nullptr;
154 }
155
161 virtual void process(int num_samples) override;
162
169 virtual void processWithInput(const poly_float* audio_in, int num_samples) override;
170
184 template<poly_float(*distort)(poly_float, poly_float), poly_float(*scale)(poly_float)>
185 void processTimeInvariant(int num_samples, const poly_float* audio_in, const poly_float* drive,
186 poly_float* audio_out);
187
196 void processDownSample(int num_samples, const poly_float* audio_in, const poly_float* drive,
197 poly_float* audio_out);
198
199 private:
203 poly_float last_distorted_value_;
204
208 poly_float current_samples_;
209
213 int type_;
214
215 JUCE_LEAK_DETECTOR(Distortion)
216 };
217} // namespace vital
A Processor that applies various types of distortion to an audio signal.
Definition distortion.h:16
static force_inline poly_float downSampleScale(poly_float db)
Scales a drive (dB) value for downsampling distortion.
Definition distortion.h:104
virtual void processWithInput(const poly_float *audio_in, int num_samples) override
Processes a block of audio using a provided input buffer.
Definition distortion.cpp:210
@ kAudio
Audio input buffer.
Definition distortion.h:43
@ kType
Distortion type (see Type enum)
Definition distortion.h:44
@ kNumInputs
Definition distortion.h:46
@ kDrive
Drive amount in dB.
Definition distortion.h:45
virtual Processor * clone() const override
Creates a clone of this Processor. (Not implemented for Distortion).
Definition distortion.h:151
static poly_float getDriveValue(int type, poly_float input_drive)
Converts an input drive in dB to a linear multiplier depending on distortion type.
Definition distortion.h:121
virtual void process(int num_samples) override
Processes a block of audio using the stored input buffer.
Definition distortion.cpp:253
static force_inline poly_float driveDbScale(poly_float db)
Scales a drive (dB) value into a linear multiplier for standard distortions.
Definition distortion.h:79
virtual ~Distortion()
Default destructor.
Definition distortion.h:145
static constexpr mono_float kMinDistortionMult
Minimum distortion multiplier used for certain distortion styles (e.g., bitcrush).
Definition distortion.h:36
static constexpr float kPeriodScale
Factor used to scale downsampling period relative to sample rate.
Definition distortion.h:31
static poly_float getDrivenValue(int type, poly_float value, poly_float drive)
Applies the specified distortion to a single sample given the drive multiplier.
Definition distortion.cpp:135
void processTimeInvariant(int num_samples, const poly_float *audio_in, const poly_float *drive, poly_float *audio_out)
Processes samples with a time-invariant distortion function (no dynamic changes).
Definition distortion.cpp:177
@ kDriveOut
Drive values used in calculations (for reference or debugging)
Definition distortion.h:55
@ kAudioOut
Distorted audio output.
Definition distortion.h:54
@ kNumOutputs
Definition distortion.h:56
void processDownSample(int num_samples, const poly_float *audio_in, const poly_float *drive, poly_float *audio_out)
Processes samples using a downsampling approach for distortion.
Definition distortion.cpp:188
static constexpr mono_float kMinDrive
Minimum allowed drive in decibels.
Definition distortion.h:26
Distortion()
Constructs a Distortion object with the default number of inputs/outputs.
Definition distortion.cpp:160
static constexpr mono_float kMaxDrive
Maximum allowed drive in decibels.
Definition distortion.h:21
Type
Distortion algorithms supported by this class.
Definition distortion.h:63
@ kSinFold
Sine-based waveform folding.
Definition distortion.h:67
@ kSoftClip
Soft clipping (tanh-based waveshaping)
Definition distortion.h:64
@ kHardClip
Hard clipping.
Definition distortion.h:65
@ kNumTypes
Definition distortion.h:70
@ kLinearFold
Linear waveform folding.
Definition distortion.h:66
@ kBitCrush
Bitcrushing (quantizing samples)
Definition distortion.h:68
@ kDownSample
Downsampling.
Definition distortion.h:69
static force_inline poly_float bitCrushScale(poly_float db)
Scales a drive (dB) value for bitcrush distortion (controls quantization level).
Definition distortion.h:89
Base class for all signal-processing units in Vital.
Definition processor.h:212
#define VITAL_ASSERT(x)
Definition common.h:11
#define force_inline
Definition common.h:23
Contains faster but less accurate versions of utility math functions, such as exponential,...
force_inline mono_float dbToMagnitude(mono_float decibels)
Converts decibels (dB) to magnitude (linear).
Definition futils.h:217
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 poly_float max(poly_float left, poly_float right)
Returns the maximum of two poly_floats lane-by-lane.
Definition poly_utils.h:327
Contains classes and functions used within the Vital synthesizer framework.
float mono_float
Definition common.h:33
Declares the Processor class and related structures for handling audio processing in a polyphonic con...
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600