-
Notifications
You must be signed in to change notification settings - Fork 12
/
Delay.h
373 lines (317 loc) · 12.3 KB
/
Delay.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* SurgeXT for VCV Rack - a Surge Synth Team product
*
* A set of modules expressing Surge XT into the VCV Rack Module Ecosystem
*
* Copyright 2019 - 2024, Various authors, as described in the github
* transaction log.
*
* Surge XT for VCV Rack is released under the GNU General Public License
* 3.0 or later (GPL-3.0-or-later). A copy of the license is in this
* repository in the file "LICENSE" or at:
*
* or at https://www.gnu.org/licenses/gpl-3.0.en.html
*
* All source for Surge XT for VCV Rack is available at
* https://github.com/surge-synthesizer/surge-rack/
*/
#ifndef SURGE_XT_RACK_SRC_DELAY_H
#define SURGE_XT_RACK_SRC_DELAY_H
#include "SurgeXT.h"
#include "XTModule.h"
#include "rack.hpp"
#include <cstring>
#include "DebugHelpers.h"
#include "globals.h"
#include "BiquadFilter.h"
#include "dsp/utilities/SSESincDelayLine.h"
#include "TemposyncSupport.h"
#include "sst/rackhelpers/neighbor_connectable.h"
namespace sst::surgext_rack::delay
{
struct Delay : modules::XTModule, sst::rackhelpers::module_connector::NeighborConnectable_V1
{
static constexpr int n_delay_params{10};
static constexpr int n_mod_inputs{4};
enum ParamIds
{
TIME_L,
TIME_R,
TIME_S,
FEEDBACK,
CROSSFEED,
LOCUT,
HICUT,
MODRATE,
MODDEPTH,
MIX,
DELAY_MOD_PARAM_0,
CLIP_MODE_PARAM = DELAY_MOD_PARAM_0 + n_delay_params * n_mod_inputs,
NUM_PARAMS
};
enum InputIds
{
INPUT_L,
INPUT_R,
INPUT_CLOCK,
DELAY_MOD_INPUT,
NUM_INPUTS = DELAY_MOD_INPUT + n_mod_inputs,
};
enum OutputIds
{
OUTPUT_L,
OUTPUT_R,
NUM_OUTPUTS
};
enum LightIds
{
NUM_LIGHTS
};
static int modulatorIndexFor(int baseParam, int modulator)
{
int offset = baseParam - TIME_L;
return DELAY_MOD_PARAM_0 + offset * n_mod_inputs + modulator;
}
std::optional<std::vector<labeledStereoPort_t>> getPrimaryInputs() override
{
return {{std::make_pair("Input", std::make_pair(INPUT_L, INPUT_R))}};
}
std::optional<std::vector<labeledStereoPort_t>> getPrimaryOutputs() override
{
return {{std::make_pair("Output", std::make_pair(OUTPUT_L, OUTPUT_R))}};
}
modules::MonophonicModulationAssistant<Delay, n_delay_params, TIME_L, n_mod_inputs,
DELAY_MOD_INPUT>
modulationAssistant;
typedef modules::ClockProcessor<Delay> clockProcessor_t;
clockProcessor_t clockProc;
enum ClipMode
{
TRANSPARENT,
SOFTCLIP_DELAYLINE_5V,
SOFTCLIP_DELAYLINE_10V,
HARDCLIP_DELAYLINE_10V
};
struct DelayTimeParamQuantity : public rack::engine::ParamQuantity
{
inline Delay *dm() { return static_cast<Delay *>(module); }
virtual std::string getDisplayValueString() override
{
auto m = dm();
if (!m)
return "ERROR";
auto v = getValue();
if (m->tempoSync)
{
return temposync_support::temposyncLabel(v);
}
else
{
auto tl = m->storage->note_to_pitch_ignoring_tuning(12 * v);
tl = std::clamp(m->storage->samplerate * tl, 0.f, delayLineLength * 1.f) *
m->storage->samplerate_inv;
return fmt::format("{:7.3f} s", tl);
}
return "ERROR";
}
void setDisplayValue(float displayValue) override
{
auto dv = std::max(displayValue, 0.00001f);
setValue(log2(dv));
}
};
struct QuadRateParamQuantity : public rack::engine::ParamQuantity
{
virtual std::string getDisplayValueString() override
{
auto v = getValue();
return fmt::format("{:6.2f} Hz", v * v);
}
void setDisplayValue(float displayValue) override
{
auto dv = std::max(displayValue, 0.f);
setValue(sqrt(dv));
}
};
Delay() : XTModule()
{
setupSurgeCommon(NUM_PARAMS, false, false);
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
configParam<DelayTimeParamQuantity>(TIME_L, log2(0.01), log2(10.0), 0, "Left Delay");
configParam<DelayTimeParamQuantity>(TIME_R, log2(0.01), log2(10.0), 0, "Right Delay");
configParam(TIME_S, -1, 1, 0, "Time Tweak", "%", 0, 2);
configParam(FEEDBACK, 0, 1, .5, "Feedback", "%", 0, 100);
configParam(CROSSFEED, 0, 1, 0, "CrossFeed", "%", 0, 100);
configParam<modules::MidiNoteParamQuantity<69>>(LOCUT, -60, 70, -60, "LoCut");
configParam<modules::MidiNoteParamQuantity<69>>(HICUT, -60, 70, 70, "HiCut");
configParam<QuadRateParamQuantity>(MODRATE, 0, 4, 2, "ModRate"); // 0 - 16 hz quadratic
configParam(MODDEPTH, 0, 1, 0, "ModDepth", "%", 0, 100);
configParam(MIX, 0, 1, 1, "Mix", "%", 0, 100);
for (int i = 0; i < n_delay_params * n_mod_inputs; ++i)
{
int tp = paramModulatedBy(i + DELAY_MOD_PARAM_0);
auto lb = paramQuantities[tp]->getLabel();
std::string name = std::string("Mod ") + std::to_string(i % 4 + 1) + " to " + lb;
configParamNoRand(DELAY_MOD_PARAM_0 + i, -1, 1, 0, name, "%", 0, 100);
}
configParamNoRand(CLIP_MODE_PARAM, TRANSPARENT, HARDCLIP_DELAYLINE_10V,
HARDCLIP_DELAYLINE_10V, "Clip Mode");
configInput(INPUT_L, "Left");
configInput(INPUT_R, "Right");
configInput(INPUT_CLOCK, "Clock/BPM");
for (int i = 0; i < n_mod_inputs; ++i)
configInput(DELAY_MOD_INPUT + i, std::string("Mod ") + std::to_string(i + 1));
configOutput(OUTPUT_L, "Left");
configOutput(OUTPUT_R, "Right");
lineL = std::make_unique<SSESincDelayLine<delayLineLength>>(storage->sinctable);
lineR = std::make_unique<SSESincDelayLine<delayLineLength>>(storage->sinctable);
lpPost = std::make_unique<BiquadFilter>(storage.get());
lpPost->suspend();
hpPost = std::make_unique<BiquadFilter>(storage.get());
hpPost->suspend();
modulationAssistant.initialize(this);
configBypass(INPUT_L, OUTPUT_L);
configBypass(INPUT_R, OUTPUT_R);
}
std::string getName() override { return "Delay"; }
float modulationDisplayValue(int paramId) override
{
int idx = paramId - TIME_L;
if (idx < 0 || idx >= n_delay_params)
return 0;
return modulationAssistant.modvalues[idx] * modulationAssistant.fInv[idx];
}
static constexpr size_t delayLineLength = 1 << 19;
std::unique_ptr<SSESincDelayLine<delayLineLength>> lineL, lineR;
std::unique_ptr<BiquadFilter> lpPost, hpPost;
static int paramModulatedBy(int modIndex)
{
int offset = modIndex - DELAY_MOD_PARAM_0;
if (offset >= n_mod_inputs * (n_delay_params + 1) || offset < 0)
return -1;
return offset / n_mod_inputs;
}
bool tempoSync{false};
void activateTempoSync() { tempoSync = true; }
void deactivateTempoSync() { tempoSync = false; }
static constexpr int slowUpdate{8};
int blockPos{0};
float tsL{0}, tsR{0};
float modVal{0}, dMod{0}, modPhase{0};
ClipMode currentClipMode{HARDCLIP_DELAYLINE_10V};
void process(const ProcessArgs &args) override
{
// auto fpuguard = sst::plugininfra::cpufeatures::FPUStateGuard();
if (inputs[INPUT_CLOCK].isConnected())
clockProc.process(this, INPUT_CLOCK);
else
clockProc.disconnect(this);
if (blockPos == slowUpdate)
{
modulationAssistant.setupMatrix(this);
blockPos = 0;
currentClipMode = (ClipMode)std::round(params[CLIP_MODE_PARAM].getValue());
lpPost->coeff_LP2B(lpPost->calc_omega(modulationAssistant.values[HICUT] / 12.0), 0.707);
hpPost->coeff_HP(lpPost->calc_omega(modulationAssistant.values[LOCUT] / 12.0), 0.707);
auto modFreq = std::clamp(modulationAssistant.values[MODRATE], 0.f, 4.f);
modFreq = modFreq * modFreq;
// 0 -> 16 hz
auto dPhase = slowUpdate * storage->samplerate_inv * modFreq;
modPhase += dPhase;
if (modPhase > 1)
modPhase -= 1;
float modTarget = std::sin(modPhase * 2.0 * M_PI);
dMod = (modTarget - modVal) / slowUpdate;
if (tempoSync)
{
tsL = temposync_support::roundTemposync(params[TIME_L].getValue());
/*Parameter p;
std::cout << tsL << " " << p.tempoSyncNotationValue(tsL) << std::endl;
*/
tsR = temposync_support::roundTemposync(params[TIME_R].getValue());
}
}
modulationAssistant.updateValues(this);
auto il = inputs[INPUT_L].getVoltageSum() * RACK_TO_SURGE_OSC_MUL;
auto ir = inputs[INPUT_R].getVoltageSum() * RACK_TO_SURGE_OSC_MUL;
if (!inputs[INPUT_R].isConnected())
ir = il;
modVal += dMod;
auto wobble = 1.0 + 0.02 * modulationAssistant.values[TIME_S] +
0.005 * modulationAssistant.values[MODDEPTH] * modVal;
// FIXME - temposync
float tl{0.f}, tr{0.f};
if (tempoSync)
{
auto tvl = 12 * wobble * (tsL + modulationAssistant.modvalues[TIME_L]);
tl = storage->samplerate * storage->temposyncratio_inv *
storage->note_to_pitch_ignoring_tuning(tvl);
auto tvr = 12 * wobble * (tsR + modulationAssistant.modvalues[TIME_R]);
tr = storage->samplerate * storage->temposyncratio_inv *
storage->note_to_pitch_ignoring_tuning(tvr);
}
else
{
tl = storage->samplerate * storage->note_to_pitch_ignoring_tuning(
12 * wobble * modulationAssistant.values[TIME_L]);
tr = storage->samplerate * storage->note_to_pitch_ignoring_tuning(
12 * wobble * modulationAssistant.values[TIME_R]);
}
tl = std::clamp(tl, 0.f, delayLineLength * 1.f);
tr = std::clamp(tr, 0.f, delayLineLength * 1.f);
auto dl = lineL->read(tl);
auto dr = lineR->read(tr);
auto wl{0.f}, wr{0.f};
float fb = modulationAssistant.values[FEEDBACK];
float cf = modulationAssistant.values[CROSSFEED];
switch (currentClipMode)
{
case TRANSPARENT:
break;
case HARDCLIP_DELAYLINE_10V:
// write the clean signal, clamp the output at 10v
dl = std::clamp(dl, -2.f, 2.f); // 10V
dr = std::clamp(dr, -2.f, 2.f);
break;
case SOFTCLIP_DELAYLINE_5V:
// Write the clean signal softclip the output
dl = std::clamp(dl, -1.5f, 1.5f); // 5V since this yields +/- 1
dr = std::clamp(dr, -1.5f, 1.5f);
dl = dl - 4.0f / 27.0f * dl * dl * dl;
dr = dr - 4.0f / 27.0f * dr * dr * dr;
break;
case SOFTCLIP_DELAYLINE_10V:
// Write the clean signal softclip the output
dl = std::clamp(dl, -3.f, 3.f); // 10V since this yields +/- 2
dr = std::clamp(dr, -3.f, 3.f);
dl = dl * 0.5;
dr = dr * 0.5;
dl = 2.0f * dl - 8.0f / 27.0f * dl * dl * dl;
dr = 2.0f * dr - 8.0f / 27.0f * dr * dr * dr;
break;
}
wl = il + fb * dl + cf * dr;
wr = ir + fb * dr + cf * dl;
lpPost->process_sample(wl, wr, wl, wr);
hpPost->process_sample(wl, wr, wl, wr);
lineL->write(wl);
lineR->write(wr);
auto mx = modulationAssistant.values[MIX];
outputs[OUTPUT_L].setVoltage((mx * dl + (1 - mx) * il) * SURGE_TO_RACK_OSC_MUL);
outputs[OUTPUT_R].setVoltage((mx * dr + (1 - mx) * ir) * SURGE_TO_RACK_OSC_MUL);
blockPos++;
}
json_t *makeModuleSpecificJson() override
{
auto fx = json_object();
clockProc.toJson(fx);
return fx;
}
void readModuleSpecificJson(json_t *modJ) override { clockProc.fromJson(modJ); }
void moduleSpecificSampleRateChange() override
{
clockProc.setSampleRate(APP->engine->getSampleRate());
}
};
} // namespace sst::surgext_rack::delay
#endif