This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathaudio.js
More file actions
391 lines (355 loc) · 14.8 KB
/
audio.js
File metadata and controls
391 lines (355 loc) · 14.8 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* See LICENSE file.
*
* Sound implementation.
*/
(function() {
/**
* This class is a sound manager implementation which can play at least 'numChannels' sounds at the same time.
* By default, CAAT.Director instances will set eight channels to play sound.
* <p>
* If more than 'numChannels' sounds want to be played at the same time the requests will be dropped,
* so no more than 'numChannels' sounds can be concurrently played.
* <p>
* Available sounds to be played must be supplied to every CAAT.Director instance by calling <code>addSound</code>
* method. The default implementation will accept a URL/URI or a HTMLAudioElement as source.
* <p>
* The cached elements can be played, or looped. The <code>loop</code> method will return a handler to
* give the opportunity of cancelling the sound.
* <p>
* Be aware of Audio.canPlay, is able to return 'yes', 'no', 'maybe', ..., so anything different from
* '' and 'no' will do.
*
* @constructor
*
*/
CAAT.AudioManager= function() {
this.browserInfo= new CAAT.BrowserDetect();
return this;
};
CAAT.AudioManager.prototype= {
browserInfo: null,
musicEnabled: true,
fxEnabled: true,
audioCache: null, // audio elements.
channels: null, // available playing channels.
workingChannels: null, // currently playing channels.
loopingChannels: [],
audioTypes: { // supported audio formats. Don't remember where i took them from :S
'mp3': 'audio/mpeg;',
'ogg': 'audio/ogg; codecs="vorbis"',
'wav': 'audio/wav; codecs="1"',
'mp4': 'audio/mp4; codecs="mp4a.40.2"'
},
/**
* Initializes the sound subsystem by creating a fixed number of Audio channels.
* Every channel registers a handler for sound playing finalization. If a callback is set, the
* callback function will be called with the associated sound id in the cache.
*
* @param numChannels {number} number of channels to pre-create. 8 by default.
*
* @return this.
*/
initialize : function(numChannels) {
this.audioCache= [];
this.channels= [];
this.workingChannels= [];
for( var i=0; i<numChannels; i++ ) {
var channel= document.createElement('audio');
if ( null!==channel ) {
channel.finished= -1;
this.channels.push( channel );
var me= this;
channel.addEventListener(
'ended',
// on sound end, set channel to available channels list.
function(audioEvent) {
var target= audioEvent.target;
var i;
// remove from workingChannels
for( i=0; i<me.workingChannels.length; i++ ) {
if (me.workingChannels[i]===target ) {
me.workingChannels.splice(i,1);
break;
}
}
if ( target.caat_callback ) {
target.caat_callback(target.caat_id);
}
// set back to channels.
me.channels.push(target);
},
false
);
}
}
return this;
},
/**
* Tries to add an audio tag to the available list of valid audios. The audio is described by a url.
* @param id {object} an object to associate the audio element (if suitable to be played).
* @param url {string} a string describing an url.
* @param endplaying_callback {function} callback to be called upon sound end.
*
* @return {boolean} a boolean indicating whether the browser can play this resource.
*
* @private
*/
addAudioFromURL : function( id, url, endplaying_callback ) {
var extension= null;
var audio= document.createElement('audio');
if ( null!==audio ) {
if(!audio.canPlayType) {
return false;
}
extension= url.substr(url.lastIndexOf('.')+1);
var canplay= audio.canPlayType(this.audioTypes[extension]);
if(canplay!=="" && canplay!=="no") {
audio.src= url;
audio.preload = "auto";
audio.load();
if ( endplaying_callback ) {
audio.caat_callback= endplaying_callback;
audio.caat_id= id;
}
this.audioCache.push( { id:id, audio:audio } );
return true;
}
}
return false;
},
/**
* Tries to add an audio tag to the available list of valid audios. The audio element comes from
* an HTMLAudioElement.
* @param id {object} an object to associate the audio element (if suitable to be played).
* @param audio {HTMLAudioElement} a DOM audio node.
* @param endplaying_callback {function} callback to be called upon sound end.
*
* @return {boolean} a boolean indicating whether the browser can play this resource.
*
* @private
*/
addAudioFromDomNode : function( id, audio, endplaying_callback ) {
var extension= audio.src.substr(audio.src.lastIndexOf('.')+1);
if ( audio.canPlayType(this.audioTypes[extension]) ) {
if ( endplaying_callback ) {
audio.caat_callback= endplaying_callback;
audio.caat_id= id;
}
this.audioCache.push( { id:id, audio:audio } );
return true;
}
return false;
},
/**
* Adds an elements to the audio cache.
* @param id {object} an object to associate the audio element (if suitable to be played).
* @param element {URL|HTMLElement} an url or html audio tag.
* @param endplaying_callback {function} callback to be called upon sound end.
*
* @return {boolean} a boolean indicating whether the browser can play this resource.
*
* @private
*/
addAudioElement : function( id, element, endplaying_callback ) {
if ( typeof element === "string" ) {
return this.addAudioFromURL( id, element, endplaying_callback );
} else {
try {
if ( element instanceof HTMLAudioElement ) {
return this.addAudioFromDomNode( id, element, endplaying_callback );
}
}
catch(e) {
}
}
return false;
},
/**
* creates an Audio object and adds it to the audio cache.
* This function expects audio data described by two elements, an id and an object which will
* describe an audio element to be associated with the id. The object will be of the form
* array, dom node or a url string.
*
* <p>
* The audio element can be one of the two forms:
*
* <ol>
* <li>Either an HTMLAudioElement/Audio object or a string url.
* <li>An array of elements of the previous form.
* </ol>
*
* <p>
* When the audio attribute is an array, this function will iterate throught the array elements
* until a suitable audio element to be played is found. When this is the case, the other array
* elements won't be taken into account. The valid form of using this addAudio method will be:
*
* <p>
* 1.<br>
* addAudio( id, url } ). In this case, if the resource pointed by url is
* not suitable to be played (i.e. a call to the Audio element's canPlayType method return 'no')
* no resource will be added under such id, so no sound will be played when invoking the play(id)
* method.
* <p>
* 2.<br>
* addAudio( id, dom_audio_tag ). In this case, the same logic than previous case is applied, but
* this time, the parameter url is expected to be an audio tag present in the html file.
* <p>
* 3.<br>
* addAudio( id, [array_of_url_or_domaudiotag] ). In this case, the function tries to locate a valid
* resource to be played in any of the elements contained in the array. The array element's can
* be any type of case 1 and 2. As soon as a valid resource is found, it will be associated to the
* id in the valid audio resources to be played list.
*
* @return this
*/
addAudio : function( id, array_of_url_or_domnodes, endplaying_callback ) {
if ( array_of_url_or_domnodes instanceof Array ) {
/*
iterate throught array elements until we can safely add an audio element.
*/
for( var i=0; i<array_of_url_or_domnodes.length; i++ ) {
if ( this.addAudioElement(id, array_of_url_or_domnodes[i], endplaying_callback) ) {
break;
}
}
} else {
this.addAudioElement(id, array_of_url_or_domnodes, endplaying_callback);
}
return this;
},
/**
* Returns an audio object.
* @param aId {object} the id associated to the target Audio object.
* @return {object} the HTMLAudioElement addociated to the given id.
*/
getAudio : function(aId) {
for( var i=0; i<this.audioCache.length; i++ ) {
if ( this.audioCache[i].id===aId ) {
return this.audioCache[i].audio;
}
}
return null;
},
/**
* Set an audio object volume.
* @param id {object} an audio Id
* @param volume {number} volume to set. The volume value is not checked.
*
* @return this
*/
setVolume : function( id, volume ) {
var audio= this.getAudio(id);
if ( null!=audio ) {
audio.volume= volume;
}
return this;
},
/**
* Plays an audio file from the cache if any sound channel is available.
* The playing sound will occupy a sound channel and when ends playing will leave
* the channel free for any other sound to be played in.
* @param id {object} an object identifying a sound in the sound cache.
* @return this.
*/
play : function( id, callback ) {
if ( !this.fxEnabled ) {
return this;
}
var audio= this.getAudio(id);
// existe el audio, y ademas hay un canal de audio disponible.
if ( null!==audio && this.channels.length>0 ) {
var channel= this.channels.shift();
channel.src= audio.src;
channel.load();
channel.addEventListener('ended', function() {
if(typeof(callback) === "function") callback();
});
channel.volume= audio.volume;
channel.play();
this.workingChannels.push(channel);
}
return this;
},
/**
* This method creates a new AudioChannel to loop the sound with.
* It returns an Audio object so that the developer can cancel the sound loop at will.
* The user must call <code>pause()</code> method to stop playing a loop.
* <p>
* Firefox does not honor the loop property, so looping is performed by attending end playing
* event on audio elements.
*
* @return {HTMLElement} an Audio instance if a valid sound id is supplied. Null otherwise
*/
loop : function( id ) {
if (!this.musicEnabled) {
return this;
}
var audio_in_cache= this.getAudio(id);
// existe el audio, y ademas hay un canal de audio disponible.
if ( null!==audio_in_cache ) {
var audio= document.createElement('audio');
if ( null!==audio ) {
audio.src= audio_in_cache.src;
audio.preload = "auto";
if ( this.browserInfo.browser==='Firefox') {
audio.addEventListener(
'ended',
// on sound end, set channel to available channels list.
function(audioEvent) {
var target= audioEvent.target;
target.currentTime=0;
},
false
);
} else {
audio.loop= true;
}
audio.load();
audio.play();
this.loopingChannels.push(audio);
return audio;
}
}
return null;
},
/**
* Cancel all playing audio channels
* Get back the playing channels to available channel list.
*
* @return this
*/
endSound : function() {
var i;
for( i=0; i<this.workingChannels.length; i++ ) {
this.workingChannels[i].pause();
this.channels.push( this.workingChannels[i] );
}
for( i=0; i<this.loopingChannels.length; i++ ) {
this.loopingChannels[i].pause();
}
return this;
},
setSoundEffectsEnabled : function( enable ) {
this.fxEnabled= enable;
return this;
},
isSoundEffectsEnabled : function() {
return this.fxEnabled;
},
setMusicEnabled : function( enable ) {
this.musicEnabled= enable;
for( var i=0; i<this.loopingChannels.length; i++ ) {
if ( enable ) {
this.loopingChannels[i].play();
} else {
this.loopingChannels[i].pause();
}
}
return this;
},
isMusicEnabled : function() {
return this.musicEnabled;
}
};
})();