Permalink
Browse files

Add radio.

  • Loading branch information...
Osmose committed Mar 19, 2012
1 parent 8f6db57 commit f842b66140550022399956e9f734f26be404b888
View
@@ -8,3 +8,10 @@
* http://www.freesound.org/people/HardPCM/sounds/32954/ (shooting)
* http://en.wikipedia.org/wiki/File:WilhelmScream.ogg (injury)
* http://www.freesound.org/people/Benboncan/sounds/73581/ (game over)
+* http://freemusicarchive.org/music/2NRO8OT/Ugol_Padeniya/01-2nro8ot-CrashOfTheFirstInterstellarChannel
+* http://freemusicarchive.org/music/Sycamore_Drive/Sycamore_Drive/Hare_Versus_Tortoise
+* http://freemusicarchive.org/music/Anamanaguchi/Live_at_WFMU_on_Beastin_the_Airwaves_with_Keili_on_July_17_2011/Anamanaguchi_-_07_-_My_Skateboard_Will_Go_On
+* http://freemusicarchive.org/music/Trash80/Icarus/8bp086-01-trash80-icarus
+* http://freemusicarchive.org/music/Trash80/Icarus/8bp086-03-trash80-missing_you
+* http://freemusicarchive.org/music/Sycamore_Drive/Sycamore_Drive/City_Sounds
+* http://www.freesound.org/people/FreqMan/sounds/46728/ (cut a bit)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -9,12 +9,19 @@
"bullet": "images/bullet.png"
},
"sounds": {
- "jump": "assets/audio/jump.ogg",
- "grenade": "assets/audio/grenade.ogg",
- "shoot": "assets/audio/shoot.ogg",
- "scream": "assets/audio/scream.ogg",
- "clunk": "assets/audio/clunk.ogg",
- "talk": "assets/audio/talk.ogg"
+ "jump": "audio/jump.ogg",
+ "grenade": "audio/grenade.ogg",
+ "shoot": "audio/shoot.ogg",
+ "scream": "audio/scream.ogg",
+ "clunk": "audio/clunk.ogg",
+ "talk": "audio/talk.ogg",
+ "music0": "audio/radio/2NRO8OT_-_01_-_Crash_Of_The_First_Interstellar_Channel.ogg",
+ "music1": "audio/radio/Anamanaguchi_-_07_-_My_Skateboard_Will_Go_On.ogg",
+ "music2": "audio/radio/Sycamore_Drive_-_03_-_City_Sounds.ogg",
+ "music3": "audio/radio/Sycamore_Drive_-_04_-_Hare_Versus_Tortoise.ogg",
+ "music4": "audio/radio/Trash80_-_01_-_Icarus.ogg",
+ "music5": "audio/radio/Trash80_-_03_-_Missing_You.ogg",
+ "static": "audio/radio/static.ogg"
},
"json": {
"maps": "maps.json"
View
@@ -7,9 +7,9 @@
</head>
<body id="game">
<!-- Background Ambient Loop -->
- <audio id="audio" preload="auto" autobuffer loop>
+ <!--<audio id="audio" preload="auto" autobuffer loop>
<source src="assets/audio/loop2.ogg">
- </audio>
+ </audio>-->
<script data-main="js/game.js" src="js/require.js"></script>
<button id="snd_toggle">Turn sound on</button>
View
@@ -12,6 +12,7 @@ define(function(require) {
Tilemap = require('core/tilemap'),
Textbox = require('entities/textbox'),
Neartree = require('core/neartree'),
+ Radio = require('core/radio'),
DoubleJump = require('entities/items/double_jump');
@@ -49,6 +50,8 @@ define(function(require) {
tilemap_id: null,
neartree: new Neartree(),
+ radio: new Radio(),
+
camera: {
x: 0,
y: 0
@@ -273,6 +276,7 @@ define(function(require) {
// Start the game loop.
startGame: function() {
this.running = true;
+ this.radio.start();
this.loop();
},
View
@@ -69,10 +69,11 @@ define(function(require) {
// Preload a sound.
loadSound: function(url, id) {
- $('body').append(
- $('<audio>').attr('id', 'preload-' + id)
- .attr('src', url)
- );
+ var snd = new Audio();
+ this.resources[id] = {res: snd, loaded: false};
+ snd.addEventListener('canplaythrough', this._done_loading(id), true);
+ snd.setAttribute('id', 'preload-' + id);
+ snd.src = this._path(url);
},
// Load a JSON file that lists other resources to load.
@@ -110,6 +111,7 @@ define(function(require) {
self.resources[id].loaded = true;
if (self.loadingCallback && self.isLoadingComplete()) {
self.loadingCallback();
+ self.loadingCallback = false;
}
};
},
View
@@ -0,0 +1,57 @@
+define(function(require) {
+ var _ = require('underscore'),
+ loader = require('core/loader'),
+ util = require('util');
+
+ function Radio() {
+ var self = this;
+ _.extend(this, {
+ path: 'assets/audio/radio/',
+ curSong: 0,
+ songs: _.shuffle([
+ loader.get('music0'),
+ loader.get('music1'),
+ loader.get('music2'),
+ loader.get('music3'),
+ loader.get('music4'),
+ loader.get('music5')
+ ]),
+ snd_static: loader.get('static')
+ });
+
+ // Bind post-playback events
+ _.each(this.songs, function(song) {
+ song.addEventListener('ended', self.playstatic.bind(self), true);
+ song.volume = 0.5;
+ });
+ }
+
+ _.extend(Radio.prototype, {
+ start: function() {
+ this.playstatic();
+ },
+ playstatic: function() {
+ var self = this;
+ this.snd_static.play();
+ setTimeout(function() {
+ self.curSong++;
+ if (self.curSong >= self.songs.length) {
+ self.curSong = 0;
+ }
+
+ util.audio.fade(self.snd_static, self.songs[self.curSong],
+ 1500, 200);
+ }, 3000);
+ },
+ playnext: function() {
+ this.curSong++;
+ if (this.curSong >= this.songs.length) {
+ this.curSong = 0;
+ }
+
+ this.songs[this.curSong].play();
+ }
+ });
+
+ return Radio;
+});
View
@@ -1,7 +1,8 @@
define(function(require) {
var _ = require('underscore'),
$ = require('jquery'),
- util = require('util');
+ util = require('util'),
+ loader = require('core/loader');
// take care of playing a bunch of sounds
function Sounds() {
@@ -30,24 +31,23 @@ define(function(require) {
// Play an audio file.
// src can either be the path to an audio file or the name
// of a sound resource from resources.json.
- play: function(src) {
+ play: function(id) {
var player = this.get_next_player(),
- preloaded = $('audio#preload-' + src);
+ audio = loader.get(id),
+ src = id;
- if (preloaded.length) {
- player.src = preloaded.attr('src');
- } else {
- player.src = src;
+ if (audio !== undefined) {
+ src = audio.src;
}
+ player.src = src;
player.play();
},
// Play an audio file, but do not play again while it's playing.
// src can only be a sound resource name from resources.json.
- play_once: function(src) {
- var snd = $('audio#preload-' + src);
- if (snd.length) snd.get(0).play();
+ play_once: function(id) {
+ loader.get(id).play();
}
});
View
@@ -120,6 +120,39 @@ define(function(require) {
tx = Math.floor(x / tw),
ty = Math.floor(y / th);
return {tx: tx, ty: ty};
+ },
+
+ audio: {
+ fade: function(snd1, snd2, duration, tick) {
+ var snd1vol = snd1.volume,
+ snd1dv = snd1vol / (duration / tick),
+ snd2vol = snd2.volume,
+ snd2dv = snd2vol / (duration / tick),
+ start = 0,
+ elapsed = 0;
+
+ snd2.volume = 0;
+ snd2.play();
+ function ramp() {
+ // Cap volume so it doesn't over or underflow (floating
+ // point math sucks).
+ snd1.volume = Math.max(snd1.volume - snd1dv, 0);
+ snd2.volume = Math.min(snd2.volume + snd2dv, 1);
+ elapsed = Date.now() - start;
+
+ if (elapsed > duration) {
+ snd1.pause();
+ snd1.currentTime = 0;
+ snd1.volume = snd1vol;
+ snd2.volume = snd2vol;
+ } else {
+ setTimeout(ramp, tick);
+ }
+ }
+
+ start = Date.now();
+ setTimeout(ramp, tick);
+ }
}
};
});

0 comments on commit f842b66

Please sign in to comment.