rough implementation for changing background music

This commit is contained in:
jorsi
2020-05-29 13:59:58 -04:00
parent 9ae328d49b
commit 978f53d40b
6 changed files with 66 additions and 10 deletions
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+63 -8
View File
@@ -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
View File
@@ -597,7 +597,7 @@
Engine.activeModule = module;
module.onArrival(diff);
Notifications.printQueue(module);
AudioEngine.changeMusic(module.music);
}
},
+1
View File
@@ -3,6 +3,7 @@
*/
var Outside = {
name: _("Outside"),
music: '/audio/outside.wav',
_STORES_OFFSET: 0,
_GATHER_DELAY: 60,
+1 -1
View File
@@ -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'),