This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
SoundData.Sfxr.cs
executable file
·1036 lines (859 loc) · 33.9 KB
/
SoundData.Sfxr.cs
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
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//
using System;
namespace PixelVision8.Player
{
public partial class SoundData
{
public static readonly string DEFAULT_SOUND_PARAM = "0,,.0185,.4397,.1783,.8434,,,,,,,,,,,,,1,,,,,.5";
public static SoundData Empty(string name = "Empty")
{
return new SoundData(name, DEFAULT_SOUND_PARAM);
}
public string param {
get
{
return GetSettingsString();
}
set
{
SetSettingsString(value);
}
}
public bool isWav => bytes != null;
public SoundData(string name, string param)
{
this.param = param != "" ? param : DEFAULT_SOUND_PARAM;
// SetSettingsString(param);
}
#region Sfxr Props
private static readonly Random _random = new Random();
private float _attackTime; // Length of the volume envelope attack (0 to 1)
private float _changeAmount; // Shift in note, either up or down (-1 to 1)
// private float _changeAmount2; // Shift in note, either up or down (-1 to 1)
private float
_changeRepeat; // Pitch Jump Repeat Speed: larger Values means more pitch jumps, which can be useful for arpeggiation (0 to 1)
private float _changeSpeed; // How fast the note shift happens (only happens once) (0 to 1)
private float _changeSpeed2; // How fast the note shift happens (only happens once) (0 to 1)
private float
_compressionAmount; // Compression: pushes amplitudes together into a narrower range to make them stand out more. Very good for sound effects, where you want them to stick out against background music (0 to 1)
private float _decayTime; // Length of the volume envelope decay (yes, I know it's called release) (0 to 1)
private float _deltaSlide; // Accelerates the slide (-1 to 1)
private float _dutySweep; // Sweeps the duty up or down (-1 to 1)
private float
_hpFilterCutoff; // Frequency at which the high-pass filter starts attenuating lower frequencies (0 to 1)
private float _hpFilterCutoffSweep; // Sweeps the high-pass cutoff up or down (-1 to 1)
private float
_lpFilterCutoff; // Frequency at which the low-pass filter starts attenuating higher frequencies (0 to 1)
private float _lpFilterCutoffSweep; // Sweeps the low-pass cutoff up or down (-1 to 1)
private float
_lpFilterResonance; // Changes the attenuation rate for the low-pass filter, changing the timbre (0 to 1)
private float _masterVolume = 0.5f; // Overall volume of the sound (0 to 1)
private float
_minFrequency; // If sliding, the sound will stop at this frequency, to prevent really low notes (0 to 1)
private float _phaserOffset; // Offsets a second copy of the wave by a small phase, changing the tibre (-1 to 1)
private float _phaserSweep; // Sweeps the phase up or down (-1 to 1)
private float _repeatSpeed; // Speed of the note repeating - certain variables are reset each time (0 to 1)
private float _slide; // Slides the note up or down (-1 to 1)
private float
_squareDuty; // Controls the ratio between the up and down states of the square wave, changing the tibre (0 to 1)
private float _startFrequency; // Base note of the sound (0 to 1)
private float _sustainPunch; // Tilts the sustain envelope for more 'pop' (0 to 1)
private float _sustainTime; // Length of the volume envelope sustain (0 to 1)
private float _vibratoDepth; // Strength of the vibrato effect (0 to 1)
private float _vibratoSpeed; // Speed of the vibrato effect (i.e. frequency) (0 to 1)
private WaveType _waveType; // Shape of wave to generate (see enum WaveType)
// public bool
// paramsDirty; // Whether the parameters have been changed since last time (shouldn't used cached sound)
/// <summary>
/// Shape of the wave (0:square, 1:sawtooth, 2:sin, 3:noise)
/// </summary>
public WaveType waveType
{
get => _waveType;
set
{
_waveType = value; // > 8 ? 0 : value;
Invalidate();
}
}
/// <summary>
/// Overall volume of the sound (0 to 1)
/// </summary>
public float masterVolume
{
get => _masterVolume;
set
{
_masterVolume = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Length of the volume envelope attack (0 to 1)
/// </summary>
public float attackTime
{
get => _attackTime;
set
{
_attackTime = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Length of the volume envelope sustain (0 to 1)
/// </summary>
public float sustainTime
{
get => _sustainTime;
set
{
_sustainTime = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Tilts the sustain envelope for more 'pop' (0 to 1)
/// </summary>
public float sustainPunch
{
get => _sustainPunch;
set
{
_sustainPunch = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Length of the volume envelope decay (yes, I know it's called release) (0 to 1)
/// </summary>
public float decayTime
{
get => _decayTime;
set
{
_decayTime = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Base note of the sound (0 to 1)
/// </summary>
public float startFrequency
{
get => _startFrequency;
set
{
_startFrequency = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// If sliding, the sound will stop at this frequency, to prevent really low notes (0 to 1)
/// </summary>
public float minFrequency
{
get => _minFrequency;
set
{
_minFrequency = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Slides the note up or down (-1 to 1)
/// </summary>
public float slide
{
get => _slide;
set
{
_slide = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Accelerates the slide (-1 to 1)
/// </summary>
public float deltaSlide
{
get => _deltaSlide;
set
{
_deltaSlide = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Strength of the vibrato effect (0 to 1)
/// </summary>
public float vibratoDepth
{
get => _vibratoDepth;
set
{
_vibratoDepth = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Speed of the vibrato effect (i.e. frequency) (0 to 1)
/// </summary>
public float vibratoSpeed
{
get => _vibratoSpeed;
set
{
_vibratoSpeed = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Shift in note, either up or down (-1 to 1)
/// </summary>
public float changeAmount
{
get => _changeAmount;
set
{
_changeAmount = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// How fast the note shift happens (only happens once) (0 to 1)
/// </summary>
public float changeSpeed
{
get => _changeSpeed;
set
{
_changeSpeed = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Controls the ratio between the up and down states of the square wave, changing the tibre (0 to 1)
/// </summary>
public float squareDuty
{
get => _squareDuty;
set
{
_squareDuty = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Sweeps the duty up or down (-1 to 1)
/// </summary>
public float dutySweep
{
get => _dutySweep;
set
{
_dutySweep = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Speed of the note repeating - certain variables are reset each time (0 to 1)
/// </summary>
public float repeatSpeed
{
get => _repeatSpeed;
set
{
_repeatSpeed = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Offsets a second copy of the wave by a small phase, changing the tibre (-1 to 1)
/// </summary>
public float phaserOffset
{
get => _phaserOffset;
set
{
_phaserOffset = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Sweeps the phase up or down (-1 to 1)
/// </summary>
public float phaserSweep
{
get => _phaserSweep;
set
{
_phaserSweep = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Frequency at which the low-pass filter starts attenuating higher frequencies (0 to 1)
/// </summary>
public float lpFilterCutoff
{
get => _lpFilterCutoff;
set
{
_lpFilterCutoff = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Sweeps the low-pass cutoff up or down (-1 to 1)
/// </summary>
public float lpFilterCutoffSweep
{
get => _lpFilterCutoffSweep;
set
{
_lpFilterCutoffSweep = Clamp(value, -1, 1);
Invalidate();
}
}
/// <summary>
/// Changes the attenuation rate for the low-pass filter, changing the timbre (0 to 1)
/// </summary>
public float lpFilterResonance
{
get => _lpFilterResonance;
set
{
_lpFilterResonance = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Frequency at which the high-pass filter starts attenuating lower frequencies (0 to 1)
/// </summary>
public float hpFilterCutoff
{
get => _hpFilterCutoff;
set
{
_hpFilterCutoff = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Sweeps the high-pass cutoff up or down (-1 to 1)
/// </summary>
public float hpFilterCutoffSweep
{
get => _hpFilterCutoffSweep;
set
{
_hpFilterCutoffSweep = Clamp(value, -1, 1);
Invalidate();
}
}
// From BFXR
/// <summary>
/// Pitch Jump Repeat Speed: larger Values means more pitch jumps, which can be useful for arpeggiation (0 to 1)
/// </summary>
public float changeRepeat
{
get => _changeRepeat;
set
{
_changeRepeat = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// How fast the note shift happens (only happens once) (0 to 1)
/// </summary>
public float changeSpeed2
{
get => _changeSpeed2;
set
{
_changeSpeed2 = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Pushes amplitudes together into a narrower range to make them stand out more. Very good for sound effects, where
/// you want them to stick out against background music (0 to 1)
/// </summary>
public float compressionAmount
{
get => _compressionAmount;
set
{
_compressionAmount = Clamp(value, 0, 1);
Invalidate();
}
}
/// <summary>
/// Sets the parameters to generate a pickup/coin sound
/// </summary>
public void GeneratePickupCoin()
{
resetParams();
_startFrequency = 0.4f + GetRandom() * 0.5f;
_sustainTime = GetRandom() * 0.1f;
_decayTime = 0.1f + GetRandom() * 0.4f;
_sustainPunch = 0.3f + GetRandom() * 0.3f;
if (GetRandomBool())
{
_changeSpeed = 0.5f + GetRandom() * 0.2f;
var cnum = (int) (GetRandom() * 7f) + 1;
var cden = cnum + (int) (GetRandom() * 7f) + 2;
_changeAmount = cnum / (float) cden;
}
}
/// <summary>
/// Sets the parameters to generate a laser/shoot sound
/// </summary>
public void GenerateLaserShoot()
{
resetParams();
_waveType = (WaveType) (GetRandom() * 3);
// TODO need to remove sine
if (_waveType == WaveType.Sine && GetRandomBool()) _waveType = (WaveType) (GetRandom() * 2f);
_startFrequency = 0.5f + GetRandom() * 0.5f;
_minFrequency = _startFrequency - 0.2f - GetRandom() * 0.6f;
if (_minFrequency < 0.2f) _minFrequency = 0.2f;
_slide = -0.15f - GetRandom() * 0.2f;
if (GetRandom() < 0.33f)
{
_startFrequency = 0.3f + GetRandom() * 0.6f;
_minFrequency = GetRandom() * 0.1f;
_slide = -0.35f - GetRandom() * 0.3f;
}
if (GetRandomBool())
{
_squareDuty = GetRandom() * 0.5f;
_dutySweep = GetRandom() * 0.2f;
}
else
{
_squareDuty = 0.4f + GetRandom() * 0.5f;
_dutySweep = -GetRandom() * 0.7f;
}
_sustainTime = 0.1f + GetRandom() * 0.2f;
_decayTime = GetRandom() * 0.4f;
if (GetRandomBool()) _sustainPunch = GetRandom() * 0.3f;
if (GetRandom() < 0.33f)
{
_phaserOffset = GetRandom() * 0.2f;
_phaserSweep = -GetRandom() * 0.2f;
}
if (GetRandomBool()) _hpFilterCutoff = GetRandom() * 0.3f;
}
/// <summary>
/// Sets the parameters to generate an explosion sound
/// </summary>
public void GenerateExplosion()
{
resetParams();
_waveType = WaveType.Noise;
if (GetRandomBool())
{
_startFrequency = 0.1f + GetRandom() * 0.4f;
_slide = -0.1f + GetRandom() * 0.4f;
}
else
{
_startFrequency = 0.2f + GetRandom() * 0.7f;
_slide = -0.2f - GetRandom() * 0.2f;
}
_startFrequency *= _startFrequency;
if (GetRandom() < 0.2f) _slide = 0.0f;
if (GetRandom() < 0.33f) _repeatSpeed = 0.3f + GetRandom() * 0.5f;
_sustainTime = 0.1f + GetRandom() * 0.3f;
_decayTime = GetRandom() * 0.5f;
_sustainPunch = 0.2f + GetRandom() * 0.6f;
if (GetRandomBool())
{
_phaserOffset = -0.3f + GetRandom() * 0.9f;
_phaserSweep = -GetRandom() * 0.3f;
}
if (GetRandom() < 0.33f)
{
_changeSpeed = 0.6f + GetRandom() * 0.3f;
_changeAmount = 0.8f - GetRandom() * 1.6f;
}
}
/// <summary>
/// Sets the parameters to generate a powerup sound
/// </summary>
public void GeneratePowerup()
{
resetParams();
if (GetRandomBool())
_waveType = WaveType.Saw;
else
_squareDuty = GetRandom() * 0.6f;
if (GetRandomBool())
{
_startFrequency = 0.2f + GetRandom() * 0.3f;
_slide = 0.1f + GetRandom() * 0.4f;
_repeatSpeed = 0.4f + GetRandom() * 0.4f;
}
else
{
_startFrequency = 0.2f + GetRandom() * 0.3f;
_slide = 0.05f + GetRandom() * 0.2f;
if (GetRandomBool())
{
_vibratoDepth = GetRandom() * 0.7f;
_vibratoSpeed = GetRandom() * 0.6f;
}
}
_sustainTime = GetRandom() * 0.4f;
_decayTime = 0.1f + GetRandom() * 0.4f;
}
/// <summary>
/// Sets the parameters to generate a hit/hurt sound
/// </summary>
public void GenerateHitHurt()
{
resetParams();
_waveType = (WaveType) (GetRandom() * 3f);
if (_waveType == WaveType.Sine)
_waveType = WaveType.Noise;
else if (_waveType == 0) _squareDuty = GetRandom() * 0.6f;
_startFrequency = 0.2f + GetRandom() * 0.6f;
_slide = -0.3f - GetRandom() * 0.4f;
_sustainTime = GetRandom() * 0.1f;
_decayTime = 0.1f + GetRandom() * 0.2f;
if (GetRandomBool()) _hpFilterCutoff = GetRandom() * 0.3f;
}
/// <summary>
/// Sets the parameters to generate a jump sound
/// </summary>
public void GenerateJump()
{
resetParams();
_waveType = 0;
_squareDuty = GetRandom() * 0.6f;
_startFrequency = 0.3f + GetRandom() * 0.3f;
_slide = 0.1f + GetRandom() * 0.2f;
_sustainTime = 0.1f + GetRandom() * 0.3f;
_decayTime = 0.1f + GetRandom() * 0.2f;
if (GetRandomBool()) _hpFilterCutoff = GetRandom() * 0.3f;
if (GetRandomBool()) _lpFilterCutoff = 1.0f - GetRandom() * 0.6f;
}
/// <summary>
/// Sets the parameters to generate a blip/select sound
/// </summary>
public void GenerateBlipSelect()
{
resetParams();
_waveType = (WaveType) (GetRandom() * 2f);
if (_waveType == 0) _squareDuty = GetRandom() * 0.6f;
_startFrequency = 0.2f + GetRandom() * 0.4f;
_sustainTime = 0.1f + GetRandom() * 0.1f;
_decayTime = GetRandom() * 0.2f;
_hpFilterCutoff = 0.1f;
}
/// <summary>
/// Resets the parameters, used at the start of each generate function
/// </summary>
protected void resetParams()
{
Invalidate();
_waveType = 0;
_startFrequency = 0.3f;
_minFrequency = 0.0f;
_slide = 0.0f;
_deltaSlide = 0.0f;
_squareDuty = 0.0f;
_dutySweep = 0.0f;
_vibratoDepth = 0.0f;
_vibratoSpeed = 0.0f;
_attackTime = 0.0f;
_sustainTime = 0.3f;
_decayTime = 0.4f;
_sustainPunch = 0.0f;
_lpFilterResonance = 0.0f;
_lpFilterCutoff = 1.0f;
_lpFilterCutoffSweep = 0.0f;
_hpFilterCutoff = 0.0f;
_hpFilterCutoffSweep = 0.0f;
_phaserOffset = 0.0f;
_phaserSweep = 0.0f;
_repeatSpeed = 0.0f;
_changeSpeed = 0.0f;
_changeAmount = 0.0f;
// From BFXR
_changeRepeat = 0.0f;
// _changeAmount2 = 0.0f;
_changeSpeed2 = 0.0f;
_compressionAmount = 0.3f;
// _overtones = 0.0f;
// _overtoneFalloff = 0.0f;
// _bitCrush = 0.0f;
// _bitCrushSweep = 0.0f;
}
/// <summary>
/// Randomly adjusts the parameters ever so slightly
/// </summary>
/// <param name="__mutation"></param>
public void Mutate(float __mutation = 0.05f)
{
if (GetRandomBool()) startFrequency += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) minFrequency += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) slide += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) deltaSlide += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) squareDuty += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) dutySweep += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) vibratoDepth += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) vibratoSpeed += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) attackTime += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) sustainTime += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) decayTime += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) sustainPunch += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) lpFilterCutoff += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) lpFilterCutoffSweep += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) lpFilterResonance += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) hpFilterCutoff += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) hpFilterCutoffSweep += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) phaserOffset += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) phaserSweep += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) repeatSpeed += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) changeSpeed += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) changeAmount += GetRandom() * __mutation * 2f - __mutation;
// From BFXR
if (GetRandomBool()) changeRepeat += GetRandom() * __mutation * 2f - __mutation;
// if (GetRandomBool()) changeAmount2 += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) changeSpeed2 += GetRandom() * __mutation * 2f - __mutation;
if (GetRandomBool()) compressionAmount += GetRandom() * __mutation * 2f - __mutation;
}
/// <summary>
/// Sets all parameters to random values
/// </summary>
public void Randomize()
{
resetParams();
// TODO Need to make sure this stays within range
_waveType = (WaveType) (GetRandom() * 9f);
_attackTime = Pow(GetRandom() * 2f - 1f, 4);
_sustainTime = Pow(GetRandom() * 2f - 1f, 2);
_sustainPunch = Pow(GetRandom() * 0.8f, 2);
_decayTime = GetRandom();
_startFrequency = GetRandomBool() ? Pow(GetRandom() * 2f - 1f, 2) : Pow(GetRandom() * 0.5f, 3) + 0.5f;
_minFrequency = 0.0f;
_slide = Pow(GetRandom() * 2f - 1f, 3);
_deltaSlide = Pow(GetRandom() * 2f - 1f, 3);
_vibratoDepth = Pow(GetRandom() * 2f - 1f, 3);
_vibratoSpeed = GetRandom() * 2f - 1f;
_changeAmount = GetRandom() * 2f - 1f;
_changeSpeed = GetRandom() * 2f - 1f;
_squareDuty = GetRandom() * 2f - 1f;
_dutySweep = Pow(GetRandom() * 2f - 1f, 3);
_repeatSpeed = GetRandom() * 2f - 1f;
_phaserOffset = Pow(GetRandom() * 2f - 1f, 3);
_phaserSweep = Pow(GetRandom() * 2f - 1f, 3);
_lpFilterCutoff = 1f - Pow(GetRandom(), 3);
_lpFilterCutoffSweep = Pow(GetRandom() * 2f - 1f, 3);
_lpFilterResonance = GetRandom() * 2f - 1f;
_hpFilterCutoff = Pow(GetRandom(), 5);
_hpFilterCutoffSweep = Pow(GetRandom() * 2f - 1f, 5);
if (_attackTime + _sustainTime + _decayTime < 0.2f)
{
_sustainTime = 0.2f + GetRandom() * 0.3f;
_decayTime = 0.2f + GetRandom() * 0.3f;
}
if (_startFrequency > 0.7f && _slide > 0.2 || _startFrequency < 0.2 && _slide < -0.05) _slide = -_slide;
if (_lpFilterCutoff < 0.1f && _lpFilterCutoffSweep < -0.05f) _lpFilterCutoffSweep = -_lpFilterCutoffSweep;
// From BFXR
_changeRepeat = GetRandom();
// _changeAmount2 = GetRandom() * 2f - 1f;
_changeSpeed2 = GetRandom();
_compressionAmount = GetRandom();
}
// Setting string methods
/// <summary>
/// Returns a string representation of the parameters for copy/paste sharing in the old format (24 parameters,
/// SFXR/AS3SFXR compatible)
/// </summary>
/// <returns>A comma-delimited list of parameter values</returns>
private string GetSettingsString()
{
var str = "";
// 24 params
str += (int) waveType + ",";
str += To4DP(_attackTime) + ",";
str += To4DP(_sustainTime) + ",";
str += To4DP(_sustainPunch) + ",";
str += To4DP(_decayTime) + ",";
str += To4DP(_startFrequency) + ",";
str += To4DP(_minFrequency) + ",";
str += To4DP(_slide) + ",";
str += To4DP(_deltaSlide) + ",";
str += To4DP(_vibratoDepth) + ",";
str += To4DP(_vibratoSpeed) + ",";
str += To4DP(_changeAmount) + ",";
str += To4DP(_changeSpeed) + ",";
str += To4DP(_squareDuty) + ",";
str += To4DP(_dutySweep) + ",";
str += To4DP(_repeatSpeed) + ",";
str += To4DP(_phaserOffset) + ",";
str += To4DP(_phaserSweep) + ",";
str += To4DP(_lpFilterCutoff) + ",";
str += To4DP(_lpFilterCutoffSweep) + ",";
str += To4DP(_lpFilterResonance) + ",";
str += To4DP(_hpFilterCutoff) + ",";
str += To4DP(_hpFilterCutoffSweep) + ",";
str += To4DP(_masterVolume);
return str;
}
/// <summary>
/// Parses a settings string into the parameters
/// </summary>
/// <param name="__string">string Settings string to parse</param>
/// <returns>If the string successfully parsed</returns>
private void SetSettingsString(string __string)
{
var values = __string.Split(',');
resetParams();
// Parse the params if there are enough values
if (values.Length == 24)
{
waveType = (WaveType) ParseUint(values[0]);
attackTime = ParseFloat(values[1]);
sustainTime = ParseFloat(values[2]);
sustainPunch = ParseFloat(values[3]);
decayTime = ParseFloat(values[4]);
startFrequency = ParseFloat(values[5]);
minFrequency = ParseFloat(values[6]);
slide = ParseFloat(values[7]);
deltaSlide = ParseFloat(values[8]);
vibratoDepth = ParseFloat(values[9]);
vibratoSpeed = ParseFloat(values[10]);
changeAmount = ParseFloat(values[11]);
changeSpeed = ParseFloat(values[12]);
squareDuty = ParseFloat(values[13]);
dutySweep = ParseFloat(values[14]);
repeatSpeed = ParseFloat(values[15]);
phaserOffset = ParseFloat(values[16]);
phaserSweep = ParseFloat(values[17]);
lpFilterCutoff = ParseFloat(values[18]);
lpFilterCutoffSweep = ParseFloat(values[19]);
lpFilterResonance = ParseFloat(values[20]);
hpFilterCutoff = ParseFloat(values[21]);
hpFilterCutoffSweep = ParseFloat(values[22]);
masterVolume = ParseFloat(values[23]);
}
}
// Copying methods
/// <summary>
/// Returns a copy of this SfxrParams with all settings duplicated
/// </summary>
/// <returns>A copy of this SfxrParams</returns>
public SoundData Clone()
{
var outp = new SoundData(name, param);
outp.CopyFrom(this);
return outp;
}
/// <summary>
/// Copies parameters from another instance
/// </summary>
/// <param name="__params">Instance to copy parameters from</param>
/// <param name="__makeDirty"></param>
public void CopyFrom(SoundData __params, bool __makeDirty = false)
{
var wasDirty = Invalid;
SetSettingsString(GetSettingsString());
Invalid = wasDirty || __makeDirty;
}
// Utility methods
/// <summary>
/// Faster power function; this function takes about 36% of the time Mathf.Pow() would take in our use cases
/// </summary>
/// <param name="__pbase">base Base to raise to power</param>
/// <param name="__power">power Power to raise base by</param>
/// <returns>The calculated power</returns>
private float Pow(float __pbase, int __power)
{
switch (__power)
{
case 2: return __pbase * __pbase;
case 3: return __pbase * __pbase * __pbase;
case 4: return __pbase * __pbase * __pbase * __pbase;
case 5: return __pbase * __pbase * __pbase * __pbase * __pbase;
}
return 1f;
}
/// <summary>
/// Returns the number as a string to 4 decimal places
/// </summary>
/// <param name="__value">Number to convert</param>
/// <returns>Number to 4dp as a string</returns>
private string To4DP(float __value)
{
if (__value < 0.0001f && __value > -0.0001f) return "";
return __value.ToString("#.####");
}
/// <summary>
/// Parses a string into an uint value; also returns 0 if the string is empty, rather than an error
/// </summary>
/// <param name="__value"></param>
/// <returns></returns>
private uint ParseUint(string __value)
{
if (__value.Length == 0) return 0;
return uint.Parse(__value);
}
/// <summary>
/// Parses a string into a float value; also returns 0 if the string is empty, rather than an error
/// </summary>
/// <param name="__value"></param>
/// <returns></returns>
private float ParseFloat(string __value)