-
Notifications
You must be signed in to change notification settings - Fork 2
/
localaudio.html
967 lines (900 loc) · 31.8 KB
/
localaudio.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
<!doctype html>
<html lang="en" class="h-100" data-bs-theme="auto">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="color-scheme" content="light dark" />
<meta name="description" content="Reverb effect written in JavaScript." />
<meta name="author" content="Masashi Yoshikawa" />
<!-- ogp -->
<meta property="og:title" content="JavaScript Reverb Effect Test" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://logue.github.io/Reverb.js/" />
<meta
property="og:image"
content="https://repository-images.githubusercontent.com/194181712/20bdd780-9995-11e9-8811-42e3b44d1cec"
/>
<meta property="og:site_name" content="Logue's Lab" />
<meta
property="og:description"
content="Reverb effect written in JavaScript."
/>
<meta property="fb:app_id" content="129144050466298" />
<meta
property="article:publisher"
content="https://www.facebook.com/logue256"
/>
<meta name="twitter:card" content="Summary" />
<meta name="twitter:site" content="@logue256" />
<meta name="twitter:title" content="JavaScript Reverb Effect Test" />
<meta name="twitter:url" content="https://logue.dev/Reverb.js/" />
<meta
name="twitter:description"
content="Reverb effect written in JavaScript."
/>
<meta
name="twitter:image"
content="https://repository-images.githubusercontent.com/194181712/20bdd780-9995-11e9-8811-42e3b44d1cec"
/>
<title>Reverb.js Demo</title>
<!-- Google tag (gtag.js) -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-2Y2FW3QEG4"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-2Y2FW3QEG4');
</script>
<!-- Bootstrap core CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
/>
</head>
<body class="d-flex flex-column h-100">
<header>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<div class="container-fluid d-flex justify-content-between">
<a class="navbar-brand" href="#">Reverb.js</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarCollapse"
aria-controls="navbarCollapse"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse flex-grow-0" id="navbarCollapse">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="./">Demo</a>
</li>
<li class="nav-item">
<a
class="nav-link active"
aria-current="page"
href="./localaudio.html"
>
LocalAudio Demo
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/logue/Reverb.js">
<em class="bi bi-github"></em>
</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
href="#"
id="navbarDarkDropdownMenuLink"
role="button"
data-bs-toggle="dropdown"
aria-expanded="false"
>
See Also
</a>
<ul
class="dropdown-menu dropdown-menu-end"
aria-labelledby="navbarDarkDropdownMenuLink"
>
<li>
<a
class="dropdown-item"
href="https://github.com/logue/smfplayer.js/"
>
smfplayer.js
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://github.com/logue/sf2synth.js/"
>
sf2synth.js
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://github.com/logue/Reverb.js/"
>
Reverb.js
</a>
</li>
<li>
<a
class="dropdown-item"
href="https://github.com/logue/MabiMmlEmu/"
>
MabiMmlEmu
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</header>
<!-- Begin page content -->
<main role="main" class="flex-shrink-0 my-3">
<div class="container-fluid bg-body-tertiary p-5 mb-3">
<h1 class="display-5 fw-bold">Reverb.js Local Audio Demo</h1>
<p class="fs-4">Play own audio file.</p>
<div>
<label for="file" class="form-label">Load Audio file</label>
<input type="file" class="form-control" id="file" accept="audio/*" />
</div>
<div class="my-3">
<button id="play" class="btn btn-primary" disabled>
<em class="bi bi-play"></em>
Play
</button>
<button id="stop" class="btn btn-secondary" disabled>
<em class="bi bi-stop"></em>
Stop
</button>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<h2>Reverb Control</h2>
<p>
Changing these values will regenerate the impulse response each
time, but due to its nature, it will take some time to see the
effect. It may be helpful to maximize the value of Wet.
</p>
<div class="form-check form-switch">
<input id="reverb" type="checkbox" class="form-check-input" />
<label class="form-check-label" for="reverb">Reverb</label>
</div>
<div class="form-check form-switch">
<input type="checkbox" class="form-check-input" id="reverse" />
<label class="form-check-label" for="reverse">
Reverse Inpulse Response
</label>
</div>
</div>
<div class="col">
<h2>Spectrum Analysis</h2>
<div class="overflow-auto">
<canvas id="canvas" class="d-none"></canvas>
<img id="test" class="d-none" alt="test" />
<canvas id="graph"></canvas>
</div>
</div>
</div>
<section class="mb-3">
<h3>Impulse response generation algorithm settings</h3>
<p>
<a
href="https://github.com/thi-ng/umbrella/tree/develop/packages/colored-noise"
target="_blank"
>
@thi.ng/colored-noise
<em class="bi bi-box-arrow-up-right"></em>
</a>
is used for random noise generation process used for impulse
response generation. These settings are the parameters of the noise
generation process.
</p>
<fieldset>
<legend>Noise algorithm</legend>
<div class="row align-items-center">
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="blue"
value="blue"
/>
<label class="form-check-label" for="blue">
<em
class="bi bi-square-fill"
style="color: var(--bs-blue)"
></em>
Blue Noise
</label>
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="green"
value="green"
/>
<label class="form-check-label" for="green">
<em
class="bi bi-square-fill"
style="color: var(--bs-green)"
></em>
Green Noise
</label>
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="pink"
value="pink"
/>
<label class="form-check-label" for="pink">
<em
class="bi bi-square-fill"
style="color: var(--bs-pink)"
></em>
Pink Noise
</label>
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="red"
value="red"
/>
<label class="form-check-label" for="red">
<em
class="bi bi-square-fill"
style="color: var(--bs-red)"
></em>
Red (Brown) Noise
</label>
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="violet"
value="violet"
/>
<label class="form-check-label" for="violet">
<em
class="bi bi-square-fill"
style="color: var(--bs-purple)"
></em>
Violet Noise
</label>
</div>
</div>
<div class="col-auto">
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="noise"
id="white"
value="white"
checked
/>
<label class="form-check-label" for="white">
<em class="bi bi-square"></em>
White Noise
</label>
</div>
</div>
</div>
<p class="small">
<a
href="https://en.wikipedia.org/wiki/Colors_of_noise"
target="_blank"
>
What is colord noise?
<em class="bi bi-box-arrow-up-right"></em>
</a>
</p>
</fieldset>
<div class="row">
<div class="col-md">
<label for="peaks" class="form-label">Random noise peaks</label>
<input
type="number"
class="form-control"
id="peaks"
value="2"
step="1"
min="0"
max="64"
disabled
/>
<div id="scale-help" class="form-text">
The number of frequency band peaks when generating noise. This
value is ignored for white noise.
</div>
</div>
<div class="col-md">
<label for="scale" class="form-label">Random noise scales</label>
<input
type="number"
class="form-control"
id="scale"
value="1"
step="1"
min="0"
max="64"
/>
</div>
<div class="col-md">
<label for="randomAlgorithm" class="form-label">
Random Generator
</label>
<select id="randomAlgorithm" class="form-select" disabled>
<option value="SYSTEM" selected="selected">
SYSTEM (Default)
</option>
<option value="CRYPTO">CRYPTO</option>
</select>
</div>
</div>
</section>
<section class="mb-3">
<h3>Impulse response setting</h3>
<p>
Generate damped sound from the generated noise and make it an
impulse response. These are its settings.
</p>
<div class="row">
<div class="col-md">
<label for="time" class="form-label" aria-describedby="time-help">
Time
</label>
<input
type="range"
class="form-range"
title="1.1"
value="2"
min="0"
max="50"
id="time"
/>
<div id="time-help" class="form-text">Room size</div>
</div>
<div class="col-md">
<label for="decay" class="form-label">Decay</label>
<input
type="range"
class="form-range"
title="2"
value="2"
min="0"
max="100"
id="decay"
/>
<div id="decay-help" class="form-text">
Hardness of room wall size
</div>
</div>
<div class="col-md">
<label for="delay" class="form-label">Delay</label>
<input
type="range"
class="form-range"
title="0"
value="0"
min="0"
max="100"
id="delay"
/>
<div id="delay-help" class="form-text">
Delays due to obstacles in front of room walls
</div>
</div>
<div class="col-md">
<label for="mix" class="form-label">Dry / Wet</label>
<input
type="range"
id="mix"
class="form-range"
min="0"
max="1"
step="0.05"
value="0.5"
title="0.5"
/>
<div id="delay-help" class="form-text">
Closer to the original sound or closer to the effector
</div>
</div>
</div>
</section>
<section class="mb-3">
<h3>Filter for Impulse response</h3>
<p>
If the value of the filter is allpass, the filtering will be
bypassed.
</p>
<div class="row">
<div class="col-md">
<label for="filter" class="form-label">Filter Type</label>
<select id="filter" class="form-select">
<option value="allpass" selected="selected">
allpass (Through)
</option>
<option value="bandpass">bandpass</option>
<option value="highpass">highpass</option>
<option value="highshelf">highshelf</option>
<option value="lowpass">lowpass</option>
<option value="lowshelf">lowshelf</option>
<option value="notch">notch</option>
<option value="peaking">peaking</option>
</select>
</div>
<div class="col-md">
<label for="freq" class="form-label">Filter frequency</label>
<input
type="range"
class="form-range"
value="2500"
min="20"
max="5000"
step="5"
id="freq"
title="2500"
/>
</div>
<div class="col-md">
<label for="freq" class="form-label">Q value</label>
<input
type="range"
class="form-range"
value="1"
min=".0001"
max="10"
step=".0005"
id="q"
title="1"
/>
</div>
</div>
</section>
</div>
</main>
<footer class="footer mt-auto py-3 bg-body-tertiary">
<div class="container">
<address class="text-muted">
© 2019-2024 by
<a href="http://logue.dev/">Logue</a>
. Licensed under the
<a href="http://opensource.org/licenses/mit-license.php">
MIT License
</a>
.
</address>
</div>
</footer>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/three.js"
integrity="sha256-A2ofbc4qfSfxXRlxuONE0Bj7ulHxEAIiYk537fvbkJU="
crossorigin="anonymous"
></script>
<script type="module">
import Reverb from './src/Reverb';
// Setup Audio Context
const AudioCtx = new AudioContext();
// Defreeze AudioContext for iOS.
document.addEventListener('touchstart', initAudioContext);
function initAudioContext() {
document.removeEventListener('touchstart', initAudioContext);
// wake up AudioContext
const emptySource = AudioCtx.createBufferSource();
emptySource.start();
emptySource.stop();
}
// Audio object
const audio = new Audio();
// Audio Source
const AudioSrc = AudioCtx.createMediaElementSource(audio);
// Analyzer Node
const AnalyserNode = AudioCtx.createAnalyser();
// Setup Reverb Class
const reverb = new Reverb(AudioCtx);
// decode audio
const LoadSample = async input => {
if (audio) {
audio.pause();
audio.src = '';
}
audio.src = await loadDataUri(input);
};
// Load array buffer
const loadDataUri = async input => {
document.getElementById('play').disabled = 'disabled';
document.getElementById('stop').disabled = 'disabled';
return await new Promise(resolve => {
if (input instanceof File) {
const reader = new FileReader();
reader.onload = event => {
resolve(event.target.result);
document.getElementById('play').disabled = '';
document.getElementById('stop').disabled = '';
};
reader.readAsDataURL(input);
} else {
fetch(input)
.then(response => {
return {
mime: response.headers.get('content-type'),
buffer: response.arrayBuffer(),
};
})
.then(res => {
// to base64
let binary = '';
let bytes = [].slice.call(new Uint8Array(res.buffer));
bytes.forEach(b => (binary += String.fromCharCode(b)));
const base64 = window.btoa(binary);
// prepend data scheme
resolve('data:' + res.mime + ';base64;' + base64);
document.getElementById('play').disabled = '';
document.getElementById('stop').disabled = '';
});
}
}).catch(e => alert(e));
};
const handleFileSelect = async e => {
const file = e.target.files[0];
AudioSrc.buffer = await LoadSample(file);
};
// Reverb switch handler
const setReverb = () => {
AudioSrc.disconnect();
if (document.getElementById('reverb').checked) {
// Connect Reverb
reverb.connect(AudioSrc).connect(AnalyserNode);
} else {
reverb.disconnect(AudioSrc).connect(AnalyserNode);
}
AnalyserNode.connect(AudioCtx.destination);
};
/** Draw Texture Functions */
const PIXEL_RATIO = () => {
const ctx = document.createElement('canvas').getContext('2d'),
dpr = window.devicePixelRatio || 1,
bsr =
ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio ||
ctx.msBackingStorePixelRatio ||
ctx.oBackingStorePixelRatio ||
ctx.backingStorePixelRatio ||
1;
return dpr / bsr;
};
// Create retina quality canvas
const createRetinaCanvas = (w, h, ratio) => {
if (!ratio) {
ratio = PIXEL_RATIO;
}
const canvas = document.getElementById('canvas');
// const canvas = document.createElement('canvas');
canvas.width = w * ratio;
canvas.height = h * ratio;
canvas.style.width = w + 'px';
canvas.style.height = h + 'px';
canvas.getContext('2d').setTransform(ratio, 0, 0, ratio, 0, 0);
return canvas;
};
// Spectrum Analysis canvas size
const SPRITE_HEIGHT = 256;
const SPRITE_WIDTH = 512;
// Sprite Canvas
const spriteCanvas = createRetinaCanvas(SPRITE_WIDTH, SPRITE_HEIGHT);
// Draw Graph
const DrawGraph = () => {
const AnalyseData = new Float32Array(1024);
AnalyserNode.getFloatFrequencyData(AnalyseData);
const sprite = spriteCanvas.getContext('2d', { antialias: false });
// Background
//sprite.fillStyle = "#343a40";
//sprite.fillRect(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
sprite.clearRect(0, 0, spriteCanvas.width, spriteCanvas.height);
// Graph
const grad = sprite.createLinearGradient(0, 0, spriteCanvas.width, 0);
grad.addColorStop(0, 'rgb(255, 0, 0)');
grad.addColorStop(0.2, 'rgb(255, 255, 0)');
grad.addColorStop(0.4, 'rgb(0, 255, 0)');
grad.addColorStop(0.6, 'rgb(0, 255, 255)');
grad.addColorStop(0.8, 'rgb(0, 0, 255)');
grad.addColorStop(1, 'rgb(255, 0, 255)');
sprite.fillStyle = grad;
//sprite.fillStyle = document.getElementById('reverb').checked ? "#17a2b8" : "#28a745";
for (let i = 0; i < spriteCanvas.width; ++i) {
const f = (AudioCtx.sampleRate * i) / 1024;
let y = 128 + (AnalyseData[i] + 48.16) * 2.56 + 1;
sprite.fillRect(i, spriteCanvas.height - y, 1, y);
}
return spriteCanvas.toDataURL();
};
// Draw Axis
const DrawAxis = () => {
const sprite = spriteCanvas.getContext('2d', { antialias: false });
// y axis (dB)
for (let d = -50; d < 50; d += 10) {
const y = (SPRITE_HEIGHT / 2 - (d * SPRITE_HEIGHT) / 100) | 0;
// Line
sprite.fillStyle = '#6c757d';
sprite.fillRect(20, y, SPRITE_WIDTH, 1);
// Label
sprite.fillStyle = '#fd7e14';
sprite.fillText(d + 'dB', 5, y);
}
sprite.fillStyle = '#ffc107';
sprite.fillRect(20, SPRITE_HEIGHT / 2, SPRITE_WIDTH, 1);
// x axis (frequency)
for (let f = 2000; f < AudioCtx.sampleRate / 2; f += 2000) {
const x = ((f * 1024) / AudioCtx.sampleRate) | 0;
// Line
sprite.fillStyle = '#6c757d';
sprite.fillRect(x, 0, 1, 245);
// Label
sprite.fillStyle = '#e83e8c';
sprite.fillText(f / 1000 + 'kHz', x - 10, 255);
}
return spriteCanvas.toDataURL();
};
// スプライトのデバッグ
const debug = src => {
const canvas = document.getElementById('canvas');
canvas.style.width = SPRITE_WIDTH + 'px';
canvas.style.height = SPRITE_HEIGHT + 'px';
canvas.width = SPRITE_WIDTH;
canvas.height = SPRITE_HEIGHT;
const img = document.getElementById('test');
img.style.width = SPRITE_WIDTH + 'px';
img.style.height = SPRITE_HEIGHT + 'px';
img.src = src;
// ctx.clearRect(0, 0, canvas.width, canvas.height);
const image = new Image();
image.addEventListener('load', () => {
canvas.getContext('2d').drawImage(image, 0, 0); // Or at whatever offset you like
});
image.src = src;
};
/** 3D Spectrum Analyzer Code */
// 3D canvas size
const CANVAS_WIDTH = 960;
const CANVAS_HEIGHT = 540;
// Create renderer
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#graph'),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
// Create Scene
const scene = new THREE.Scene();
// Create camera
const camera = new THREE.PerspectiveCamera(
30,
CANVAS_WIDTH / CANVAS_HEIGHT,
1,
2000
);
// Move Initial camera position (x, y, z)
camera.position.set(100, 50, 1000);
//const camera = new THREE.OrthographicCamera(-CANVAS_WIDTH, CANVAS_WIDTH, CANVAS_HEIGHT, -CANVAS_HEIGHT, 1, 100000);
// Append Fog
// new THREE.Fog(color, start distance, end distance);
scene.fog = new THREE.Fog(0x000000, 1000, 2000);
//debug(DrawAxis())
const AxisMesh = new THREE.Mesh(
// Plane
new THREE.PlaneGeometry(SPRITE_WIDTH, SPRITE_HEIGHT, 5, 5),
// Texture
new THREE.MeshBasicMaterial({
transparent: true,
needsUpdate: true,
map: new THREE.TextureLoader().load(DrawAxis()),
side: THREE.DoubleSide,
})
);
// Move Mesh object
AxisMesh.position.set(0, 0, 0);
scene.add(AxisMesh);
let depth = 0;
let geometries = [];
let materials = [];
let textures = [];
let meshs = [];
const MAX_DEPTH = 1024;
// Loop event
const tick = () => {
// Draw Texture image
const sprite = DrawGraph();
debug(sprite);
textures[depth] = new THREE.TextureLoader().load(sprite);
geometries[depth] = new THREE.PlaneGeometry(
SPRITE_WIDTH,
SPRITE_HEIGHT,
5,
5
);
materials[depth] = new THREE.MeshBasicMaterial({
color: 0xccddee,
transparent: true,
needsUpdate: true,
map: textures[depth],
side: THREE.DoubleSide,
});
// Mesh object
meshs[depth] = new THREE.Mesh(geometries[depth], materials[depth]);
// Move Mesh object
meshs[depth].position.set(0, 0, depth);
AxisMesh.position.set(0, 0, depth);
// Append Mesh object to 3D scene
scene.add(meshs[depth]);
// Render
renderer.render(scene, camera);
// Move Camera
camera.position.z = depth + 650;
// camera.zoom = 2;
camera.updateProjectionMatrix();
// Look to origin
camera.lookAt(new THREE.Vector3(0, 0, depth));
// Remove old Mesh
if (depth > MAX_DEPTH) {
scene.remove(meshs[depth - MAX_DEPTH]);
// meshs[depth - MAX_DEPTH].dispose();
geometries[depth - MAX_DEPTH].dispose();
materials[depth - MAX_DEPTH].dispose();
textures[depth - MAX_DEPTH].dispose();
// Manual Gabarge Collection
delete geometries[depth - MAX_DEPTH];
delete materials[depth - MAX_DEPTH];
delete textures[depth - MAX_DEPTH];
}
// incriment depth
depth++;
// Update Animation
requestAnimationFrame(tick);
};
// Regist Event
window.addEventListener('load', async () => {
audio.src = await LoadSample('./demo.flac');
// Play button
document.getElementById('play').addEventListener('click', async () => {
if (AudioCtx.state == 'suspended') AudioCtx.resume();
setReverb();
audio.play();
});
// Stop button
document.getElementById('stop').addEventListener('click', () => {
audio.stop();
});
// File form
document.getElementById('file').addEventListener('dragover', e => {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
return false;
});
document
.getElementById('file')
.addEventListener('drag', handleFileSelect, false);
document
.getElementById('file')
.addEventListener('change', handleFileSelect, false);
// Reverb switch button
document.getElementById('reverb').addEventListener('click', setReverb);
// Reverse checkbox
document
.getElementById('reverse')
.addEventListener('click', e => reverb.reverse(e.target.checked));
// Reverb dualation time
document.getElementById('time').addEventListener('change', e => {
reverb.time(e.target.value);
e.target.title = e.target.value;
});
// Decay time
document.getElementById('decay').addEventListener('change', e => {
reverb.decay(e.target.value);
e.target.title = e.target.value;
});
// Delay time
document.getElementById('delay').addEventListener('change', e => {
reverb.delay(e.target.value);
e.target.title = e.target.value;
});
// Filter select box
document.getElementById('filter').addEventListener('change', e => {
reverb.filterType(e.target.value);
e.target.title = e.target.value;
});
// Filter frequency
document.getElementById('freq').addEventListener('change', e => {
reverb.filterFreq(e.target.value);
e.target.title = e.target.value;
});
// Filter quality
document.getElementById('q').addEventListener('change', e => {
reverb.filterQ(e.target.value);
e.target.title = e.target.value;
});
// Dry/Wet
document.getElementById('mix').addEventListener('change', e => {
reverb.mix(e.target.value);
e.target.title = e.target.value;
});
// Noise Type
document.querySelectorAll('[name=noise]').forEach(dom =>
dom.addEventListener('click', e => {
document.getElementById('peaks').disabled =
e.target.value === 'white';
reverb.setNoise(e.target.value);
})
);
document.getElementById('peaks').addEventListener('change', e => {
reverb.peaks(e.target.value);
e.target.title = e.target.value;
});
document.getElementById('scale').addEventListener('change', e => {
reverb.scale(e.target.value);
e.target.title = e.target.value;
});
document
.getElementById('randomAlgorithm')
.addEventListener('change', e => {
reverb.randomAlgorithm(e.target.value);
e.target.title = e.target.value;
});
// Execute Animation Thread
tick();
});
</script>
</body>
</html>