11template <
typename SampleType =
float>
17 void set_drive(SampleType drive) { drive_ = drive; }
19 SampleType
process(SampleType input)
const {
20 const SampleType x = input * drive_;
22 case Curve::soft_clip:
23 return shape<Curve::soft_clip>(x);
24 case Curve::hard_clip:
25 return shape<Curve::hard_clip>(x);
26 case Curve::tanh_clip:
27 return shape<Curve::tanh_clip>(x);
29 return shape<Curve::fold>(x);
30 case Curve::sine_fold:
31 return shape<Curve::sine_fold>(x);
49 case Curve::soft_clip:
50 return run<Curve::soft_clip>(
buffer, num_samples, drive_);
51 case Curve::hard_clip:
52 return run<Curve::hard_clip>(
buffer, num_samples, drive_);
53 case Curve::tanh_clip:
54 return run<Curve::tanh_clip>(
buffer, num_samples, drive_);
56 return run<Curve::fold>(
buffer, num_samples, drive_);
57 case Curve::sine_fold:
58 return run<Curve::sine_fold>(
buffer, num_samples, drive_);
60 for (
int i = 0;
i < num_samples; ++
i)
66 template <Curve C>
static SampleType shape(SampleType x) {
67 if constexpr (C == Curve::soft_clip) {
68 return x / (SampleType{1.0f} + std::abs(x));
69 }
else if constexpr (C == Curve::hard_clip) {
70 return std::clamp(x, SampleType{-1.0f}, SampleType{1.0f});
71 }
else if constexpr (C == Curve::tanh_clip) {
73 }
else if constexpr (C == Curve::fold) {
75 while (x > SampleType{1.0f} ||
x < SampleType{-1.0f}) {
76 if (x > SampleType{1.0f})
77 x = SampleType{2.0f} -
x;
78 if (x < SampleType{-1.0f})
79 x = SampleType{-2.0f} -
x;
83 return std::sin(x * SampleType{1.5707963f});
87 template <Curve C>
static void run(SampleType*
buffer,
int num_samples, SampleType drive) {
92 if (drive == SampleType{1.0f}) {
93 for (
int i = 0;
i < num_samples; ++
i)
97 for (
int i = 0;
i < num_samples; ++
i)
101 Curve curve_ = Curve::tanh_clip;
102 SampleType drive_ = SampleType{1.0f};
Definition waveshaper.hpp:12
void process(SampleType *buffer, int num_samples) const
Definition waveshaper.hpp:47
void set_drive(SampleType drive)
Definition waveshaper.hpp:17
SampleType process(SampleType input) const
Definition waveshaper.hpp:19
Curve
Definition waveshaper.hpp:14
@ fold
Definition waveshaper.hpp:14
@ hard_clip
Definition waveshaper.hpp:14
@ sine_fold
Definition waveshaper.hpp:14
@ soft_clip
Definition waveshaper.hpp:14
@ tanh_clip
Definition waveshaper.hpp:14
void set_curve(Curve c)
Definition waveshaper.hpp:16
Definition additive_bank.hpp:218
@ buffer
A borrowed static buffer with a traversable playhead.
Definition granular.hpp:110
WaveShaperT< float > WaveShaper
Definition waveshaper.hpp:105
@ c
Definition spectrum_trace.hpp:35
@ i
Definition formant_filter_bank.hpp:20
WaveShaperT< double > WaveShaper64
Definition waveshaper.hpp:106
@ x
Definition input_events.hpp:41