forked from doublespeakgames/adarkroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
286 lines (267 loc) · 11.9 KB
/
audio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Module that takes care of audio playback
*/
var AudioEngine = {
FADE_TIME: 1,
AUDIO_BUFFER_CACHE: {},
_audioContext: null,
_master: null,
_currentBackgroundMusic: null,
_currentEventAudio: null,
_currentSoundEffectAudio: null,
_initialized: false,
init: function () {
AudioEngine._initAudioContext();
// AudioEngine._preloadAudio(); // removed to save bandwidth
AudioEngine._initialized = true;
},
_preloadAudio: function () {
// start loading music and events early
// ** could be used later if we specify a better set of
// audio files to preload -- i.e. we probably don't need to load
// the later villages or events audio, and esp. not the ending
for (var key in AudioLibrary) {
if (
key.toString().indexOf('MUSIC_') > -1 ||
key.toString().indexOf('EVENT_') > -1) {
AudioEngine.loadAudioFile(AudioLibrary[key]);
}
}
},
_initAudioContext: function () {
AudioEngine._audioContext = new (window.AudioContext || window.webkitAudioContext);
AudioEngine._createMasterChannel();
},
_createMasterChannel: function () {
// create master
AudioEngine._master = AudioEngine._audioContext.createGain();
AudioEngine._master.gain.setValueAtTime(1.0, AudioEngine._audioContext.currentTime);
AudioEngine._master.connect(AudioEngine._audioContext.destination);
},
_getMissingAudioBuffer: function () {
// plays beeping sound to indicate missing audio
var buffer = AudioEngine._audioContext.createBuffer(
1,
AudioEngine._audioContext.sampleRate,
AudioEngine._audioContext.sampleRate
);
// Fill the buffer
var bufferData = buffer.getChannelData(0);
for (var i = 0; i < buffer.length / 2; i++) {
bufferData[i] = Math.sin(i * 0.05) / 4; // max .25 gain value
}
return buffer;
},
_playSound: function (buffer) {
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
return;
}
var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.onended = function(event) {
// dereference current sound effect when finished
if (AudioEngine._currentSoundEffectAudio &&
AudioEngine._currentSoundEffectAudio.source.buffer == buffer) {
AudioEngine._currentSoundEffectAudio = null;
}
};
source.connect(AudioEngine._master);
source.start();
AudioEngine._currentSoundEffectAudio = {
source: source
};
},
_playBackgroundMusic: function (buffer) {
var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.loop = true;
var envelope = AudioEngine._audioContext.createGain();
envelope.gain.setValueAtTime(0.0, AudioEngine._audioContext.currentTime);
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME;
// fade out current background music
if (AudioEngine._currentBackgroundMusic &&
AudioEngine._currentBackgroundMusic.source &&
AudioEngine._currentBackgroundMusic.source.playbackState !== 0) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(0.0, fadeTime);
AudioEngine._currentBackgroundMusic.source.stop(fadeTime + 0.3); // make sure fade has completed
}
// fade in new backgorund music
source.connect(envelope);
envelope.connect(AudioEngine._master);
source.start();
envelope.gain.linearRampToValueAtTime(1.0, fadeTime);
// update current background music
AudioEngine._currentBackgroundMusic = {
source: source,
envelope: envelope
};
},
_playEventMusic: function (buffer) {
var source = AudioEngine._audioContext.createBufferSource();
source.buffer = buffer;
source.loop = true;
var envelope = AudioEngine._audioContext.createGain();
envelope.gain.setValueAtTime(0.0, AudioEngine._audioContext.currentTime);
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;
// turn down current background music
if (AudioEngine._currentBackgroundMusic != null) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(0.2, fadeTime);
}
// fade in event music
source.connect(envelope);
envelope.connect(AudioEngine._master);
source.start();
envelope.gain.linearRampToValueAtTime(1.0, fadeTime);
// update reference
AudioEngine._currentEventAudio = {
source: source,
envelope: envelope
};
},
_stopEventMusic: function () {
var fadeTime = AudioEngine._audioContext.currentTime + AudioEngine.FADE_TIME * 2;
// fade out event music and stop
if (AudioEngine._currentEventAudio &&
AudioEngine._currentEventAudio.source &&
AudioEngine._currentEventAudio.source.buffer) {
var currentEventGainValue = AudioEngine._currentEventAudio.envelope.gain.value;
AudioEngine._currentEventAudio.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.setValueAtTime(currentEventGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentEventAudio.envelope.gain.linearRampToValueAtTime(0.0, fadeTime);
AudioEngine._currentEventAudio.source.stop(fadeTime + 1); // make sure fade has completed
AudioEngine._currentEventAudio = null;
}
// turn up background music
if (AudioEngine._currentBackgroundMusic) {
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(1.0, fadeTime);
}
},
isAudioContextRunning: function () {
return AudioEngine._audioContext.state !== 'suspended';
},
tryResumingAudioContext: function() {
if (AudioEngine._audioContext.state === 'suspended') {
AudioEngine._audioContext.resume();
}
},
playBackgroundMusic: function (src) {
if (!AudioEngine._initialized) {
return;
}
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playBackgroundMusic(buffer);
});
},
playEventMusic: function (src) {
if (!AudioEngine._initialized) {
return;
}
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playEventMusic(buffer);
});
},
stopEventMusic: function () {
if (!AudioEngine._initialized) {
return;
}
AudioEngine._stopEventMusic();
},
playSound: function (src) {
if (!AudioEngine._initialized) {
return;
}
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
AudioEngine._playSound(buffer);
});
},
loadAudioFile: function (src) {
if (src.indexOf('http') === -1) {
var path = window.location.protocol + '//' + window.location.hostname + (window.location.port ?(':' + window.location.port) : '') + window.location.pathname;
if(path.endsWith('index.html')){
path = path.slice(0, - 10);
}
src = path + src;
}
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
return new Promise(function (resolve, reject) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
});
} else {
var request = new Request(src);
return fetch(request).then(function (response) {
return response.arrayBuffer();
}).then(function (buffer) {
if (buffer.byteLength === 0) {
console.error('cannot load audio from ' + src);
return AudioEngine._getMissingAudioBuffer();
}
var decodeAudioDataPromise = AudioEngine._audioContext.decodeAudioData(buffer, function (decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src];
});
// Safari WebAudio does not return a promise based API for
// decodeAudioData, so we need to fake it if we want to play
// audio immediately on first fetch
if (decodeAudioDataPromise) {
return decodeAudioDataPromise;
} else {
return new Promise(function (resolve, reject) {
var fakePromiseId = setInterval(function() {
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
clearInterval(fakePromiseId);
}
}, 20);
});
}
});
}
},
setBackgroundMusicVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
}
if (s === undefined) {
s = 1.0;
}
// cancel any current schedules and then ramp
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(
volume,
AudioEngine._audioContext.currentTime + s
);
},
setMasterVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
}
if (s === undefined) {
s = 1.0;
}
// cancel any current schedules and then ramp
var currentGainValue = AudioEngine._master.gain.value;
AudioEngine._master.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._master.gain.setValueAtTime(currentGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._master.gain.linearRampToValueAtTime(
volume,
AudioEngine._audioContext.currentTime + s
);
}
};