| 1 |
//---------------------------------------------------------------------------------------------------- |
|---|
| 2 |
// Flash Media Sound player class |
|---|
| 3 |
// Copyright (c) 2009 keim All rights reserved. |
|---|
| 4 |
// Distributed under BSD-style license (see org.si.license.txt). |
|---|
| 5 |
//---------------------------------------------------------------------------------------------------- |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
package org.si.sound { |
|---|
| 9 |
import flash.events.Event; |
|---|
| 10 |
import flash.events.ProgressEvent; |
|---|
| 11 |
import flash.events.IOErrorEvent; |
|---|
| 12 |
import flash.net.URLRequest; |
|---|
| 13 |
import flash.media.Sound; |
|---|
| 14 |
import flash.media.SoundLoaderContext; |
|---|
| 15 |
import org.si.sion.*; |
|---|
| 16 |
//import org.si.sion.sequencer.base.*; |
|---|
| 17 |
import org.si.sion.sequencer.SiMMLTrack; |
|---|
| 18 |
import org.si.sound.synthesizers.*; |
|---|
| 19 |
import org.si.sound.events.FlashSoundPlayerEvent; |
|---|
| 20 |
import org.si.sound.namespaces._sound_object_internal; |
|---|
| 21 |
import org.si.sound.synthesizers._synthesizer_internal; |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
/** @eventType flash.events.Event */ |
|---|
| 25 |
[Event(name="fspComplete", type="org.si.sound.events.FlashSoundPlayerEvent")] |
|---|
| 26 |
/** @eventType flash.events.Event */ |
|---|
| 27 |
[Event(name="open", type="flash.events.Event")] |
|---|
| 28 |
/** @eventType flash.events.Event */ |
|---|
| 29 |
[Event(name="id3", type="flash.events.Event")] |
|---|
| 30 |
/** @eventType flash.events.IOErrorEvent */ |
|---|
| 31 |
[Event(name="ioError", type="flash.events.IOErrorEvent")] |
|---|
| 32 |
/** @eventType flash.events.ProgressEvent */ |
|---|
| 33 |
[Event(name="progress", type="flash.events.ProgressEvent")] |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
/** FlashSoundPlayer provides advanced operations of Sound class (in flash media package). */ |
|---|
| 37 |
public class FlashSoundPlayer extends PatternSequencer |
|---|
| 38 |
{ |
|---|
| 39 |
// namespace |
|---|
| 40 |
//---------------------------------------- |
|---|
| 41 |
use namespace _sound_object_internal; |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
// variables |
|---|
| 47 |
//---------------------------------------- |
|---|
| 48 |
/** @private [protected] sound instance to play */ |
|---|
| 49 |
protected var _soundData:Sound = null; |
|---|
| 50 |
|
|---|
| 51 |
/** @private [protected] is sound data available to play ? */ |
|---|
| 52 |
protected var _isSoundDataAvailable:Boolean; |
|---|
| 53 |
|
|---|
| 54 |
/** @private [protected] synthsizer to play sound */ |
|---|
| 55 |
protected var _flashSoundOperator:SamplerSynth; |
|---|
| 56 |
|
|---|
| 57 |
/** @private [protected] playing mode, 0=stopped, 1=wait for loading, 2=play as single note, 3=play by pattern sequencer */ |
|---|
| 58 |
protected var _playingMode:int; |
|---|
| 59 |
|
|---|
| 60 |
/** @private [protected] waiting loading event count */ |
|---|
| 61 |
protected var _createdEventCount:int; |
|---|
| 62 |
|
|---|
| 63 |
/** @private [protected] completed loading event count */ |
|---|
| 64 |
protected var _completedEventCount:int; |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
// properties |
|---|
| 69 |
//---------------------------------------- |
|---|
| 70 |
/** the Sequencer instance belonging to this PatternSequencer, where the sequence pattern appears. */ |
|---|
| 71 |
public function get soundData() : Sound { return _soundData; } |
|---|
| 72 |
public function set soundData(s:Sound) : void { |
|---|
| 73 |
_soundData = s; |
|---|
| 74 |
if (_soundData == null || (_soundData.bytesTotal > 0 && _soundData.bytesLoaded == _soundData.bytesTotal)) _setSoundData(_soundData); |
|---|
| 75 |
else _addLoadingJob(_soundData); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
/** is playing ? */ |
|---|
| 79 |
override public function get isPlaying() : Boolean { return (_playingMode != 0); } |
|---|
| 80 |
|
|---|
| 81 |
/** is sound data available to play ? */ |
|---|
| 82 |
public function get isSoundDataAvailable() : Boolean { return _isSoundDataAvailable; } |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
/** Voice data to play, You cannot change the voice of this sound object. */ |
|---|
| 86 |
override public function get voice() : SiONVoice { return _synthesizer._synthesizer_internal::_voice; } |
|---|
| 87 |
override public function set voice(v:SiONVoice) : void { |
|---|
| 88 |
throw new Error("FlashSoundPlayer; You cannot change voice of this sound object."); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
/** Synthesizer to generate sound, You cannot change the synthesizer of this sound object */ |
|---|
| 93 |
override public function get synthesizer() : VoiceReference { return _synthesizer; } |
|---|
| 94 |
override public function set synthesizer(s:VoiceReference) : void { |
|---|
| 95 |
throw new Error("FlashSoundPlayer; You cannot change synthesizer of this sound object."); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
// constructor |
|---|
| 103 |
//---------------------------------------- |
|---|
| 104 |
/** constructor |
|---|
| 105 |
* @param soundData flash.media.Sound instance to control. |
|---|
| 106 |
*/ |
|---|
| 107 |
function FlashSoundPlayer(soundData:Sound = null) |
|---|
| 108 |
{ |
|---|
| 109 |
super(68, 128, 0); |
|---|
| 110 |
name = "FlashSoundPlayer"; |
|---|
| 111 |
_isSoundDataAvailable = false; |
|---|
| 112 |
_playingMode = 0; |
|---|
| 113 |
_flashSoundOperator = new SamplerSynth(); |
|---|
| 114 |
_synthesizer = _flashSoundOperator; |
|---|
| 115 |
_createdEventCount = 0; |
|---|
| 116 |
_completedEventCount = 0; |
|---|
| 117 |
this.soundData = soundData; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
// operations |
|---|
| 124 |
//---------------------------------------- |
|---|
| 125 |
/** start sequence */ |
|---|
| 126 |
override public function play() : void |
|---|
| 127 |
{ |
|---|
| 128 |
_playingMode = 1; |
|---|
| 129 |
if (_isSoundDataAvailable) _playSound(); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
/** stop sequence */ |
|---|
| 134 |
override public function stop() : void |
|---|
| 135 |
{ |
|---|
| 136 |
switch (_playingMode) { |
|---|
| 137 |
case 2: |
|---|
| 138 |
if (_track) { |
|---|
| 139 |
_synthesizer._unregisterTracks(_track); |
|---|
| 140 |
_track.setDisposable(); |
|---|
| 141 |
_track = null; |
|---|
| 142 |
_noteOff(-1, false); |
|---|
| 143 |
} |
|---|
| 144 |
_stopEffect(); |
|---|
| 145 |
break; |
|---|
| 146 |
case 3: |
|---|
| 147 |
super.stop(); |
|---|
| 148 |
break; |
|---|
| 149 |
} |
|---|
| 150 |
_playingMode = 0; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
/** load sound from url, this method is the simplificaion of setSoundData(new Sound(url, context)). |
|---|
| 155 |
* @private url same as Sound.load |
|---|
| 156 |
* @private context same as Sound.load |
|---|
| 157 |
*/ |
|---|
| 158 |
public function load(url:URLRequest, context:SoundLoaderContext=null) : void |
|---|
| 159 |
{ |
|---|
| 160 |
_soundData = new Sound(url, context); |
|---|
| 161 |
_addLoadingJob(_soundData); |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
/** Set flash sound instance with key range. |
|---|
| 166 |
* @param sound Sound instance to assign |
|---|
| 167 |
* @param keyRangeFrom Assigning key range starts from |
|---|
| 168 |
* @param keyRangeTo Assigning key range ends at. -1 to set only at the key of argument "keyRangeFrom". |
|---|
| 169 |
* @param startPoint slicing point to start data. |
|---|
| 170 |
* @param endPoint slicing point to end data. The negative value plays whole data. |
|---|
| 171 |
* @param loopPoint slicing point to repeat data. -1 means no repeat |
|---|
| 172 |
*/ |
|---|
| 173 |
public function setSoundData(sound:Sound, keyRangeFrom:int=0, keyRangeTo:int=127, startPoint:int=0, endPoint:int=-1, loopPoint:int=-1) : void |
|---|
| 174 |
{ |
|---|
| 175 |
if (sound.bytesLoaded == sound.bytesTotal) _setSoundData(sound, keyRangeFrom, keyRangeTo, startPoint, endPoint, loopPoint); |
|---|
| 176 |
else _addLoadingJob(sound, keyRangeFrom, keyRangeTo, startPoint, endPoint, loopPoint); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
// internal |
|---|
| 183 |
//---------------------------------------- |
|---|
| 184 |
private function _setSoundData(sound:Sound, keyRangeFrom:int=0, keyRangeTo:int=127, startPoint:int=0, endPoint:int=-1, loopPoint:int=-1) : void |
|---|
| 185 |
{ |
|---|
| 186 |
_isSoundDataAvailable = true; |
|---|
| 187 |
_flashSoundOperator.setSample(sound, false, keyRangeFrom, keyRangeTo).slice(startPoint, endPoint, loopPoint); |
|---|
| 188 |
if (_createdEventCount == _completedEventCount && _playingMode == 1) _playSound(); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
private function _playSound() : void |
|---|
| 193 |
{ |
|---|
| 194 |
if (_sequencer.pattern != null) { |
|---|
| 195 |
// play by PatternSequencer |
|---|
| 196 |
_playingMode = 3; |
|---|
| 197 |
super.play(); |
|---|
| 198 |
} else { |
|---|
| 199 |
// play as single note |
|---|
| 200 |
_playingMode = 2; |
|---|
| 201 |
stop(); |
|---|
| 202 |
_track = _noteOn(_note, false); |
|---|
| 203 |
if (_track) _synthesizer._registerTrack(_track); |
|---|
| 204 |
} |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
private function _addLoadingJob(sound:Sound, keyRangeFrom:int=0, keyRangeTo:int=127, startPoint:int=0, endPoint:int=-1, loopPoint:int=-1) : void |
|---|
| 209 |
{ |
|---|
| 210 |
var event:FlashSoundPlayerEvent = new FlashSoundPlayerEvent(sound, _onComplete, _onError, keyRangeFrom, keyRangeTo, startPoint, endPoint, loopPoint); |
|---|
| 211 |
_createdEventCount++; |
|---|
| 212 |
sound.addEventListener(Event.ID3, _onID3); |
|---|
| 213 |
sound.addEventListener(Event.OPEN, _onOpen); |
|---|
| 214 |
sound.addEventListener(ProgressEvent.PROGRESS, _onProgress); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
private function _removeAllEventListeners(event:FlashSoundPlayerEvent) : void |
|---|
| 219 |
{ |
|---|
| 220 |
_completedEventCount++; |
|---|
| 221 |
event._sound.removeEventListener(Event.ID3, _onID3); |
|---|
| 222 |
event._sound.removeEventListener(Event.OPEN, _onOpen); |
|---|
| 223 |
event._sound.removeEventListener(ProgressEvent.PROGRESS, _onProgress); |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
private function _onComplete(event:FlashSoundPlayerEvent) : void |
|---|
| 228 |
{ |
|---|
| 229 |
_removeAllEventListeners(event); |
|---|
| 230 |
dispatchEvent(event); |
|---|
| 231 |
_setSoundData(event._sound, event._keyRangeFrom, event._keyRangeTo, event._startPoint, event._endPoint, event._loopPoint); |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
private function _onError(event:FlashSoundPlayerEvent) : void |
|---|
| 236 |
{ |
|---|
| 237 |
_removeAllEventListeners(event); |
|---|
| 238 |
dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR, false, false, "IOError during loading Sound.")); |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
private function _onID3(event:Event) : void |
|---|
| 243 |
{ |
|---|
| 244 |
dispatchEvent(new Event(Event.ID3)); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
private function _onOpen(event:Event) : void |
|---|
| 249 |
{ |
|---|
| 250 |
dispatchEvent(new Event(Event.OPEN)); |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
private function _onProgress(event:ProgressEvent) : void |
|---|
| 255 |
{ |
|---|
| 256 |
dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _completedEventCount, _createdEventCount)); |
|---|
| 257 |
} |
|---|
| 258 |
} |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|