-
Notifications
You must be signed in to change notification settings - Fork 8
/
transverbformalities.cpp
724 lines (618 loc) · 25.7 KB
/
transverbformalities.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*------------------------------------------------------------------------
Copyright (C) 2001-2022 Tom Murphy 7 and Sophia Poirier
This file is part of Transverb.
Transverb is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Transverb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Transverb. If not, see <https://www.gnu.org/licenses/>.
To contact the author, use the contact form at https://destroyfx.org/
------------------------------------------------------------------------*/
#include "transverb.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstring>
#include <numeric>
#include "dfxmisc.h"
#include "firfilter.h"
// these are macros that do boring entry point stuff for us
DFX_EFFECT_ENTRY(Transverb)
DFX_CORE_ENTRY(TransverbDSP)
using namespace dfx::TV;
Transverb::Transverb(TARGET_API_BASE_INSTANCE_TYPE inInstance)
: DfxPlugin(inInstance, kNumParameters, kNumPresets) {
initparameter_f(kBsize, {"buffer size", "BufSize", "BufSiz", "BfSz"}, 2700.0, 333.0, 1.0, 3000.0, DfxParam::Unit::MS);
initparameter_f(kDrymix, {"dry mix", "DryMix", "Dry"}, 1.0, 1.0, 0.0, 1.0, DfxParam::Unit::LinearGain, DfxParam::Curve::Squared);
initparameter_f(kMix1, {"1:mix", "1mix"}, 1.0, 1.0, 0.0, 1.0, DfxParam::Unit::LinearGain, DfxParam::Curve::Squared);
initparameter_f(kDist1, {"1:distance", "1:dist", "1dst"}, 0.90009, 0.5, 0.0, 1.0, DfxParam::Unit::Scalar);
initparameter_f(kSpeed1, {"1:speed", "1speed", "1spd"}, 0.0, 0.0, -3.0, 6.0, DfxParam::Unit::Octaves);
initparameter_f(kFeed1, {"1:feedback", "1feedbk", "1fedbk", "1fdb"}, 0.0, 33.3, 0.0, 100.0, DfxParam::Unit::Percent);
initparameter_f(kMix2, {"2:mix", "2mix"}, 0.0, 1.0, 0.0, 1.0, DfxParam::Unit::LinearGain, DfxParam::Curve::Squared);
initparameter_f(kDist2, {"2:distance", "2:dist", "2dst"}, 0.1, 0.5, 0.0, 1.0, DfxParam::Unit::Scalar);
initparameter_f(kSpeed2, {"2:speed", "2speed", "2spd"}, 1.0, 0.0, -3.0, 6.0, DfxParam::Unit::Octaves);
initparameter_f(kFeed2, {"2:feedback", "2feedbk", "2fedbk", "2fdb"}, 0.0, 33.3, 0.0, 100.0, DfxParam::Unit::Percent);
initparameter_list(kQuality, {"quality", "Qualty", "Qlty"}, kQualityMode_UltraHiFi, kQualityMode_UltraHiFi, kQualityMode_NumModes);
initparameter_b(kTomsound, {"TOMSOUND", "TomSnd", "Tom7"}, false);
initparameter_b(kFreeze, dfx::MakeParameterNames(dfx::kParameterNames_Freeze), false);
initparameter_b(kAttenuateFeedbackByMixLevel, {"attenuate feedback by mix level", "AtnFdbk", "AtnFdb", "-fdb"}, false);
initparameter_list(kDistChangeMode, {"distance change mode", "DistMod", "DstMod", "DMod"}, kDistChangeMode_Reverse, kDistChangeMode_Reverse, kDistChangeMode_Count);
setparameterenforcevaluelimits(kBsize, true);
setparameterenforcevaluelimits(kDist1, true);
setparameterenforcevaluelimits(kDist2, true);
setparametervaluestring(kQuality, kQualityMode_DirtFi, "dirt-fi");
setparametervaluestring(kQuality, kQualityMode_HiFi, "hi-fi");
setparametervaluestring(kQuality, kQualityMode_UltraHiFi, "ultra hi-fi");
setparametervaluestring(kDistChangeMode, kDistChangeMode_Reverse, "reverse");
setparametervaluestring(kDistChangeMode, kDistChangeMode_AdHocVarispeed, "ad hoc varispeed");
setparametervaluestring(kDistChangeMode, kDistChangeMode_DistanceVarispeed, "distance varispeed");
setparametervaluestring(kDistChangeMode, kDistChangeMode_BufferVarispeed, "buffer varispeed");
setparametervaluestring(kDistChangeMode, kDistChangeMode_LoopingBufferVarispeed, "looping buffer varispeed");
addparameterattributes(kFreeze, DfxParam::kAttribute_OmitFromRandomizeAll);
addparameterattributes(kAttenuateFeedbackByMixLevel, DfxParam::kAttribute_OmitFromRandomizeAll);
std::vector<long> mixparameters(1, kDrymix);
for (size_t head = 0; head < kNumDelays; head++) {
addparametergroup("head #" + std::to_string(head + 1),
{kSpeedParameters[head], kFeedParameters[head], kDistParameters[head]});
mixparameters.push_back(kMixParameters[head]);
}
addparametergroup("mix", mixparameters);
settailsize_seconds(getparametermax_f(kBsize) * 0.001);
setpresetname(0, PLUGIN_NAME_STRING); // default preset name
initPresets();
addchannelconfig(kChannelConfig_AnyMatchedIO); // N-in/N-out
addchannelconfig(1, kNumDelays); // mono-input / per-head-output
speedModeStates.fill(kSpeedMode_Fine);
}
void Transverb::dfx_PostConstructor() {
#if TARGET_PLUGIN_USES_MIDI
// since we don't use notes for any specialized control of Transverb,
// allow them to be assigned to control parameters via MIDI learn
getsettings().setAllowPitchbendEvents(true);
getsettings().setAllowNoteEvents(true);
#endif
}
TransverbDSP::TransverbDSP(DfxPlugin* inDfxPlugin)
: DfxPluginCore(inDfxPlugin),
MAXBUF(static_cast<int>(getparametermax_f(kBsize) * 0.001 * getsamplerate())),
buftemp(dynamic_cast<Transverb*>(inDfxPlugin)->getscratchbuffer()),
firCoefficientsWindow(dfx::FIRFilter::generateKaiserWindow(kNumFIRTaps, 60.0f)) {
buftemp.assign(MAXBUF, 0.f);
registerSmoothedAudioValue(&drymix);
for (auto& head : heads) {
head.buf.assign(MAXBUF, 0.f);
head.filter.setSampleRate(getsamplerate());
registerSmoothedAudioValue(&head.speed);
registerSmoothedAudioValue(&head.mix);
registerSmoothedAudioValue(&head.feed);
}
}
void TransverbDSP::reset() {
std::for_each(heads.begin(), heads.end(), [](Head& head){ head.reset(); });
firstrendersincereset = true;
}
void TransverbDSP::Head::reset() {
smoothcount = 0;
lastdelayval = 0.f;
targetdist.reset();
filter.reset();
speedHasChanged = true;
std::fill(buf.begin(), buf.end(), 0.f);
}
void TransverbDSP::processparameters() {
if (auto const value = getparameterifchanged_f(kDrymix))
{
// balance the audio energy when mono input is fanned out to multiple output channels
auto const dryGainScalar = std::sqrt(static_cast<double>(getplugin()->getnuminputs()) /
static_cast<double>(getplugin()->getnumoutputs()));
drymix = *value * dryGainScalar;
}
for (size_t head = 0; head < kNumDelays; head++)
{
if (auto const value = getparameterifchanged_f(kMixParameters[head]))
{
heads[head].mix = *value;
}
if (auto const value = getparameterifchanged_f(kSpeedParameters[head]))
{
heads[head].speed = std::pow(2., *value);
heads[head].speedHasChanged = true;
}
if (auto const value = getparameterifchanged_scalar(kFeedParameters[head]))
{
heads[head].feed = *value;
}
}
if (auto const value = getparameterifchanged_f(kBsize))
{
auto const entryBsize = std::exchange(bsize, std::clamp(static_cast<int>(*value * getsamplerate() * 0.001), 1, MAXBUF));
// the commented-out logic below is the beginning of an attempt to clean up the buffer states
// when the buffer resizes, however the tidiness maybe feels counter to the spirit of Transverb?
if (bsize > entryBsize)
{
//for (auto& head : heads)
{
//std::fill(std::next(head.buf.begin(), entryBsize), std::next(head.buf.begin(), bsize), 0.f);
}
}
else if (writer > bsize)
{
//auto const entryWriter = writer;
writer %= bsize;
//auto const copyCount = std::min(entryBsize - bsize, writer);
//for (auto& head : heads)
{
//std::copy_n(std::next(head.buf.cbegin(), entryWriter - copyCount), copyCount, std::next(head.buf.begin(), writer - copyCount));
}
}
auto const bsize_f = static_cast<double>(bsize);
std::for_each(heads.begin(), heads.end(), [bsize_f](Head& head){ head.read = fmod_bipolar(head.read, bsize_f); });
}
distchangemode = getparameter_i(kDistChangeMode);
for (size_t head = 0; head < kNumDelays; head++)
{
if (auto const dist = getparameterifchanged_f(kDistParameters[head]))
{
processdist(*dist, heads[head]);
}
}
if (getparameterchanged(kQuality) || getparameterchanged(kTomsound))
{
std::for_each(heads.begin(), heads.end(), [](Head& head){ head.speedHasChanged = true; });
}
// stereo-split-heads mode (head 1 goes to left output and 2 to right)
if (getplugin()->asymmetricalchannels())
{
static_assert(kNumDelays >= 2);
assert(getplugin()->getnumoutputs() == 2);
if (GetChannelNum() == 0)
{
heads[1].mix.setValueNow(getparametermin_f(kMix2));
}
else if (GetChannelNum() == 1)
{
heads[0].mix.setValueNow(getparametermin_f(kMix1));
}
}
firstrendersincereset = false;
}
double TransverbDSP::getdist(double read) const {
return fmod_bipolar(static_cast<double>(writer) - read, static_cast<double>(bsize));
}
void TransverbDSP::processdist(double distnormalized, Head& head) {
auto const bsize_f = static_cast<double>(bsize);
auto const distsamples = distnormalized * bsize_f;
auto const targetread = fmod_bipolar(static_cast<double>(writer) - distsamples, bsize_f);
if (firstrendersincereset)
{
head.read = targetread;
}
else
{
if (distchangemode == kDistChangeMode_Reverse)
{
head.targetdist = distsamples;
}
else if (auto const resamplerate = getdist(head.read) / std::max(distsamples, 1.); distchangemode == kDistChangeMode_AdHocVarispeed)
{
head.targetdist = distsamples;
head.distspeedfactor = resamplerate;
head.speedHasChanged = true;
}
else
{
constexpr double modulationthresholdsamples = 1.;
if (std::fabs(targetread - head.read) >= modulationthresholdsamples)
{
auto const bsizesteps = bsize_f / resamplerate;
bool const subslice = (distchangemode == kDistChangeMode_DistanceVarispeed);
bool const looping = (distchangemode == kDistChangeMode_LoopingBufferVarispeed);
auto const copylength_f = subslice ? distsamples : (looping ? bsize_f : std::min(bsize_f, bsizesteps));
auto const copylength = static_cast<size_t>(std::lround(copylength_f));
assert(copylength <= buftemp.size());
assert(static_cast<int>(copylength) <= bsize);
auto const sourcestart = subslice ? head.read : fmod_bipolar(static_cast<double>(writer) - (copylength_f * resamplerate), bsize_f);
for (size_t i = 0; i < copylength; i++)
{
auto const sourcepos = std::fmod(sourcestart + (resamplerate * static_cast<double>(i)), bsize_f);
buftemp[i] = interpolateHermite(head.buf.data(), sourcepos, bsize, writer);
}
auto const destinationstart = subslice ? std::lround(targetread) : mod_bipolar(writer - static_cast<int>(copylength), bsize);
auto const copylength1 = std::min(static_cast<size_t>(bsize - destinationstart), copylength);
auto const copylength2 = copylength - copylength1;
std::copy_n(buftemp.cbegin(), copylength1, std::next(head.buf.begin(), destinationstart));
std::copy_n(std::next(buftemp.cbegin(), copylength1), copylength2, head.buf.begin());
}
head.read = targetread;
}
}
}
//--------- presets --------
void Transverb::initPresets() {
long i = 1;
setpresetname(i, "phaser up");
setpresetparameter_f(i, kBsize, 48.687074827);
setpresetparameter_f(i, kDrymix, 0.45);
setpresetparameter_f(i, kMix1, 0.5);
setpresetparameter_f(i, kDist1, 0.1);
setpresetparameter_f(i, kSpeed1, 0.048406605 / 12.);
setpresetparameter_f(i, kFeed1, 33.5);
setpresetparameter_f(i, kMix2, 0.0);
setpresetparameter_f(i, kDist2, 0.0);
setpresetparameter_f(i, kSpeed2, getparametermin_f(kSpeed2));
setpresetparameter_f(i, kFeed2, 0.0);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "phaser down");
setpresetparameter_f(i, kBsize, 27.0);
setpresetparameter_f(i, kDrymix, 0.45);
setpresetparameter_f(i, kMix1, 0.5);
setpresetparameter_f(i, kDist1, 0.0);
setpresetparameter_f(i, kSpeed1, -0.12 / 12.);//-0.048542333 / 12.);
setpresetparameter_f(i, kFeed1, 38.);
setpresetparameter_f(i, kMix2, 0.0);
setpresetparameter_f(i, kDist2, 0.0);
setpresetparameter_f(i, kSpeed2, getparametermin_f(kSpeed2));
setpresetparameter_f(i, kFeed2, 0.0);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "aquinas");
setpresetparameter_f(i, kBsize, 2605.1);
setpresetparameter_f(i, kDrymix, 0.6276);
setpresetparameter_f(i, kMix1, 1.0);
setpresetparameter_f(i, kDist1, 0.007);
setpresetparameter_f(i, kSpeed1, -4.665660556);
setpresetparameter_f(i, kFeed1, 54.0);
setpresetparameter_f(i, kMix2, 0.757);
setpresetparameter_f(i, kDist2, 0.557);
setpresetparameter_f(i, kSpeed2, 2.444534569);
setpresetparameter_f(i, kFeed2, 34.822);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "glup drums");
setpresetparameter_f(i, kBsize, 184.27356);
setpresetparameter_f(i, kDrymix, 0.157);
setpresetparameter_f(i, kMix1, 1.0);
setpresetparameter_f(i, kDist1, 0.9055);
setpresetparameter_f(i, kSpeed1, -8. / 12.);
setpresetparameter_f(i, kFeed1, 97.6);
setpresetparameter_f(i, kMix2, 0.0);
setpresetparameter_f(i, kDist2, 0.803);
setpresetparameter_f(i, kSpeed2, 0.978195651);
setpresetparameter_f(i, kFeed2, 0.0);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "space invaders");
setpresetparameter_f(i, kSpeed1, -0.23 / 12.);
setpresetparameter_f(i, kFeed1, 73.0);
setpresetparameter_f(i, kDist1, 0.8143);
setpresetparameter_f(i, kSpeed2, 4.3 / 12.);
setpresetparameter_f(i, kFeed2, 5.0225);
setpresetparameter_f(i, kDist2, 0.4006);
setpresetparameter_f(i, kBsize, 16.8);
setpresetparameter_f(i, kDrymix, 0.000576);
setpresetparameter_f(i, kMix1, 1.0);
setpresetparameter_f(i, kMix2, 0.1225);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "mudslap (by Styrofoam)");
setpresetparameter_f(i, kSpeed1, -0.9748);
setpresetparameter_f(i, kFeed1, 45.6285);
setpresetparameter_f(i, kDist1, 0.97655);
setpresetparameter_f(i, kSpeed2, -1.0252);
setpresetparameter_f(i, kFeed2, 46.4819);
setpresetparameter_f(i, kDist2, 0.9808);
setpresetparameter_f(i, kBsize, 122.4947);
setpresetparameter_f(i, kDrymix, 0.);
setpresetparameter_f(i, kMix1, 1.);
setpresetparameter_f(i, kMix2, 1.);
setpresetparameter_i(i, kQuality, kQualityMode_DirtFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "subverb (by Styrofoam)");
setpresetparameter_f(i, kSpeed1, -2.9232);
setpresetparameter_f(i, kFeed1, 49.6802);
setpresetparameter_f(i, kDist1, 0.94244);
setpresetparameter_f(i, kSpeed2, -2.90405);
setpresetparameter_f(i, kFeed2, 47.5475);
setpresetparameter_f(i, kDist2, 0.968);
setpresetparameter_f(i, kBsize, 2341.3708);
setpresetparameter_f(i, kDrymix, 0.);
setpresetparameter_f(i, kMix1, 1.);
setpresetparameter_f(i, kMix2, 1.);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, true);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "vocoder beat (by Styrofoam)");
setpresetparameter_f(i, kSpeed1, 2.);
setpresetparameter_f(i, kFeed1, 57.3561);
setpresetparameter_f(i, kDist1, 0.9915);
setpresetparameter_f(i, kSpeed2, 1.);
setpresetparameter_f(i, kFeed2, 27.5053);
setpresetparameter_f(i, kDist2, 0.9979);
setpresetparameter_f(i, kBsize, 64.9446);
setpresetparameter_f(i, kDrymix, 0.);
setpresetparameter_f(i, kMix1, 1.);
setpresetparameter_f(i, kMix2, 1.);
setpresetparameter_i(i, kQuality, kQualityMode_DirtFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
setpresetname(i, "yo pitch! (by Styrofoam)");
setpresetparameter_f(i, kSpeed1, -2.);
setpresetparameter_f(i, kFeed1, 80.3769);
setpresetparameter_f(i, kDist1, 0.977638);
setpresetparameter_f(i, kSpeed2, 2.);
setpresetparameter_f(i, kFeed2, 18.937530198);
setpresetparameter_f(i, kDist2, 0.989335);
setpresetparameter_f(i, kBsize, 1938.5203);
setpresetparameter_f(i, kDrymix, 0.);
setpresetparameter_f(i, kMix1, 1.);
setpresetparameter_f(i, kMix2, 0.6483);
setpresetparameter_i(i, kQuality, kQualityMode_UltraHiFi);
setpresetparameter_b(i, kTomsound, false);
setpresetparameter_b(i, kFreeze, false);
i++;
/*
setpresetname(i, "");
setpresetparameter_f(i, kSpeed1, );
setpresetparameter_f(i, kFeed1, );
setpresetparameter_f(i, kDist1, 0.0);
setpresetparameter_f(i, kSpeed2, );
setpresetparameter_f(i, kFeed2, );
setpresetparameter_f(i, kDist2, 0.0);
setpresetparameter_f(i, kBsize, );
setpresetparameter_f(i, kDrymix, 0.);
setpresetparameter_f(i, kMix1, 0.);
setpresetparameter_f(i, kMix2, 0.);
setpresetparameter_i(i, kQuality, );
setpresetparameter_b(i, kTomsound, );
setpresetparameter_b(i, kFreeze, false);
i++;
*/
// special randomizing "preset"
setpresetname(getnumpresets() - 1, "random");
}
//-----------------------------------------------------------------------------
bool Transverb::loadpreset(long index)
{
if (!presetisvalid(index))
return false;
if (getpresetname(index) == "random")
{
randomizeparameters();
return true;
}
return DfxPlugin::loadpreset(index);
}
//--------- presets (end) --------
/* this randomizes the values of all of Transverb's parameters, sometimes in smart ways */
void Transverb::randomizeparameters()
{
// randomize the non-mix-level parameters
for (long i = 0; i < kDrymix; i++)
{
if (hasparameterattribute(i, DfxParam::kAttribute_OmitFromRandomizeAll) || hasparameterattribute(i, DfxParam::kAttribute_Unused))
{
continue;
}
// make slow speeds more probable (for fairer distribution)
if (std::find(kSpeedParameters.cbegin(), kSpeedParameters.cend(), i) != kSpeedParameters.cend())
{
auto const rand = generateParameterRandomValue<double>(-1., 1.);
auto const range = (rand < 0.) ? getparametermin_f(i) : getparametermax_f(i);
setparameter_f(i, std::fabs(rand) * range);
}
// make smaller buffer sizes more probable (because they sound better), though prevent smallest
else if (i == kBsize)
{
setparameter_gen(kBsize, std::pow(generateParameterRandomValue<double>(0.07, 1.), 1.38));
}
else
{
// TODO: this is not thread-safe (wrt randomizer state) given that Transverb can execute this method
// from the main thread (GUI) or MIDI thread (though likelihood of concurrency is quite low)
randomizeparameter(i);
}
}
// do fancy mix level randomization
// store the current total gain sum
auto const mixSum = getparameter_f(kDrymix) + getparameter_f(kMix1) + getparameter_f(kMix2);
// randomize the mix parameters
auto newDrymix = expandparametervalue(kDrymix, generateParameterRandomValue<double>());
std::array<double, kNumDelays> newMix {};
for (size_t head = 0; head < kNumDelays; head++)
{
newMix[head] = expandparametervalue(kMixParameters[head], generateParameterRandomValue<double>());
}
// calculate a scalar to make up for total gain changes
auto const mixDiffScalar = mixSum / (newDrymix + std::accumulate(newMix.cbegin(), newMix.cend(), 0.));
// apply the scalar to the new mix parameter values
newDrymix *= mixDiffScalar;
std::transform(newMix.cbegin(), newMix.cend(), newMix.begin(),
[mixDiffScalar](double value){ return value * mixDiffScalar; });
// clip the the delay head mix values at unity gain so that we don't get mega-feedback blasts
newDrymix = std::clamp(newDrymix, 0., getparametermax_f(kDrymix));
std::transform(newMix.cbegin(), newMix.cend(), newMix.begin(),
[](double value){ return std::clamp(value, 0., 1.); });
// set the new randomized mix parameter values as the new values
setparameter_f(kDrymix, newDrymix);
for (size_t head = 0; head < kNumDelays; head++)
{
setparameter_f(kMixParameters[head], newMix[head]);
}
// randomize the state parameters
// make TOMSOUND less probable (only 1/3 of the time)
auto const newTomsound = static_cast<bool>(generateParameterRandomValue<int64_t>(0, 2) % 2);
setparameter_b(kTomsound, newTomsound);
if (newTomsound)
{
// split probability evenly between lowest and higher qualities
setparameter_i(kQuality, generateParameterRandomValue<int64_t>(0, kQualityMode_NumModes) % kQualityMode_NumModes);
}
else
{
// make higher qualities more probable (happen 4/5 of the time)
setparameter_i(kQuality, generateParameterRandomValue<int64_t>(1, (kQualityMode_NumModes * 2) - 1) % kQualityMode_NumModes);
}
for (long i = 0; i < kNumParameters; i++)
{
if (hasparameterattribute(i, DfxParam::kAttribute_OmitFromRandomizeAll) || hasparameterattribute(i, DfxParam::kAttribute_Unused))
{
continue;
}
postupdate_parameter(i); // inform any parameter listeners of the changes
}
}
long Transverb::dfx_GetPropertyInfo(dfx::PropertyID inPropertyID, dfx::Scope inScope, unsigned long inItemIndex,
size_t& outDataSize, dfx::PropertyFlags& outFlags)
{
if (isSpeedModePropertyID(inPropertyID))
{
outDataSize = sizeof(uint8_t); // single-byte representation allows for no concerns about transmission endianness
outFlags = dfx::kPropertyFlag_Readable | dfx::kPropertyFlag_Writable;
return dfx::kStatus_NoError;
}
return DfxPlugin::dfx_GetPropertyInfo(inPropertyID, inScope, inItemIndex, outDataSize, outFlags);
}
long Transverb::dfx_GetProperty(dfx::PropertyID inPropertyID, dfx::Scope inScope, unsigned long inItemIndex,
void* outData)
{
if (isSpeedModePropertyID(inPropertyID))
{
*static_cast<uint8_t*>(outData) = static_cast<uint8_t>(speedModeStateFromPropertyID(inPropertyID));
return dfx::kStatus_NoError;
}
return DfxPlugin::dfx_GetProperty(inPropertyID, inScope, inItemIndex, outData);
}
long Transverb::dfx_SetProperty(dfx::PropertyID inPropertyID, dfx::Scope inScope, unsigned long inItemIndex,
void const* inData, size_t inDataSize)
{
if (isSpeedModePropertyID(inPropertyID))
{
speedModeStateFromPropertyID(inPropertyID) = *static_cast<uint8_t const*>(inData);
#ifdef TARGET_API_AUDIOUNIT
PropertyChanged(inPropertyID, inScope, inItemIndex);
#endif
return dfx::kStatus_NoError;
}
return DfxPlugin::dfx_SetProperty(inPropertyID, inScope, inItemIndex, inData, inDataSize);
}
size_t Transverb::settings_sizeOfExtendedData() const noexcept
{
return speedModeStates.size() * sizeof(speedModeStates.front());
}
void Transverb::settings_saveExtendedData(void* outData, bool /*isPreset*/)
{
auto speedModeStatesSerialization = speedModeStates;
if constexpr (!DfxSettings::serializationIsNativeEndian())
{
dfx::ReverseBytes(speedModeStatesSerialization.data(), speedModeStatesSerialization.size());
}
memcpy(outData, speedModeStatesSerialization.data(), settings_sizeOfExtendedData());
}
void Transverb::settings_restoreExtendedData(void const* inData, size_t storedExtendedDataSize,
long dataVersion, bool /*isPreset*/)
{
if (storedExtendedDataSize >= settings_sizeOfExtendedData())
{
memcpy(speedModeStates.data(), inData, settings_sizeOfExtendedData());
if constexpr (!DfxSettings::serializationIsNativeEndian())
{
dfx::ReverseBytes(speedModeStates.data(), speedModeStates.size());
}
}
}
void Transverb::settings_doChunkRestoreSetParameterStuff(long tag, float value, long dataVersion, long presetNum)
{
// prevent old speed mode 1 settings from applying to freeze
if ((dataVersion < 0x00010501) && (tag == kFreeze))
{
auto const defaultValue = getparameterdefault_b(tag);
if (presetisvalid(presetNum))
{
setpresetparameter_b(presetNum, tag, defaultValue);
}
else
{
setparameter_b(tag, defaultValue);
}
}
if (dataVersion <= 0x00010502)
{
// invert old erroneously reversed dist parameter values
if ((tag == kDist1) || (tag == kDist2))
{
auto const valueNormalized = 1. - contractparametervalue(tag, value);
if (presetisvalid(presetNum))
{
setpresetparameter_gen(presetNum, tag, valueNormalized);
}
else
{
setparameter_gen(tag, valueNormalized);
}
}
// hi-fi mode used to behave the same as and ultra hi-fi mode in TOMSOUND
if (tag == kTomsound) // assumes that TOMSOUND parameter is restored after quality
{
if (presetisvalid(presetNum))
{
if (getpresetparameter_f(presetNum, kTomsound) && (getpresetparameter_i(presetNum, kQuality) == kQualityMode_HiFi))
{
setpresetparameter_i(presetNum, kQuality, kQualityMode_UltraHiFi);
}
}
else
{
if (getparameter_b(kTomsound) && (getparameter_i(kQuality) == kQualityMode_HiFi))
{
setparameter_i(kQuality, kQualityMode_UltraHiFi);
}
}
}
// apply legacy feedback behavior
// https://github.com/sophiapoirier/destroyfx/issues/61
constexpr bool attenuate = true;
if (presetisvalid(presetNum))
{
setpresetparameter_b(presetNum, kAttenuateFeedbackByMixLevel, attenuate);
}
else
{
setparameter_b(kAttenuateFeedbackByMixLevel, attenuate);
}
}
}
#pragma mark -
dfx::PropertyID dfx::TV::speedModeIndexToPropertyID(size_t inIndex) noexcept
{
return kTransverbProperty_SpeedModeBase + static_cast<dfx::PropertyID>(inIndex);
}
size_t dfx::TV::speedModePropertyIDToIndex(dfx::PropertyID inPropertyID) noexcept
{
assert(inPropertyID >= kTransverbProperty_SpeedModeBase);
return inPropertyID - kTransverbProperty_SpeedModeBase;
}
bool dfx::TV::isSpeedModePropertyID(dfx::PropertyID inPropertyID) noexcept
{
return (inPropertyID >= kTransverbProperty_SpeedModeBase) && (speedModePropertyIDToIndex(inPropertyID) < kNumDelays);
}