-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.html
1392 lines (1223 loc) · 44.2 KB
/
index.html
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
<!--
ZzFX - Zuper Zmall Zound Zynth by Frank Force
https://github.com/KilledByAPixel/ZzFX
ZzFX UI Features
- Generates random sounds from presets.
- Sound list is saved automatically.
- Each parameter can be modified with constraints.
- Lock and mutate buttons for each parameter.
- Sound name can be changed for easier workflow.
- Shortens code for zzfx sound calls.
- Displays image of sound wave when played.
- Sounds can be download as a wave file.
- Sounds can be marked as favorites to prevent removal.
- Sounds can be loaded by pasting zzfx code for easy sharing.
- List of sounds can be exported and imported.
- Supports drag-and-drop of exported files into sound list.
GitHub Corner is Copyright (c) 2016 Tim Holman - http://tholman.com
-->
<!--
ZzFX MIT License
Copyright (c) 2019 - Frank Force
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<!doctype html>
<html>
<head>
<title>ZzFX - Zuper Zmall Zound Zynth</title>
<meta charset="utf-8">
<meta name="google" content="notranslate">
<meta property="og:title" content="ZzFX - Zuper Zmall Zound Zynth">
<meta property="og:description" content="A tiny sound effects generator for games and websites!">
<meta name="twitter:image" content="screenshot.jpg">
<meta property="og:image" content="screenshot.jpg">
<meta name="mobile-wep-app-capable" content="yes">
<meta name="apple-mobile-wep-app-capable" content="yes">
<meta name="viewport" content="user-scalable=no" />
<link rel="shortcut icon" type="image/x-icon" href="icon.png"/>
<link rel="manifest" href="manifest.json">
<style>
* { font-family:courier; }
a { color:#5AF; }
button { margin:3px; font-size:18px;}
button.large { height:26px; }
button.rand { width:15px; height:15px; background:#3ff; }
button.reset { width:15px; height:15px; background:#3F3; }
.soundSelect { overflow-x:hidden; height: 400px; }
.slider { cursor: pointer; }
input.lock { width:15px; height:15px; }
.settingRow { width:200px; display:flex }
.setting { width:100%; }
textarea { height:50px; cursor:text; resize:none; }
body,input,select,textarea { color:#FFF; font-size:18px;}
select,input,textarea,canvas { background:#000; border:1px solid #FFF; }
option:hover { background:#888; }
option:checked { background:#444; }
body { user-select:none; }
</style>
</head>
<body bgcolor=#222>
<div id=UI style=display:none>
<center>
<table>
<tr>
<td style=text-align:right;>
<font size=7><b><div id=div_logo style='display:inline'>ℤ𝕫𝔽𝕏</div></b></font>
</td>
<td style=text-align:left>
<div style=display:inline;font-size:22px id=div_logo2>
<i>Zuper Zmall Zound Zynth</i>
</div>
</td>
<td style=text-align:center;width:380px rowspan=2>
<div id=div_masterVolume></div>
<input id=slider_masterVolume title='Volume to scale all sounds by in percent' type=range min=0 max=100 value=25 style=width:90% oninput=UpdateSettings();SaveLocalStorage()>
<canvas title='Image of sound wave, click to play the sound [SPACE]' id=canvas_soundWave width=350 height=80 style=width:100%></canvas>
<button class=large title='Create new random sound [INS]' onclick=AddPresetSound()>Random</button><button class=large title='Make a new sound with mutated parameters [M]' onclick=CopySelected(1)>Mutate</button><button class=large title='Make copy of selected sound [C]' onclick=CopySelected()>Copy</button>
<br>
<button class=large title='Generate random pickup sound' onclick="AddPresetSound('Pickup')">Pickup</button><button class=large title='Generate random powerup sound' onclick="AddPresetSound('Powerup')">Powerup</button><button class=large title='Generate random shoot sound' onclick="AddPresetSound('Shoot')">Shoot</button><button class=large title='Generate a tonal sound for music' onclick="AddPresetSound('Music')">Note</button>
<br>
<button class=large title='Generate random jump sound' onclick="AddPresetSound('Jump')">Jump</button><button class=large title='Generate random blip sound' onclick="AddPresetSound('Blip')">Blip</button><button class=large title='Generate random hit sound' onclick="AddPresetSound('Hit')">Hit</button><button class=large title='Generate random explosion sound' onclick="AddPresetSound('Explosion')">Explosion</button>
<br>
<button class=large title='Toggle favorite on current sound [F]' onclick=FavoriteSelected()>Favorite</button><button class=large title='Move selected to top of the list' onclick="SelectedToTop()">Top</button><button title='Remove selected sound [DEL]' class=large onclick=RemoveSelected()>Remove</button>
<br>
<select id=select_soundList class=soundSelect onclick='loadedSound=0;LoadSelected(1)' onchange=LoadSelected(1) size=2 style=margin:5px;width:100%></select>
</td></tr>
<tr>
<td colspan=2>
<div id=div_settingsTable></div>
</td>
</tr>
<tr><td colspan=3 style=padding:5px>
<center>
<button title='Download the selected wave file' onclick=SaveWave()>Save Wav</button>
<button title='Load sound from zzfx code' onclick=LoadSound()>Load Sound</button>
<button title='Copy ZzFX code to clipboard' onclick=if(CopyToCliboard(textarea_code.value))eval(textarea_code.value)>Copy Sound</button>
<button title='Import all sounds from a text file.' onclick=Import()>Import</button>
<button title='Export all sounds to a text file.' onclick=Export()>Export</button>
<button title='Clear non-favorite sounds' onclick=ClearSoundsButton()>Clear</button>
<button title='Clear all sounds' onclick=ClearSoundsButton(1)>Clear All</button>
</center>
</td></tr>
<tr><td colspan=3 style=padding:5px>
<center>ZzFX JavaScript
<input type=radio name=radio_codeStyle id=input_codeStyleCompact checked onchange="SaveLocalStorage();textarea_code.value=GetCode(GetSelectedSound())">Compact
<input type=radio name=radio_codeStyle id=input_codeStyleFull onchange="SaveLocalStorage();textarea_code.value=GetCode(GetSelectedSound())">Full
<input type=radio name=radio_codeStyle id=input_codeStyleLittleJS onchange="SaveLocalStorage();textarea_code.value=GetCode(GetSelectedSound())">LittleJS
<br><textarea title='Use this code to play the selected sound' id=textarea_code style=width:90% readonly></textarea>
</center>
</td></tr>
</table>
ZzFX © <a href=https://www.frankforce.com target=_blank>Frank Force</a> 2019 ☮♥☻␌
<a hidden id=a_downloadLink></a>
<input hidden id=input_importFile type=file accept=.txt>
</center>
<script src=ZzFXMicro.js></script>
<script type=module>
// load zzfx module into global scope
import {ZZFX} from './ZzFX.js';
window.ZZFX = ZZFX;
import {buildWavURL} from './wav.js';
window.buildWavURL = buildWavURL;
Init();
</script>
<script>
'use strict'; // strict mode
let sounds = [];
let generatedSoundCount = 0;
let loadedSound;
let lastPlayedSound;
let defaultSound;
const favoriteColor = '#f00';
const SeededRandom=_=>
(
randomSeed ^= randomSeed << 13,
randomSeed ^= randomSeed >> 17,
randomSeed ^= randomSeed << 5,
(randomSeed%1e9)/1e9
)
let randomSeed = Date.now();
let loadedSoundSeed = 0;
// build a sound object with same defaults as zzfx
function BuildSound
(
volume = 1,
randomness = .05,
frequency = 220,
attack = 0,
sustain = 0,
release = .1,
shape = 0,
shapeCurve = 1,
slide = 0,
deltaSlide = 0,
pitchJump = 0,
pitchJumpTime = 0,
repeatTime = 0,
noise = 0,
modulation = 0,
bitCrush = 0,
delay = 0,
sustainVolume = 1,
decay = 0,
tremolo = 0,
filter = 0
)
{
const sound =
{
volume,
randomness,
frequency,
attack,
sustain,
release,
shape,
shapeCurve,
slide,
deltaSlide,
pitchJump,
pitchJumpTime,
repeatTime,
noise,
modulation,
bitCrush,
delay,
sustainVolume,
decay,
tremolo,
filter
};
return sound;
}
// convert sound parameters object to array
function SoundToArray(sound)
{
// use default sound for keys and order
const array = [];
for(const key in defaultSound)
array.push(sound[key]);
return array;
}
function Init()
{
defaultSound = BuildSound();
UI.style.display = '';
BuildSettingsTable();
RandomizeLogo();
LoadLocalStorage();
}
///////////////////////////////////////////////////////////////////////////////
// settings
const SETTING_TYPE_NAME = 1;
const SETTING_TYPE_SHAPE = 2;
function BuildSetting(step, min, max, name, niceName, help, canMutate=1, type=0)
{ return {name, step, min, max, niceName, help, type, canMutate}; }
const settings =
[
BuildSetting(0,0,0,'name','Name','Name of sound',0,SETTING_TYPE_NAME),
BuildSetting(.1,-1e9,1e9,'volume','Volume','Volume scale (percent)',0),
BuildSetting(.05,-1e9,1e9,'randomness','Randomness','How much to randomize frequency (percent Hz)',0),
BuildSetting(1,-1e9,1e9,'frequency','Frequency','Frequency of sound (Hz)'),
BuildSetting(1,0,0,'shape','Wave Shape','Shape of the sound wave',1,SETTING_TYPE_SHAPE),
BuildSetting(.1,0,1e9,'shapeCurve','Shape Curve','Squarenes of wave (0=square, 1=normal, 2=pointy)'),
BuildSetting(.01,0,3,'attack','Attack','Attack time, how fast sound starts (seconds)'),
BuildSetting(.01,0,1,'decay','Decay','Decay time, how long to reach sustain after attack (seconds)'),
BuildSetting(.01,0,3,'sustain','Sustain','Sustain time, how long sound holds (seconds)'),
BuildSetting(.01,0,3,'release','Release','Release time, how fast sound fades out (seconds)'),
BuildSetting(.1,-1e9,1e9,'sustainVolume','Sustain Volume','Volume level for sustain (percent)'),
BuildSetting(.1,-1e9,1e9,'slide','Slide','How much to slide frequency (kHz/s)'),
BuildSetting(.1,-1e9,1e9,'deltaSlide','Delta Slide','How much to change slide (kHz/s/s)'),
BuildSetting(50,-1e9,1e9,'pitchJump','Pitch Jump','Frequency of pitch jump (Hz)'),
BuildSetting(.01,-1e9,1e9,'pitchJumpTime','Pitch Jump Time','Time of pitch jump (seconds)'),
BuildSetting(.01,-1e9,1e9,'repeatTime','Repeat Time','Resets some parameters periodically (seconds)'),
BuildSetting(.01,0,1,'tremolo','Tremolo','Trembling effect, rate controlled by repeat time (precent)'),
BuildSetting(.1,-1e9,1e9,'noise','Noise','How much random noise to add (percent)'),
BuildSetting(.1,-1e9,1e9,'bitCrush','Bit Crush','Resamples at a lower frequency in (samples*100)'),
BuildSetting(.01,0,1e9,'delay','Delay','Overlap sound with itself for reverb and flanger effects (seconds)'),
BuildSetting(1,-1e9,1e9,'modulation','Modulation','Frequency of modulation wave, negative flips phase (Hz)'),
BuildSetting(1,-1e9,1e9,'filter','Filter','Filter cutoff frequency, positive for HPF, negative for LPF (Hz)'),
]
let noteScale;
function BuildSettingsTable()
{
const NoteName = n=>"CCDDEFFGGAAB"[n%12|0] + ('02579'.indexOf(n%12-1) < 0 ? '' : '#') + (n/12|0);
noteScale = [];
for(let i = 0; i < 37; i++)
{
// skip sharps
if ([1,3,6,8,10].includes(i%12))
continue;
noteScale.push([ZZFX.getNote(i+3-36).toPrecision(7), NoteName(i)]);
}
const s = settings[0];
let html = '<center><table style=text-align:center>';
html += '<tr><td style=text-align:right>' + s.niceName + ' </td><td class=settingRow>'
html += `<input title='${s.help}' class=setting step=${s.step} id=input_${s.name} oninput=SelectedWasChanged('${s.name}')>`
html += '</td><td>';
html += '<button title="Toggle lock all parameters" onclick=ToggleLockAll()>🔒</button>'
html += '</td><td>';
html += '<button title="Mutate all unlocked parameters" onclick=if(MutateAllSettings())PlaySelected()>🎲</button>';
html += '</td><td>';
html += '<button title="Sets all unlocked parameters to their default values." onclick=if(ResetAllSettings())PlaySelected()>♻️</button>';
html += '</td></tr>';
for(const i in settings)
{
const s = settings[i];
if (s.name == 'name')
continue;
html += '<tr>';
html += '<td style=text-align:right>';
html += s.niceName + ' ';
html += '</td><td class=settingRow>';
const isFrequency = s.name == 'frequency';
if (!s.type)
html += `<input id=input_${s.name} class=setting ${isFrequency?'style=width:140px':''} title='${s.help}' type=number step=${s.step} oninput=SelectedWasChanged('${s.name}') min=${s.min} max=${s.max} onfocusout=UpdateSettings()>`
else if (s.type == SETTING_TYPE_SHAPE)
{
html += `<select id=input_${s.name} class=setting title='${s.help}' oninput=SelectedWasChanged('${s.name}')>`;
html += `<option value=0>sine</option>`;
html += `<option value=1>triangle</option>`;
html += `<option value=2>saw</option>`;
html += `<option value=3>tan</option>`;
html += `<option value=4>noise</option>`;
html += `</select>`;
}
if (isFrequency)
{
html += `<select id=input_note class=setting style=width:100px title='Set frequency to note' oninput=SetFrequencyToNote()>`;
html += `<option value=-1></option>`;
noteScale.map( (note, i)=>html += `<option value=${i}>${note[1]}</option>`);
html += `</select>`;
}
html += '</td><td>';
html += `<input id=input_lock_${s.name} class=lock title='Lock ${s.niceName}' oninput=SelectedWasChanged('${s.name}') type=checkbox>`;
html += '</td><td>';
html += `<button id=input_mutate_${s.name} class=rand title='Mutate ${s.niceName}' onclick=MutateSetting(settings[${i}]);PlaySelected()></button>`;
html += '</td><td>';
html += `<button id=input_reset_${s.name} class=reset title='Reset ${s.niceName}' onclick=ResetSetting(settings[${i}]);PlaySelected()></button>`;
html += '</td></tr>';
}
html += '</table></center>'
div_settingsTable.innerHTML = html;
}
function GetSelectedSound()
{
if (select_soundList.selectedIndex < 0)
return;
const index = select_soundList.options[select_soundList.selectedIndex].value;
return sounds[index];
}
function PlaySelected()
{
if (lastPlayedSound)
{
try
{
const context = lastPlayedSound.context;
const gain = context.createGain();
lastPlayedSound.disconnect();
lastPlayedSound.connect(gain);
gain.connect(context.destination);
const t = lastPlayedSound.context.currentTime + .02;
gain.gain.linearRampToValueAtTime(1, t);
gain.gain.linearRampToValueAtTime(0, t + .1);
}
catch (e) { lastPlayedSound.stop(); }
lastPlayedSound = 0;
}
const sound = BuildSoundFromSettings();
const params = SoundToArray(sound);
if (ZZFX.volume > 0)
{
const samples = ZZFX.buildSamples(...params);
lastPlayedSound = ZZFX.play(...params);
DrawSoundWave(samples, ZZFX.volume, sound);
}
else
{
// just build samples without playing
const saveVolume = ZZFX.volume;
ZZFX.volume = 1;
const samples = ZZFX.buildSamples(...params);
DrawSoundWave(samples, 1, sound);
ZZFX.volume = saveVolume;
}
RandomizeLogo();
}
function BuildSoundFromSettings()
{
const sound = {};
for(const s of settings)
{
let v = document.getElementById("input_"+s.name).value;
if (s.type != SETTING_TYPE_NAME)
v = parseFloat(v) || 0;
sound[s.name] = v;
}
return sound;
}
function UpdateSettings()
{
const v = slider_masterVolume.value;
div_masterVolume.innerHTML = 'Master Volume ' + v + '%';
ZZFX.volume = v / 100;
const sound = GetSelectedSound();
if (sound)
{
// copy settings to selected
const option = select_soundList.options[select_soundList.selectedIndex];
const index = parseInt(option.value);
for(const s of settings)
{
const element = document.getElementById("input_"+s.name);
let v = element.value;
if (!s.type && element != document.activeElement)
{
// sanitize input
v = parseFloat(v);
if (isNaN(v) || !Number.isFinite(v))
v = 0;
else
v = Math.max(Math.min(v,s.max),s.min);
element.value = v;
}
sound[s.name] = v;
if (s.name == 'frequency')
{
// try to find matching note
const eNote = document.getElementById("input_note");
const noteIndex = noteScale.findIndex(e=>e[0] == v);
eNote.value = noteIndex;
}
const elementReset = document.getElementById("input_reset_"+s.name);
if (elementReset)
{
const isDefault = v == defaultSound[s.name];
elementReset.style.background = isDefault ? '' : '#f3f';
}
}
option.style.color = sound.favorite? favoriteColor : "inherit";
option.innerHTML = (sound.favorite?'-> ':sound.modified?'* ':'') + sound.name;
textarea_code.value = GetCode(sound);
}
SaveLocalStorage();
}
function BuildRandomSound(lengthScale=1, volume=1, randomness=.05)
{
// generate a random sound
const R=()=>Math.random(), C=()=>R()<.5?R():0, S=()=>C()?1:-1,
// randomize sound length
attack = R()**3/2*lengthScale,
decay = R()**3/2*lengthScale,
sustain = R()**3/2*lengthScale,
release = R()**3/2*lengthScale,
length = attack + decay + sustain + release,
filter = C()? 0 : R()<.5? 99+R()**2*900 : R()**2*1e3-1500;
// create random sound
return BuildSound
(
volume, // volume
randomness, // randomness
9+R()**2*1e3, // frequency
attack, // attack
sustain, // sustain
release, // release
R()*5|0, // shape
R()*5, // shapeCurve
C()**3*99*S(), // slide
C()**3*99*S(), // deltaSlide
C()**2*500*S(), // pitchJump
R()**2 * length, // pitchJumpTime
C() * length/4, // repeatTime
C()**4, // noise
R()*C()**2*500, // modulation
C()**4, // bitCrush
C()**3/2, // delay
1 - R()*.5, // sustain volume
decay, // decay
C()**2*.5, // tremolo
filter // filter
);
}
function AddPresetSound(presetName='Random')
{
let sound = BuildSound();
const R =(a=1,b=0) => b+(a-b)*Math.random();
if (R()<.5) // apply random filter
sound.filter = R()<.5 ? 99+R()**2*900 : R()**2*1e3-1500;
sound.shapeCurve = R(sound.shape == 4 ? 9 : 4);
switch (presetName)
{
case 'Random':
{
sound = BuildRandomSound();
break;
}
case 'Default': break;
case 'Pickup':
{
sound.frequency = R(200,700);
sound.shape = R(2)|0;
sound.attack = R(.03);
sound.decay = R(.05);
sound.sustain = R(.1);
sound.sustainVolume = R(.5, 1);
sound.release = R(.05,.2);
sound.noise = R()<.8 ? 0 : R(.5);
if (R()<.5)
{
sound.pitchJump = R(99,500);
sound.pitchJumpTime = R(.04,.1);
}
sound.slide = R()<.5 ? 0 : R(-1,1)**3*10;
sound.deltaSlide = R()<.5 ? 0 : R(-1,1)**3*100;
sound.repeatTime = R()<.5 ? 0 : R(.1);
sound.bitCrush = R()<.5 ? 0 : R(.1);
sound.delay = R()<.7 ? 0 : R(.1);
sound.modulation = R()<.8 ? 0 : R()**2*50;
break;
}
case 'Powerup':
{
sound.frequency = R(99,700);
sound.shape = R(2)|0;
sound.attack = R(.1);
sound.decay = R(.1,.3);
sound.sustain = R(.1,.3);
sound.sustainVolume = R(.5, 1);
sound.release = R(.05,.5);
sound.delay = R()<.8 ? 0 : R(.2);
sound.repeatTime = R(.01,.1);
sound.slide = R()<.5 ? 0 : R(-1,1)**3*10;
sound.deltaSlide = R()<.5 ? 0 : R(-1,1)**3*200;
sound.noise = R()<.8 ? 0 : R(.5);
sound.bitCrush = R()<.5 ? 0 : R(.2);
sound.tremolo = R()<.5 ? 0 : R(.5);
sound.modulation = R()<.8 ? 0 : R()**2*50;
if (R()<.5)
{
sound.pitchJump = sound.repeatTime && R()<.5 ? -R(50,200) : R(9,500);
sound.pitchJumpTime = !sound.pitchJump ? 0 : R(.05,.1);
}
break;
}
case 'Jump':
{
sound.frequency = R(50,500);
sound.shape = R(2)|0;
sound.attack = R(.05);
sound.decay = R(.1);
sound.sustain = R(.1);
sound.sustainVolume = R(.5, 1);
sound.release = R(.05,.2);
sound.noise = R()<.5 ? 0 : R(1);
sound.slide = R()<.5 ? 0 : R()**2*50;
sound.deltaSlide = R()<.5 && sound.slide ? 0 : R(-50,99);
sound.bitCrush = R()<.5 ? 0 : R(.1);
sound.delay = R()<.8 ? 0 : R(.05);
break;
}
case 'Shoot':
{
sound.frequency = R(50,500);
sound.shape = R(4)|0;
sound.attack = R(.03);
sound.decay = R(.1,.2);
sound.sustain = R(.2);
sound.sustainVolume = R(.5, 1);
sound.release = R(.05,.2);
sound.delay = R()<.5 ? 0 : R(.3);
sound.slide = R(-1,1)*20;
sound.deltaSlide = R(-1,1)*20;
sound.noise = R()<.8 ? 0 : R(1);
sound.modulation = R()<.8 ? 0 : R()**2*50;
sound.bitCrush = R()<.5 ? 0 : R(.5);
if (R()<.5)
{
sound.tremolo = R(.3);
sound.repeatTime = R(.01,.1);
}
break;
}
case 'Blip':
{
sound = BuildRandomSound();
sound.attack = R(.03);
sound.decay = R(.03,.01);
sound.sustain = R(.05);
sound.release = R(.05);
break;
}
case 'Hit':
{
sound.frequency = R(30,500);
sound.shape = R(5)|0;
sound.attack = R(.03);
sound.decay = R(.1);
sound.sustain = R(.1);
sound.sustainVolume = R(.4, 1);
sound.release = R(.3);
sound.delay = R()<.5 ? 0 : R(.2);
sound.slide = R()<.5 ? 0 : R(-1,1)**3*10;
sound.deltaSlide = R()<.5 ? 0 : R(-1,1)**3*20;
sound.noise = R(2);
sound.modulation = R()<.8 ? 0 : R()**2*50;
sound.bitCrush = R(.5);
sound.tremolo = R()<.5 ? 0 : R(.5);
if (sound.tremolo || R()<.5 && sound.pitchJump)
sound.repeatTime = R(.01,.1);
sound.filter = R()<.5 ? 0 : R()<.5 ? 99+R()**2*2e3 : R()**2*1e3-2500;
break;
}
case 'Explosion':
{
sound.frequency = R(30,99);
sound.shape = R(5)|0;
sound.attack = R(.1);
sound.decay = R(.05,.3);
sound.sustain = R(.3);
sound.sustainVolume = R(.3, .5);
sound.release = R(.3, .8);
sound.slide = R()<.5 ? 0 : R(-9,9);
sound.deltaSlide = R()<.5 ? 0 : R(-9,9);
sound.delay = R()<.5 ? 0 : R(.5);
sound.noise = R(2);
sound.modulation = R()<.8 ? 0 : R()**2*99;
sound.bitCrush = R(1,.1);
sound.tremolo = R()<.5 ? 0 : R(.5);
if (sound.tremolo || R()<.5 && sound.pitchJump)
sound.repeatTime = R(.05,.3);
sound.filter = R()<.5 ? 0 : R()<.5 ? 99+R()**2*2e3 : R()**2*2e3-3500;
break;
}
case 'Music':
{
sound.frequency = noteScale[(R(3)|0)*7][0];
sound.randomness = 0;
sound.shape = R(3)|0;
sound.attack = R()<.3 ? R(.05) : R(.2);
sound.decay = R(.2);
sound.sustain = R(1);
sound.sustainVolume = R(.3, 1);
sound.release = R(.05,.5);
sound.delay = R()<.5 ? 0 : R(.2);
sound.noise = R()<.3 ? 0 : R(.4);
sound.bitCrush = R()<.3 ? 0 : R(.1);
if (R()<.5)
{
// tremolo
sound.repeatTime = R(.1,.4);
sound.tremolo = R(.5);
}
}
}
// finalize settings
if (!sound.pitchJumpTime || !sound.pitchJump)
sound.pitchJumpTime = sound.pitchJump = 0;
const length = sound.attack + sound.sustain + sound.delay;
if (sound.repeatTime > length)
sound.repeatTime = 0;
const Fixed = (v,l=2) =>
{
if (v>10 || v < -10)
l = 0;
const f = v.toFixed(l);
return !parseFloat(f) ? 0 : f;
}
// convert to fixed point
if (typeof sound.frequency != 'string')
sound.frequency = Fixed(sound.frequency,0);
sound.shapeCurve = Fixed(sound.shapeCurve,1);
sound.attack = Fixed(sound.attack);
sound.sustain = Fixed(sound.sustain);
sound.release = Fixed(sound.release);
sound.slide = Fixed(sound.slide,0);
sound.deltaSlide = Fixed(sound.deltaSlide,0);
sound.noise = Fixed(sound.noise,1);
sound.pitchJump = Fixed(sound.pitchJump,0);
sound.pitchJumpTime = Fixed(sound.pitchJumpTime);
sound.repeatTime = Fixed(sound.repeatTime, 2);
sound.modulation = Fixed(sound.modulation,1);
sound.bitCrush = Fixed(sound.bitCrush,1);
sound.delay = Fixed(sound.delay);
sound.sustainVolume = Fixed(sound.sustainVolume);
sound.decay = Fixed(sound.decay);
sound.tremolo = Fixed(sound.tremolo);
sound.filter = Fixed(sound.filter,0);
// must have some release to prevent pop
if (parseFloat(sound.release) == 0)
sound.release = Fixed(R(.01),3);
// replace with locked
for(const s of settings)
if (IsLocked(s.name))
sound[s.name]=document.getElementById('input_'+s.name).value;
{
// renormalize sound volume
const params = SoundToArray(sound);
const saveVolume = ZZFX.volume;
ZZFX.volume = 1;
const samples = ZZFX.buildSamples(...params);
DrawSoundWave(samples, 1, sound);
ZZFX.volume = saveVolume;
// get max sample
let maxSample = 0;
for(let i=0; i<samples.length; i++)
maxSample = Math.max(maxSample, Math.abs(samples[i]));
let volume = 1/maxSample;
volume = Fixed(volume,1);
if (volume > 1) // prevent rounding up causing sound to go above 1
volume = Math.max(1, Fixed(volume - .01,1));
volume = Math.min(5,volume); // prevent too loud
volume = Math.max(.1,volume); // prevent too quiet
sound.volume = volume;
}
sound.name = presetName + ' ' + generatedSoundCount++;
AddToList(sound);
LoadSelected();
UpdateSettings();
PlaySelected();
}
function CopySelected(mutate=0)
{
const sound = Object.assign({}, GetSelectedSound());
if (!sound.originalName)
sound.originalName = sound.name;
sound.copyCount = !sound.copyCount ? 1 : sound.copyCount + 1;
sound.name = sound.originalName
+ ` - ${mutate?'Mutation':'Copy'} `
+ sound.copyCount;
delete sound.favorite;
AddToList(sound);
LoadSelected();
UpdateSettings();
if (mutate)
{
MutateAllSettings();
sound.modified = 0;
UpdateSettings();
}
PlaySelected();
}
function AddToList(sound)
{
// copy default values if missing
for(const key in defaultSound)
{
if (key in sound)
continue;
sound[key] = defaultSound[key];
}
if (!(parseInt(sound.shape)<=4))
sound.shape = 0;
const i = sounds.push(sound) - 1;
const option = document.createElement('option');
option.value = i;
option.style.color = sound.favorite? favoriteColor : "inherit";
option.innerHTML = (sound.favorite?'-> ':sound.modified?'* ':'') + sound.name;
select_soundList.add(option, 0);
select_soundList.selectedIndex = 0;
}
function SetFrequencyToNote()
{
const eNote = document.getElementById("input_note");
const v = eNote.value|0;
if (v >= 0)
{
const eFrequency = document.getElementById("input_frequency");
eFrequency.value = noteScale[v][0];
//const eRandom = document.getElementById("input_randomness");
//eRandom.value = 0;
}
const sound = GetSelectedSound();
sound.modified = 1;
UpdateSettings();
PlaySelected();
}
function SelectedWasChanged(settingName)
{
const sound = GetSelectedSound();
sound.modified = 1;
UpdateSettings();
if (settingName != 'name')
PlaySelected();
}
function LoadSelected(playOnSelect = false)
{
const sound = GetSelectedSound();
if (!sound)
return;
for(const s of settings)
document.getElementById("input_"+s.name).value = sound[s.name];
const isNew = loadedSound != sound;
if (isNew)
loadedSoundSeed = Date.now();
loadedSound = sound;
UpdateSettings();
if (playOnSelect && isNew)
PlaySelected();
}
function FavoriteSelected()
{
let i = select_soundList.selectedIndex;
let index = select_soundList.options[i].value;
let s = sounds[index];
if (!s)
return;
s.favorite = !s.favorite
UpdateSettings();
PlaySelected();
}
function SelectedToTop()
{
const selectedSound = GetSelectedSound();
const sound = Object.assign({}, selectedSound);
selectedSound.favorite = 0;
RemoveSelected(0);
AddToList(sound);
LoadSelected(1);
}
function RemoveSelected(playOnSelect=1)
{
let i = select_soundList.selectedIndex;
let index = select_soundList.options[i].value;
if (sounds[index].favorite)
{
if (!confirm('Are you sure you want remove this favorited sound?'))
return;
}
delete sounds[index];
select_soundList.options.remove(i);
AddDefaultIfEmpty();
i = Math.min(i, select_soundList.length-1);
select_soundList.selectedIndex = i;
LoadSelected(playOnSelect);
}
function IsLocked(name)
{
const lock = document.getElementById("input_lock_"+name);
return lock && lock.checked;
}
function MutateAllSettings()
{
const sound = GetSelectedSound();
if (sound.favorite)
{
if (!confirm('Are you sure you want mutate this favorite sound?'))
return false;
}
let mutateCount = 0;
for(const s of settings)
if (s.canMutate && !IsLocked(s.name))
++mutateCount;
for(let i=6;i--;)
{
if (mutateCount)
{
let mutateIndex = (Math.random()*1e5)%mutateCount|0;
for(const s of settings)
if (s.canMutate && !IsLocked(s.name) && !mutateIndex--)
MutateSetting(s, 0);
}
}
UpdateSettings();
return true;
}
function MutateSetting(setting, refresh = true)
{
const step = setting.step;
const name = setting.name;
if (IsLocked(name)) // unlock
document.getElementById("input_lock_"+name).checked = 0;
const sound = GetSelectedSound();
sound.modified = 1;
let v = sound[name];
if (setting.type == SETTING_TYPE_SHAPE)
v = Math.random()*6|0;
else
{
let r = (Math.random())*(refresh?10:2)|0
if (refresh)
r += 1;
if (Math.random() > .5)
r *= -1;
v += r * step;
v = parseFloat(v.toFixed(2));
}
document.getElementById('input_'+name).value = v;
sound[setting.name] = v;
if (refresh)
UpdateSettings();
}
function ResetAllSettings()
{
const sound = GetSelectedSound();
if (sound.favorite)
{
if (!confirm('Are you sure you want reset this favorite sound?'))
return false;
}
for(const s of settings)
if (!IsLocked(s.name))
ResetSetting(s, 0);
UpdateSettings();
return true;
}
function ResetSetting(setting, refresh = true)
{
const name = setting.name;
if (IsLocked(name)) // unlock