mirror of
https://github.com/doublespeakgames/adarkroom.git
synced 2026-05-28 00:01:54 +08:00
rough implementation for changing background music
This commit is contained in:
Binary file not shown.
Binary file not shown.
+63
-8
@@ -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) {
|
||||
|
||||
+1
-1
@@ -597,7 +597,7 @@
|
||||
Engine.activeModule = module;
|
||||
module.onArrival(diff);
|
||||
Notifications.printQueue(module);
|
||||
|
||||
AudioEngine.changeMusic(module.music);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
var Outside = {
|
||||
name: _("Outside"),
|
||||
music: '/audio/outside.wav',
|
||||
|
||||
_STORES_OFFSET: 0,
|
||||
_GATHER_DELAY: 60,
|
||||
|
||||
+1
-1
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user