diff --git a/audio/outside.wav b/audio/outside.wav new file mode 100644 index 0000000..91bfb5f Binary files /dev/null and b/audio/outside.wav differ diff --git a/audio/room.wav b/audio/room.wav new file mode 100644 index 0000000..fff4fe1 Binary files /dev/null and b/audio/room.wav differ diff --git a/script/audio.js b/script/audio.js index b244c7f..8d1776b 100644 --- a/script/audio.js +++ b/script/audio.js @@ -4,26 +4,81 @@ var AudioEngine = { AUDIO_BUFFER_CACHE: {}, audioContext: null, - output: null, + master: null, + + // Tracks for playing music and sound effects + // 0 - Background music + // 1 - Background music + // 2 - Sound effects + // 3 - Sound effects + tracks: null, + currentBackgroundChannel: 0, + currentTrack: null, init: function(options) { // for legacy browsers this.audioContext = new (window.AudioContext || window.webkitAudioContext); - this.output = this.audioContext.createGain(); - this.output.connect(this.audioContext.destination); + + // create master + this.master = this.audioContext.createGain(); + this.master.connect(this.audioContext.destination); + + // create 4 tracks to output to master + this.tracks = []; + for (var i = 0; i < 4; i++) { + this.tracks[i] = this.audioContext.createGain(); + this.tracks[i].connect(this.master); + } }, options: {}, // Nothing for now - _playAudioBuffer: function(buffer, loop) { + _playSound: function(buffer) { var source = this.audioContext.createBufferSource(); source.buffer = buffer; - source.connect(this.output); - source.loop = loop || false; + source.connect(this.tracks[1]); source.start(0); }, - playSound: function(src, loop) { + _fadeTrack: function(buffer) { + var newTrack = this.audioContext.createBufferSource(); + newTrack.buffer = buffer; + newTrack.loop = true; + + // figure out which background track to start on + // in order to do crossfade + var nextBackgroundChannel; + if (this.currentBackgroundChannel === 0) { + nextBackgroundChannel = 1; + } else { + nextBackgroundChannel = 0; + } + + // fade in new track + var fadeTime = this.audioContext.currentTime + 5.0; + newTrack.connect(this.tracks[nextBackgroundChannel]); + newTrack.start(0); + this.tracks[nextBackgroundChannel].gain.setValueAtTime(0.0, this.audioContext.currentTime); + this.tracks[nextBackgroundChannel].gain.linearRampToValueAtTime(1.0, fadeTime); + + // fade out old track + this.tracks[this.currentBackgroundChannel].gain.linearRampToValueAtTime(0.0, fadeTime); + if (this.currentTrack) { + this.currentTrack.stop(fadeTime + 1.0); // make sure fade has completed + } + + // switch background track + this.currentBackgroundChannel = nextBackgroundChannel; + this.currentTrack = newTrack; + }, + changeMusic: function(src) { var self = this; this.loadAudioFile(src) .then(function (buffer) { - self._playAudioBuffer(buffer, loop); + self._fadeTrack(buffer); + }); + }, + playSound: function(src) { + var self = this; + this.loadAudioFile(src) + .then(function (buffer) { + self._playSound(buffer); }); }, loadAudioFile(src) { diff --git a/script/engine.js b/script/engine.js index 072007e..45b4c36 100644 --- a/script/engine.js +++ b/script/engine.js @@ -597,7 +597,7 @@ Engine.activeModule = module; module.onArrival(diff); Notifications.printQueue(module); - + AudioEngine.changeMusic(module.music); } }, diff --git a/script/outside.js b/script/outside.js index 881bc43..0bd31b6 100644 --- a/script/outside.js +++ b/script/outside.js @@ -3,6 +3,7 @@ */ var Outside = { name: _("Outside"), + music: '/audio/outside.wav', _STORES_OFFSET: 0, _GATHER_DELAY: 60, diff --git a/script/room.js b/script/room.js index 7705994..41b5d82 100644 --- a/script/room.js +++ b/script/room.js @@ -10,7 +10,7 @@ var Room = { _NEED_WOOD_DELAY: 15 * 1000, // from when the stranger shows up, to when you need wood buttons:{}, - + music: '/audio/room.wav', Craftables: { 'trap': { name: _('trap'),