This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
webaudio_sandbox.html
276 lines (191 loc) · 8.58 KB
/
webaudio_sandbox.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webaudio - sandbox</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">
</head>
<body>
<audio id="song" preload="auto" style="display: none">
<source src="sounds/358232_j_s_song.ogg" type="audio/ogg">
<source src="sounds/358232_j_s_song.mp3" type="audio/mpeg">
</audio>
<audio id="skullbeatz" preload="auto" style="display: none">
<source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
<source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
</audio>
<audio id="utopia" loop preload="auto" style="display: none">
<source src="sounds/Project_Utopia.ogg" type="audio/ogg">
<source src="sounds/Project_Utopia.mp3" type="audio/mpeg">
</audio>
<div id="overlay">
<button id="startButton">Play</button>
</div>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> webaudio - sandbox<br/>
music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank" rel="noopener">larrylarrybb</a>,
<a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a> and
<a href="http://opengameart.org/content/project-utopia-seamless-loop" target="_blank" rel="noopener">congusbongus</a><br/><br/>
navigate with WASD / arrows / mouse
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import { FirstPersonControls } from 'v3d/addons/controls/FirstPersonControls.js';
let camera, controls, scene, renderer, light;
let material1, material2, material3;
let analyser1, analyser2, analyser3;
const clock = new v3d.Clock();
const startButton = document.getElementById('startButton');
startButton.addEventListener('click', init);
function init() {
const overlay = document.getElementById('overlay');
overlay.remove();
camera = new v3d.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.set(0, 25, 0);
const listener = new v3d.AudioListener();
camera.add(listener);
scene = new v3d.Scene();
scene.fog = new v3d.FogExp2(0x000000, 0.0025);
light = new v3d.DirectionalLight(0xffffff);
light.position.set(0, 0.5, 1).normalize();
scene.add(light);
const sphere = new v3d.SphereGeometry(20, 32, 16);
material1 = new v3d.MeshPhongMaterial({ color: 0xffaa00, flatShading: true, shininess: 0 });
material2 = new v3d.MeshPhongMaterial({ color: 0xff2200, flatShading: true, shininess: 0 });
material3 = new v3d.MeshPhongMaterial({ color: 0x6622aa, flatShading: true, shininess: 0 });
// sound spheres
const mesh1 = new v3d.Mesh(sphere, material1);
mesh1.position.set(- 250, 30, 0);
scene.add(mesh1);
const sound1 = new v3d.PositionalAudio(listener);
const songElement = document.getElementById('song');
sound1.setMediaElementSource(songElement);
sound1.setRefDistance(20);
songElement.play();
mesh1.add(sound1);
//
const mesh2 = new v3d.Mesh(sphere, material2);
mesh2.position.set(250, 30, 0);
scene.add(mesh2);
const sound2 = new v3d.PositionalAudio(listener);
const skullbeatzElement = document.getElementById('skullbeatz');
sound2.setMediaElementSource(skullbeatzElement);
sound2.setRefDistance(20);
skullbeatzElement.play();
mesh2.add(sound2);
//
const mesh3 = new v3d.Mesh(sphere, material3);
mesh3.position.set(0, 30, -250);
scene.add(mesh3);
const sound3 = new v3d.PositionalAudio(listener);
const oscillator = listener.context.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(144, sound3.context.currentTime);
oscillator.start(0);
sound3.setNodeSource(oscillator);
sound3.setRefDistance(20);
sound3.setVolume(0.5);
mesh3.add(sound3);
// analysers
analyser1 = new v3d.AudioAnalyser(sound1, 32);
analyser2 = new v3d.AudioAnalyser(sound2, 32);
analyser3 = new v3d.AudioAnalyser(sound3, 32);
// global ambient audio
const sound4 = new v3d.Audio(listener);
const utopiaElement = document.getElementById('utopia');
sound4.setMediaElementSource(utopiaElement);
sound4.setVolume(0.5);
utopiaElement.play();
// ground
const helper = new v3d.GridHelper(1000, 10, 0x444444, 0x444444);
helper.position.y = 0.1;
scene.add(helper);
//
const SoundControls = function() {
this.master = listener.getMasterVolume();
this.firstSphere = sound1.getVolume();
this.secondSphere = sound2.getVolume();
this.thirdSphere = sound3.getVolume();
this.Ambient = sound4.getVolume();
};
const GeneratorControls = function() {
this.frequency = oscillator.frequency.value;
this.wavetype = oscillator.type;
};
const gui = new GUI();
const soundControls = new SoundControls();
const generatorControls = new GeneratorControls();
const volumeFolder = gui.addFolder('sound volume');
const generatorFolder = gui.addFolder('sound generator');
volumeFolder.add(soundControls, 'master').min(0.0).max(1.0).step(0.01).onChange(function() {
listener.setMasterVolume(soundControls.master);
});
volumeFolder.add(soundControls, 'firstSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
sound1.setVolume(soundControls.firstSphere);
});
volumeFolder.add(soundControls, 'secondSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
sound2.setVolume(soundControls.secondSphere);
});
volumeFolder.add(soundControls, 'thirdSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
sound3.setVolume(soundControls.thirdSphere);
});
volumeFolder.add(soundControls, 'Ambient').min(0.0).max(1.0).step(0.01).onChange(function() {
sound4.setVolume(soundControls.Ambient);
});
volumeFolder.open();
generatorFolder.add(generatorControls, 'frequency').min(50.0).max(5000.0).step(1.0).onChange(function() {
oscillator.frequency.setValueAtTime(generatorControls.frequency, listener.context.currentTime);
});
generatorFolder.add(generatorControls, 'wavetype', ['sine', 'square', 'sawtooth', 'triangle']).onChange(function() {
oscillator.type = generatorControls.wavetype;
});
generatorFolder.open();
//
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//
controls = new FirstPersonControls(camera, renderer.domElement);
controls.movementSpeed = 70;
controls.lookSpeed = 0.05;
controls.noFly = true;
controls.lookVertical = false;
//
window.addEventListener('resize', onWindowResize);
animate();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
controls.handleResize();
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
const delta = clock.getDelta();
controls.update(delta);
material1.emissive.b = analyser1.getAverageFrequency() / 256;
material2.emissive.b = analyser2.getAverageFrequency() / 256;
material3.emissive.b = analyser3.getAverageFrequency() / 256;
renderer.render(scene, camera);
}
</script>
</body>
</html>