Debian Codegraph @

plugins/dragonfly-room-reverb/DSP.cpp

raw
1 /*
2 * Dragonfly Reverb, copyright (c) 2019 Michael Willis, Rob van den Berg
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 3 of
7 * the License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * For a full copy of the GNU General Public License see the LICENSE file.
15 */
16
17 #include "math.h"
18
19 #include "DistrhoPlugin.hpp"
20 #include "DistrhoPluginInfo.h"
21 #include "extra/ScopedDenormalDisable.hpp"
22
23 #include "DSP.hpp"
24
25 // Increase the late level by approx 8dB
26 #define LATE_GAIN 2.5f
27
28 DragonflyReverbDSP::DragonflyReverbDSP(double sampleRate) {
29 input_lpf_0.mute();
30 input_lpf_1.mute();
31 input_hpf_0.mute();
32 input_hpf_1.mute();
33
34 early.loadPresetReflection(FV3_EARLYREF_PRESET_1);
35 early.setMuteOnChange(false);
36 early.setdryr(0); // mute dry signal
37 early.setwet(0); // 0dB
38 early.setwidth(0.8);
39 early.setLRDelay(0.3);
40 early.setLRCrossApFreq(750, 4);
41 early.setDiffusionApFreq(150, 4);
42 early.setSampleRate(sampleRate);
43 early_send = 0.20;
44
45 late.setMuteOnChange(false);
46 late.setwet(0); // 0dB
47 late.setdryr(0); // mute dry signal
48 late.setwidth(1.0);
49 late.setSampleRate(sampleRate);
50
51 for (uint32_t param = 0; param < paramCount; param++) {
52 newParams[param] = banks[DEFAULT_BANK].presets[DEFAULT_PRESET].params[param];
53 oldParams[param] = -1.0;
54 }
55
56 sampleRateChanged(sampleRate);
57 }
58
59 float DragonflyReverbDSP::getParameterValue(uint32_t index) const {
60 if (index < paramCount) {
61 return newParams[index];
62 }
63
64 return 0.0;
65 }
66
67 void DragonflyReverbDSP::setParameterValue(uint32_t index, float value) {
68 if (index < paramCount) {
69 newParams[index] = value;
70 }
71 }
72
73 void DragonflyReverbDSP::run(const float** inputs, float** outputs, uint32_t frames) {
74 const ScopedDenormalDisable sdd;
75 for (uint32_t index = 0; index < paramCount; index++) {
76 if (d_isNotEqual(oldParams[index], newParams[index])) {
77 oldParams[index] = newParams[index];
78 float value = newParams[index];
79
80 switch(index) {
81 case paramDry: dryLevel = (value / 100.0); break;
82 case paramEarly: earlyLevel = (value / 100.0); break;
83 case paramEarlySend: early_send = (value / 100.0); break;
84 case paramLate: lateLevel = (value / 100.0); break;
85 case paramSize: early.setRSFactor (value / 10.0);
86 late.setRSFactor (value / 10.0);
87 late.setbassboost( newParams[paramBoost] / 20.0 / pow(newParams[paramDecay], 1.5) * (newParams[paramSize] / 10.0) ); break;
88 case paramWidth: early.setwidth (value / 120.0);
89 late.setwidth (value / 100.0); break;
90 case paramPredelay:
91 // Freeverb doesn't handle zero predelay properly
92 // Instead of modifying the library, avoid it here
93 if (value < 0.1) {
94 value = 0.1;
95 }
96 late.setPreDelay (value);
97 break;
98 case paramDecay: late.setrt60 (value);
99 late.setbassboost( newParams[paramBoost] / 20.0 / pow(newParams[paramDecay], 1.5) * (newParams[paramSize] / 10.0) ); break;
100 case paramDiffuse: late.setidiffusion1(value / 120.0);
101 late.setodiffusion1(value / 120.0); break;
102 case paramSpin: late.setspin (value);
103 late.setspin2 (std::sqrt(100.0 - (10.0 - value) * (10.0 - value)) / 2.0);
104 break;
105 case paramWander: late.setwander (value / 200.0 + 0.1);
106 late.setwander2 (value / 200.0 + 0.1); break;
107 case paramInHighCut: setInputLPF (value); break;
108 case paramEarlyDamp: early.setoutputlpf (value); break;
109 case paramLateDamp: late.setdamp (value);
110 late.setoutputdamp (value); break;
111 case paramBoost: late.setbassboost( newParams[paramBoost] / 20.0 / pow(newParams[paramDecay], 1.5) * (newParams[paramSize] / 10.0) ); break;
112 case paramBoostLPF: late.setdamp2 (newParams[paramBoostLPF]); break;
113 case paramInLowCut: setInputHPF (value); break;
114 }
115 }
116 }
117
118 for (uint32_t offset = 0; offset < frames; offset += BUFFER_SIZE) {
119 long int buffer_frames = frames - offset < BUFFER_SIZE ? frames - offset : BUFFER_SIZE;
120
121 for (uint32_t i = 0; i < buffer_frames; i++) {
122 filtered_input_buffer[0][i] = input_lpf_0.process(input_hpf_0.process(inputs[0][offset + i]));
123 filtered_input_buffer[1][i] = input_lpf_1.process(input_hpf_1.process(inputs[1][offset + i]));
124 }
125
126 early.processreplace(
127 const_cast<float *>(filtered_input_buffer[0]),
128 const_cast<float *>(filtered_input_buffer[1]),
129 early_out_buffer[0],
130 early_out_buffer[1],
131 buffer_frames);
132
133 for (uint32_t i = 0; i < buffer_frames; i++) {
134 late_in_buffer[0][i] = early_send * early_out_buffer[0][i] + filtered_input_buffer[0][i];
135 late_in_buffer[1][i] = early_send * early_out_buffer[1][i] + filtered_input_buffer[1][i];
136 }
137
138 late.processreplace(
139 const_cast<float *>(late_in_buffer[0]),
140 const_cast<float *>(late_in_buffer[1]),
141 late_out_buffer[0],
142 late_out_buffer[1],
143 buffer_frames);
144
145 for (uint32_t i = 0; i < buffer_frames; i++) {
146 outputs[0][offset + i] = dryLevel * inputs[0][offset + i];
147 outputs[1][offset + i] = dryLevel * inputs[1][offset + i];
148 }
149
150 if( earlyLevel > 0.0 ){
151 for (uint32_t i = 0; i < buffer_frames; i++) {
152 outputs[0][offset + i] += earlyLevel * early_out_buffer[0][i];
153 outputs[1][offset + i] += earlyLevel * early_out_buffer[1][i];
154 }
155 }
156
157 if( lateLevel > 0.0 ){
158 for (uint32_t i = 0; i < buffer_frames; i++) {
159 outputs[0][offset + i] += lateLevel * late_out_buffer[0][i];
160 outputs[1][offset + i] += lateLevel * late_out_buffer[1][i];
161 }
162 }
163 }
164 }
165
166
167 void DragonflyReverbDSP::sampleRateChanged(double newSampleRate) {
168 sampleRate = newSampleRate;
169 early.setSampleRate(newSampleRate);
170 late.setSampleRate(newSampleRate);
171 setInputLPF(newParams[paramInHighCut]);
172 setInputHPF(newParams[paramInLowCut]);
173 }
174
175 void DragonflyReverbDSP::mute() {
176 early.mute();
177 late.mute();
178 }
179
180 void DragonflyReverbDSP::setInputLPF(float freq) {
181 if (freq < 0) {
182 freq = 0;
183 } else if (freq > sampleRate / 2.0) {
184 freq = sampleRate / 2.0;
185 }
186
187 input_lpf_0.setLPF_BW(freq, sampleRate);
188 input_lpf_1.setLPF_BW(freq, sampleRate);
189 }
190
191 void DragonflyReverbDSP::setInputHPF(float freq) {
192 if (freq < 0) {
193 freq = 0;
194 } else if (freq > sampleRate / 2.0) {
195 freq = sampleRate / 2.0;
196 }
197
198 input_hpf_0.setHPF_BW(freq, sampleRate);
199 input_hpf_1.setHPF_BW(freq, sampleRate);
200 }