-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·516 lines (442 loc) · 14.5 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web Audio API for HTML5 Games</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="bower_components/seiyria-bootstrap-slider/css/bootstrap-slider.css">
<link rel="stylesheet" href="bower_components/prism/themes/prism.css">
<link rel="stylesheet" href="bower_components/prism/plugins/line-highlight/prism-line-highlight.css">
<style>
.wrapper { width: 970px; padding: 20px }
.col-md-9 { width: 728px !important }
.col-md-3 { width: 242px !important; position: fixed; left: 738px; top: 41px }
h2, h3 { margin: 40px 0 20px 0; padding-bottom: 10px; border-bottom: 2px solid #333 }
ul { list-style-type: none; margin: 0; padding: 0 }
ul li { margin-bottom: 5px }
pre { margin-bottom: 20px !important; font-size: 1em; line-height: 1em }
button { margin-bottom: 10px !important }
.strike { text-decoration : line-through }
.slider { margin-right: 20px; width: 289px !important; height: 34px !important; margin-bottom: 10px }
.tooltip { top: -25px !important }
pre[data-line] { padding: 1em 0 1em 2em }
.line-highlight:before, .line-highlight[data-end]:after { top: 0.1em }
</style>
</head>
<body>
<div class="wrapper">
<div class="row">
<div class="col-md-9">
<h1>Web Audio API for HTML5 Games</h1>
<p>This code accompanies a tutorial on the <a href="http://nicolahibbert.com/web-audio-api-tutorial/" target="_blank">Web Audio API</a>.</p>
<!-- Test for API support -->
<div>
<h2><a id="ex-0">Test for API support</a></h2>
<pre><code class="language-javascript">var context;
try {
// still needed for Safari
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// create an AudioContext
context = new AudioContext();
} catch(e) {
// API not supported
throw new Error('Web Audio API not supported.');
}
</code></pre>
<button class="btn btn-success" data-button="example-testApi">
<span class="glyphicon glyphicon-download"></span>
Test API support
</button>
</div>
<!-- Load a sound -->
<div>
<h2><a id="ex-1a">Load a sound</a></h2>
<pre data-line="16"><code class="language-javascript">var sound;
/**
* Example 1: Load a sound
* @param {String} src Url of the sound to be loaded.
*/
function loadSound(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
// request.response is encoded... so decode it now
context.decodeAudioData(request.response, function(buffer) {
sound = buffer;
}, function(err) {
throw new Error(err);
});
}
request.send();
}
// loadSound('audio/BaseUnderAttack.mp3');
</code></pre>
<button class="btn btn-success" data-button="example-loadOne">
<span class="glyphicon glyphicon-download"></span>
Load sci-fi sound
</button>
</div>
<!-- Testing file format support -->
<div>
<h2><a id="ex-1b">Testing file format support</a></h2>
<pre><code class="language-javascript">var format = '.' + (new Audio().canPlayType('audio/ogg') !== '' ? 'ogg' : 'mp3');
// loadSound('audio/baseUnderAttack' + format);
</code></pre>
</div>
<!-- Play a sound -->
<div>
<h2><a id="ex-2">Play a sound</a></h2>
<pre><code class="language-javascript">/**
* Example 2: Play a sound
* @param {Object} buffer AudioBuffer object - a loaded sound.
*/
function playSound(buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start(0);
}
// playSound(sound);
</code></pre>
<button class="btn btn-success" data-button="example-loadOne">
<span class="glyphicon glyphicon-download"></span>
Load sci-fi sound
</button>
<button class="btn btn-primary" data-button="example-playBuffer" data-sound="scifi">
<span class="glyphicon glyphicon-play"></span>
Play sci-fi sound
</button>
</div>
<!-- Load multiple sounds -->
<div>
<h2><a id="ex-3">Load multiple sounds</a></h2>
<pre data-line="21,27,50"><code class="language-javascript">var sounds = {
laser : {
src : 'audio/laser'
},
coin : {
src : 'audio/coin'
},
explosion : {
src : 'audio/explosion'
}
};
/**
* Example 3a: Modify loadSound fn to accept changed params
* @param {Object} obj Object containing url of sound to be loaded.
*/
function loadSoundObj(obj) {
var request = new XMLHttpRequest();
request.open('GET', obj.src + format, true);
request.responseType = 'arraybuffer';
request.onload = function() {
// request.response is encoded... so decode it now
context.decodeAudioData(request.response, function(buffer) {
obj.buffer = buffer;
}, function(err) {
throw new Error(err);
});
}
request.send();
}
// loadSoundObj({ src : 'audio/baseUnderAttack' });
/**
* Example 3b: Function to loop through and load all sounds
* @param {Object} obj List of sounds to loop through.
*/
function loadSounds(obj) {
var len = obj.length, i;
// iterate over sounds obj
for (i in obj) {
if (obj.hasOwnProperty(i)) {
// load sound
loadSoundObj(obj[i]);
}
}
}
// loadSounds(sounds);
</code></pre>
<button class="btn btn-success" data-button="example-loadMultiple">
<span class="glyphicon glyphicon-download"></span>
Load laser, coin and explosion sounds
</button><br />
<button class="btn btn-primary" data-button="example-playBuffer" data-sound="laser">
<span class="glyphicon glyphicon-play"></span>
Play laser sound
</button>
<button class="btn btn-primary" data-button="example-playBuffer" data-sound="coin">
<span class="glyphicon glyphicon-play"></span>
Play coin sound
</button>
<button class="btn btn-primary" data-button="example-playBuffer" data-sound="explosion">
<span class="glyphicon glyphicon-play"></span>
Play explosion sound
</button>
</div>
<!-- Adjusting the volume -->
<div>
<h2><a id="ex-4">Adjusting the volume</a></h2>
<pre data-line="26-36"><code class="language-javascript">sounds = {
laser : {
src : 'audio/laser',
volume : 2
},
coin : {
src : 'audio/coin',
volume : 1.5
},
explosion : {
src : 'audio/explosion',
volume : 0.5
}
};
/**
* Example 4: Modify the playSoundObj function to accept volume property
* @param {Object} obj Object containing url of sound to be loaded.
*/
function playSoundObj(obj) {
var source = context.createBufferSource();
source.buffer = obj.buffer;
// create a gain node
obj.gainNode = context.createGain();
// connect the source to the gain node
source.connect(obj.gainNode);
// set the gain (volume)
obj.gainNode.gain.value = obj.volume;
// connect gain node to destination
obj.gainNode.connect(context.destination);
// play sound
source.start(0);
}
// loadSounds(sounds);
</code></pre>
<button class="btn btn-success" data-button="example-loadMultiple">
<span class="glyphicon glyphicon-download"></span>
Load laser, coin and explosion sounds
</button><br />
<input type="text" class="slider" data-sound="laser" data-slider-min="0" data-slider-max="5" data-slider-step="0.1" />
<button class="btn btn-primary" data-button="example-playObject" data-sound="laser">
<span class="glyphicon glyphicon-play"></span>
Play laser sound @ <b>1.00</b> gain
</button><br />
<input type="text" class="slider" data-sound="coin" data-slider-min="0" data-slider-max="5" data-slider-step="0.1" />
<button class="btn btn-primary" data-button="example-playObject" data-sound="coin">
<span class="glyphicon glyphicon-play"></span>
Play coin sound @ <b>1.00</b> gain
</button><br />
<input type="text" class="slider" data-sound="explosion" data-slider-min="0" data-slider-max="5" data-slider-step="0.1" />
<button class="btn btn-primary" data-button="example-playObject" data-sound="explosion">
<span class="glyphicon glyphicon-play"></span>
Play explosion sound @ <b>1.00</b> gain
</button>
</div>
<!-- Muting a sound -->
<div>
<h2><a id="ex-5">Muting a sound</a></h2>
<pre data-line="14"><code class="language-javascript">var nyan = {
src : 'audio/nyan',
volume : 1
};
loadSoundObj(nyan);
/**
* Example 5: Muting a sound
* @param {object} obj Object containing a loaded sound buffer.
*/
function muteSoundObj(obj) {
obj.gainNode.gain.value = 0;
}
// muteSoundObj(nyan);
</code></pre>
<button class="btn btn-primary" data-button="example-playNyan">
<span class="glyphicon glyphicon-play"></span>
Play <span class="strike">annoying</span> awesome music
</button>
<button class="btn btn-success" data-button="example-muteNyan">
<span class="glyphicon glyphicon-volume-off"></span>
Mute <span class="strike">annoying</span> awesome music
</button>
</div>
<!-- Looping sounds -->
<div>
<h2><a id="ex-6">Looping sounds</a></h2>
<pre data-line="30"><code class="language-javascript">sounds = {
laser : {
src : 'audio/laser',
volume : 1,
loop: true
},
coin : {
src : 'audio/coin',
volume : 1,
loop: true
},
explosion : {
src : 'audio/explosion',
volume : 1,
loop: true
}
};
/**
* Example 6: Modify the playSoundObj function again to accept a loop property
* @param {Object} obj Object containing url of sound to be loaded.
*/
function playSoundObj(obj) {
var source = context.createBufferSource();
source.buffer = obj.buffer;
// loop the audio?
source.loop = obj.loop;
// create a gain node
obj.gainNode = context.createGain();
// connect the source to the gain node
source.connect(obj.gainNode);
// set the gain (volume)
obj.gainNode.gain.value = obj.volume;
// connect gain node to destination
obj.gainNode.connect(context.destination);
// play sound
source.start(0);
}
// loadSounds(sounds);
</code></pre>
<button class="btn btn-success" data-button="example-loadMultiple">
<span class="glyphicon glyphicon-download"></span>
Load laser, coin and explosion sounds
</button><br />
<button class="btn btn-primary" data-button="example-playLooped" data-sound="laser">
<span class="glyphicon glyphicon-play"></span>
Play looped laser sound
</button>
<button class="btn btn-primary" data-button="example-playLooped" data-sound="coin">
<span class="glyphicon glyphicon-play"></span>
Play looped coin sound
</button>
<button class="btn btn-primary" data-button="example-playLooped" data-sound="explosion">
<span class="glyphicon glyphicon-play"></span>
Play looped explosion sound
</button>
<button class="btn btn-success" data-button="example-muteAllLooped">
<span class="glyphicon glyphicon-volume-off"></span>
Mute all looped sounds
</button>
</div>
<!-- Crossfading sounds -->
<div>
<h2><a id="ex-7">Crossfading sounds</a></h2>
<pre data-line="22-31"><code class="language-javascript">var crossfade = {
battle : {
src : 'audio/the-last-encounter',
volume : 1,
loop : true
},
eclipse : {
src : 'audio/red-eclipse',
volume : 0,
loop : true
}
};
/**
* Example 7: Crossfading between two sounds
* @param {Object} a Sound object to fade out.
* @param {Object} b Sound object to fade in.
*/
function crossFadeSounds(a, b) {
var currentTime = context.currentTime,
fadeTime = 3; // 3 seconds fade time
// fade out
a.gainNode.gain.linearRampToValueAtTime(1, currentTime);
a.gainNode.gain.linearRampToValueAtTime(0, currentTime + fadeTime);
// fade in
b.gainNode.gain.linearRampToValueAtTime(0, currentTime);
b.gainNode.gain.linearRampToValueAtTime(1, currentTime + fadeTime);
}
// crossFadeSounds(crossfade.battle, crossfade.eclipse);
</code></pre>
<button class="btn btn-success" data-button="example-loadMusic">
<span class="glyphicon glyphicon-download"></span>
Load 2x music tracks (~3Mb)
</button><br />
<button class="btn btn-primary" data-button="example-crossFadeMusic">
<span class="glyphicon glyphicon-play"></span>
Play track 1
</button>
<button class="btn btn-success" data-button="example-muteMusic">
<span class="glyphicon glyphicon-volume-off"></span>
Mute both tracks
</button>
</div>
<!-- Audio resources -->
<div>
<h3>Audio Resources</h3>
<p>Many thanks to <a href="http://opengameart.org/" target="_blank">OpenGameArt.Org</a> and it's contributors.</p>
<ul>
<li>
<a href="http://opengameart.org/content/scifi-rts-status-vocals" target="_blank">SciFi RTS Status Vocals</a>
</li>
<li>
<a href="http://opengameart.org/content/laser-fire" target="_blank">Laser Fire</a>
</li>
<li>
<a href="http://opengameart.org/content/8-bit-platformer-sfx" target="_blank">8-bit platformer SFX</a>
</li>
<li>
<a href="http://opengameart.org/content/rpg-battle-theme-the-last-encounter-0" target="_blank">RPG Battle Theme - The Last Encounter</a>
</li>
<li>
<a href="http://opengameart.org/content/fast-high-music" target="_blank">Fast, high music</a>
</li>
</ul>
</div>
<!-- Fixed menu -->
</div>
<div class="col-md-3">
<ul>
<li>
<a href="#ex-0">Test for API Support</a>
</li>
<li>
<a href="#ex-1a">Load a sound</a>
</li>
<li>
<a href="#ex-1b">Testing file format support</a>
</li>
<li>
<a href="#ex-2">Play a sound</a>
</li>
<li>
<a href="#ex-3">Load multiple sounds</a>
</li>
<li>
<a href="#ex-4">Adjusting the volume</a>
</li>
<li>
<a href="#ex-5">Muting a sound</a>
</li>
<li>
<a href="#ex-6">Looping sounds</a>
</li>
<li>
<a href="#ex-7">Crossfading sounds</a>
</li>
</ul>
</div>
</div>
</div>
<div class="alert alert-success alert-dismissable hidden">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Success!</strong>
<p class="message">Message here</p>
</div>
<div class="alert alert-danger alert-dismissable hidden">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong>
<p class="message">Message here</p>
</div>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="bower_components/seiyria-bootstrap-slider/js/bootstrap-slider.js"></script>
<script src="bower_components/prism/prism.js"></script>
<script src="bower_components/prism/plugins/line-highlight/prism-line-highlight.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>