-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
346 lines (283 loc) · 9.89 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<!-- <link rel="stylesheet" href="style.css"> -->
<style>
html, body{
margin: 0;
padding: 0;
overflow: hidden;
height: 100vh;
width: 100vw;
background: hsla(242, 86%, 6%, 1);
background: linear-gradient(135deg, hsla(242, 86%, 6%, 1), hsla(242, 86%, 24%, 1));
color: hsla(242, 86%, 88%, 1);
font-family: 'Saira', sans-serif;
position: relative;
}
*{
box-sizing: border-box;
transition: all 0.12s cubic-bezier(0.42, 0.54, 0.22, 1.26);
}
#canvas {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
audio {
position: fixed;
left: 10px;
bottom: -10px;
width: calc(100% - 20px);
}
audio.active{
bottom: 10px;
}
#thefile{
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: 1;
}
label.file{
display: inline-block;
position: absolute;
left: 50%;
top: 50%;
transform: translate3d(-50%, -50%, 0);
padding: 1rem 2rem;
border-radius: 4px;
background: hsla(242, 86%, 44%, 1);
color: hsla(242, 86%, 88%, 1);
font-size: 1.25em;
font-weight: 700;
box-shadow: 0 20px 60px hsla(0, 0%, 0%, 0.2);
cursor: pointer;
}
label.file:hover{
background: hsla(242, 86%, 34%, 1);
transform: translate3d(-50%, -55%, 0);
}
label.file:active{
background: hsla(242, 86%, 34%, 1);
transform: translate3d(-50%, -45%, 0);
}
label.file.normal{
transform: translate3d(10%, 50%, 0);
padding: 0.2rem 2rem;
font-size: 1rem;
top: 0;
left: 0;
}
</style>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="content">
<label for="thefile" class="file"> Choose an audio file
<input type="file" id="thefile" accept="audio/*" />
</label>
<audio id="audio" controls></audio>
<div id="out"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.3/dat.gui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/simplex-noise/2.3.0/simplex-noise.min.js"></script>
<script>
//initialise simplex noise instance
let noise = new SimplexNoise();
// the main visualiser function
let vizInit = function (){
let file = document.getElementById("thefile");
let audio = document.getElementById("audio");
let fileLabel = document.querySelector("label.file");
document.onload = function(e){
console.log(e);
audio.play();
play();
}
file.onchange = function(){
fileLabel.classList.add('normal');
audio.classList.add('active');
let files = this.files;
audio.src = URL.createObjectURL(files[0]);
audio.load();
audio.play();
play();
}
function play() {
let context = new AudioContext();
let src = context.createMediaElementSource(audio);
let analyser = context.createAnalyser();
src.connect(analyser);
analyser.connect(context.destination);
analyser.fftSize = 2048;
let bufferLength = analyser.frequencyBinCount;
let dataArray = new Uint8Array(bufferLength);
//here comes the webgl
let scene = new THREE.Scene();
let group = new THREE.Group();
let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0,0,100);
camera.lookAt(scene.position);
scene.add(camera);
let renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
let planeGeometry = new THREE.PlaneGeometry(800, 800, 95, 40);
let planeMaterial = new THREE.MeshLambertMaterial({
color: 0x6904ce,
side: THREE.DoubleSide,
wireframe: true
});
let plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.set(0, 30, 0);
group.add(plane);
let plane2 = new THREE.Mesh(planeGeometry, planeMaterial);
plane2.rotation.x = -0.5 * Math.PI;
plane2.position.set(0, -30, 0);
group.add(plane2);
let icosahedronGeometry = new THREE.IcosahedronGeometry(6, 3);
let torusGeomerty = new THREE.TorusGeometry(4, 3, 40, 40);
let sphereGeometry = new THREE.SphereGeometry(4, 100, 100);
let greenMaterial = new THREE.MeshLambertMaterial({
color: 0x48ff00,
wireframe: true
});
let redMaterial = new THREE.MeshLambertMaterial({
color: 0xff00f2,
wireframe: true
});
let yellowMaterial = new THREE.MeshLambertMaterial({
color: 0xeeff00,
wireframe: true,
})
let greenBall = new THREE.Mesh(icosahedronGeometry, greenMaterial);
greenBall.position.set(-30, 10, -10);
group.add(greenBall);
let redBall = new THREE.Mesh(torusGeomerty, redMaterial);
redBall.position.set(0, 0, 0);
group.add(redBall);
let yellowBall = new THREE.Mesh(sphereGeometry, yellowMaterial);
yellowBall.position.set(30, -10, 10);
group.add(yellowBall);
let ambientLight = new THREE.AmbientLight(0x8ffe09);
scene.add(ambientLight);
let spotLight = new THREE.SpotLight(0xffffff);
spotLight.intensity = 0.9;
spotLight.position.set(-10, 40, 20);
spotLight.lookAt(greenBall);
spotLight.lookAt(redBall);
spotLight.lookAt(yellowBall);
spotLight.castShadow = true;
scene.add(spotLight);
const addDiscoBall = () => {
const starGeometry = new THREE.SphereGeometry(0.25, 7, 5);
const starMaterial = new THREE.MeshBasicMaterial({color: 0xeeff00});
const [x, y, z] = Array(3).fill().map(() => Math.random() * 100);
const star = new THREE.Mesh(starGeometry, starMaterial);
star.position.set(x, y, z);
scene.add(star);
}
Array(200).fill().forEach(addDiscoBall);
scene.add(group);
document.getElementById('out').appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
render();
function render() {
analyser.getByteFrequencyData(dataArray);
console.log(analyser);
let lowerHalfArray = (dataArray.slice(0, (dataArray.length/2) - 1)).map(el => el * 1.1);
let upperHalfArray = (dataArray.slice((dataArray.length/2) - 1, dataArray.length - 1)).map(el => el * 1.25);
let overallAvg = avg(dataArray);
let lowerMax = max(lowerHalfArray);
let lowerAvg = avg(lowerHalfArray);
let upperMax = max(upperHalfArray);
let upperAvg = avg(upperHalfArray);
let lowerMaxFr = lowerMax / lowerHalfArray.length;
let lowerAvgFr = lowerAvg / lowerHalfArray.length;
let upperMaxFr = upperMax / upperHalfArray.length;
let upperAvgFr = upperAvg / upperHalfArray.length;
makeRoughGround(plane, modulate(upperAvgFr, -2, 2, 0.5, 8));
makeRoughGround(plane2, modulate(lowerMaxFr, -2, 2, 0.5, 8));
makeRoughBall(greenBall, modulate(Math.pow(lowerMaxFr, 0.2), 0, 1, 0, 8), modulate(upperAvgFr, 0, 1, 0, 4), 19);
makeRoughBall(redBall, modulate(Math.pow(lowerMaxFr, 0.4), 0, 1, 0, 8), modulate(upperAvgFr, 0, 1, 0, 4), 10);
makeRoughBall(yellowBall, modulate(Math.pow(lowerMaxFr, 0.9), 0, 1, 0, 8), modulate(upperAvgFr, 0, 1, 0, 4), 1);
greenBall.rotation.x += 0.009;
redBall.rotation.y += 0.009;
yellowBall.rotation.z += 0.009;
// camera.rotation.x += 0.008;
// camera.rotation.y += 0.008;
// camera.rotation.z += 0.009;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function makeRoughBall(mesh, bassFr, treFr, amp) {
mesh.geometry.vertices.forEach((vertex, i) => {
let offset = mesh.geometry.parameters.radius;
let amplitude = amp;
let time = window.performance.now();
vertex.normalize();
let rf = 0.0001;
let distance = (offset + bassFr ) + noise.noise3D(vertex.x + time * 0.004, vertex.y + time*0.007, vertex.z + time*0.009) * amplitude * treFr;
vertex.multiplyScalar(distance);
});
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();
}
function makeRoughGround(mesh, distortionFr) {
mesh.geometry.vertices.forEach((vertex, i) => {
let amp = 1.5; // ground amplitude
let time = Date.now(); //timestamp 11778312 ex
let distance = (noise.noise2D(vertex.x + time * 0.002, vertex.y + time * 0.001) + 0) * distortionFr * amp;
vertex.z = distance;
});
mesh.geometry.verticesNeedUpdate = true;
mesh.geometry.normalsNeedUpdate = true;
mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();
}
audio.play();
};
}
window.onload = vizInit();
document.body.addEventListener('touchend', function(ev) { context.resume(); });
//some helper functions here
const fractionate = (val, minVal, maxVal) => {
return (val - minVal)/(maxVal - minVal);
}
const modulate = (val, minVal, maxVal, outMin, outMax) => {
let fr = fractionate(val, minVal, maxVal);
let delta = outMax - outMin;
return outMin + (fr * delta);
}
const avg = (arr) => {
let total = arr.reduce((sum, b) => sum + b);
return (total / arr.length);
}
const max = (arr) => {
return arr.reduce((a, b) => Math.max(a, b));
}
</script>
</body>
</html>