-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
Copy pathwebaudio_visualizer.html
172 lines (102 loc) · 3.63 KB
/
webaudio_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webaudio - visualizer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<script id="vertexShader" type="x-shader/x-vertex">
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform sampler2D tAudioData;
varying vec2 vUv;
void main() {
vec3 backgroundColor = vec3( 0.125, 0.125, 0.125 );
vec3 color = vec3( 1.0, 1.0, 0.0 );
float f = texture2D( tAudioData, vec2( vUv.x, 0.0 ) ).r;
float i = step( vUv.y, f ) * step( f - 0.0125, vUv.y );
gl_FragColor = vec4( mix( backgroundColor, color, i ), 1.0 );
}
</script>
</head>
<body>
<div id="overlay">
<button id="startButton">Play</button>
</div>
<div id="container"></div>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - visualizer<br/>
music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a>
</div>
<script type="module">
import * as THREE from '../build/three.module.js';
let scene, camera, renderer, analyser, uniforms;
const startButton = document.getElementById( 'startButton' );
startButton.addEventListener( 'click', init );
function init() {
const fftSize = 128;
//
const overlay = document.getElementById( 'overlay' );
overlay.remove();
//
const container = document.getElementById( 'container' );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x000000 );
renderer.setPixelRatio( window.devicePixelRatio );
container.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.Camera();
//
const listener = new THREE.AudioListener();
const audio = new THREE.Audio( listener );
const file = './sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3';
if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {
const loader = new THREE.AudioLoader();
loader.load( file, function ( buffer ) {
audio.setBuffer( buffer );
audio.play();
} );
} else {
const mediaElement = new Audio( file );
mediaElement.play();
audio.setMediaElementSource( mediaElement );
}
analyser = new THREE.AudioAnalyser( audio, fftSize );
//
const format = ( renderer.capabilities.isWebGL2 ) ? THREE.RedFormat : THREE.LuminanceFormat;
uniforms = {
tAudioData: { value: new THREE.DataTexture( analyser.data, fftSize / 2, 1, format ) }
};
const material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
const geometry = new THREE.PlaneGeometry( 1, 1 );
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
//
window.addEventListener( 'resize', onWindowResize );
animate();
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
analyser.getFrequencyData();
uniforms.tAudioData.value.needsUpdate = true;
renderer.render( scene, camera );
}
</script>
</body>
</html>