-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginProcessor.cpp
More file actions
825 lines (704 loc) · 32.1 KB
/
Copy pathPluginProcessor.cpp
File metadata and controls
825 lines (704 loc) · 32.1 KB
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#include "PluginProcessor.h"
#include "PluginEditor.h"
namespace {
constexpr auto uiLanguageStateKey = "uiLanguage";
constexpr float haasCompDeadZoneMs = 0.75f;
constexpr float haasCompMaxEffectiveDelayMs = 20.0f;
constexpr float haasCompTauMs = 5.0f;
constexpr float haasCompMaxDbAt100Percent = 2.0f;
constexpr float haasCompNewToOldPercentScale = 0.3f;
constexpr float adtMaxSegmentSeconds = 1.25f;
constexpr float adtSmoothstepPeakSlope = 1.5f;
constexpr double latencyCommitIdleWindowMs = 200.0;
struct HaasCompensationState {
float leftCompDb = 0.0f;
float rightCompDb = 0.0f;
float earlierPath = -1.0f;
};
struct ProcessActivityGuard {
explicit ProcessActivityGuard(std::atomic<int> &counterIn)
: counter(counterIn) {
counter.fetch_add(1, std::memory_order_acq_rel);
}
~ProcessActivityGuard() { counter.fetch_sub(1, std::memory_order_acq_rel); }
std::atomic<int> &counter;
};
bool isLatencyAffectingParameter(const juce::String ¶meterID) {
return parameterID == "offsetTime" || parameterID == "centeredTiming" ||
parameterID == "pitchDiff";
}
bool matchesLatencyState(float snapshotOffsetMs, bool snapshotCentered,
float snapshotDriftAmountCents,
int snapshotLatencySamples, float offsetMs,
bool centered, float driftAmountCents,
int latencySamples) {
return snapshotLatencySamples == latencySamples &&
std::abs(snapshotOffsetMs - offsetMs) < 1.0e-4f &&
snapshotCentered == centered &&
std::abs(snapshotDriftAmountCents - driftAmountCents) < 1.0e-4f;
}
HaasCompensationState computeHaasCompensationState(
float leftDelayMs, float rightDelayMs, bool enabled, float amountNorm,
float panSeparationWeight, bool hasMeaningfulPanSeparation) {
HaasCompensationState state;
if (!enabled || !hasMeaningfulPanSeparation)
return state;
const float deltaMs = std::abs(leftDelayMs - rightDelayMs);
if (deltaMs <= haasCompDeadZoneMs)
return state;
const float effectiveDeltaMs =
juce::jlimit(0.0f, haasCompMaxEffectiveDelayMs - haasCompDeadZoneMs,
deltaMs - haasCompDeadZoneMs);
const float factor = 1.0f - std::exp(-effectiveDeltaMs / haasCompTauMs);
// Stretch the control so 100% now lands where the old 30% setting was.
// The 300% top end therefore reaches roughly the old 90% response.
const float compDiffDb =
factor * amountNorm * haasCompMaxDbAt100Percent * panSeparationWeight;
// Convert the desired lead-vs-lag differential into constant-power path
// gains so the compensation acts like image balancing, not an accidental
// loudness knob.
const float gainRatio = juce::Decibels::decibelsToGain(compDiffDb);
const float earlyGain =
std::sqrt(2.0f / (1.0f + (gainRatio * gainRatio)));
const float lateGain = gainRatio * earlyGain;
const float earlyCompDb = juce::Decibels::gainToDecibels(earlyGain);
const float lateCompDb = juce::Decibels::gainToDecibels(lateGain);
if (leftDelayMs < rightDelayMs) {
state.leftCompDb = earlyCompDb;
state.rightCompDb = lateCompDb;
state.earlierPath = 0.0f;
} else if (rightDelayMs < leftDelayMs) {
state.leftCompDb = lateCompDb;
state.rightCompDb = earlyCompDb;
state.earlierPath = 1.0f;
}
return state;
}
}
VocalWidenerProcessor::VocalWidenerProcessor()
#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor(
BusesProperties()
// Keep the default/main VST3 arrangement stereo->stereo so
// hosts that decide insert eligibility from the initial bus
// layout will still offer the plugin on stereo channels.
.withInput("Input", juce::AudioChannelSet::stereo(), true)
.withOutput("Output", juce::AudioChannelSet::stereo(), true)),
#else
:
#endif
apvts(*this, nullptr, "Parameters", createParameterLayout()) {
offsetTimeParam = apvts.getRawParameterValue("offsetTime");
leftPanParam = apvts.getRawParameterValue("leftPan");
rightPanParam = apvts.getRawParameterValue("rightPan");
centeredTimingParam = apvts.getRawParameterValue("centeredTiming");
pitchDiffParam = apvts.getRawParameterValue("pitchDiff");
outputGainParam = apvts.getRawParameterValue("outGain");
bypassParam = apvts.getRawParameterValue("bypass");
linkPanParam = apvts.getRawParameterValue("linkPan");
flipPanParam = apvts.getRawParameterValue("flipPan");
haasCompEnableParam = apvts.getRawParameterValue("haasCompEn");
haasCompAmtParam = apvts.getRawParameterValue("haasCompAmt");
apvts.addParameterListener("leftPan", this);
apvts.addParameterListener("rightPan", this);
apvts.addParameterListener("linkPan", this);
apvts.addParameterListener("offsetTime", this);
apvts.addParameterListener("centeredTiming", this);
apvts.addParameterListener("pitchDiff", this);
setLanguageCode(apvts.state.getProperty(uiLanguageStateKey, "en").toString());
syncLinkedPanStateFromParameters(false);
}
VocalWidenerProcessor::~VocalWidenerProcessor() {
apvts.removeParameterListener("leftPan", this);
apvts.removeParameterListener("rightPan", this);
apvts.removeParameterListener("linkPan", this);
apvts.removeParameterListener("offsetTime", this);
apvts.removeParameterListener("centeredTiming", this);
apvts.removeParameterListener("pitchDiff", this);
}
void VocalWidenerProcessor::parameterChanged(const juce::String ¶meterID,
float newValue) {
if (isLatencyAffectingParameter(parameterID)) {
handleLatencyParameterChanged(parameterID, newValue);
return;
}
const auto mirroredTarget = static_cast<PanMirrorTarget>(
panMirrorWriteTarget.load(std::memory_order_relaxed));
if (((parameterID == "leftPan" && mirroredTarget == mirrorLeftPan) ||
(parameterID == "rightPan" && mirroredTarget == mirrorRightPan)) &&
std::abs(newValue -
panMirrorWriteValue.load(std::memory_order_relaxed)) < 0.0001f) {
return;
}
const bool linked =
parameterID == "linkPan"
? (newValue > 0.5f)
: (linkPanParam != nullptr &&
linkPanParam->load(std::memory_order_relaxed) > 0.5f);
if (!linked) {
if (parameterID == "leftPan" || parameterID == "rightPan")
linkedPanValue.store(newValue, std::memory_order_relaxed);
pendingPanMirrorTarget.store(noPanMirrorPending, std::memory_order_relaxed);
return;
}
if (parameterID == "leftPan") {
linkedPanValue.store(newValue, std::memory_order_relaxed);
scheduleLinkedPanMirror(mirrorRightPan, newValue);
} else if (parameterID == "rightPan") {
linkedPanValue.store(newValue, std::memory_order_relaxed);
scheduleLinkedPanMirror(mirrorLeftPan, newValue);
} else if (parameterID == "linkPan") {
const float leftValue =
leftPanParam != nullptr
? leftPanParam->load(std::memory_order_relaxed)
: 100.0f;
linkedPanValue.store(leftValue, std::memory_order_relaxed);
scheduleLinkedPanMirror(mirrorRightPan, leftValue);
}
}
juce::AudioProcessorValueTreeState::ParameterLayout
VocalWidenerProcessor::createParameterLayout() {
std::vector<std::unique_ptr<juce::RangedAudioParameter>> params;
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("offsetTime", 1), "offset time", 0.0f, maxOffsetMs,
15.0f));
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("leftPan", 1), "left pan",
juce::NormalisableRange<float>(0.0f, 100.0f, 1.0f), 100.0f,
juce::AudioParameterFloatAttributes().withStringFromValueFunction(
[](float v, int) { return juce::String(juce::roundToInt(v)); })));
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("rightPan", 1), "right pan",
juce::NormalisableRange<float>(0.0f, 100.0f, 1.0f), 100.0f,
juce::AudioParameterFloatAttributes().withStringFromValueFunction(
[](float v, int) { return juce::String(juce::roundToInt(v)); })));
params.push_back(std::make_unique<juce::AudioParameterBool>(
juce::ParameterID("centeredTiming", 1), "equal delay", false));
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("pitchDiff", 1), "adt drift",
juce::NormalisableRange<float>(0.0f, 20.0f, 0.01f), 0.0f,
juce::AudioParameterFloatAttributes().withStringFromValueFunction(
[](float v, int) { return juce::String(v, 2); })));
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("outGain", 1), "output gain",
juce::NormalisableRange<float>(-12.0f, 12.0f, 0.1f), 0.0f,
juce::AudioParameterFloatAttributes().withStringFromValueFunction(
[](float v, int) {
return juce::String(std::abs(v) < 0.05f ? 0.0f : v, 1);
})));
params.push_back(std::make_unique<juce::AudioParameterBool>(
juce::ParameterID("bypass", 1), "bypass", false));
params.push_back(std::make_unique<juce::AudioParameterBool>(
juce::ParameterID("linkPan", 1), "link pan", true));
params.push_back(std::make_unique<juce::AudioParameterBool>(
juce::ParameterID("flipPan", 1), "flip pan", false));
params.push_back(std::make_unique<juce::AudioParameterBool>(
juce::ParameterID("haasCompEn", 1), "haas comp", false));
params.push_back(std::make_unique<juce::AudioParameterFloat>(
juce::ParameterID("haasCompAmt", 1), "haas comp amount",
juce::NormalisableRange<float>(0.0f, 500.0f, 1.0f), 100.0f,
juce::AudioParameterFloatAttributes().withStringFromValueFunction(
[](float v, int) { return juce::String(juce::roundToInt(v)); })));
return {params.begin(), params.end()};
}
bool VocalWidenerProcessor::isBusesLayoutSupported(
const BusesLayout &layouts) const {
const auto input = layouts.getMainInputChannelSet();
const auto output = layouts.getMainOutputChannelSet();
if (output != juce::AudioChannelSet::stereo())
return false;
return input == juce::AudioChannelSet::mono() ||
input == juce::AudioChannelSet::stereo();
}
void VocalWidenerProcessor::prepareToPlay(double sampleRate,
int samplesPerBlock) {
juce::ignoreUnused(samplesPerBlock);
currentSampleRate = sampleRate;
isStereoLayout = ((getTotalNumInputChannels() == 1 ||
getTotalNumInputChannels() == 2) &&
getTotalNumOutputChannels() == 2);
const float maxVoiceDelayMs =
maxCenteredRightDelayMs + computeAdtSharedLatencyMs(maxPitchDiffCents) +
computeAdtMaxExcursionMs(maxPitchDiffCents) + maxLatencyGuardMs;
voiceLeft.prepare(sampleRate, maxVoiceDelayMs, 0x13579BDF);
voiceRight.prepare(sampleRate, maxVoiceDelayMs, 0x2468ACE0);
const int maxLatencySamples =
juce::roundToInt(((computeAdtSharedLatencyMs(maxPitchDiffCents) +
maxOffsetMs) *
static_cast<float>(sampleRate)) /
1000.0f);
dryDelayLeft.prepare(maxLatencySamples);
dryDelayRight.prepare(maxLatencySamples);
syncLatencyStateFromParameters(true);
syncLinkedPanStateFromParameters(false);
}
void VocalWidenerProcessor::releaseResources() {}
void VocalWidenerProcessor::reset() {
voiceLeft.reset();
voiceRight.reset();
if (dryDelayLeft.isPrepared())
dryDelayLeft.reset();
if (dryDelayRight.isPrepared())
dryDelayRight.reset();
currentLeftCompDb = 0.0f;
currentRightCompDb = 0.0f;
leftDelayReadout.store(0.0f, std::memory_order_relaxed);
rightDelayReadout.store(0.0f, std::memory_order_relaxed);
leftPitchReadout.store(0.0f, std::memory_order_relaxed);
rightPitchReadout.store(0.0f, std::memory_order_relaxed);
earlierPathReadout.store(-1.0f, std::memory_order_relaxed);
leftCompReadout.store(0.0f, std::memory_order_relaxed);
rightCompReadout.store(0.0f, std::memory_order_relaxed);
}
void VocalWidenerProcessor::processBlock(juce::AudioBuffer<float> &buffer,
juce::MidiBuffer &midiMessages) {
juce::ignoreUnused(midiMessages);
processAudioBlock(buffer, false);
}
void VocalWidenerProcessor::processBlockBypassed(
juce::AudioBuffer<float> &buffer, juce::MidiBuffer &midiMessages) {
juce::ignoreUnused(midiMessages);
processAudioBlock(buffer, true);
}
void VocalWidenerProcessor::processAudioBlock(juce::AudioBuffer<float> &buffer,
bool forceBypassed) {
juce::ScopedNoDenormals noDenormals;
ProcessActivityGuard processGuard(activeProcessBlockCount);
const auto processExit = juce::ScopeGuard([this] {
lastProcessBlockEndMs.store(juce::Time::getMillisecondCounterHiRes(),
std::memory_order_relaxed);
});
// Fast bypass and Non-Stereo Layout safe exit
if (!isStereoLayout)
return;
// Load parameters (Realtime safe)
const float requestedOffsetMs =
offsetTimeParam->load(std::memory_order_relaxed);
const bool requestedCentered =
centeredTimingParam->load(std::memory_order_relaxed) > 0.5f;
const bool parameterBypassed =
bypassParam->load(std::memory_order_relaxed) > 0.5f;
const bool bypassed = forceBypassed || parameterBypassed;
const float requestedPitchDiff =
pitchDiffParam->load(std::memory_order_relaxed);
const int requestedLatencySamples =
computeLatencySamples(requestedOffsetMs, requestedCentered,
requestedPitchDiff);
if (requestedLatencySamples != activeLatencySamples) {
activeLatencySamples = requestedLatencySamples;
dryDelayLeft.setDelaySamples(activeLatencySamples);
dryDelayRight.setDelaySamples(activeLatencySamples);
}
const auto committedLatencyState = getCommittedLatencyState();
if (!matchesLatencyState(committedLatencyState.offsetMs,
committedLatencyState.centered,
committedLatencyState.driftAmountCents,
committedLatencyState.latencySamples,
requestedOffsetMs, requestedCentered,
requestedPitchDiff, requestedLatencySamples))
queueLatencyUpdate(requestedOffsetMs, requestedCentered, requestedPitchDiff,
requestedLatencySamples);
const float offsetMs = requestedOffsetMs;
const bool centered = requestedCentered;
const float pDiff = requestedPitchDiff;
const bool pitchShiftActive = isPitchShiftActive(pDiff);
float gainLinear = juce::Decibels::decibelsToGain(
outputGainParam->load(std::memory_order_relaxed));
const bool linkPanEnabled =
linkPanParam->load(std::memory_order_relaxed) > 0.5f;
float leftPanAmount =
juce::jlimit(0.0f, 100.0f, leftPanParam->load(std::memory_order_relaxed));
float rightPanAmount = juce::jlimit(
0.0f, 100.0f, rightPanParam->load(std::memory_order_relaxed));
if (linkPanEnabled) {
const float linkedPanAmount = juce::jlimit(
0.0f, 100.0f, linkedPanValue.load(std::memory_order_relaxed));
leftPanAmount = linkedPanAmount;
rightPanAmount = linkedPanAmount;
}
const bool flipPan = flipPanParam->load(std::memory_order_relaxed) > 0.5f;
const float panDirection = flipPan ? 1.0f : -1.0f;
float leftPan = panDirection * (leftPanAmount / 100.0f);
float rightPan = -panDirection * (rightPanAmount / 100.0f);
// Equal-power pan coefficients.
// Math: left = cos((pan+1) * pi/4), right = sin((pan+1) * pi/4)
const float leftPL =
std::cos((leftPan + 1.0f) * juce::MathConstants<float>::pi * 0.25f);
const float leftPR =
std::sin((leftPan + 1.0f) * juce::MathConstants<float>::pi * 0.25f);
const float rightPL =
std::cos((rightPan + 1.0f) * juce::MathConstants<float>::pi * 0.25f);
const float rightPR =
std::sin((rightPan + 1.0f) * juce::MathConstants<float>::pi * 0.25f);
// Practical Haas heuristic: scale the gain compensation by the actual
// left/right separation created by the equal-power panners. This makes the
// compensation collapse toward zero as both voices approach the center.
const float leftBalance = leftPR - leftPL;
const float rightBalance = rightPR - rightPL;
const float panSeparationWeight =
juce::jlimit(0.0f, 1.0f, std::abs(rightBalance - leftBalance) * 0.5f);
const bool hasMeaningfulPanSeparation = panSeparationWeight >= 0.03f;
// Calculate DSP state per path.
// Equal-delay mode now splits the actual voice delays instead of relying on
// host compensation alone. The reported latency stays at the centre point so
// bypass and host PDC remain aligned to the perceived middle of the pair.
const float delayTLeftMs = centered ? (offsetMs * 0.5f) : 0.0f;
const float delayTRightMs =
centered ? (offsetMs * 1.5f) : offsetMs;
const auto [leftDriftAmountCents, rightDriftAmountCents] =
computeVoiceDriftAmounts(pDiff);
const float sharedAdtLatencyMs = computeAdtSharedLatencyMs(pDiff);
const float readoutReferenceMs =
sharedAdtLatencyMs + (centered ? offsetMs : 0.0f);
voiceLeft.configure(delayTLeftMs, sharedAdtLatencyMs, leftDriftAmountCents);
voiceRight.configure(delayTRightMs, sharedAdtLatencyMs, rightDriftAmountCents);
// Precedence Compensation
bool haasEnable = haasCompEnableParam->load(std::memory_order_relaxed) > 0.5f &&
linkPanEnabled;
float haasAmtNorm =
(haasCompAmtParam->load(std::memory_order_relaxed) *
haasCompNewToOldPercentScale) /
100.0f;
auto *channelL = buffer.getWritePointer(0);
auto *channelR = buffer.getWritePointer(1);
const auto *inputL = buffer.getReadPointer(0);
const auto *inputR =
getTotalNumInputChannels() > 1 ? buffer.getReadPointer(1) : nullptr;
int numSamples = buffer.getNumSamples();
for (int channel = getTotalNumOutputChannels(); channel < buffer.getNumChannels();
++channel)
buffer.clear(channel, 0, numSamples);
// Haas comp smoothing coefficient (~5ms time constant, sample-rate independent)
const float smoothCoeff = 1.0f - std::exp(-1.0f / (0.005f * static_cast<float>(currentSampleRate)));
float displayedLeftCompDb = 0.0f;
float displayedRightCompDb = 0.0f;
float displayedEarlierPath = -1.0f;
for (int i = 0; i < numSamples; ++i) {
const float sourceL = inputL[i];
const float sourceR = inputR != nullptr ? inputR[i] : sourceL;
const float dryL = dryDelayLeft.processSample(sourceL);
const float dryR = dryDelayRight.processSample(sourceR);
// Path A (left voice / input)
float sA = voiceLeft.processSample(sourceL);
// Path B (right voice / input)
float sB = voiceRight.processSample(sourceR);
const auto haasState = computeHaasCompensationState(
voiceLeft.getCurrentDelayMs(), voiceRight.getCurrentDelayMs(),
haasEnable, haasAmtNorm, panSeparationWeight,
hasMeaningfulPanSeparation);
currentLeftCompDb += (haasState.leftCompDb - currentLeftCompDb) * smoothCoeff;
currentRightCompDb +=
(haasState.rightCompDb - currentRightCompDb) * smoothCoeff;
const float gPrecLeft = juce::Decibels::decibelsToGain(currentLeftCompDb);
const float gPrecRight = juce::Decibels::decibelsToGain(currentRightCompDb);
sA *= gPrecLeft;
sB *= gPrecRight;
displayedLeftCompDb = currentLeftCompDb;
displayedRightCompDb = currentRightCompDb;
displayedEarlierPath = haasState.earlierPath;
// Pan and Mix
float outL = (sA * leftPL) + (sB * rightPL);
float outR = (sA * leftPR) + (sB * rightPR);
if (bypassed) {
channelL[i] = dryL;
channelR[i] = dryR;
} else {
// Output Gain
channelL[i] = outL * gainLinear;
channelR[i] = outR * gainLinear;
}
}
leftDelayReadout.store(voiceLeft.getCurrentDelayMs() - readoutReferenceMs,
std::memory_order_relaxed);
rightDelayReadout.store(voiceRight.getCurrentDelayMs() - readoutReferenceMs,
std::memory_order_relaxed);
leftPitchReadout.store(
pitchShiftActive ? voiceLeft.getCurrentDriftCents() : 0.0f,
std::memory_order_relaxed);
rightPitchReadout.store(
pitchShiftActive ? voiceRight.getCurrentDriftCents() : 0.0f,
std::memory_order_relaxed);
earlierPathReadout.store(displayedEarlierPath, std::memory_order_relaxed);
leftCompReadout.store(displayedLeftCompDb, std::memory_order_relaxed);
rightCompReadout.store(displayedRightCompDb, std::memory_order_relaxed);
}
bool VocalWidenerProcessor::isPitchShiftActive(float pitchDiffCents) const {
return std::abs(pitchDiffCents) >= 0.01f;
}
float VocalWidenerProcessor::computeAdtMaxExcursionMs(
float driftAmountCents) const {
const auto [leftDriftCents, rightDriftCents] =
computeVoiceDriftAmounts(driftAmountCents);
const float maxVoiceDriftCents = juce::jmax(leftDriftCents, rightDriftCents);
if (maxVoiceDriftCents < 0.01f)
return 0.0f;
const double speedDelta =
std::abs(1.0 - std::pow(2.0, maxVoiceDriftCents / 1200.0));
const double excursionSeconds =
(speedDelta * static_cast<double>(adtMaxSegmentSeconds)) /
adtSmoothstepPeakSlope;
return static_cast<float>(excursionSeconds * 1000.0);
}
int VocalWidenerProcessor::computeLatencySamples(float offsetMs, bool centered,
float driftAmountCents) const {
const float totalLatencyMs =
computeAdtSharedLatencyMs(driftAmountCents) +
(centered ? offsetMs : 0.0f);
return juce::roundToInt(
(totalLatencyMs * static_cast<float>(currentSampleRate)) / 1000.0f);
}
float VocalWidenerProcessor::computeAdtSharedLatencyMs(float driftAmountCents) const {
const float excursionMs = computeAdtMaxExcursionMs(driftAmountCents);
if (excursionMs < 0.001f)
return 0.0f;
return excursionMs + 0.5f;
}
std::pair<float, float> VocalWidenerProcessor::computeVoiceDriftAmounts(
float driftAmountCents) const {
const float clampedAmount = juce::jmax(0.0f, driftAmountCents);
return {clampedAmount, clampedAmount};
}
float VocalWidenerProcessor::getReportedLatencyMs() const {
if (currentSampleRate <= 0.0)
return 0.0f;
const int latencySamples = getCommittedLatencyState().latencySamples;
return (static_cast<float>(latencySamples) * 1000.0f) /
static_cast<float>(currentSampleRate);
}
double VocalWidenerProcessor::getTailLengthSeconds() const {
const double maxTailMs =
static_cast<double>(maxCenteredRightDelayMs) +
static_cast<double>(computeAdtSharedLatencyMs(maxPitchDiffCents)) +
static_cast<double>(computeAdtMaxExcursionMs(maxPitchDiffCents));
return maxTailMs /
1000.0;
}
juce::AudioProcessorParameter* VocalWidenerProcessor::getBypassParameter() const {
return apvts.getParameter("bypass");
}
void VocalWidenerProcessor::queueLatencyUpdate(float offsetMs, bool centered,
float driftAmountCents,
int latencySamples) {
pendingLatencyOffsetMs.store(offsetMs, std::memory_order_relaxed);
pendingLatencyCentered.store(centered ? 1 : 0, std::memory_order_relaxed);
pendingLatencyDriftAmountCents.store(driftAmountCents,
std::memory_order_relaxed);
pendingLatencySamples.store(latencySamples, std::memory_order_relaxed);
pendingLatencyUpdate.store(1, std::memory_order_relaxed);
triggerAsyncUpdate();
}
void VocalWidenerProcessor::handleLatencyParameterChanged(
const juce::String ¶meterID, float newValue) {
const float offsetMs =
parameterID == "offsetTime"
? newValue
: (offsetTimeParam != nullptr
? offsetTimeParam->load(std::memory_order_relaxed)
: 0.0f);
const bool centered =
parameterID == "centeredTiming"
? (newValue > 0.5f)
: (centeredTimingParam != nullptr &&
centeredTimingParam->load(std::memory_order_relaxed) > 0.5f);
const float driftAmountCents =
parameterID == "pitchDiff"
? newValue
: (pitchDiffParam != nullptr
? pitchDiffParam->load(std::memory_order_relaxed)
: 0.0f);
const int latencySamples =
computeLatencySamples(offsetMs, centered, driftAmountCents);
const auto committedLatencyState = getCommittedLatencyState();
const bool matchesCommitted = matchesLatencyState(
committedLatencyState.offsetMs, committedLatencyState.centered,
committedLatencyState.driftAmountCents,
committedLatencyState.latencySamples, offsetMs, centered,
driftAmountCents, latencySamples);
const bool hasPendingLatencyUpdate =
pendingLatencyUpdate.load(std::memory_order_relaxed) != 0;
if (matchesCommitted && !hasPendingLatencyUpdate)
return;
if (canSynchronouslyCommitLatencyChange()) {
const juce::ScopedLock callbackLock(getCallbackLock());
if (canSynchronouslyCommitLatencyChange()) {
syncLatencyStateFromParameters(true);
return;
}
}
queueLatencyUpdate(offsetMs, centered, driftAmountCents, latencySamples);
}
bool VocalWidenerProcessor::canSynchronouslyCommitLatencyChange() const {
if (activeProcessBlockCount.load(std::memory_order_acquire) != 0)
return false;
const double lastBlockEndMs =
lastProcessBlockEndMs.load(std::memory_order_relaxed);
if (lastBlockEndMs <= 0.0)
return true;
return (juce::Time::getMillisecondCounterHiRes() - lastBlockEndMs) >=
latencyCommitIdleWindowMs;
}
void VocalWidenerProcessor::syncLatencyStateFromParameters(
bool updateHostLatencyNow) {
const float offsetMs =
offsetTimeParam != nullptr
? offsetTimeParam->load(std::memory_order_relaxed)
: 0.0f;
const bool centered =
centeredTimingParam != nullptr &&
centeredTimingParam->load(std::memory_order_relaxed) > 0.5f;
const float driftAmountCents =
pitchDiffParam != nullptr
? pitchDiffParam->load(std::memory_order_relaxed)
: 0.0f;
const int latencySamples =
computeLatencySamples(offsetMs, centered, driftAmountCents);
storeCommittedLatencyState(offsetMs, centered, driftAmountCents,
latencySamples);
activeLatencySamples = latencySamples;
pendingLatencyUpdate.store(0, std::memory_order_relaxed);
pendingLatencySamples.store(latencySamples, std::memory_order_relaxed);
if (dryDelayLeft.isPrepared())
dryDelayLeft.setDelaySamples(latencySamples);
if (dryDelayRight.isPrepared())
dryDelayRight.setDelaySamples(latencySamples);
if (updateHostLatencyNow)
setLatencySamples(latencySamples);
}
VocalWidenerProcessor::LatencyStateSnapshot
VocalWidenerProcessor::getCommittedLatencyState() const {
LatencyStateSnapshot snapshot;
for (;;) {
const auto versionStart =
committedLatencyStateVersion.load(std::memory_order_acquire);
if ((versionStart & 1u) != 0u)
continue;
snapshot.offsetMs = committedOffsetMs.load(std::memory_order_relaxed);
snapshot.centered =
committedCentered.load(std::memory_order_relaxed) > 0;
snapshot.driftAmountCents =
committedDriftAmountCents.load(std::memory_order_relaxed);
snapshot.latencySamples =
committedLatencySamples.load(std::memory_order_relaxed);
const auto versionEnd =
committedLatencyStateVersion.load(std::memory_order_acquire);
if (versionStart == versionEnd)
return snapshot;
}
}
void VocalWidenerProcessor::storeCommittedLatencyState(float offsetMs,
bool centered,
float driftAmountCents,
int latencySamples) {
committedLatencyStateVersion.fetch_add(1, std::memory_order_acq_rel);
committedOffsetMs.store(offsetMs, std::memory_order_relaxed);
committedCentered.store(centered ? 1 : 0, std::memory_order_relaxed);
committedDriftAmountCents.store(driftAmountCents,
std::memory_order_relaxed);
committedLatencySamples.store(latencySamples, std::memory_order_relaxed);
committedLatencyStateVersion.fetch_add(1, std::memory_order_release);
}
void VocalWidenerProcessor::syncLinkedPanStateFromParameters(
bool scheduleMirror) {
const float leftValue =
leftPanParam != nullptr
? leftPanParam->load(std::memory_order_relaxed)
: 100.0f;
const bool linked =
linkPanParam != nullptr &&
linkPanParam->load(std::memory_order_relaxed) > 0.5f;
linkedPanValue.store(leftValue, std::memory_order_relaxed);
if (!linked) {
pendingPanMirrorTarget.store(noPanMirrorPending, std::memory_order_relaxed);
return;
}
if (scheduleMirror)
this->scheduleLinkedPanMirror(mirrorRightPan, leftValue);
}
void VocalWidenerProcessor::scheduleLinkedPanMirror(PanMirrorTarget target,
float value) {
pendingPanMirrorValue.store(value, std::memory_order_relaxed);
pendingPanMirrorTarget.store(target, std::memory_order_relaxed);
triggerAsyncUpdate();
}
juce::String VocalWidenerProcessor::normaliseLanguageCode(
juce::String languageCodeToNormalise) {
auto code = languageCodeToNormalise.trim().toLowerCase();
if (code == "ja" || code == "jp" || code == "japanese" ||
code == juce::String::fromUTF8("日本語"))
return "ja";
return "en";
}
juce::String VocalWidenerProcessor::getLanguageCode() const {
return normaliseLanguageCode(languageCode);
}
void VocalWidenerProcessor::setLanguageCode(
const juce::String &newLanguageCode) {
languageCode = normaliseLanguageCode(newLanguageCode);
apvts.state.setProperty(uiLanguageStateKey, languageCode, nullptr);
}
void VocalWidenerProcessor::handleAsyncUpdate() {
if (pendingLatencyUpdate.exchange(0, std::memory_order_relaxed) != 0) {
const int latencySamples =
pendingLatencySamples.load(std::memory_order_relaxed);
const float offsetMs =
pendingLatencyOffsetMs.load(std::memory_order_relaxed);
const bool centered =
pendingLatencyCentered.load(std::memory_order_relaxed) > 0;
const float driftAmountCents =
pendingLatencyDriftAmountCents.load(std::memory_order_relaxed);
setLatencySamples(latencySamples);
storeCommittedLatencyState(offsetMs, centered, driftAmountCents,
latencySamples);
}
const auto target = static_cast<PanMirrorTarget>(
pendingPanMirrorTarget.exchange(noPanMirrorPending,
std::memory_order_relaxed));
if (target == noPanMirrorPending)
return;
const bool linked =
linkPanParam != nullptr &&
linkPanParam->load(std::memory_order_relaxed) > 0.5f;
if (!linked)
return;
auto *parameter =
apvts.getParameter(target == mirrorLeftPan ? "leftPan" : "rightPan");
if (parameter == nullptr)
return;
const float mirroredValue = juce::jlimit(
0.0f, 100.0f, pendingPanMirrorValue.load(std::memory_order_relaxed));
const float normalizedValue = parameter->convertTo0to1(mirroredValue);
if (std::abs(parameter->getValue() - normalizedValue) < 1.0e-6f)
return;
panMirrorWriteTarget.store(target, std::memory_order_relaxed);
panMirrorWriteValue.store(mirroredValue, std::memory_order_relaxed);
parameter->setValueNotifyingHost(normalizedValue);
panMirrorWriteTarget.store(noPanMirrorPending, std::memory_order_relaxed);
}
void VocalWidenerProcessor::getStateInformation(juce::MemoryBlock &destData) {
auto state = apvts.copyState();
state.setProperty(uiLanguageStateKey, getLanguageCode(), nullptr);
std::unique_ptr<juce::XmlElement> xml(state.createXml());
copyXmlToBinary(*xml, destData);
}
void VocalWidenerProcessor::setStateInformation(const void *data,
int sizeInBytes) {
std::unique_ptr<juce::XmlElement> xmlState(
getXmlFromBinary(data, sizeInBytes));
if (xmlState.get() != nullptr)
if (xmlState->hasTagName(apvts.state.getType())) {
const juce::ScopedLock callbackLock(getCallbackLock());
apvts.replaceState(juce::ValueTree::fromXml(*xmlState));
syncLatencyStateFromParameters(true);
syncLinkedPanStateFromParameters(true);
setLanguageCode(apvts.state.getProperty(uiLanguageStateKey, "en")
.toString());
}
}
juce::AudioProcessorEditor *VocalWidenerProcessor::createEditor() {
return new VocalWidenerEditor(*this);
}
//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor *JUCE_CALLTYPE createPluginFilter() {
return new VocalWidenerProcessor();
}