/
SoundManager.as
526 lines (526 loc) · 18.5 KB
/
SoundManager.as
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package skyboy.media {
/**
* SoundManager by skyboy. April 20th 2010.
* Visit http://github.com/skyboy for documentation, updates
* and more free code.
*
*
* Copyright (c) 2010, skyboy
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software with
* restriction, with limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions and limitations:
*
* ^ Attribution will be given to:
* skyboy, http://www.kongregate.com/accounts/skyboy;
* http://github.com/skyboy; http://skybov.deviantart.com
*
* ^ Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer in all copies or
* substantial portions of the Software.
*
* ^ Redistributions of modified source code must be marked as such, with
* the modifications marked and ducumented and the modifer's name clearly
* listed as having modified the source code.
*
* ^ Redistributions of source code may not add to, subtract from, or in
* any other way modify the above copyright notice, this list of conditions,
* or the following disclaimer for any reason.
*
* ^ Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* OR OTHER LIABILITY,(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER AN ACTION OF IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING FROM, OUT OF, IN CONNECTION OR
* IN ANY OTHER WAY OUT OF THE USE OF OR OTHER DEALINGS WITH THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* imports
*/
import flash.events.Event;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.utils.setTimeout;
/**
* @author skyboy
* @update 15/6/2010: add methods from UnknownGuardian
* @editor UnknownGuardian
* @editor
*/
public class SoundManager {
/**
* protected variables
**/
protected var timeDelay:int = 50, soundTypes:Vector.<int>, Sounds:Vector.<Sound>, channels:Vector.<DataStore>;
protected var currentPlayingSounds:int, maxPlayableSounds:int = 16, maxPlayableOfType:int = 4, soundTimers:Vector.<Boolean>;
protected var maximumSounds:int, soundLoops:Vector.<int>, Transforms:Vector.<SoundTransform>;
/**
* public variables
**/
public function get soundsPlaying():int {
return currentPlayingSounds;
}
/**
* constructor
* @param int: maxSounds The maximum number of sounds the SoundManager can store (4096)
* @param int: maxPlayable Maxmum number sounds that can be playing at one time (16)
* @param int: maxOfTypePlayable Maxmum number of sounds of a specific type(id) that can be playing at one time (4)
* @param int: delayForPlays The delay (in milliseconds) before another sound of type(id) X can be played again (15)
**/
public function SoundManager(maxSounds:int = 4096, maxPlayable:int = 16, maxOfTypePlayable:int = 4, delayForPlays:int = 15) {
maximumSounds = min(maxSounds);
maxPlayableSounds = min(maxPlayable);
maxPlayableOfType = min(maxOfTypePlayable);
timeDelay = min(delayForPlays);
soundTypes = new Vector.<int>(maximumSounds, true);
soundTimers = new Vector.<Boolean>(maximumSounds, true);
Sounds = new Vector.<Sound>(maximumSounds, true);
Transforms = new Vector.<SoundTransform>(maximumSounds, true);
channels = new Vector.<DataStore>(maxPlayableSounds, true);
soundLoops = new Vector.<int>(maxPlayableSounds, true);
}
/**
* public functions
**/
/**
* addSound
* @param Sound: snd The Sound object to add to the manager
* @param SoundTransform: sndTransform The SoundTransform object to apply to all new instances of the sound (null)
* @return int: The ID representing the sound type you just pushed into the manager
*/
public function addSound(snd:Sound, sndTransform:SoundTransform = null):int {
var b:int = Sounds.indexOf(null);
Sounds[b] = snd;
Transforms[b] = sndTransform;
soundTypes[b] = 0;
soundTimers[b] = true;
return b;
}
/**
* addSoundAndPlay
* @param Sound: snd The Sound object to add to the manager
* @param int: loops The number of times the sound will run (0)
* @param SoundTransform: sndTransform A SoundTransform object for use with the sound (null)
* @param Number: startTime The position to start playing the sound from (0.0)
* @param Function: callback A function that will be called when the sound completes (null)
* @param SoundTransform: defaultSoundTransform The SoundTransform object to apply to all new instances of the sound (null)
* @return int: The ID representing the sound type you just pushed into the manager
*/
public function addSoundAndPlay(snd:Sound, loops:int = 0, sndTransform:SoundTransform = null, startTime:Number = 0.0, callback:Function = null, defaultSoundTransform:SoundTransform = null):int {
var a:int = addSound(snd, defaultSoundTransform);
playSound(a, loops, sndTransform, startTime, callback);
return a;
}
/**
* addMusicAndPlay
* @param Sound: snd The Sound object to add to the manager
* @param int: loops The number of times the sound will run (0)
* @param SoundTransform: sndTransform A SoundTransform object for use with the sound (null)
* @param Number: startTime The position to start playing the sound from (0.0)
* @param SoundTransform: defaultSoundTransform The SoundTransform object to apply to all new instances of the sound (null)
* @return Array: An array with the ID representing the sound type you just pushed into the manager as element 0 and the ID of the now playing music as element 2
*/
public function addMusicAndPlay(snd:Sound, loops:int = 0, sndTransform:SoundTransform = null, startTime:Number = 0.0, defaultSoundTransform:SoundTransform = null):Array {
var a:int = addSound(snd, defaultSoundTransform);
var b:int = playMusic(a, loops, sndTransform, startTime);
return [a, b];
}
/**
* deleteSound
* @param int: type The ID of a sound added to the manager
* @param Boolean: stopPlayingAll Stop playing all sounds of type? (false)
* @return Boolean: true if the sound was removed, false if an invalid type was given
*/
public function deleteSound(type:int, stopPlayingAll:Boolean = false):Boolean {
if (valid(type)) {
if (stopPlayingAll) stopAll(type);
Sounds[type] = null;
Transforms[type] = null;
soundTypes[type] = 0;
soundTimers[type] = false;
return true;
}
return false;
}
/**
* soundsOfTypePlaying
* @param int: id The ID of a sound added to the manager
* @return int: The number of sounds of type are currently playing
*/
public function soundsOfTypePlaying(type:int):int {
return soundTypes[type];
}
/**
* playSound
* @param int: type The ID of a sound added to the manager
* @param int: loops The number of times the sound will run (0)
* @param SoundTransform: sndTransform A SoundTransform object for use with the sound (null)
* @param Number: startTime The position to start playing the sound from (0.0)
* @param Function: callback A function that will be called when the sound completes (null)
* @return Boolean: true if the sound was sucessfully started playing
*/
public function playSound(type:int, loops:int = 0, sndTransform:SoundTransform = null, startTime:Number = 0.0, callback:Function = null):Boolean {
if (valid(type)) {
if (canPlay(type)) {
increment(type);
sndTransform ||= Transforms[type];
var b:int = channels.indexOf(null);
var a:DataStore = channels[b] = new DataStore(Sounds[type], b, loops, sndTransform);
callback ||= VOID;
a.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void { e.target.removeEventListener(Event.SOUND_COMPLETE, arguments.callee); soundEnded(e, type, a); callback.call(null, e); } );
a.play(startTime);
return true;
}
} else {
throw new Error("Sound #" + type + " does not exist.", 2068);
}
return false;
}
/**
* playMusic
* @param int: type The ID of a sound added to the manager
* @param Number: loops the number of times the sound will run, Infinity will continuously run (0)
* @param SoundTransform: sndTransform a SoundTransform object for use with the sound (null)
* @param Number: startTime the position to start playing the sound from (0.0)
* @return int: the ID of the now playing SoundChannel object
*/
public function playMusic(type:int, loops:Number = 0, sndTransform:SoundTransform = null, startTime:Number = 0.0):int {
if (valid(type)) {
if (canPlay(type)) {
increment(type);
sndTransform ||= Transforms[type];
var b:int = channels.indexOf(null);
var a:DataStore = channels[b] = new DataStore(Sounds[type], b, loops, sndTransform);
a.addEventListener(Event.SOUND_COMPLETE, function(e:Event, func:Function = null):void { e.target.removeEventListener(e.type, arguments.callee); soundEnded(e, type, a); } );
a.play(startTime);
return b;
}
} else {
throw new Error("Sound #" + type + " does not exist.", 2068);
}
return -1;
}
/**
* stopMusic
* @param int: id An ID returned by playMusic
* @return Boolean: true if the sound was sucessfully stopped
*/
public function stopMusic(id:int):Boolean {
if (validC(id)) {
channels[id].stop();
return true;
}
return false;
}
/**
* stopAll
* @param int: type The ID of a sound added to the manager, or -1 for all (-1)
* @return Boolean: true if all sounds were stopped.
*/
public function stopAll(type:int = -1):Boolean {
var i:int = maxPlayableSounds, b:DataStore;
if (type == -1) {
while (~--i) {
if ((b = channels[i])) {
b.stop();
}
}
return true;
} else if (valid(type)) {
while (~--i) {
if ((b = channels[i]) && b.id == type) {
b.stop();
}
}
return true;
}
return false;
}
/**
* pauseMusic
* @param int: id An ID returned by playMusic
* @return Boolean: true if the sound was sucessfully stopped
*/
public function pause(id:int):Boolean {
if (validC(id)) {
channels[id].pause();
return true;
}
return false
}
/**
* unpauseMusic
* @param int: id An ID returned by playMusic
* @return Boolean: true if the sound was sucessfully started
*/
public function unpause(id:int):Boolean {
if (validC(id)) {
channels[id].unpause();
return true;
}
return false;
}
/**
* isPaused
* @param int: id An ID returned by playMusic
* @return Boolean: true if paused, otherwise false
*/
public function isPaused(id:int):Boolean {
if (validC(id)) {
return channels[id].paused;
}
return false;
}
/**
* changeSoundVolume
* @param int: type The ID of a sound added to the manager
* @param Number: volume A Number to set the volume to (1)
* @return Boolean: true if the sound volume was succssfully changed
*/
public function changeSoundVolume(type:int, volume:Number = 1):Boolean {
if (valid(type)) {
var sT:SoundTransform = getSoundTransform(type);
sT.volume = volume;
return setSoundTransform(type, sT);
}
return false;
}
/**
* setSoundTransform
* @param int: type The ID of a sound added to the manager
* @param SoundTransform: sndTransform The transform to set (null)
* @return Boolean: true if the transform was applied sucessfully
*/
public function setSoundTransform(type:int, sndTransform:SoundTransform = null):Boolean {
if (valid(type)) {
Transforms[type] = sndTransform;
return true;
}
return false;
}
/**
* getSoundTransform
* @param int: type The ID of a sound added to the manager
* @return SoundTransform: The sound transfrom or null
*/
public function getSoundTransform(type:int):SoundTransform {
if (valid(type)) {
return Transforms[type] || new SoundTransform(1, 0);
}
return null;
}
/**
* changeVolume
* @author UnknownGuardian
* @param int: id An ID returned by playMusic
* @param Number: volume The volume to set it to (1)
* @return Boolean: true if the sound volume was succssfully changed
* @update 15/6/2010(skyboy): Added method, changed to use setTransform, and made it so other parts of the tasnform aren't changed
*/
public function changeVolume(id:int, volume:Number = 1):Boolean {
if (validC(id)) {
var sT:SoundTransform = getTransform(id);
sT.volume = volume; // let flash throw it's own error here
return setTransform(id, sT);
}
return false;
}
/**
* setTransform
* @param int: id An ID returned by playMusic
* @param SoundTransform: sndTransform The transform to apply (null)
* @return Boolean: true if the transform was applied sucessfully
*/
public function setTransform(id:int, sndTransform:SoundTransform = null):Boolean {
if (validC(id)) {
channels[id].setTransform(sndTransform);
return true;
}
return false;
}
/**
* getTransform
* @param int: id An ID returned by playMusic
* @return SoundTransform: The sound transfrom or null
*/
public function getTransform(id:int):SoundTransform {
if (validC(id)) {
return channels[id].getTransform();
}
return null;
}
/**
* protected functions
**/
protected function valid(id:uint):Boolean {
return id < maximumSounds && Sounds[id];
}
protected function validC(id:uint):Boolean {
return id < maxPlayableSounds && channels[id];
}
protected function increment(id:int):void {
++currentPlayingSounds;
++soundTypes[id];
switchTypeCanPlay(id);
setTimeout(switchTypeCanPlay, timeDelay, id);
}
protected function canPlay(id:int):Boolean {
return soundTimers[id] && currentPlayingSounds < maxPlayableSounds && soundTypes[id] < maxPlayableOfType;
}
protected function switchTypeCanPlay(id:int):void {
soundTimers[id] = !soundTimers[id];
}
protected function soundEnded(e:Event, id:int, dStore:DataStore = null):void {
if (dStore) {
var b:int = channels.indexOf(dStore);
if (~b && b < maxPlayableSounds) {
channels[b] = null;
}
}
--soundTypes[id];
--currentPlayingSounds;
}
protected function min(x:int):int {
if (x < 0) {
x = -x;
}
return x < 2147483647 ? x : 2147483647;
}
/**
* private functions
**/
private function VOID(...Void):void {
return;
}
}
}
internal class DataStore {
private var sChannel:flash.media.SoundChannel, s:flash.media.Sound, loops:int, listner:Array, sT:flash.media.SoundTransform;
private var pausePos:Number = 0, finitePlays:Boolean, listener:Function, _p:Boolean = false, _id:int;
private function listenerHelper(e:flash.events.Event):void {
var b:flash.media.SoundChannel = e.target as flash.media.SoundChannel;
b.removeEventListener(e.type, arguments.callee);
var a:Array = listner;
if (a[1] == arguments.callee) {
loop();
} else {
b.addEventListener(e.type, a[1]);
b.dispatchEvent(e);
}
}
private function listenerRepeater(e:flash.events.Event):void {
var b:flash.media.SoundChannel = e.target as flash.media.SoundChannel;
b.removeEventListener(e.type, arguments.callee);
var remove:Boolean = loop();
if (remove) {
if (listener.length == 2) try { listener(e, this); return; } catch (er:*) { }
var a:Array = listner;
a[2] = listener;
b.addEventListener.apply(b, a);
b.dispatchEvent(e)
}
}
public function get id():int {
return _id
}
public function DataStore(_s:flash.media.Sound, id:int, _loops:Number, _sT:flash.media.SoundTransform = null) {
s = _s;
sT = _sT;
_id = id;
setLoops(_loops);
listner = [flash.events.Event.SOUND_COMPLETE, listenerHelper, false, 0, false];
}
public function loop():Boolean {
if (loops > 0) {
play();
return false;
}
return true;
}
public function play(startTime:Number = 0.0, _loops:Number = NaN):DataStore {
setLoops(_loops);
if (finitePlays) --loops;
if (sChannel) sChannel.stop();
(sChannel = s.play(startTime, 0, sT)).addEventListener.apply(sChannel, listner);
return this;
}
public function setLoops(_loops:Number):void {
if (_loops == _loops) { // this is to make sure it's not NaN (NaN == NaN is false, NaN != NaN is true)
if ((finitePlays = _loops != Infinity)) {
loops = 1;
} else {
loops = _loops < 0 ? 0 : _loops < int.MAX_VALUE ? _loops : int.MAX_VALUE;
}
}
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
this.listener = listener;
listner = [type, listenerRepeater, useCapture, priority, useWeakReference];
}
public function stop():void {
if (sChannel) {
loops = 0;
sChannel.stop();
sChannel.dispatchEvent(new flash.events.Event(flash.events.Event.SOUND_COMPLETE));
_p = false;
pausePos = 0;
}
}
public function pause():void {
if (!_p) {
pausePos = sChannel.position;
sChannel.stop();
_p = true;
}
}
public function unpause():void {
if (_p) {
(sChannel = s.play(pausePos, 0, sT)).addEventListener.apply(sChannel, listner);
pausePos = 0;
_p = false;
}
}
public function get paused():Boolean {
return _p;
}
public function dispatchEvent(...Void):void {
}
/**
* Change soundTransform
* @author UnknownGuardian
* @param New soundTransform data to be applied
* @return Void
* @update 15/6/2010(skyboy) also apply transform immediately instead of at next loop
*/
public function setTransform(t:flash.media.SoundTransform):void {
sT = t;
if (!sChannel) return;
sChannel.soundTransform = t;
}
/**
* getTransform
* @return SoundTransform: the current sound transform of the SoundChannel
*/
public function getTransform():flash.media.SoundTransform {
if (!sChannel) return null;
return sChannel.soundTransform;
}
}