-
Notifications
You must be signed in to change notification settings - Fork 0
/
waveform-visualizer.html
150 lines (101 loc) · 3.48 KB
/
waveform-visualizer.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - waveform visualizer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #222;
margin: 0px;
overflow: hidden;
}
a {
color: #f80;
}
</style>
</head>
<body>
<script src="three.min.js"></script>
<script src="TrackballControls.js"></script>
<script src="waveform-visualizer.js"></script>
<script>
var container;
var camera, scene, renderer, controls;
var meshes;
var SPACE = 10;
var COUNT = 150;
var slices;
init();
animate();
function init() {
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.style.color = '#fff';
info.style.link = '#f80';
info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - audio waveform';
document.body.appendChild( info );
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0x222222 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000000 );
camera.position.set( 0, 0, 800 );
controls = new THREE.TrackballControls( camera, renderer.domElement );
controls.minDistance = 200;
controls.maxDistance = 1500;
scene.add( new THREE.AmbientLight( 0x222222 ) );
var light = new THREE.PointLight( 0xffffff );
light.position.copy( camera.position );
scene.add( light );
light = new THREE.PointLight( 0xffffff );
light.position.set( 100, 100, 100 );
scene.add( light );
//
var geometry = new THREE.BoxGeometry(4, 200, 200);
// var material = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );
var material = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
var mesh;
slices = new THREE.Object3D();
window.ss = slices;
scene.add( slices );
meshes = slices.children;
for (var i = 0; i < COUNT; i++) {
mesh = new THREE.Mesh( geometry, material );
material = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
slices.add( mesh );
mesh.position.x = (i - COUNT / 2) * SPACE;
}
}
function animate() {
requestAnimationFrame( animate );
controls.update();
if (!sampled) return;
var whichPixel = Math.max(0, audio.currentTime * SAMPLES_PER_SECOND | 0);
var startIndex = Math.max(0, whichPixel - COUNT / 2 | 0);
var current = 0;
for (i = 0 ; i < COUNT; i ++) {
var v = sampled[startIndex + i];
meshes[i].scale.y = v * 10;
meshes[i].scale.z = v * 10;
if (Math.abs(startIndex + i - whichPixel) == 0) current = sampled[startIndex + i];
if (Math.abs(startIndex + i - whichPixel) < 10) {
meshes[i].material.color.setHex(0xff0000); // 0xff8000 0xff0000
} else {
meshes[i].material.color.setHex(0x333333); // 0x333333
}
}
// slices.rotation.x += current * 0.1;
slices.rotation.x += 0.01;
slices.scale.y = slices.scale.z = 0.5 + current;
renderer.render( scene, camera );
}
</script>
</body>
</html>