Permalink
Browse files

Ogg support, with WAV fall-back; increases browser compatibility.

  • Loading branch information...
stephank committed Jun 6, 2010
1 parent 943eb0e commit 1be21543d88c8c56d972423bf3d7632dc931dd53
Showing with 59 additions and 41 deletions.
  1. +0 −20 arashi.html
  2. +0 −3 src/engine.js
  3. +59 −18 src/soundkit.js
View
@@ -35,26 +35,6 @@
</head>
<body>
-<audio src="snd/blast.wav" autobuffer></audio>
-<audio src="snd/blow.wav" autobuffer></audio>
-<audio src="snd/bonk.wav" autobuffer></audio>
-<audio src="snd/dziuung.wav" autobuffer></audio>
-<audio src="snd/fadeinphazer.wav" autobuffer></audio>
-<audio src="snd/fadeoutphazer.wav" autobuffer></audio>
-<audio src="snd/fallyell.wav" autobuffer></audio>
-<audio src="snd/fallzap.wav" autobuffer></audio>
-<audio src="snd/flythru.wav" autobuffer></audio>
-<audio src="snd/phazer.wav" autobuffer></audio>
-<audio src="snd/splitter.wav" autobuffer></audio>
-<audio src="snd/springy.wav" autobuffer></audio>
-<audio src="snd/swish.wav" autobuffer></audio>
-<audio src="snd/swoosh.wav" autobuffer></audio>
-<audio src="snd/tadaum.wav" autobuffer></audio>
-<audio src="snd/whiz.wav" autobuffer></audio>
-<audio src="snd/zrooming.wav" autobuffer></audio>
-<audio src="snd/zzfreelife.wav" autobuffer></audio>
-<audio src="snd/zzsuperzap.wav" autobuffer></audio>
-
<div id="h"></div>
<div id="v"></div>
<div id="c"><canvas id="game" width="800" height="600"></canvas></div>
View
@@ -22,9 +22,6 @@ Engine.start = function() {
frame.mouseY = e.pageY - canvas.offsetTop;
});
- // Init sound kit
- Snd.init();
-
// All systems, go!
this.resume();
this.startMachine(TitleMachine);
View
@@ -4,23 +4,64 @@
// For each sample, a helper method is created in +Snd+, named after the file's basename.
// So for example, 'snd/blast.wav' can be played using 'Snd.blast()'.
-var Snd = {};
+// This construct is so we can have a local inner function
+var Snd = (function() {
+ var retval = {};
-Snd.init = function() {
- // Iterate all <audio> elements
- $('audio').each(function() {
- var src = this.getAttribute('src');
- // Filter by elements in the 'snd' directory
- if (src.substring(0, 4) !== "snd/") { return; }
- // Cut out the basename (strip directory and extension)
- var name = src.substring(4, src.length - 4);
- // Create the helper function
- Snd[name] = function() { return Snd.play(src); };
- });
-};
+ // Let's see what kind of audio support we have.
+ var dummyAudio = $('<audio></audio>')[0],
+ extension = 'none';
+ if (dummyAudio.canPlayType) {
+ if (dummyAudio.canPlayType('audio/ogg; codecs="vorbis"') !== 'no') {
+ extension = 'ogg';
+ }
+ else {
+ extension = 'wav';
+ }
+ }
-Snd.play = function(src) {
- var element = new Audio(src);
- element.play();
- return element;
-};
+ // Inner function for preloading sounds.
+ var cache = {};
+ var load = function(name) {
+ if (extension === 'none') {
+ // Create a dummy and be done with it.
+ retval[name] = function() {};
+ }
+ else {
+ var src = 'snd/' + name + '.' + extension;
+ // Preload the audio file
+ cache[name] = new Audio(src);
+ cache[name].load();
+ // Create the helper function
+ retval[name] = function() {
+ // FIXME: this causes a new request for every sound effect in Chrome.
+ // Works peachy in Firefox, though. How to prevent?
+ var element = new Audio(src);
+ element.play();
+ return element;
+ };
+ }
+ };
+
+ load('blast');
+ load('blow');
+ load('bonk');
+ load('dziuung');
+ load('fadeinphazer');
+ load('fadeoutphazer');
+ load('fallyell');
+ load('fallzap');
+ load('flythru');
+ load('phazer');
+ load('splitter');
+ load('springy');
+ load('swish');
+ load('swoosh');
+ load('tadaum');
+ load('whiz');
+ load('zrooming');
+ load('zzfreelife');
+ load('zzsuperzap');
+
+ return retval;
+}());

0 comments on commit 1be2154

Please sign in to comment.