Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
util.hpp
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#include "deluge/util/fixedpoint.h"
20#include "deluge/util/functions.h"
21#include <algorithm>
22#include <cmath>
23
24namespace deluge::dsp {
28constexpr q31_t FOLD_MIN = 0.1 * ONE_Q31;
29constexpr q31_t THREE_FOURTHS = 0.75 * ONE_Q31;
30inline q31_t fold(q31_t input, q31_t level) {
31 // no folding occurs if max is 0 or if max is greater than input
32 // to keep the knob range consistent fold starts from 0 and
33 // increases, decreasing would lead to a large deadspace until
34 // suddenly clipping occured
35 // note 9db loss
36 q31_t extra = 0;
37 q31_t max = level >> 8;
38 if (input > max) {
39 extra = input - max;
40 }
41 else if (input < -max) {
42 extra = input + max;
43 }
44 // this avoids inverting the wave
45 return 2 * extra - input;
46}
51inline q31_t polynomialOscillatorApproximation(q31_t x) {
52 // requires 1 to be ONE_Q31
53
54 q31_t x2 = 2 * multiply_32x32_rshift32(x, x);
55 q31_t x3 = 2 * multiply_32x32_rshift32(x2, x);
56 // this is 4(3*x/4 - x^3) which is a nice shape
57 q31_t r1 = 8 * (multiply_32x32_rshift32(THREE_FOURTHS, x) - x3);
58
59 q31_t r2 = 2 * multiply_32x32_rshift32(r1, r1);
60 q31_t r3 = 2 * multiply_32x32_rshift32(r2, r1);
61 // at this point we've applied the polynomial twice
62 q31_t out = 8 * (multiply_32x32_rshift32(THREE_FOURTHS, r1) - r3);
63
64 return out;
65}
66
67inline void foldBufferPolyApproximation(q31_t* startSample, q31_t* endSample, q31_t level) {
68 q31_t* currentSample = startSample;
69 q31_t fold_level = add_saturate(level, FOLD_MIN);
70
71 do {
72 q31_t c = *currentSample;
73
74 q31_t x = lshiftAndSaturateUnknown(multiply_32x32_rshift32(fold_level, c), 8);
75
76 // volume compensation
77 *currentSample = polynomialOscillatorApproximation(x) >> 7;
78
79 currentSample += 1;
80 } while (currentSample < endSample);
81}
85inline void foldBuffer(q31_t* startSample, q31_t* endSample, q31_t foldLevel) {
86 q31_t* currentSample = startSample;
87 do {
88 q31_t outs = fold(*currentSample, foldLevel);
89 // volume compensation
90 *currentSample = outs + 4 * multiply_32x32_rshift32(outs, foldLevel);
91
92 currentSample += 1;
93 } while (currentSample < endSample);
94}
95
104inline q31_t toQ31Saturating(float scaled) {
105 // (float)INT32_MAX rounds up to 2^31, which is itself out of range, so the ceiling has to
106 // be the largest float below it.
107 constexpr float kMax = 2147483520.f; // 0x7FFFFF80
108 constexpr float kMin = -2147483648.f;
109 return static_cast<q31_t>(std::clamp(scaled, kMin, kMax));
110}
111} // namespace deluge::dsp