File: PluginProcessor.cpp

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (1215 lines) | stat: -rw-r--r-- 45,363 bytes parent folder | download
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Stefan Riedel
 Copyright (c) 2022 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite 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 3 of the License, or
 (at your option) any later version.

 The IEM plug-in suite 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 this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#include "PluginProcessor.h"
#include "PluginEditor.h"

//==============================================================================
GranularEncoderAudioProcessor::GranularEncoderAudioProcessor() :
    AudioProcessorBase (
#ifndef JucePlugin_PreferredChannelConfigurations
        BusesProperties()
    #if ! JucePlugin_IsMidiEffect
        #if ! JucePlugin_IsSynth
            .withInput ("Input", juce::AudioChannelSet::stereo(), true)
        #endif
            .withOutput ("Output",
                         ((juce::PluginHostType::getPluginLoadedAs()
                           == juce::AudioProcessor::wrapperType_VST3)
                              ? juce::AudioChannelSet::ambisonic (1)
                              : juce::AudioChannelSet::ambisonic (7)),
                         true)
    #endif
            ,
#endif
        createParameterLayout()),
    posC (1.0f, 0.0f, 0.0f),
    posL (1.0f, 0.0f, 0.0f),
    posR (1.0f, 0.0f, 0.0f),
    updatedPositionData (true)
{
    parameters.addParameterListener ("qw", this);
    parameters.addParameterListener ("qx", this);
    parameters.addParameterListener ("qy", this);
    parameters.addParameterListener ("qz", this);
    parameters.addParameterListener ("azimuth", this);
    parameters.addParameterListener ("elevation", this);
    parameters.addParameterListener ("roll", this);
    parameters.addParameterListener ("width", this);

    parameters.addParameterListener ("deltaTime", this);
    parameters.addParameterListener ("grainLength", this);

    parameters.addParameterListener ("orderSetting", this);
    parameters.addParameterListener ("useSN3D", this);

    orderSetting = parameters.getRawParameterValue ("orderSetting");
    useSN3D = parameters.getRawParameterValue ("useSN3D");
    qw = parameters.getRawParameterValue ("qw");
    qx = parameters.getRawParameterValue ("qx");
    qy = parameters.getRawParameterValue ("qy");
    qz = parameters.getRawParameterValue ("qz");
    azimuth = parameters.getRawParameterValue ("azimuth");
    elevation = parameters.getRawParameterValue ("elevation");

    shape = parameters.getRawParameterValue ("shape");
    size = parameters.getRawParameterValue ("size");

    roll = parameters.getRawParameterValue ("roll");
    width = parameters.getRawParameterValue ("width");

    deltaTime = parameters.getRawParameterValue ("deltaTime");
    deltaTimeMod = parameters.getRawParameterValue ("deltaTimeMod");

    grainLength = parameters.getRawParameterValue ("grainLength");
    grainLengthMod = parameters.getRawParameterValue ("grainLengthMod");

    position = parameters.getRawParameterValue ("position");
    positionMod = parameters.getRawParameterValue ("positionMod");

    pitch = parameters.getRawParameterValue ("pitch");
    pitchMod = parameters.getRawParameterValue ("pitchMod");

    windowAttack = parameters.getRawParameterValue ("windowAttack");
    windowAttackMod = parameters.getRawParameterValue ("windowAttackMod");

    windowDecay = parameters.getRawParameterValue ("windowDecay");
    windowDecayMod = parameters.getRawParameterValue ("windowDecayMod");

    mix = parameters.getRawParameterValue ("mix");
    sourceProbability = parameters.getRawParameterValue ("sourceProbability");

    freeze = parameters.getRawParameterValue ("freeze");
    spatialize2D = parameters.getRawParameterValue ("spatialize2D");

    highQuality = parameters.getRawParameterValue ("highQuality");

    processorUpdatingParams = false;

    sphericalInput = true; // input from ypr

    juce::FloatVectorOperations::clear (SHC, 64);
}

GranularEncoderAudioProcessor::~GranularEncoderAudioProcessor() = default;

//==============================================================================

int GranularEncoderAudioProcessor::getNumPrograms()
{
    return 1; // NB: some hosts don't cope very well if you tell them there are 0 programs,
    // so this should be at least 1, even if you're not really implementing programs.
}

int GranularEncoderAudioProcessor::getCurrentProgram()
{
    return 0;
}

void GranularEncoderAudioProcessor::setCurrentProgram (int index)
{
}

const juce::String GranularEncoderAudioProcessor::getProgramName (int index)
{
    return juce::String();
}

void GranularEncoderAudioProcessor::changeProgramName (int index, const juce::String& newName)
{
}

//==============================================================================
void GranularEncoderAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    checkInputAndOutput (this, 2, *orderSetting, true);

    bufferCopy.setSize (2, samplesPerBlock);

    dryAmbiBuffer.setSize (64, samplesPerBlock);
    wetAmbiBuffer.setSize (64, samplesPerBlock);

    circularBuffer.setSize (2,
                            juce::roundToInt (sampleRate * CIRC_BUFFER_SECONDS),
                            true); // seconds long circular buffer
    circularBufferLength = circularBuffer.getNumSamples();
    if (*freeze < 0.5f) // if in real-time mode, clean up state before processing
    {
        circularBufferWriteHead = 0;
        circularBuffer.clear();
    }

    writeGainCircBuffer.reset (sampleRate, 0.005f);
    writeGainCircBuffer.setCurrentAndTargetValue (1.0f);

    lastSampleRate = sampleRate;
    deltaTimeSamples = 0;

    for (int g = 0; g < maxNumGrains; g++)
    {
        grains[g].setBlockSize (samplesPerBlock);
    }

    const iem::Quaternion<float> quatC = quaternionDirection;

    const auto center = quatC.getCartesian();

    SHEval (7, center, _SHC);

    positionHasChanged = true; // just to be sure
}

void GranularEncoderAudioProcessor::releaseResources()
{
    // When playback stops, you can use this as an opportunity to free up any
    // spare memory, etc.
}

inline void GranularEncoderAudioProcessor::updateQuaternions()
{
    float ypr[3];
    ypr[0] = Conversions<float>::degreesToRadians (*azimuth);
    ypr[1] = -Conversions<float>::degreesToRadians (*elevation); // pitch
    ypr[2] = Conversions<float>::degreesToRadians (*roll);

    // updating not active params
    quaternionDirection.fromYPR (ypr);
    processorUpdatingParams = true;
    parameters.getParameter ("qw")->setValueNotifyingHost (
        parameters.getParameterRange ("qw").convertTo0to1 (quaternionDirection.w));
    parameters.getParameter ("qx")->setValueNotifyingHost (
        parameters.getParameterRange ("qx").convertTo0to1 (quaternionDirection.x));
    parameters.getParameter ("qy")->setValueNotifyingHost (
        parameters.getParameterRange ("qy").convertTo0to1 (quaternionDirection.y));
    parameters.getParameter ("qz")->setValueNotifyingHost (
        parameters.getParameterRange ("qz").convertTo0to1 (quaternionDirection.z));
    processorUpdatingParams = false;
}

void GranularEncoderAudioProcessor::updateEuler()
{
    float ypr[3];
    quaternionDirection = iem::Quaternion<float> (*qw, *qx, *qy, *qz);
    quaternionDirection.normalize();
    quaternionDirection.toYPR (ypr);

    // updating not active params
    processorUpdatingParams = true;
    parameters.getParameter ("azimuth")->setValueNotifyingHost (
        parameters.getParameterRange ("azimuth").convertTo0to1 (
            Conversions<float>::radiansToDegrees (ypr[0])));
    parameters.getParameter ("elevation")
        ->setValueNotifyingHost (
            parameters.getParameterRange ("elevation")
                .convertTo0to1 (-Conversions<float>::radiansToDegrees (ypr[1])));
    parameters.getParameter ("roll")->setValueNotifyingHost (
        parameters.getParameterRange ("roll").convertTo0to1 (
            Conversions<float>::radiansToDegrees (ypr[2])));
    processorUpdatingParams = false;
}

juce::Vector3D<float> GranularEncoderAudioProcessor::getRandomGrainDirection3D()
{
    float shape_abs = std::abs (*shape);
    float shape_param = std::pow (2, shape_abs);
    float size_param = *size;
    juce::Vector3D<float> centerDir = quaternionDirection.getCartesian();

    float azi_center;
    float ele_center;
    Conversions<float>::cartesianToSpherical (centerDir, azi_center, ele_center);
    float zen_center = (juce::MathConstants<float>::pi / 2.0f) - ele_center;

    // Uniform random distribution in [0,2*pi]
    float rand_phi =
        juce::Random::getSystemRandom().nextFloat() * 2.0f * juce::MathConstants<float>::pi;

    // Beta distribution to control shape of rotationally symmetric distribution around centerDir
    jassert (shape_param >= 1.0f);
    // beta_distribution<float> dist(shape, shape); // -> realized with std::gamma_distribution
    std::gamma_distribution<float> dist1 (shape_param, 1.0f);
    std::gamma_distribution<float> dist2 (shape_param, 1.0f);
    float gamma1 = dist1 (rng);
    float gamma2 = dist2 (rng);
    float eps = 1e-16f;
    float beta_val = (gamma1 + eps) / (gamma1 + gamma2 + eps);

    float sym_beta_sample;
    if (*shape < 0.0f)
        sym_beta_sample = 1.0f - abs (beta_val - 0.5f) * 2.0f;
    else
        sym_beta_sample = abs (beta_val - 0.5f) * 2.0f;

    // Input parameter size defines opening angle of distribution cap
    jassert (size_param >= 0.0f && size_param <= 360.0f);
    float size_factor = (size_param / 2.0f) / 180.0f;
    float beta_sized = sym_beta_sample * size_factor;

    // To achieve uniform distribution on a sphere
    float rand_theta = acos (2 * (1 - beta_sized) - 1);

    float sinZenith = sin (rand_theta);
    float x = cos (rand_phi) * sinZenith;
    float y = sin (rand_phi) * sinZenith;
    float z = cos (rand_theta);

    // Rotate random direction (on 'polar cap') to target center direction
    float cosAzi = cos (azi_center);
    float sinAzi = sin (azi_center);
    float cosZen = cos (zen_center);
    float sinZen = sin (zen_center);

    float target_x = cosAzi * cosZen * x - sinAzi * y + cosAzi * sinZen * z;
    float target_y = sinAzi * cosZen * x + cosAzi * y + sinAzi * sinZen * z;
    float target_z = -sinZen * x + cosZen * z;

    juce::Vector3D<float> vec (target_x, target_y, target_z);
    return vec;
}

juce::Vector3D<float> GranularEncoderAudioProcessor::getRandomGrainDirection2D()
{
    float shape_abs = std::abs (*shape);
    float shape_param = std::pow (2, shape_abs);
    float size_param = *size;
    juce::Vector3D<float> centerDir = quaternionDirection.getCartesian();

    float azi_center;
    float ele_center;
    Conversions<float>::cartesianToSpherical (centerDir, azi_center, ele_center);
    float zen_center = (juce::MathConstants<float>::pi / 2.0f) - ele_center;

    // Uniform random distribution in [0,2*pi]
    // float rand_phi = juce::Random::getSystemRandom().nextFloat() * 2.0f * juce::MathConstants<float>::pi;

    // Beta distribution to control shape of rotationally symmetric distribution around centerDir
    jassert (shape_param >= 1.0f);
    // beta_distribution<float> dist(shape, shape); // -> realized with std::gamma_distribution
    std::gamma_distribution<float> dist1 (shape_param, 1.0f);
    std::gamma_distribution<float> dist2 (shape_param, 1.0f);
    float gamma1 = dist1 (rng);
    float gamma2 = dist2 (rng);
    float eps = 1e-16f;
    float beta_val = (gamma1 + eps) / (gamma1 + gamma2 + eps);

    float sym_beta_sample;
    if (*shape < 0.0f)
        sym_beta_sample = 1.0f - abs (beta_val - 0.5f) * 2.0f;
    else
        sym_beta_sample = abs (beta_val - 0.5f) * 2.0f;

    // Input parameter size defines opening angle of distribution cap
    jassert (size_param >= 0.0f && size_param <= 360.0f);
    float size_factor = (size_param / 2.0f) / 180.0f;
    float beta_sized = sym_beta_sample * size_factor;

    // azi_center +- random spread
    float sign = (juce::Random::getSystemRandom().nextFloat() > 0.5f) * 2.0f - 1.0f;
    float rand_phi = azi_center + juce::MathConstants<float>::pi * beta_sized * sign;

    float sinZenith = sin (zen_center);
    float x = cos (rand_phi) * sinZenith;
    float y = sin (rand_phi) * sinZenith;
    float z = cos (zen_center);

    juce::Vector3D<float> vec (x, y, z);
    return vec;
}

juce::AudioBuffer<float> GranularEncoderAudioProcessor::getWindowBuffer (float modWeight)
{
    const float attackPercentage = *windowAttack;
    const float decayPercentage = *windowDecay;

    const float attackModPercentage = *windowAttackMod;
    const float decayModPercentage = *windowDecayMod;

    float newAttackPercentage = attackPercentage
                                + (juce::Random::getSystemRandom().nextFloat() - 0.5f) * 2.0f
                                      * modWeight * attackModPercentage;
    newAttackPercentage = std::min (newAttackPercentage, 50.0f);
    newAttackPercentage = std::max (newAttackPercentage, 0.0f);

    float newDecayPercentage = decayPercentage
                               + (juce::Random::getSystemRandom().nextFloat() - 0.5f) * 2.0f
                                     * modWeight * decayModPercentage;
    newDecayPercentage = std::min (newDecayPercentage, 50.0f);
    newDecayPercentage = std::max (newDecayPercentage, 0.0f);

    const int windowNumSamples = windowResolution;
    const int windowHalfNumSamples = windowResolution / 2;

    const int windowAttackSamples = newAttackPercentage / 100.0f * windowNumSamples;
    const int windowDecaySamples = newDecayPercentage / 100.0f * windowNumSamples;

    juce::AudioBuffer<float> windowBuffer;
    windowBuffer.setSize (1, windowResolution);

    float* windowBufferWritePtr = windowBuffer.getWritePointer (0);

    float pi_over_two = (juce::MathConstants<float>::pi / 2.0f);

    float windowAttackSamplesFloat = static_cast<float> (windowAttackSamples);
    float windowDecaySamplesFloat = static_cast<float> (windowDecaySamples);
    for (int i = 0; i < windowAttackSamples; i++)
    {
        // sine-squared fade-in
        windowBufferWritePtr[i] =
            std::pow (std::sin (static_cast<float> (i) / windowAttackSamplesFloat * pi_over_two),
                      2);
    }
    for (int i = windowAttackSamples; i < (windowNumSamples - windowDecaySamples); i++)
    {
        // rectangular part
        windowBufferWritePtr[i] = 1.0f;
    }
    int c = 0;
    for (int i = (windowNumSamples - windowDecaySamples); i < windowNumSamples; i++)
    {
        // cosine-squared fade-out
        windowBufferWritePtr[i] =
            std::pow (std::cos (static_cast<float> (c) / windowDecaySamplesFloat * pi_over_two), 2);
        c++;
    }

    return windowBuffer;
}

int GranularEncoderAudioProcessor::getStartPositionCircBuffer() const
{
    float modulatedPosition =
        *position + (*positionMod * juce::Random::getSystemRandom().nextFloat());

    //  Unidirectional modulation of seed index parameter
    int startPositionCircBuffer =
        circularBufferWriteHead - juce::roundToInt (modulatedPosition * lastSampleRate);
    if (startPositionCircBuffer < 0)
    {
        startPositionCircBuffer += circularBufferLength;
    }

    return startPositionCircBuffer;
}

std::pair<int, float> GranularEncoderAudioProcessor::getGrainLengthAndPitchFactor() const
{
    // Bidirectional modulation of grain length
    float grainLengthModSeconds = *grainLengthMod / 100.0f * *grainLength * 2
                                  * (juce::Random::getSystemRandom().nextFloat() - 0.5f);
    float newGrainLengthSeconds = *grainLength + grainLengthModSeconds;
    newGrainLengthSeconds = std::min (newGrainLengthSeconds, MAX_GRAIN_LENGTH);
    newGrainLengthSeconds = std::max (newGrainLengthSeconds, MIN_GRAIN_LENGTH);

    // jassert(newGrainLengthSeconds >= 0.001f && newGrainLengthSeconds =< 0.5f);
    float grainLengthSamplesFloat = newGrainLengthSeconds * lastSampleRate;

    // Unidirectional modulation of pitch (due to hard real-time constraint)
    const float maxPitchModulation = 12.0f;
    float pitchModSemitones =
        *pitchMod * (juce::Random::getSystemRandom().nextFloat() - 0.5f) * 2.0f;
    float pitchToUse = *pitch - pitchModSemitones;
    if (mode != OperationMode::Freeze)
    {
        // If in real-time mode, only pitching down is available.
        pitchToUse = std::min (pitchToUse, 0.0f);
        pitchToUse = std::max (pitchToUse, -12.0f);
    }
    float pitchReadFactor = std::pow (2.0f, (pitchToUse) / 12.0f);

    // Updated length of grain in samples
    int grainLengthSamples = static_cast<int> (
        std::min (newGrainLengthSeconds * (1 / pitchReadFactor), MAX_GRAIN_LENGTH * 2.0f)
        * lastSampleRate);

    return std::make_pair (grainLengthSamples, pitchReadFactor);
}

int GranularEncoderAudioProcessor::getDeltaTimeSamples()
{
    // Bidirectional modulation of deltaTime between grains
    float deltaTimeModSeconds = *deltaTimeMod / 100.0f * *deltaTime * 2.0f
                                * (juce::Random::getSystemRandom().nextFloat() - 0.5f);
    float newDeltaTime = *deltaTime + deltaTimeModSeconds;
    newDeltaTime = std::min (newDeltaTime, MAX_DELTA_T);
    newDeltaTime = std::max (newDeltaTime, MIN_DELTA_T);
    jassert (MIN_DELTA_T >= 0.001f && newDeltaTime <= MAX_DELTA_T);
    int deltaTimeSamples = juce::roundToInt (lastSampleRate * newDeltaTime);
    return deltaTimeSamples;
}

bool GranularEncoderAudioProcessor::getChannelToSeed()
{
    float seedSetting = (*sourceProbability / 2.0f) + 0.5f;
    float randomNumber = juce::Random::getSystemRandom().nextFloat();

    bool seedLeft;
    if (randomNumber > seedSetting)
        seedLeft = true;
    else
        seedLeft = false;

    return seedLeft;
}

bool GranularEncoderAudioProcessor::getFreezeGUIBool()
{
    if (*freeze < 0.5f)
        return false;
    else
        return true;
}

void GranularEncoderAudioProcessor::initializeModeTransition (bool freeze)
{
    if (mode == OperationMode::Realtime && freeze)
    {
        mode = OperationMode::ToFreeze;
        writeGainCircBuffer.setTargetValue (0.0f);
    }
    if (mode == OperationMode::Freeze && ! freeze)
    {
        mode = OperationMode::ToRealtime;
        writeGainCircBuffer.setTargetValue (1.0f);
    }
}

void GranularEncoderAudioProcessor::finishModeTransition()
{
    if (mode == OperationMode::ToFreeze && ! writeGainCircBuffer.isSmoothing())
    {
        // Reached Freeze State
        mode = OperationMode::Freeze;
        // writeCircularBufferToDisk("/Users/stefanriedel/Documents/CircularBufferFreezed.wav");
    }
    if (mode == OperationMode::ToRealtime && ! writeGainCircBuffer.isSmoothing())
    {
        // Reached Realtime State
        mode = OperationMode::Realtime;
    }
}

float GranularEncoderAudioProcessor::getMeanWindowGain()
{
    juce::AudioBuffer<float> meanWindow = getWindowBuffer (0.0f);
    const float* meanWindowReadPtr = meanWindow.getReadPointer (0);
    const int numSamplesWindow = meanWindow.getNumSamples();
    float windowGain = 0.0f;
    for (int i = 0; i < numSamplesWindow; i++)
    {
        windowGain += std::pow (meanWindowReadPtr[i], 2.0f);
    }
    windowGain = windowGain / static_cast<float> (numSamplesWindow);

    return windowGain;
}

// void GranularEncoderAudioProcessor::writeCircularBufferToDisk(juce::String filename)
// {
//     // Just a debug function to write buffer state to disk
//     float *writePointerLeft = circularBuffer.getWritePointer(0);
//     float *writePointerRight = circularBuffer.getWritePointer(1);

//     writePointerLeft[circularBufferWriteHead] = 0.5f;
//     writePointerRight[circularBufferWriteHead] = -0.5f;

//     juce::WavAudioFormat format;
//     std::unique_ptr<juce::AudioFormatWriter> writer;
//     writer.reset(format.createWriterFor(new juce::FileOutputStream(filename),
//                                         lastSampleRate,
//                                         circularBuffer.getNumChannels(),
//                                         24,
//                                         {},
//                                         0));
//     if (writer != nullptr)
//         writer->writeFromAudioSampleBuffer(circularBuffer, 0, circularBuffer.getNumSamples());
// }

void GranularEncoderAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer,
                                                  juce::MidiBuffer& midiMessages)
{
    checkInputAndOutput (this, 2, *orderSetting);

    const int L = buffer.getNumSamples();
    const int totalNumInputChannels = getTotalNumInputChannels() < 2 ? 1 : 2;

    const int ambisonicOrder =
        *orderSetting < 0.5f ? output.getOrder() : juce::roundToInt (orderSetting->load()) - 1;
    const int nChOut = juce::jmin (buffer.getNumChannels(), juce::square (ambisonicOrder + 1));

    for (int i = 0; i < totalNumInputChannels; ++i)
        bufferCopy.copyFrom (i, 0, buffer.getReadPointer (i), buffer.getNumSamples());

    // SH eval for center direction
    const iem::Quaternion<float> quatC = quaternionDirection;
    const auto center = quatC.getCartesian();

    if (positionHasChanged.compareAndSetBool (false, true))
    {
        SHEval (ambisonicOrder, center.x, center.y, center.z, SHC);

        if (*useSN3D > 0.5f)
        {
            juce::FloatVectorOperations::multiply (SHC, SHC, n3d2sn3d, nChOut);
        }
    }

    // init dry and wet ambi buffers
    buffer.clear();
    dryAmbiBuffer.clear();
    wetAmbiBuffer.clear();

    float mixAmount = *mix / 100.0f;
    float dryFactor = std::sqrt (1 - mixAmount);
    float wetFactor = std::sqrt (mixAmount);

    // DRY PROCESSING
    const float* leftIn = bufferCopy.getReadPointer (0);
    const float* rightIn = bufferCopy.getReadPointer (1);
    for (int i = 0; i < nChOut; ++i)
    {
        dryAmbiBuffer.copyFromWithRamp (i, 0, leftIn, buffer.getNumSamples(), _SHC[i], SHC[i]);
        dryAmbiBuffer.addFromWithRamp (i, 0, rightIn, buffer.getNumSamples(), _SHC[i], SHC[i]);
    }

    // GRANULAR PROCESSING
    float windowGain = getMeanWindowGain();
    float gainFactor;
    float overlap = std::min (*grainLength / *deltaTime, static_cast<float> (maxNumGrains));
    if (*positionMod > 0.0f)
    {
        // Formula for uncorrelated grain signals
        gainFactor = juce::jmin (std::sqrt (1.0f / overlap / windowGain), 1.0f) * 1.41f;
    }
    else
    {
        // More gain reduction if grains are highly correlated
        gainFactor = juce::jmin (1.0f / overlap / windowGain, 1.0f) * 1.41f;
    }

    // Get GUI state of Freeze button
    bool freeze_gui_state = getFreezeGUIBool();
    initializeModeTransition (freeze_gui_state);

    for (int i = 0; i < buffer.getNumSamples(); i++)
    {
        float nextCircBuffGain = writeGainCircBuffer.getNextValue();
        finishModeTransition();

        if (mode != OperationMode::Freeze)
        {
            // Fill circular buffer with audio input
            circularBuffer.setSample (0, circularBufferWriteHead, leftIn[i] * nextCircBuffGain);
            circularBuffer.setSample (1, circularBufferWriteHead, rightIn[i] * nextCircBuffGain);
        }

        if (grainTimeCounter >= deltaTimeSamples)
        {
            // start a grain at this sample time stamp (index i)
            grainTimeCounter = 0;
            // reset (possibly modulating) deltaTime after a grain is started
            deltaTimeSamples = getDeltaTimeSamples();
            for (int g = 0; g < maxNumGrains; g++)
            {
                if (! grains[g].isActive())
                {
                    // find the first free grain processor and pass parameters
                    juce::Vector3D<float> grainDir;
                    if (*spatialize2D > 0.5f)
                        grainDir = getRandomGrainDirection2D();
                    else
                        grainDir = getRandomGrainDirection3D();
                    SHEval (ambisonicOrder, grainDir.x, grainDir.y, grainDir.z, _grainSH[g]);
                    if (*useSN3D > 0.5f)
                    {
                        juce::FloatVectorOperations::multiply (_grainSH[g],
                                                               _grainSH[g],
                                                               n3d2sn3d,
                                                               nChOut);
                    }

                    std::array<float, 64> channelWeights;
                    std::copy (_grainSH[g], _grainSH[g] + 64, channelWeights.begin());
                    Grain::GrainJobParameters params;
                    params.startPositionCircBuffer = getStartPositionCircBuffer();
                    auto grainLengthAndPitch = getGrainLengthAndPitchFactor();
                    params.grainLengthSamples = grainLengthAndPitch.first;
                    params.pitchReadFactor = grainLengthAndPitch.second;
                    params.startOffsetInBlock = i;
                    params.channelWeights = channelWeights;
                    params.gainFactor = gainFactor;
                    params.seedFromLeftCircBuffer = getChannelToSeed();

                    params.windowBuffer = getWindowBuffer (1.0f);
                    grains[g].startGrain (params);
                    break;
                }
            }
        }
        else
        {
            // if it's not time for a grain yet, just increment the counter
            grainTimeCounter++;
        }

        if (mode != OperationMode::Freeze)
        {
            // increment circular buffer write head and wrap if needed
            circularBufferWriteHead++;
            if (circularBufferWriteHead >= circularBufferLength)
            {
                circularBufferWriteHead = 0;
            }
        }
    }

    // Render all active grains
    int numActiveGrains = 0;
    for (int g = 0; g < maxNumGrains; g++)
    {
        if (grains[g].isActive())
        {
            numActiveGrains++;
            grains[g].processBlock (wetAmbiBuffer, circularBuffer);
        }
    }

    for (int i = 0; i < nChOut; ++i)
    {
        buffer.addFrom (i, 0, dryAmbiBuffer, i, 0, buffer.getNumSamples(), dryFactor);
        buffer.addFrom (i, 0, wetAmbiBuffer, i, 0, buffer.getNumSamples(), wetFactor);
    }

    juce::FloatVectorOperations::copy (_SHC, SHC, nChOut);
}

//==============================================================================
bool GranularEncoderAudioProcessor::hasEditor() const
{
    return true; // (change this to false if you choose to not supply an editor)
}

juce::AudioProcessorEditor* GranularEncoderAudioProcessor::createEditor()
{
    return new GranularEncoderAudioProcessorEditor (*this, parameters);
}

void GranularEncoderAudioProcessor::parameterChanged (const juce::String& parameterID,
                                                      float newValue)
{
    if (! processorUpdatingParams)
    {
        if (parameterID == "qw" || parameterID == "qx" || parameterID == "qy"
            || parameterID == "qz")
        {
            sphericalInput = false;
            updateEuler();
            updatedPositionData = true;
            positionHasChanged = true;
        }
        else if (parameterID == "azimuth" || parameterID == "elevation" || parameterID == "roll")
        {
            sphericalInput = true;
            updateQuaternions();
            updatedPositionData = true;
            positionHasChanged = true;
        }
        else if (parameterID == "width")
        {
            updatedPositionData = true;
            positionHasChanged = true;
        }
    }
    if (parameterID == "orderSetting")
    {
        userChangedIOSettings = true;
        positionHasChanged = true;
    }
    else if (parameterID == "useSN3D")
    {
        positionHasChanged = true;
    }
}

//==============================================================================
void GranularEncoderAudioProcessor::getStateInformation (juce::MemoryBlock& destData)
{
    auto state = parameters.copyState();

    auto oscConfig = state.getOrCreateChildWithName ("OSCConfig", nullptr);
    oscConfig.copyPropertiesFrom (oscParameterInterface.getConfig(), nullptr);

    if (mode == OperationMode::Freeze)
    {
        for (int i = 0; i < circularBuffer.getNumChannels(); i++)
        {
            juce::MemoryBlock channelMemory (circularBuffer.getReadPointer (i),
                                             circularBuffer.getNumSamples() * sizeof (float));
            auto strXmlData = channelMemory.toBase64Encoding();
            juce::String attribute_name = "CircularBufferChannel" + juce::String (i);
            state.setProperty (attribute_name, strXmlData, nullptr);
        }

        sampleRateAtSerialize = lastSampleRate;

        state.setProperty ("SampleRateAtSerialize", sampleRateAtSerialize, nullptr);
        state.setProperty ("WriteHead", circularBufferWriteHead, nullptr);
    }

    state.setProperty ("FreezeModeState", (int) mode, nullptr);

    std::unique_ptr<juce::XmlElement> xml (state.createXml());
    copyXmlToBinary (*xml, destData);
}

void GranularEncoderAudioProcessor::setStateInformation (const void* data, int sizeInBytes)
{
    std::unique_ptr<juce::XmlElement> xmlState (getXmlFromBinary (data, sizeInBytes));
    if (xmlState.get() != nullptr)
    {
        if (xmlState->hasTagName (parameters.state.getType()))
        {
            parameters.replaceState (juce::ValueTree::fromXml (*xmlState));
            if (parameters.state.hasProperty ("OSCPort")) // legacy
            {
                oscParameterInterface.getOSCReceiver().connect (
                    parameters.state.getProperty ("OSCPort", juce::var (-1)));
                parameters.state.removeProperty ("OSCPort", nullptr);
            }

            auto oscConfig = parameters.state.getChildWithName ("OSCConfig");

            if (oscConfig.isValid())
                oscParameterInterface.setConfig (oscConfig);

            mode = static_cast<OperationMode> (
                (int) parameters.state.getProperty ("FreezeModeState", 0));

            if (mode == OperationMode::Freeze)
            {
                sampleRateAtSerialize =
                    parameters.state.getProperty ("SampleRateAtSerialize", lastSampleRate);
                juce::AudioBuffer<float> tempCircularBuffer;
                tempCircularBuffer.setSize (
                    2,
                    juce::roundToInt (sampleRateAtSerialize * CIRC_BUFFER_SECONDS));
                tempCircularBuffer.clear();

                for (int i = 0; i < circularBuffer.getNumChannels(); i++)
                {
                    juce::String attribute_name = "CircularBufferChannel" + juce::String (i);

                    if (parameters.state.hasProperty (attribute_name)) // legacy
                    {
                        juce::String strXmlData = parameters.state.getProperty (attribute_name);
                        juce::StringRef ref = juce::StringRef (strXmlData);
                        juce::MemoryBlock channelMemory;
                        channelMemory.fromBase64Encoding (strXmlData);
                        tempCircularBuffer.copyFrom (
                            i,
                            0,
                            static_cast<const float*> (channelMemory.getData()),
                            tempCircularBuffer.getNumSamples());
                    }
                }

                if (sampleRateAtSerialize != lastSampleRate)
                    resampleAudioBuffer (tempCircularBuffer,
                                         sampleRateAtSerialize,
                                         circularBuffer,
                                         lastSampleRate);
                else
                    circularBuffer = tempCircularBuffer;

                circularBufferWriteHead = parameters.state.getProperty ("WriteHead", 0);
                circularBufferWriteHead =
                    static_cast<int> (lastSampleRate / sampleRateAtSerialize
                                      * static_cast<float> (circularBufferWriteHead));
            }
        }
    }
}

void GranularEncoderAudioProcessor::resampleAudioBuffer (juce::AudioBuffer<float>& inAudioBuffer,
                                                         float inSampleRate,
                                                         juce::AudioBuffer<float>& outAudioBuffer,
                                                         float outSampleRate)
{
    double ratio = static_cast<double> (inSampleRate / outSampleRate);

    const float* const* inputs = inAudioBuffer.getArrayOfReadPointers();
    float* const* outputs = outAudioBuffer.getArrayOfWritePointers();
    for (int c = 0; c < outAudioBuffer.getNumChannels(); c++)
    {
        std::unique_ptr<juce::LagrangeInterpolator> resampler =
            std::make_unique<juce::LagrangeInterpolator>();
        resampler->reset();
        resampler->process (ratio, inputs[c], outputs[c], outAudioBuffer.getNumSamples());
    }
}

//==============================================================================

const bool
    GranularEncoderAudioProcessor::processNotYetConsumedOSCMessage (const juce::OSCMessage& message)
{
    juce::String prefix ("/" + juce::String (JucePlugin_Name));
    if (! message.getAddressPattern().toString().startsWith (prefix))
        return false;

    juce::OSCMessage msg (message);
    msg.setAddressPattern (message.getAddressPattern().toString().substring (
        juce::String (JucePlugin_Name).length() + 1));

    if (msg.getAddressPattern().toString().equalsIgnoreCase ("/quaternions") && msg.size() == 4)
    {
        float qs[4];
        for (int i = 0; i < 4; ++i)
            if (msg[i].isFloat32())
                qs[i] = msg[i].getFloat32();
            else if (msg[i].isInt32())
                qs[i] = msg[i].getInt32();

        oscParameterInterface.setValue ("qw", qs[0]);
        oscParameterInterface.setValue ("qx", qs[1]);
        oscParameterInterface.setValue ("qy", qs[2]);
        oscParameterInterface.setValue ("qz", qs[3]);

        return true;
    }

    return false;
}

//==============================================================================
std::vector<std::unique_ptr<juce::RangedAudioParameter>>
    GranularEncoderAudioProcessor::createParameterLayout()
{
    // add your audio parameters here
    std::vector<std::unique_ptr<juce::RangedAudioParameter>> params;

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "orderSetting",
        "Ambisonics Order",
        "",
        juce::NormalisableRange<float> (0.0f, 8.0f, 1.0f),
        0.0f,
        [] (float value)
        {
            if (value >= 0.5f && value < 1.5f)
                return "0th";
            else if (value >= 1.5f && value < 2.5f)
                return "1st";
            else if (value >= 2.5f && value < 3.5f)
                return "2nd";
            else if (value >= 3.5f && value < 4.5f)
                return "3rd";
            else if (value >= 4.5f && value < 5.5f)
                return "4th";
            else if (value >= 5.5f && value < 6.5f)
                return "5th";
            else if (value >= 6.5f && value < 7.5f)
                return "6th";
            else if (value >= 7.5f)
                return "7th";
            else
                return "Auto";
        },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "useSN3D",
        "Normalization",
        "",
        juce::NormalisableRange<float> (0.0f, 1.0f, 1.0f),
        1.0f,
        [] (float value)
        {
            if (value >= 0.5f)
                return "SN3D";
            else
                return "N3D";
        },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "qw",
        "Quaternion W",
        "",
        juce::NormalisableRange<float> (-1.0f, 1.0f, 0.001f),
        1.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "qx",
        "Quaternion X",
        "",
        juce::NormalisableRange<float> (-1.0f, 1.0f, 0.001f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "qy",
        "Quaternion Y",
        "",
        juce::NormalisableRange<float> (-1.0f, 1.0f, 0.001f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "qz",
        "Quaternion Z",
        "",
        juce::NormalisableRange<float> (-1.0f, 1.0f, 0.001f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "azimuth",
        "Azimuth Angle",
        juce::CharPointer_UTF8 (R"(°)"),
        juce::NormalisableRange<float> (-180.0f, 180.0f, 0.01f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "elevation",
        "Elevation Angle",
        juce::CharPointer_UTF8 (R"(°)"),
        juce::NormalisableRange<float> (-180.0f, 180.0f, 0.01f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "shape",
        "Grain Shape",
        juce::CharPointer_UTF8 (R"()"),
        juce::NormalisableRange<float> (-10.0f, 10.0f, 0.1f),
        0.0,
        [] (float value) { return juce::String (value, 1); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "size",
        "Size",
        juce::CharPointer_UTF8 (R"(°)"),
        juce::NormalisableRange<float> (0.0f, 360.0f, 0.01f),
        180.0f,
        [] (float value) { return juce::String (value, 2); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "roll",
        "Roll Angle",
        juce::CharPointer_UTF8 (R"(°)"),
        juce::NormalisableRange<float> (-180.0f, 180.0f, 0.01f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr,
        true));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "width",
        "Stereo Width",
        juce::CharPointer_UTF8 (R"(°)"),
        juce::NormalisableRange<float> (-360.0f, 360.0f, 0.01f),
        0.0,
        [] (float value) { return juce::String (value, 2); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "deltaTime",
        "Delta Time",
        juce::CharPointer_UTF8 (R"(s)"),
        juce::NormalisableRange<float> (MIN_DELTA_T, MAX_DELTA_T, 1e-6f, GUI_SKEW),
        0.005f,
        [] (float value) { return juce::String (value, 3); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "deltaTimeMod",
        "Delta Time Modulation",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 100.0f, 0.1f),
        0.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "grainLength",
        "Grain Length",
        juce::CharPointer_UTF8 (R"(s)"),
        juce::NormalisableRange<float> (MIN_GRAIN_LENGTH, MAX_GRAIN_LENGTH, 0.0001f, GUI_SKEW),
        0.250f,
        [] (float value) { return juce::String (value, 3); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "grainLengthMod",
        "Grain Length Modulation",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 100.0f, 0.1f),
        0.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "position",
        "Read Position in Buffer",
        juce::CharPointer_UTF8 (R"(s)"),
        juce::NormalisableRange<float> (0.0f, CIRC_BUFFER_SECONDS / 2, 1e-6f),
        0.0f,
        [] (float value) { return juce::String (value, 3); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "positionMod",
        "ReadPosition Modulation",
        juce::CharPointer_UTF8 (R"(s)"),
        juce::NormalisableRange<float> (0.0f, CIRC_BUFFER_SECONDS / 2, 1e-6f, GUI_SKEW),
        0.05f,
        [] (float value) { return juce::String (value, 3); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "pitch",
        "Pitch",
        juce::CharPointer_UTF8 (R"(st)"),
        juce::NormalisableRange<float> (-12.0f, 12.0f, 0.001f),
        0.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "pitchMod",
        "Pitch Modulation",
        juce::CharPointer_UTF8 (R"(st)"),
        juce::NormalisableRange<float> (0.0f, 12.0f, 0.001f, GUI_SKEW),
        0.0f,
        [] (float value) { return juce::String (value, 2); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "windowAttack",
        "Grain Attack Time",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 50.0f, 0.1f),
        50.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "windowAttackMod",
        "Attack Time Modulation",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 100.0f, 0.1f),
        0.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "windowDecay",
        "Grain Decay Time",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 50.0f, 0.1f),
        50.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "windowDecayMod",
        "Decay Time Modulation",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 100.0f, 0.1f),
        0.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "mix",
        "Mix",
        juce::CharPointer_UTF8 (R"(%)"),
        juce::NormalisableRange<float> (0.0f, 100.0f, 0.1f),
        50.0f,
        [] (float value) { return juce::String (value, 1); },
        nullptr));
    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "sourceProbability",
        "Source Probability",
        juce::CharPointer_UTF8 (R"()"),
        juce::NormalisableRange<float> (-1.0f, 1.0f, 0.01f),
        0.0f,
        [] (float value) { return juce::String (value, 2); },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "freeze",
        "Freeze Mode",
        "",
        juce::NormalisableRange<float> (0.0f, 1.0f, 1.0f),
        0.0f,
        [] (float value)
        {
            if (value >= 0.5f)
                return "Yes";
            else
                return "No";
        },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "spatialize2D",
        "2D mode",
        "",
        juce::NormalisableRange<float> (0.0f, 1.0f, 1.0f),
        0.0f,
        [] (float value)
        {
            if (value >= 0.5f)
                return "2D";
            else
                return "3D";
        },
        nullptr));

    params.push_back (OSCParameterInterface::createParameterTheOldWay (
        "highQuality",
        "Sample-wise Panning",
        "",
        juce::NormalisableRange<float> (0.0f, 1.0f, 1.0f),
        0.0f,
        [] (float value) { return value < 0.5f ? "OFF" : "ON"; },
        nullptr));

    return params;
}

//==============================================================================
// This creates new instances of the plugin..
juce::AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
    return new GranularEncoderAudioProcessor();
}