forked from robhawkes/webgl-html5-audio-visualiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
307 lines (241 loc) · 8.27 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
<!DOCTYPE html>
<html>
<head>
<title>WebGL & HTML5 Audio Visualiser - Assembly 2011</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="dsp.js"></script>
<script src="Stats.js"></script>
<script src="Three.js"></script>
</head>
<body>
<div id="display"></div>
<audio id="audio" src="Impromptu in A.ogg" controls autoplay></audio>
<script>
// Set up variables
var audio,
display,
stats,
frameBufferLength,
bufferSize,
signal,
peak,
fft,
stop,
renderer,
camera,
scene,
sceneWidth,
sceneHeight,
cameraViewAngle,
cameraAspect,
cameraNear,
cameraFar,
cameraAngle,
cubes,
numCubes;
// Start the visualisation
init();
function init() {
// Grab audio DOM element
audio = document.getElementById("audio");
// Grab display DOM element
display = document.getElementById("display");
// Stop the visualisation
stop = false;
// Set the scene size
sceneWidth = window.innerWidth;
sceneHeight = window.innerHeight;
// Set some camera attributes
cameraViewAngle = 45;
cameraAspect = sceneWidth / sceneHeight;
cameraNear = 0.1;
cameraFar = 10000;
cameraAngle = 0;
// Create a WebGL renderer and camera
renderer = new THREE.WebGLRenderer();
camera = new THREE.Camera(cameraViewAngle, cameraAspect, cameraNear, cameraFar);
// Create a WebGL scene
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000000, 0.0007);
// Camera starts at [0, 0, 0] so pull it back so you can see everything
camera.position.z = 400;
// Start the renderer
renderer.setSize(sceneWidth, sceneHeight);
// Attach the render-supplied DOM element
display.appendChild(renderer.domElement);
// Create the sphere
createSphere();
// Set up stats.js for FPS display
stats = new Stats();
// Align stat.js top-left
stats.domElement.style.position = "absolute";
stats.domElement.style.left = "0px";
stats.domElement.style.top = "0px";
// Add stats.js to the page
document.body.appendChild(stats.domElement);
// Set up event listeners
audio.addEventListener("loadedmetadata", onLoadedmetadata, false);
audio.addEventListener("MozAudioAvailable", onAudioAvailable, false);
document.addEventListener("keyup", onKeyup, false);
};
function createSphere() {
// Set up sphere variables
var radius = 100,
segments = 16,
rings = 16,
sphere,
sphereMaterial,
sphereFaces,
cubeSize = 10,
cubeHalfsize = cubeSize / 2,
particleGeometry,
particles;
// Create a sphere
sphere = new THREE.Mesh(
new THREE.SphereGeometry(radius, segments, rings)
);
// Access the faces of the sphere
sphereFaces = sphere.geometry.faces;
// Create a new cubes array
cubes = [];
// Loop through every face on the sphere
var i, v, cube, sphereFace, vertices, scaleValue;
for (i = 0; i < sphereFaces.length; i++) {
// Find the sphere face
sphereFace = sphereFaces[i];
// Create a cube
cube = new THREE.Mesh(
new THREE.CubeGeometry(cubeSize, cubeSize, cubeSize),
new THREE.MeshNormalMaterial({shading: THREE.SmoothShading})
);
// Grab the cube vertices (each corner)
vertices = cube.geometry.vertices;
for (v = 0; v < vertices.length; v++) {
// Shift each vertex by half its size
// This means it will scale from the bottom rather than the centre
vertices[v].position.z -= cubeHalfsize;
};
// Move the cube to the sphere face
cube.position = sphereFace.centroid;
// Rotate the cube so it faces towards the centre of the sphere
cube.lookAt(sphere.position);
// Scale the cube so it is smaller near the poles of the sphere
scaleValue = Math.abs(cube.position.y) / 100;
cube.scale.x = 1 - scaleValue * 0.7;
cube.scale.y = 1 - scaleValue * 0.7;
cube.scale.z = 1 - scaleValue * 0.7;
// Assign scale value to the cube so we can grab it later
cube.scaleValue = 1 - (scaleValue * 0.7);
// Add the cube to the array
cubes.push(cube);
// Add the cube to the scene
scene.addChild(cube);
};
// Update the number of cubes in the scene
numCubes = cubes.length;
// Particle system from Mr.doob
// Create a new geometry object for the particles
particleGeometry = new THREE.Geometry();
// Create as many ramdomly-positioned particles as we want
var vector;
for (i = 0; i < 4000; i++) {
vector = new THREE.Vector3(Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 - 1000);
particleGeometry.vertices.push(new THREE.Vertex(vector));
};
// Assign some colour and size parameters
var parameters = [[0xffffff, 5], [0xdddddd, 4], [0xaaaaaa, 3], [0x999999, 2], [0x777777, 1]];
// Create a particle system for each size and colour
var size, color, materials = [];
for (i = 0; i < parameters.length; i++) {
size = parameters[i][1];
color = parameters[i][0];
materials[i] = new THREE.ParticleBasicMaterial( { size: size } );
materials[i].color.setHex(color);
particles = new THREE.ParticleSystem(particleGeometry, materials[i]);
particles.rotation.x = Math.random() * 6;
particles.rotation.y = Math.random() * 6;
particles.rotation.z = Math.random() * 6;
scene.addObject(particles);
};
};
function loop() {
// Continue if animation is running
if (!stop) {
// Loop through each cube in the scene
var i, cube;
for (i = 0; i < numCubes; i++) {
cube = cubes[i];
// Scale the cube based on the respective audio value (could be refined)
cube.scale.z = cube.scaleValue + ((peak[i]) ? 1 + (3 * (fft.spectrum[i] / peak[i])) : 0);
};
// Move the camera in a non-circular orbit
camera.position.z = Math.cos(cameraAngle) * 200;
camera.position.x = Math.sin(cameraAngle) * 400;
camera.position.y = Math.sin(cameraAngle * 1.35) * 300;
// Update camera angle for orbit
cameraAngle += 0.01;
};
// Render the scene
renderer.render(scene, camera);
// Update stats.js
stats.update();
// Request a new animation frame
window.mozRequestAnimationFrame(loop);
};
function onLoadedmetadata() {
// Number of channels in the audio (mono = 1, stereo = 2, etc)
channels = audio.mozChannels;
// The frameBuffer has a length of [audio channels * 1024] by default
frameBufferLength = audio.mozFrameBufferLength;
// Find frameBuffer length for a single channel
// This is because we use mono for dsp.js
bufferSize = frameBufferLength/channels;
// Set up dsp.js for audio analysis
signal = new Float32Array(bufferSize);
peak = new Float32Array(bufferSize);
fft = new FFT(bufferSize, 44100);
loop();
};
function onAudioAvailable(e) {
// e.frameBuffer is a Float32Array with the raw audio data (32-bit float values)
// obtained from decoding the audio
// Audio FFT visualisation code from dsp.js
// Deinterleave and mix down to mono
signal = DSP.getChannel(DSP.MIX, e.frameBuffer);
// Perform forward transform
fft.forward(signal);
// Calculate peak values
for (var i = 0; i < bufferSize; i++) {
// Equalize: attenuates low freqs and boosts highs
fft.spectrum[i] *= -1 * Math.log((fft.bufferSize/2 - i) * (0.5/fft.bufferSize/2)) * fft.bufferSize;
if (peak[i] < fft.spectrum[i]) {
// Assign a new peak value
peak[i] = fft.spectrum[i];
} else {
// Peak slowly falls until a new peak is found
peak[i] *= 0.99;
};
};
};
// Keyboard controls
function onKeyup(e) {
switch (e.keyCode) {
case 65: // a
audio.style.display = (audio.style.display != "block") ? "block" : "none";
break;
case 83: // s
stop = (stop) ? false : true;
if (!stop) {
audio.play();
} else {
audio.pause();
};
break;
case 86: // v
audio.volume = (audio.volume > 0) ? 0 : 100;
break;
};
};
</script>
</body>
</html>