Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
lpladder.h
1/*
2 * Copyright © 2015-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "dsp/filter/filter.h"
21#include "dsp/filter/ladder_components.h"
22#include "util/fixedpoint.h"
23
24namespace deluge::dsp::filter {
25
26class LpLadderFilter : public Filter<LpLadderFilter> {
27public:
28 LpLadderFilter() = default;
29 // returns a compensatory gain value
30 q31_t setConfig(q31_t hpfFrequency, q31_t hpfResonance, FilterMode lpfMode, q31_t lpfMorph, q31_t filterGain);
31 void doFilter(q31_t* outputSample, q31_t* endSample, int32_t sampleIncrememt);
32 void doFilterStereo(q31_t* startSample, q31_t* endSample);
33 void resetFilter() {
34 l.reset();
35 r.reset();
36 }
37
38private:
40 q31_t noiseLastValue;
45 void reset() {
46 lpfLPF1.reset();
47 lpfLPF2.reset();
48 lpfLPF3.reset();
49 lpfLPF4.reset();
50 }
51 };
52 // Per-render copy of the configuration. doFilter() works on local copies of the coefficients, the filter state and
53 // the noise seed, so the compiler can keep them in registers: stores to the sample buffer (q31_t*) may otherwise
54 // alias the q31_t members and the global noise seed, forcing reloads and stores of all of them every sample.
55 struct Coeffs {
56 q31_t moveability;
57 q31_t processedResonance;
58 q31_t divideByTotalMoveabilityAndProcessedResonance;
59 q31_t morph;
60 q31_t lpf1Feedback;
61 q31_t lpf2Feedback;
62 q31_t lpf3Feedback;
63 q31_t lpf4Feedback; // divideBy1PlusTannedFrequency
64 };
65 [[gnu::always_inline]] Coeffs getCoeffs() const {
66 return Coeffs{moveability,
67 processedResonance,
68 divideByTotalMoveabilityAndProcessedResonance,
69 morph,
70 lpf1Feedback,
71 lpf2Feedback,
72 lpf3Feedback,
73 divideBy1PlusTannedFrequency};
74 }
75
76 // kSaturate: morph > 0 || processedResonance > 510000000 (loop invariant, so decided once per buffer)
77 // kFeedback: false only if processedResonance == 0 and divideByTotalMoveabilityAndProcessedResonance == 1 << 30.
78 // multiply_32x32_rshift32_rounded(feedbacksSum, 0) is exactly 0 for any feedbacksSum, so skipping the feedback
79 // path is bit-exact.
80 template <bool kSaturate, bool kFeedback>
81 [[gnu::always_inline]] static inline q31_t scaleInput(q31_t input, q31_t feedbacksSum, const Coeffs& c) {
82 q31_t temp;
83 if constexpr (kFeedback) {
84 q31_t in = input - (multiply_32x32_rshift32_rounded(feedbacksSum, c.processedResonance) << 3);
85 temp = multiply_32x32_rshift32_rounded(in, c.divideByTotalMoveabilityAndProcessedResonance) << 2;
86 }
87 else {
88 // Only used when divideByTotalMoveabilityAndProcessedResonance == 1 << 30, which is what setConfig()
89 // produces whenever processedResonance ends up 0. multiply_32x32_rshift32_rounded(input, 1 << 30) << 2
90 // is floor((input + 2) / 4) * 4, i.e. input rounded to a multiple of 4.
91 temp = (q31_t)(((uint32_t)input + 2u) & ~3u);
92 }
93 if constexpr (kSaturate) {
94 q31_t extra = 2 * multiply_32x32_rshift32(input, c.morph);
95 temp = getTanHUnknown(temp + extra, 2);
96 }
97 return temp;
98 }
99 template <bool kSaturate, bool kFeedback>
100 [[gnu::always_inline]] static inline q31_t do24dBLPFOnSample(q31_t input, LpLadderState& state, const Coeffs& c,
101 uint32_t& seed);
102 template <bool kSaturate, bool kFeedback>
103 [[gnu::always_inline]] static inline q31_t do12dBLPFOnSample(q31_t input, LpLadderState& state, const Coeffs& c,
104 uint32_t& seed);
105 template <bool kSaturate, bool kFeedback>
106 [[gnu::always_inline]] static inline q31_t doDriveLPFOnSample(q31_t input, LpLadderState& state, const Coeffs& c,
107 uint32_t& seed);
108 template <bool kSaturate, bool kFeedback>
109 void doFilterT(q31_t* startSample, q31_t* endSample, int32_t sampleIncrement);
110 template <bool kSaturate, bool kFeedback>
111 void doFilterStereoT(q31_t* startSample, q31_t* endSample);
112 // all ladders are in this class to share the basic components
113 // this differentiates between them
114 FilterMode lpfMode;
115
116 // state
119
120 // configuration
121 q31_t processedResonance;
122 q31_t divideByTotalMoveabilityAndProcessedResonance;
123
124 // moveability is tan(f)/(1+tan(f))
125 q31_t moveability;
126
127 q31_t morph;
128
129 // All feedbacks have 1 represented as 1073741824
130 q31_t lpf1Feedback;
131 q31_t lpf2Feedback;
132 q31_t lpf3Feedback;
133
134 bool doOversampling;
135};
136} // namespace deluge::dsp::filter
Definition ladder_components.h:23