-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Sound.ts
322 lines (261 loc) · 8.52 KB
/
Sound.ts
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { ExResponse } from '../../Interfaces/IAudioImplementation';
import { IAudio } from '../../Interfaces/IAudio';
import { Engine } from '../../Engine';
import { Resource } from '../Resource';
import { AudioInstance, AudioInstanceFactory } from './AudioInstance';
import { AudioContextFactory } from './AudioContext';
import { NativeSoundEvent } from '../../Events/MediaEvents';
import { Promise } from '../../Promises';
import { canPlayFile } from '../../Util/Sound';
/**
* The [[Sound]] object allows games built in Excalibur to load audio
* components, from soundtracks to sound effects. [[Sound]] is an [[ILoadable]]
* which means it can be passed to a [[Loader]] to pre-load before a game or level.
*
* [[include:Sounds.md]]
*/
export class Sound extends Resource<Blob | ArrayBuffer> implements IAudio {
/**
* Indicates whether the clip should loop when complete
* @param value Set the looping flag
*/
public set loop(value: boolean) {
this._loop = value;
for (const track of this._tracks) {
track.loop = this._loop;
}
this.logger.debug('Set loop for all instances of sound', this.path, 'to', this._loop);
}
public get loop(): boolean {
return this._loop;
}
public set volume(value: number) {
this._volume = value;
for (const track of this._tracks) {
track.volume = this._volume;
}
this.emit('volumechange', new NativeSoundEvent(this));
this.logger.debug('Set loop for all instances of sound', this.path, 'to', this._volume);
}
public get volume(): number {
return this._volume;
}
/**
* Return array of Current AudioInstances playing or being paused
*/
public get instances(): AudioInstance[] {
return this._tracks;
}
public path: string;
private _loop = false;
private _volume = 1;
private _isPaused = false;
private _tracks: AudioInstance[] = [];
private _engine: Engine;
private _wasPlayingOnHidden: boolean = false;
private _processedData = new Promise<string | AudioBuffer>();
private _audioContext = AudioContextFactory.create();
/**
* @param paths A list of audio sources (clip.wav, clip.mp3, clip.ogg) for this audio clip. This is done for browser compatibility.
*/
constructor(...paths: string[]) {
super('', '');
this._detectResponseType();
/* Chrome : MP3, WAV, Ogg
* Firefox : WAV, Ogg,
* IE : MP3, WAV coming soon
* Safari MP3, WAV, Ogg
*/
for (var path of paths) {
if (canPlayFile(path)) {
this.path = path;
break;
}
}
if (!this.path) {
this.logger.warn('This browser does not support any of the audio files specified:', paths.join(', '));
this.logger.warn('Attempting to use', paths[0]);
this.path = paths[0]; // select the first specified
}
}
public wireEngine(engine: Engine) {
if (engine) {
this._engine = engine;
this._engine.on('hidden', () => {
if (engine.pauseAudioWhenHidden && this.isPlaying()) {
this._wasPlayingOnHidden = true;
this.pause();
}
});
this._engine.on('visible', () => {
if (engine.pauseAudioWhenHidden && this._wasPlayingOnHidden) {
this.play();
this._wasPlayingOnHidden = false;
}
});
}
}
/**
* Returns how many instances of the sound are currently playing
*/
public instanceCount(): number {
return this._tracks.length;
}
/**
* Whether or not the sound is playing right now
*/
public isPlaying(): boolean {
return this._tracks.some((t) => t.isPlaying());
}
/**
* Play the sound, returns a promise that resolves when the sound is done playing
* An optional volume argument can be passed in to play the sound. Max volume is 1.0
*/
public play(volume?: number): Promise<boolean> {
if (!this.isLoaded()) {
this.logger.warn('Cannot start playing. Resource', this.path, 'is not loaded yet');
return Promise.resolve(true);
}
this.volume = volume || this.volume;
if (this._isPaused) {
return this._resumePlayback();
} else {
return this._startPlayback();
}
}
/**
* Stop the sound, and do not rewind
*/
public pause() {
if (!this.isPlaying()) {
return;
}
for (const track of this._tracks) {
track.pause();
}
this._isPaused = true;
this.emit('pause', new NativeSoundEvent(this));
this.logger.debug('Paused all instances of sound', this.path);
}
/**
* Stop the sound and rewind
*/
public stop() {
if (!this.isPlaying()) {
return;
}
for (const track of this._tracks) {
track.stop();
}
this.emit('stop', new NativeSoundEvent(this));
this._isPaused = false;
this._tracks.length = 0;
this.logger.debug('Stopped all instances of sound', this.path);
}
public setData(data: any) {
this.emit('emptied', new NativeSoundEvent(this));
this.data = data;
this._processedData = new Promise<string | AudioBuffer>();
}
public processData(data: Blob | ArrayBuffer): Promise<string | AudioBuffer> {
/**
* Processes raw arraybuffer data and decodes into WebAudio buffer (async).
*/
const processPromise: Promise<string | AudioBuffer> =
data instanceof ArrayBuffer ? this._processArrayBufferData(data) : this._processBlobData(data);
return processPromise.then((processedData) => this._setProcessedData(processedData));
}
/**
* Get Id of provided AudioInstance in current trackList
* @param track [[AudioInstance]] which Id is to be given
*/
public getTrackId(track: AudioInstance): number {
return this._tracks.indexOf(track);
}
private _resumePlayback(): Promise<boolean> {
if (this._isPaused) {
const resumed = [];
// ensure we resume *current* tracks (if paused)
for (const track of this._tracks) {
resumed.push(track.play());
}
this._isPaused = false;
this.emit('resume', new NativeSoundEvent(this));
this.logger.debug('Resuming paused instances for sound', this.path, this._tracks);
// resolve when resumed tracks are done
return Promise.join(resumed);
} else {
return Promise.resolve(true);
}
}
private _startPlayback(): Promise<boolean> {
const newTrack = this._createNewTrack();
let playPromise = new Promise<boolean>();
newTrack.then((track) => {
track.play().then((resolved) => {
// when done, remove track
this.emit('playbackend', new NativeSoundEvent(this, track));
this._tracks.splice(this.getTrackId(track), 1);
playPromise.resolve(resolved);
return resolved;
});
this.emit('playbackstart', new NativeSoundEvent(this, track));
this.logger.debug('Playing new instance for sound', this.path);
});
return playPromise;
}
private _processArrayBufferData(data: ArrayBuffer): Promise<AudioBuffer> {
const complete = new Promise<AudioBuffer>();
this._audioContext.decodeAudioData(
data,
(buffer: AudioBuffer) => {
complete.resolve(buffer);
},
() => {
this.logger.error(
'Unable to decode ' +
' this browser may not fully support this format, or the file may be corrupt, ' +
'if this is an mp3 try removing id3 tags and album art from the file.'
);
complete.resolve(undefined);
}
);
return complete;
}
private _processBlobData(data: Blob): Promise<string> {
return new Promise<string>().resolve(super.processData(data));
}
private _setProcessedData(processedData: string | AudioBuffer): void {
this._processedData.resolve(processedData);
}
private _createNewTrack(): Promise<AudioInstance> {
const aiPromise = new Promise<AudioInstance>();
if (this._processedData.state() !== 0) {
this.processData(this.data);
}
this._processedData.then(
(processedData) => {
aiPromise.resolve(this._getTrackInstance(processedData));
return processedData;
},
(error) => {
this.logger.error(error, 'Cannot create AudioInstance due to wrong processed data.');
}
);
return aiPromise;
}
private _getTrackInstance(data: string | AudioBuffer): AudioInstance {
const newTrack = AudioInstanceFactory.create(data);
newTrack.loop = this.loop;
newTrack.volume = this.volume;
this._tracks.push(newTrack);
return newTrack;
}
private _detectResponseType() {
if ((<any>window).AudioContext) {
this.responseType = ExResponse.type.arraybuffer;
} else {
this.responseType = ExResponse.type.blob;
}
}
}