-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1254 lines (1046 loc) · 35.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CS 4621: Final Project -- Music Visualizer</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<script language="javascript" type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script language="javascript" type="text/javascript" src="js/gl-matrix-min.js"></script>
<script language="javascript" type="text/javascript" src="js/boilerplate.js"></script>
<style>
body {
background-color: white;
color: black;
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
button {
color: black;
background-color: white;
display: block;
margin-top: 10px;
padding-left: 18px; padding-right: 18px;
border: none;
}
#play-bar {
display: black;
margin-left: auto; margin-right: auto;
margin-bottom: 25px;
width: 50%;
}
select{
float: right;
color: black;
background-color: white;
font-weight: 500;
width: 65%;
}
.flex{
width: 100%;
height: 85vh;
display: inline-flex;
justify-content: center;
align-items: center;
}
.controls div{
margin-bottom: 5px;
}
.controls p {
margin: 10px auto;
}
audio{
width: 87%;
display: block;
margin: 0 auto;
/*margin-top: 30px;*/
}
.title-main{
float: right;
font-weight: 700;
font-size: 16px;
padding: 12px;
}
.title-main span{
font-weight: 400;
font-size: 12px;
}
.title-control{
font-size: 16px;
font-weight: 700;
text-align: center;
margin-bottom: 20px;
}
.canvas{
display: block;
width: 65%;
height: 85%;
}
.sliders{
margin: 0 auto;
display: block;
width: 100%;
}
.sliders input[type=range]{
width: 62%;
}
.controls{
background-color: #FAFAFA;
border: 1px solid #DDDDDD;
padding: 15px;
width: 20%;
height: 85%;
margin-left: 2%;
}
input[type=range] {
-webkit-appearance: none;
margin: 10px 0;
float: right;
}
select:focus {
outline: none;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 2px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px #000000;
background: #5A5A5A;
border-radius: 1px;
border: 0px solid #000000;
}
input[type=range]::-webkit-slider-thumb {
box-shadow: 0px 0px 0px #000000;
height: 12px;
width: 12px;
border-radius: 25px;
background: #2E84FC;
cursor: pointer;
-webkit-appearance: none;
margin-top: -5px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: #2E84FC;
}
audio::-internal-media-controls-download-button {
display:none;
}
audio::-webkit-media-controls-enclosure {
overflow:hidden;
}
audio::-webkit-media-controls-panel {
width: calc(100% + 30px); /* Adjust as needed */
}
.btn {
background-color:#2E84FC;
color: white;
width: 100%;
}
.btn:hover { color: white; }
</style>
</head>
<body>
<p class="title-main">Cool Visualization</p>
<div class="flex">
<canvas tabindex="1" id="webglCanvas" style="border: none; background-color: black;" width="1000" height="600" class="canvas"></canvas>
<div class="controls">
<p class='title-control'>Control Panel</p>
<!-- sliders -->
<div class="sliders">
<label for="accel">Gravity</label>
<input type="range" class="js-acceleration" min="0" value="13" max="26"/><br>
<label for="velocity">Velocity</label>
<input type="range" class="js-velocity" min="0" value="5" max="10"/><br>
<label for="rotation">Rotation*</label>
<input type="range" class="js-rotationSpeed" min="-6" value="0" max="6"/><br>
</div>
<p>*Click and drag the canvas to rotate at your own speed.</p>
<!-- Song -->
<div>
<label for="song">Song*:</label>
<select name="songs" id="js-songs" class="song-select">
<option value="" disabled="disabled">Acoustic ------------------</option>
<option data-bpm="120" value="https://soundcloud.com/kevinrianto/radwimps-nandemonaiya">Your Name</option>
<option data-bpm="122" value="https://soundcloud.com/qwertyastrid/michigan">Michigan</option>
<option value="" disabled="disabled"></option>
<option value="" disabled="disabled">Soundtrack ----------------</option>
<option data-bpm="91.972" value="https://soundcloud.com/zarsh-elsawaf/inception-mind-heist-movie">Inception</option>
<option value="" disabled="disabled"></option>
<option value="" disabled="disabled">Ambient -------------------</option>
<option data-bpm="112.4" value="https://soundcloud.com/this-will-destroy-you/a-three-legged-workhorse">A Three-Legged Workhorse</option>
<option value="" disabled="disabled"></option>
<option value="" disabled="disabled">Lo-fi -----------------------</option>
<option data-bpm="160.1" value="https://soundcloud.com/verzache/conscious">Conscious</option>
<option data-bpm="158" value="https://soundcloud.com/lidogotsongs/feel-it-still-lido-remix">Feel it Still</option>
<option data-bpm="117.0" value="https://soundcloud.com/itndylan/lemons-dylan">Lemons</option>
<option value="" disabled="disabled"></option>
<option value="" disabled="disabled">Hip-Hop -------------------</option>
<option data-bpm="69.6" value="https://soundcloud.com/travisscott-2/11-grey">Grey</option>
<option data-bpm="100" value="https://soundcloud.com/santandave/wanna-know-remix-feat-drake">Wanna Know</option>
<option data-bpm="155" value="https://soundcloud.com/liluzivert/15-xo-tour-llif3">XO TOUR Lif3</option>
<option value="" disabled="disabled"></option>
<option value="" disabled="disabled">R&B -----------------------</option>
<option data-bpm="117" value="https://soundcloud.com/partyomo/partynextdoor-break-from">Break From Toronto</option>
<option data-bpm="120" style="display:none" selected>User Input</option>
</select>
</div>
<p>*To use your own song, copy the entire url from the page of a soundcloud song, and paste it after the '#' in the url of the visualization.</p>
<!-- Theme -->
<div>
<label for="color">Theme:</label>
<select name="colors" id="js-colors">
<option value="1">Deep Sea</option>
<option value="3">Scandenavian</option>
<option value="2">Sunset</option>
</select>
</div>
<!-- Option -->
<div>
<label for="">Mode:</label>
<select name="" id="js-visID" class="song-select">
<option value="cool3">Central</option>
<option value="cool2">Sweep</option>
<option value="cool1">Frequency</option>
</select>
</div>
<button class="js-fullscreen btn">Full Screen Mode</button>
</div>
</div>
</div>
<script src="js/bundle.js" charset="utf-8"></script>
<script src="js/perlin.js" charset="utf-8"></script>
<script id="vertexShader--basic" type="x-shader/x-vertex">
attribute vec3 position;
uniform mat4 projection;
uniform mat4 toWorld;
uniform mat4 toCam;
void main() {
gl_Position = projection * toCam * toWorld * vec4(position, 1.0);
}
</script>
<script id="fragmentShader--basic" type="x-shader/x-fragment">
precision highp float;
uniform bool waveform;
uniform bool spectrum;
uniform int theme;
vec4 aqua = vec4(0.0, 207.0, 202.0, 255.0) / 255.0;
vec4 white = vec4(1.0);
vec4 purp = vec4(59.0, 38.0, 130.0, 255.0) / 255.0;
vec4 grey = vec4(230.,230.,230.,255.)/255.;
// scandenavian cube colors
vec4 scand_red = vec4(245.0, 88.0, 109.0, 255.0 ) / 255.0;
vec4 scand_black = vec4(152., 152., 152., 255.)/255.0;
void main() {
vec4 color1;
vec4 color2;
if (theme == 1) {
color1 = aqua;
color2 = white;
} else if (theme == 2) {
color1 = grey;
color2 = white;
} else {
color1 = scand_red;
color2 = scand_black;
}
if (waveform || spectrum) {
gl_FragColor = color1;
} else {
gl_FragColor = color2;
}
}
</script>
<script id="vertexShader--background" type="x-shader/x-vertex">
attribute vec3 vert_position;
uniform mat4 projection;
varying vec3 position;
void main() {
gl_Position = vec4(vert_position, 1.0);
position = vert_position;
}
</script>
<script id="fragmentShader--background" type="x-shader/x-fragment">
precision highp float;
uniform float time;
uniform float bpm;
uniform int theme;
uniform vec2 resolution;
varying vec3 position;
// COLORS ~~
// scandanavian interior
vec4 scand1 = vec4(.918, .925, .926, 1.0);
vec4 scand2 = vec4(1.0, .989, .989, 1.0);
// sunrise
vec4 sun1 = vec4(254.0, 156.0, 136.0, 255.0)/255.0;
vec4 sun2 = vec4(245.0, 217.0, 97.0, 255.0)/255.0;
// deep sea
vec4 deep2 = vec4(20.0, 30.0, 48.0, 255.0)/255.0;
vec4 deep1 = vec4(36.0, 59.0, 85.0, 255.0)/255.0;
vec4 pink = vec4(1.0, 0.27, 0.73, 1.0);
vec4 white = vec4(1.0);
vec4 blue = vec4(.2, .2, 1., 1.);
vec4 litepink = vec4(255.0, 182.0, 193.0, 255.0) / 255.0;
void main() {
float radius = 3.5;
float width = 0.4;
float hardness = 0.1;
float speed = 1.0;
float pi = 3.14159265359;
float bpms = bpm / 60000.0; // beats per ms
vec4 color1;
vec4 color2;
if (theme == 1) {
color1 = deep1;
color2 = deep2;
} else if (theme == 2) {
color1 = sun1;
color2 = sun2;
} else {
color1 = scand1;
color2 = scand2;
}
//float r = radius + width * cos( time * bpms * pi ); // To change pulses per minute, multiply/divide inside sin()
float r = radius + width * abs( cos( time * bpms * (pi/2.0) + (pi/2.0) + 0.2 ));
// adjust by 5/3 to make circular
vec2 pos = 2.5 * position.xy * resolution / min(resolution.x, resolution.y);
float t = max(0.0, hardness * ((r * r) - (pos.x * pos.x) - (pos.y * pos.y)));
//float t = max(0.0, hardness * ((r * r) - (25.0 * position.x * position.x) - (9.0 * position.y * position.y)));
t = min(t, 1.0);
gl_FragColor = (1.0 - t) * color1 + t * color2;
}
</script>
<script>
$('.js-fullscreen').on('click', function(){
var el = document.getElementById('webglCanvas');
if(el.webkitRequestFullScreen) { el.webkitRequestFullScreen();}
else { el.mozRequestFullScreen(); }
});
$(document).on("webkitfullscreenchange mozfullscreenchange fullscreenchange", function( event ) {
var isFullScreen = document.fullScreen ||
document.mozFullScreen ||
document.webkitIsFullScreen;
if ( isFullScreen ) {
var c = $('#webglCanvas');
c.css('height','100%');
c.css('width', '100%');
} else {
var c = $('#webglCanvas');
c.css('height','85%');
c.css('width', '65%');
}
});
$(window).on("resize", function(event) {
// Update PerspectiveMatrix
perspectiveMatrix = createPerspectiveMatrix();
camera.projection = perspectiveMatrix;
});
var canPlay = false;
var analyser;
var waveArray;
var freqArray;
var bufferLength;
function getSongFromURL() {
var hash = window.location.hash;
return hash.substring(1);
}
$('#js-songs').on('change', function(){
if(window.location.hash != ""){
var cur = window.location.href;
var id = cur.indexOf('#');
var cur = cur.substring(0,id);
} else {
var cur = window.location.href;
}
var url = this.value;
var newURL = cur+"#"+url;
window.location.href = newURL;
location.reload();
})
$('#js-visID').on('change', function(){
switch(this.value){
case "cool1":
break;
case "cool2":
break;
case "cool3":
break;
}
});
// Scene Objects ---------------------------------------------------
// create particle object centered at [x], [y], [z] with scale [s]
function createParticle(x, y, z, s, c) {
var particle = {};
// Instantiate particle
particle.position = [x, y, z];
particle.yVelocity = 0;
particle.acceleration = 0;
particle.scale = [s, s, s];
particle.waveform = false;
particle.spectrum = false;
particle.decay = false;
// Create [particle.toWorld] matrix
updateToWorldMatrix(particle);
return particle;
}
/*
* create 2D sheet of particles with properties:
* - [numX]
* - [numZ]
* - [minX]
* - [maxX]
* - [minZ]
* - [maxZ]
*/
function createParticleSheet(numX, numZ, minX, maxX, minZ, maxZ) {
var width = maxX - minX;
var height = maxZ - minZ;
var particles = {};
particles.list = [];
particles.position = [0, 0, 0];
particles.scale = [1, 1, 1];
for (var i = 0; i < numX; i++) {
particles.list[i] = [];
for (var j = 0; j < numZ; j++) {
// Create particle
var x = ((i / (numX-1)) * width) + minX;
var z = ((j / (numZ-1)) * height) + minZ;
var color = [Math.abs(x), Math.abs(z), Math.abs((x + z) / 2)];
particles.list[i][j] = createParticle(x, 0, z, 0.01, color);
}
}
// Create [particles.toWorld]
updateToWorldMatrix(particles);
return particles;
}
/**
* Offset the particle to [value]
* [particle] is offset along the y axis
*/
function moveParticle(particle, value) {
particle.position[1] = value;
updateToWorldMatrix(particle);
}
/**
* Update particle position & velocity based on acceleration, dt, minV
*/
function moveParticleByPhysics(particle, dt, minV) {
var dy = (particle.yVelocity * dt);
var y = particle.position[1] + dy;
if (y < minV) {
y = minV;
particle.decay = false;
}
moveParticle(particle, y, 1.0);
// Update Velocity
var dv = particle.acceleration * dt;
particle.yVelocity += dv;
}
function setParticleDecay(particle, waveformSound, decayAcceleration, velocity) {
particle.decay = true;
particle.yVelocity = velocity * waveformSound;
particle.acceleration = -decayAcceleration;
}
/**
* create the scene for our visualization
* the [scene] object has the following fields:
* - [camera]
* - [particles]
* - [shape]: shape for each particle
*/
function createScene(camera, particles, shape) {
var scene = {
camera: camera,
particles: particles,
shape: shape
};
return scene;
}
/**
* draw all the objects in the scene
* [scene] has the following properties:
* - [particles]
* - [list]
* - [toWorld]
* - [camera]
* - [toWorld]
* - [projection]
*/
function drawScene(gl, scene, theme) {
// Draw each particle
for (var i = 0; i < particles.list.length; i++) {
for (var j = 0; j < particles.list[i].length; j++) {
var particle = particles.list[i][j];
var toWorld = mat4.create();
mat4.mul(toWorld, particles.toWorld, particle.toWorld);
drawShape(gl, scene.shape, camera, toWorld, [particle.waveform, particle.spectrum], theme);
}
}
}
/**
* [waveformVal] range: [-1, 1]
*/
function getWaveformSound(center, amp, scale, waveformVal, j) {
// Update waveformVal if particle is in the waveform range
//var half = scene.particles.list[i].length / 4.0;
var val = amp * Math.abs(waveformVal);
if (center-val < j && j < center+val) {
var r = scale * val / amp;
var x = scale * ( Math.abs(j - center) ) / amp;
var y = Math.sqrt(Math.pow(r, 2) - Math.pow(x, 2));
waveformVal = y;
} else {
waveformVal = 0;
}
// Waveform
var waveformSound = waveformVal;
return waveformSound;
}
function normalizeWaveformVal(waveformVal) {
return ( waveformVal - (255.0/2.0) ) / (255.0/2.0); // range: [-1, 1];
}
function normalizeSpectrumVal(spectrumVal) {
return spectrumVal / 255.0; // range: [0, 1]
}
/*
* Particles Sweep laterally along the waveform
*/
function getSoundValueSweep(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm) {
// No spectrum for this vis
particle.spectrum = false;
// Waveform 1 ---------------------------------------------------------
var center1 = l / 4.0;
var amp1 = l / 4.0;
var scale1 = 3;
var waveformSound1 = getWaveformSound(center1, amp1, scale1, waveformVal, j);
// Waveform 2 ---------------------------------------------------------
//var sin = Math.abs( Math.sin( (time / 1000.0) ) );
var max = 4;
var sin = beats % (2.0*max);
if (sin >= max) {
sin = 2.0*max - sin;
}
sin = sin / max;
var curI = Math.round( l * sin );
//var sin = Math.cos( time * bpms * Math.PI / 2.0 / 2.0);
//var curI = Math.round( l/2.0 + (l/2.0*sin) );
var center2 = l * 3.0/4.0;
var amp2 = l / 4.0;
var scale2 = 2;
var waveformSound2 = getWaveformSound(center2, amp2, scale2, maxWaveformVal, j);
var intWaveSound2 = Math.round(waveformSound2);
if ( !(curI-intWaveSound2-1 < i && i < curI+intWaveSound2+1) ) {
waveformSound2 = 0;
} else if (waveformSound2 != 0) {
setParticleDecay(particle, waveformSound2, decayAcceleration, decaySpeed);
}
// Update particle.waveform
if (waveformSound1 != 0) {
particle.waveform = true;
} else {
particle.waveform = false;
}
// Sound
return vSound = scale * (waveformSound2);
}
/*
* Particles over waveform in center
*/
function getSoundValueCenter(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm) {
// No spectrum for this vis
particle.spectrum = false;
// Waveform 1 ---------------------------------------------------------
var center1 = l / 2.0;
var amp1 = l / 2.0;
var scale1 = 3;
var waveformSound1 = getWaveformSound(center1, amp1, scale1, waveformVal, j);
if (waveformSound1 != 0) {
setParticleDecay(particle, waveformSound1, decayAcceleration, decaySpeed);
}
// Update particle.waveform
if (waveformSound1 != 0) {
particle.waveform = true;
} else {
particle.waveform = false;
}
// Sound
return vSound = scale * (waveformSound1);
}
/*
* Frequency
*/
function getSoundValueFrequency(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm) {
// Waveform 1 ---------------------------------------------------------
var center1 = l * 3.0/4.0;
var amp1 = l / 4.0;
var scale1 = 3;
var waveformSound1 = getWaveformSound(center1, amp1, scale1, waveformVal, j);
// Update particle.waveform
if (waveformSound1 != 0) {
particle.waveform = true;
setParticleDecay(particle, waveformSound1, decayAcceleration, decaySpeed);
} else {
particle.waveform = false;
}
// Spectrum (Frequency)
var thresholdSpectrum = 0.0;
var spectrumSound = 0;
var maxSpectrumJ = l * 2.0/5.0;
var spectrumIdx = Math.round(spectrumVal * maxSpectrumJ);
if (j == spectrumIdx && spectrumVal != 0) {
spectrumSound = 2.0 * (spectrumVal - thresholdSpectrum);
setParticleDecay(particle, spectrumSound, 2.0*decayAcceleration, decaySpeed);
spectrumSound = 0;
particle.spectrum = true;
} else {
spectrumSound = 0;
particle.spectrum = false;
}
// Sound
return vSound = scale * (spectrumSound + waveformSound1);
}
/*
* Sweeping - Default baseline
*/
function getSoundValue(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm) {
// Waveform 1 ---------------------------------------------------------
var center1 = l / 4.0;
var amp1 = l / 4.0;
var scale1 = 3;
var waveformSound1 = getWaveformSound(center1, amp1, scale1, waveformVal, j);
// Waveform 2 ---------------------------------------------------------
//var sin = Math.abs( Math.sin( (time / 1000.0) ) );
var max = 4;
var sin = beats % (2.0*max);
if (sin >= max) {
sin = 2.0*max - sin;
}
sin = sin / max;
var curI = Math.round( l * sin );
//var sin = Math.cos( time * bpms * Math.PI / 2.0 / 2.0);
//var curI = Math.round( l/2.0 + (l/2.0*sin) );
var center2 = l * 3.0/4.0;
var amp2 = l / 4.0;
var scale2 = 2;
var waveformSound2 = getWaveformSound(center2, amp2, scale2, maxWaveformVal, j);
var intWaveSound2 = Math.round(waveformSound2);
if ( !(curI-intWaveSound2-1 < i && i < curI+intWaveSound2+1) ) {
waveformSound2 = 0;
} else if (waveformSound2 != 0) {
//var dist = Math.abs(curI - i);
//if (dist > 0) dist--;
//waveformSound2 = waveformSound2 - Math.pow( (dist / intWaveSound2/5), 2);
particle.decay = true;
particle.yVelocity = 0.0015 * scale * waveformSound2;
particle.acceleration = -decayAcceleration;
}
// Update particle.waveform
//if (waveformSound2 != 0 || waveformSound1 != 0) {
if (waveformSound1 != 0) {
particle.waveform = true;
} else {
particle.waveform = false;
}
// Spectrum (Frequency)
var thresholdSpectrum = 0.7;
var spectrumSound = 0;
if (spectrumVal > thresholdSpectrum) {
spectrumSound = spectrumVal - thresholdSpectrum;
//particle.spectrum = true;
} else {
particle.spectrum = false;
}
// Sound
return vSound = scale * (waveformSound2);
}
/**
* update scene according to music data
* [spectrum] - amplitudes across frequency spectrum
* range: [0, 255]
* [waveform] - snapshot of amplitude readings
* range: [-1.0, +1.0]
* [scale] - scale factor for particle offset
*/
var inc = 0;
function updateScene(scene, spectrum, waveform, prevTime, time, bpm, velocity, acceleration, id) {
var dt = time - prevTime;
var soundAmp = 2;
var noiseAmp = 0.3;
var displaced = 0;
var scale = 0.1;
var threshold = 0.0;
var bpms = bpm / 60000;
//var decaySpeed = 0.0005;
//var decaySpeed = 0.05 / bpm;
//var decaySpeed = 0.0005 / bpm;
//var decaySpeed = 0.0015 * scale
var decaySpeed = velocity;
//var decayAcceleration = 0.0000005;
var decayAcceleration = acceleration;
//var decayAcceleration = 0;
var beats = time * bpms;
var maxWaveformVal = normalizeWaveformVal( Math.max.apply(null, waveform) );
for (var i = 0; i < scene.particles.list.length; i++) {
var waveformVal = normalizeWaveformVal(waveform[i]); // range: [-1, 1];
for (var j = 0; j < scene.particles.list[i].length; j++) {
var particle = scene.particles.list[i][j];
var spectrumVal = spectrum[i]/255.0; // range: [0, 1]
var l = scene.particles.list[i].length;
// Sound
var vSound = 0;
switch (id) {
case "cool1":
vSound = getSoundValueFrequency(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm);
break;
case "cool2":
vSound = getSoundValueSweep(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm);
break;
case "cool3":
vSound = getSoundValueCenter(id, dt, soundAmp, noiseAmp, displaced, scale,
threshold, bpms, decaySpeed, decayAcceleration,
beats, maxWaveformVal, waveformVal, particle,
spectrumVal, l, i, j,
scene, spectrum, waveform, prevTime, time, bpm);
break;
default:
vSound = 0;
break;
}
// Noise
var vNoise = noiseAmp * PerlinNoise.noise((i+inc)/30,(j+inc)/30,.5);
// Final Value
var v = vNoise + soundAmp * (vSound - threshold);
// Move Particles
if (vSound > threshold) {
moveParticle(particle, v);
particle.displacement = 1.0;
displaced++;
if (scale * spectrumVal > threshold) {
} else {
particle.spectrum = false;
}
} else {
if (particle.decay) {
moveParticleByPhysics(particle, dt, vNoise);
} else {
moveParticle(particle, vNoise);
}
particle.displacement = 0.0;
}
}
}
if (displaced >= scene.particles.list.length * scene.particles.list[0].length / 2) { setWire(); }
else { setCube(); }
}
/**
* update [object.toWorld] matrix according to the [object] properties:
* - [position]: array [x, y, z]
* - [scale]: array [sx, sy, sz] or null
*/
function updateToWorldMatrix(object) {
// Create Translation matrix
var [x, y, z] = object.position;
var translation = vec3.fromValues(x,y,z);
var T = mat4.create();
mat4.fromTranslation(T, translation);
// Create Scale Matrix
var S = mat4.create();
if (object.scale != null) {
var [sx, sy, sz] = object.scale;
var scale = vec3.fromValues(sx,sy,sz);
mat4.fromScaling(S, scale);
}
// Update [object.toWorld] matrix
object.toWorld = mat4.create();
mat4.mul(object.toWorld, T, S);
return object;
}
/**
* update [camera.toWorld] matrix and create [camera.toCam] matrix to be the
* inverse of [camera.toWorld]
*/
function updateCamToWorldMatrix(camera) {
// update [camera.toWorld] matrix
updateToWorldMatrix(camera);
// update [camera.toCam] matrix
var toCam = mat4.create();
mat4.invert(toCam, camera.toWorld);
camera.toCam = toCam;
return camera;
}
/**
* update [scene] to move camera around by [offset]
*/
function updateSceneOffset(scene, offset) {
var [x, y, z] = offset;
var v = Math.PI/500;
var Rx = mat4.create();
mat4.fromXRotation(Rx, x*v);
mat4.mul(scene.particles.toWorld, Rx, scene.particles.toWorld);
// Keep rotation around x bound by 180 degrees
var vec = vec3.fromValues(0, 1, 0);
var vecTrans = vec3.create();
vec3.transformMat4(vecTrans, vec, scene.particles.toWorld);
var angle = vec3.angle(vec, vecTrans);
if (angle > Math.PI/2) {
var sign = 1;
if (vecTrans[2] < 0) {
sign = -1;
}
// Stop rotation around x
var RxBack = mat4.create();
mat4.fromXRotation(RxBack, sign * ((Math.PI/2) - angle) );
mat4.mul(scene.particles.toWorld, RxBack, scene.particles.toWorld);
}
var Ry = mat4.create();
mat4.fromYRotation(Ry, y*v);
mat4.mul(scene.particles.toWorld, scene.particles.toWorld, Ry);
var Tz = mat4.create();
var offsetZ = vec3.fromValues(0, 0, z/50);
vec3.add(scene.camera.position, scene.camera.position, offsetZ);
// Check min/max zoom
var min = 0.5;
var max = 15;
if (scene.camera.position[2] < min) {
scene.camera.position[2] = min;
} else if (scene.camera.position[2] > max) {
scene.camera.position[2] = max;
}
updateCamToWorldMatrix(scene.camera);
}
function createPerspectiveMatrix() {
var perspectiveMatrix = mat4.create();
var fovy = (45.0 / 180.0) * Math.PI;
//var aspect = $("#webglCanvas").attr("width") / $("#webglCanvas").attr("height");