Debian Codegraph @

plugins/dragonfly-plate-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 NRev::NRev() : fv3::nrev_f() { }
26
27 void NRev::setDampLpf(float value) {
28 dampLpf = limFs2(value);
29 dampLpfL.setLPF_BW(dampLpf, getTotalSampleRate());
30 dampLpfR.setLPF_BW(dampLpf, getTotalSampleRate());
31 }
32
33 void NRev::mute() {
34 fv3::nrev_f::mute();
35 dampLpfL.mute();
36 dampLpfR.mute();
37 }
38
39 void NRev::setFsFactors() {
40 fv3::nrev_f::setFsFactors();
41 setDampLpf(dampLpf);
42 }
43
44 void NRev::processloop2(long count, float *inputL, float *inputR, float *outputL, float *outputR) {
45 float outL, outR;
46 while(count-- > 0)
47 {
48 outL = outR = 0;
49 hpf = damp3_1*inDCC(*inputL + *inputR) - damp3*hpf;
50 UNDENORMAL(hpf);
51
52 hpf *= FV3_NREV_SCALE_WET;
53
54 for(long i = 0;i < FV3_NREV_NUM_COMB;i ++) outL += combL[i]._process(hpf);
55 for(long i = 0;i < 3;i ++) outL = allpassL[i]._process_ov(outL);
56 lpfL = dampLpfL(damp2*lpfL + damp2_1*outL); UNDENORMAL(lpfL);
57 outL = allpassL[3]._process_ov(lpfL);
58 outL = allpassL[5]._process_ov(outL);
59 outL = delayWL(lLDCC(outL));
60
61 for(long i = 0;i < FV3_NREV_NUM_COMB;i ++) outR += combR[i]._process(hpf);
62 for(long i = 0;i < 3;i ++) outR = allpassR[i]._process_ov(outR);
63 lpfR = dampLpfR(damp2*lpfR + damp2_1*outR); UNDENORMAL(lpfR);
64 outR = allpassR[3]._process_ov(lpfR);
65 outR = allpassR[6]._process_ov(outR);
66 outR = delayWR(lRDCC(outR));
67
68 *outputL = outL*wet1 + outR*wet2 + delayL(*inputL)*dry;
69 *outputR = outR*wet1 + outL*wet2 + delayR(*inputR)*dry;
70 inputL ++; inputR ++; outputL ++; outputR ++;
71 }
72 }
73
74
75 NRevB::NRevB() : fv3::nrevb_f() { }
76
77 void NRevB::setDampLpf(float value)
78 {
79 dampLpf = limFs2(value);
80 dampLpfL.setLPF_BW(dampLpf, getTotalSampleRate());
81 dampLpfR.setLPF_BW(dampLpf, getTotalSampleRate());
82 }
83
84 void NRevB::mute() {
85 fv3::nrevb_f::mute();
86 dampLpfL.mute();
87 dampLpfR.mute();
88 }
89
90 void NRevB::setFsFactors() {
91 fv3::nrevb_f::setFsFactors();
92 setDampLpf(dampLpf);
93 }
94
95 void NRevB::processloop2(long count, float *inputL, float *inputR, float *outputL, float *outputR) {
96 float outL, outR, tmpL, tmpR;
97 while(count-- > 0)
98 {
99 hpf = damp3_1*inDCC.process(*inputL + *inputR) - damp3*hpf; UNDENORMAL(hpf);
100 outL = outR = tmpL = tmpR = hpf;
101
102 outL += apfeedback*lastL;
103 lastL += -1*apfeedback*outL;
104
105 for(long i = 0;i < FV3_NREV_NUM_COMB;i ++) outL += combL[i]._process(tmpL);
106 for(long i = 0;i < FV3_NREVB_NUM_COMB_2;i ++) outL += comb2L[i]._process(tmpL);
107 for(long i = 0;i < 3;i ++) outL = allpassL[i]._process(outL);
108 for(long i = 0;i < FV3_NREVB_NUM_ALLPASS_2;i ++) outL = allpass2L[i]._process(outL);
109 lpfL = dampLpfL(damp2*lpfL + damp2_1*outL); UNDENORMAL(lpfL);
110 outL = allpassL[3]._process(lpfL); outL = allpassL[5]._process(outL);
111 outL = lLDCC(outL);
112
113 outR += apfeedback*lastR;
114 lastR += -1*apfeedback*outR;
115 for(long i = 0;i < FV3_NREV_NUM_COMB;i ++) outR += combR[i]._process(tmpR);
116 for(long i = 0;i < FV3_NREVB_NUM_COMB_2;i ++) outR += comb2R[i]._process(tmpR);
117 for(long i = 0;i < 3;i ++) outR = allpassR[i]._process(outR);
118 for(long i = 0;i < FV3_NREVB_NUM_ALLPASS_2;i ++) outR = allpass2R[i]._process(outR);
119 lpfR = dampLpfR(damp2*lpfR + damp2_1*outR); UNDENORMAL(lpfR);
120 outR = allpassR[3]._process(lpfR); outR = allpassL[6]._process(outR);
121 outR = lRDCC(outR);
122
123 lastL = FV3_NREVB_SCALE_WET*delayWL(lastL);
124 lastR = FV3_NREVB_SCALE_WET*delayWR(lastR);
125 *outputL = lastL*wet1 + lastR*wet2 + delayL(*inputL)*dry;
126 *outputR = lastR*wet1 + lastL*wet2 + delayR(*inputR)*dry;
127 lastL = outL; lastR = outR;
128 inputL ++; inputR ++; outputL ++; outputR ++;
129 }
130 }
131
132
133 DragonflyReverbDSP::DragonflyReverbDSP(double sampleRate) {
134 input_lpf_0.mute();
135 input_lpf_1.mute();
136 input_hpf_0.mute();
137 input_hpf_1.mute();
138
139 nrev.setdryr(0);
140 nrev.setwetr(1);
141 nrev.setMuteOnChange(false);
142 nrev.setSampleRate(sampleRate);
143
144 nrevb.setdryr(0);
145 nrevb.setwetr(1);
146 nrevb.setMuteOnChange(false);
147 nrevb.setSampleRate(sampleRate);
148
149 strev.setdryr(0);
150 strev.setwetr(1);
151 strev.setMuteOnChange(false);
152 strev.setdccutfreq(6);
153 strev.setspinlimit(12);
154 strev.setspindiff(0.15);
155 strev.setSampleRate(sampleRate);
156
157 model = &nrevb;
158
159 for (uint32_t param = 0; param < paramCount; param++) {
160 newParams[param] = presets[DEFAULT_PRESET].params[param];
161 oldParams[param] = -1.0;
162 }
163
164 sampleRateChanged(sampleRate);
165 }
166
167 float DragonflyReverbDSP::getParameterValue(uint32_t index) const {
168 if (index < paramCount) {
169 return newParams[index];
170 }
171
172 return 0.0;
173 }
174
175 void DragonflyReverbDSP::setParameterValue(uint32_t index, float value) {
176 if (index < paramCount) {
177 newParams[index] = value;
178 }
179 }
180
181 void DragonflyReverbDSP::run(const float** inputs, float** outputs, uint32_t frames) {
182 const ScopedDenormalDisable sdd;
183 for (uint32_t index = 0; index < paramCount; index++) {
184 if (d_isNotEqual(oldParams[index], newParams[index])) {
185 oldParams[index] = newParams[index];
186 float value = newParams[index];
187
188 switch(index) {
189 case paramDry: dryLevel = (value / 100.0); break;
190 case paramWet: wetLevel = (value / 100.0); break;
191 case paramWidth: strev.setwidth (value / 120.0);
192 nrev.setwidth (value / 120.0);
193 nrevb.setwidth (value / 120.0); break;
194 case paramPredelay:
195 // Freeverb doesn't handle zero predelay properly
196 // Instead of modifying the library, avoid it here
197 if (value < 0.1) {
198 value = 0.1;
199 }
200 strev.setPreDelay (value);
201 nrev.setPreDelay (value);
202 nrevb.setPreDelay (value);
203 break;
204 case paramDecay: strev.setrt60 (value);
205 nrev.setrt60 (value);
206 nrevb.setrt60 (value); break;
207 case paramLowCut: setInputHPF (value); break;
208 case paramHighCut: setInputLPF (value); break;
209 case paramDamp: nrev.setDampLpf (value);
210 nrevb.setDampLpf (value);
211 strev.setdamp (value);
212 strev.setoutputdamp (fmax(value * 2.0, 16000)); break;
213 }
214
215 if (index == paramAlgorithm) {
216 fv3::revbase_f *previous = model;
217 int algorithm = value;
218 if (algorithm == ALGORITHM_NREV) {
219 model = &nrev;
220 } else if (algorithm == ALGORITHM_NREV_B) {
221 model = &nrevb;
222 } else if (algorithm == ALGORITHM_STREV) {
223 model = &strev;
224 }
225 if (model != previous) {
226 previous->mute();
227 }
228 }
229 }
230 }
231
232 for (uint32_t offset = 0; offset < frames; offset += BUFFER_SIZE) {
233 long int buffer_frames = frames - offset < BUFFER_SIZE ? frames - offset : BUFFER_SIZE;
234
235 for (uint32_t i = 0; i < buffer_frames; i++) {
236 filtered_input_buffer[0][i] = input_lpf_0.process(input_hpf_0.process(inputs[0][offset + i]));
237 filtered_input_buffer[1][i] = input_lpf_1.process(input_hpf_1.process(inputs[1][offset + i]));
238 }
239
240 model->processreplace(
241 const_cast<float *>(filtered_input_buffer[0]),
242 const_cast<float *>(filtered_input_buffer[1]),
243 output_buffer[0],
244 output_buffer[1],
245 buffer_frames
246 );
247
248 for (uint32_t i = 0; i < buffer_frames; i++) {
249 outputs[0][offset + i] =
250 dryLevel * inputs[0][offset + i] +
251 wetLevel * output_buffer[0][i];
252
253 outputs[1][offset + i] =
254 dryLevel * inputs[1][offset + i] +
255 wetLevel * output_buffer[1][i];
256 }
257
258 }
259 }
260
261 void DragonflyReverbDSP::sampleRateChanged(double newSampleRate) {
262 sampleRate = newSampleRate;
263 nrev.setSampleRate(newSampleRate);
264 nrevb.setSampleRate(newSampleRate);
265 strev.setSampleRate(newSampleRate);
266 setInputLPF(newParams[paramHighCut]);
267 setInputHPF(newParams[paramLowCut]);
268 }
269
270 void DragonflyReverbDSP::mute() {
271 nrev.mute();
272 nrevb.mute();
273 strev.mute();
274 }
275
276 void DragonflyReverbDSP::setInputLPF(float freq) {
277 if (freq < 0) {
278 freq = 0;
279 } else if (freq > sampleRate / 2.0) {
280 freq = sampleRate / 2.0;
281 }
282
283 input_lpf_0.setLPF_BW(freq, sampleRate);
284 input_lpf_1.setLPF_BW(freq, sampleRate);
285 }
286
287 void DragonflyReverbDSP::setInputHPF(float freq) {
288 if (freq < 0) {
289 freq = 0;
290 } else if (freq > sampleRate / 2.0) {
291 freq = sampleRate / 2.0;
292 }
293
294 input_hpf_0.setHPF_BW(freq, sampleRate);
295 input_hpf_1.setHPF_BW(freq, sampleRate);
296 }