| 1 |
//---------------------------------------------------------------------------------------------------- |
|---|
| 2 |
// Sound object container |
|---|
| 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 org.si.sion.*; |
|---|
| 10 |
import org.si.sound.synthesizers.*; |
|---|
| 11 |
import org.si.sound.core.EffectChain; |
|---|
| 12 |
import org.si.sound.namespaces._sound_object_internal; |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
/** The SoundObjectContainer class is the base class for all objects that can serve as sound object containers on the sound list. |
|---|
| 16 |
*/ |
|---|
| 17 |
public class SoundObjectContainer extends SoundObject |
|---|
| 18 |
{ |
|---|
| 19 |
// namespace |
|---|
| 20 |
//---------------------------------------- |
|---|
| 21 |
use namespace _sound_object_internal; |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
// valiables |
|---|
| 27 |
//---------------------------------------- |
|---|
| 28 |
/** @private [protected] the list of child sound objects. */ |
|---|
| 29 |
protected var _soundList:Vector.<SoundObject>; |
|---|
| 30 |
|
|---|
| 31 |
/** @private [protected] playing flag of this container */ |
|---|
| 32 |
protected var _isPlaying:Boolean; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
// properties |
|---|
| 38 |
//---------------------------------------- |
|---|
| 39 |
/** Returns the number of children of this object. */ |
|---|
| 40 |
public function get numChildren() : int { return _soundList.length; } |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
// properties |
|---|
| 48 |
//---------------------------------------- |
|---|
| 49 |
/** @private */ |
|---|
| 50 |
override public function get isPlaying() : Boolean { return _isPlaying; } |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
/** @private */ |
|---|
| 54 |
override public function set note(n:int) : void { |
|---|
| 55 |
_note = n; |
|---|
| 56 |
for each (var sound:SoundObject in _soundList) sound.note = n; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
/** @private */ |
|---|
| 60 |
override public function set voice(v:SiONVoice) : void { |
|---|
| 61 |
super.voice = v; |
|---|
| 62 |
for each (var sound:SoundObject in _soundList) sound.voice = v; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
/** @private */ |
|---|
| 66 |
override public function set synthesizer(s:VoiceReference) : void { |
|---|
| 67 |
super.synthesizer = s; |
|---|
| 68 |
for each (var sound:SoundObject in _soundList) sound.synthesizer = s; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
/** @private */ |
|---|
| 72 |
override public function set length(l:Number) : void { |
|---|
| 73 |
_length = l; |
|---|
| 74 |
for each (var sound:SoundObject in _soundList) sound.length = l; |
|---|
| 75 |
} |
|---|
| 76 |
/** @private */ |
|---|
| 77 |
override public function set quantize(q:Number) : void { |
|---|
| 78 |
_quantize = q; |
|---|
| 79 |
for each (var sound:SoundObject in _soundList) sound.quantize = q; |
|---|
| 80 |
} |
|---|
| 81 |
/** @private */ |
|---|
| 82 |
override public function set delay(d:Number) : void { |
|---|
| 83 |
_delay = d; |
|---|
| 84 |
for each (var sound:SoundObject in _soundList) sound.delay = d; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
/** @private */ |
|---|
| 89 |
override public function set eventMask(m:int) : void { |
|---|
| 90 |
_eventMask = m; |
|---|
| 91 |
for each (var sound:SoundObject in _soundList) sound.eventMask = m; |
|---|
| 92 |
} |
|---|
| 93 |
/** @private */ |
|---|
| 94 |
override public function set eventTriggerID(id:int) : void { |
|---|
| 95 |
_eventTriggerID = id; |
|---|
| 96 |
for each (var sound:SoundObject in _soundList) sound.eventTriggerID = id; |
|---|
| 97 |
} |
|---|
| 98 |
/** @private */ |
|---|
| 99 |
override public function set coarseTune(n:int) : void { |
|---|
| 100 |
_noteShift = n; |
|---|
| 101 |
for each (var sound:SoundObject in _soundList) sound.coarseTune = n; |
|---|
| 102 |
} |
|---|
| 103 |
/** @private */ |
|---|
| 104 |
override public function set fineTune(p:Number) : void { |
|---|
| 105 |
_pitchShift = p; |
|---|
| 106 |
for each (var sound:SoundObject in _soundList) sound.fineTune = p; |
|---|
| 107 |
} |
|---|
| 108 |
/** @private */ |
|---|
| 109 |
override public function set gateTime(g:Number) : void { |
|---|
| 110 |
_gateTime = (g<0) ? 0 : (g>1) ? 1 : g; |
|---|
| 111 |
for each (var sound:SoundObject in _soundList) sound.gateTime = g; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
/** @private */ |
|---|
| 116 |
override public function set effectSend1(v:Number) : void { |
|---|
| 117 |
_volumes[1] = (v<0) ? 0 : (v>1) ? 1 : (v * 128); |
|---|
| 118 |
for each (var sound:SoundObject in _soundList) sound.effectSend1 = v; |
|---|
| 119 |
} |
|---|
| 120 |
/** @private */ |
|---|
| 121 |
override public function set effectSend2(v:Number) : void { |
|---|
| 122 |
_volumes[2] = (v<0) ? 0 : (v>1) ? 1 : (v * 128); |
|---|
| 123 |
for each (var sound:SoundObject in _soundList) sound.effectSend2 = v; |
|---|
| 124 |
} |
|---|
| 125 |
/** @private */ |
|---|
| 126 |
override public function set effectSend3(v:Number) : void { |
|---|
| 127 |
_volumes[3] = (v<0) ? 0 : (v>1) ? 1 : (v * 128); |
|---|
| 128 |
for each (var sound:SoundObject in _soundList) sound.effectSend3 = v; |
|---|
| 129 |
} |
|---|
| 130 |
/** @private */ |
|---|
| 131 |
override public function set effectSend4(v:Number) : void { |
|---|
| 132 |
_volumes[4] = (v<0) ? 0 : (v>1) ? 1 : (v * 128); |
|---|
| 133 |
for each (var sound:SoundObject in _soundList) sound.effectSend4 = v; |
|---|
| 134 |
} |
|---|
| 135 |
/** @private */ |
|---|
| 136 |
override public function set pitchBend(p:Number) : void { |
|---|
| 137 |
_pitchBend = p; |
|---|
| 138 |
for each (var sound:SoundObject in _soundList) sound.pitchBend = p; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
// constructor |
|---|
| 145 |
//---------------------------------------- |
|---|
| 146 |
/** constructor. */ |
|---|
| 147 |
function SoundObjectContainer(name:String = "") |
|---|
| 148 |
{ |
|---|
| 149 |
super(name); |
|---|
| 150 |
_soundList = new Vector.<SoundObject>(); |
|---|
| 151 |
_thisVolume = 1; |
|---|
| 152 |
_isPlaying = false; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
// operations |
|---|
| 159 |
//---------------------------------------- |
|---|
| 160 |
/** @inheritDoc */ |
|---|
| 161 |
override public function reset() : void |
|---|
| 162 |
{ |
|---|
| 163 |
super.reset(); |
|---|
| 164 |
_thisVolume = 1; |
|---|
| 165 |
for each (var sound:SoundObject in _soundList) sound.reset(); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
/** Set all children's volume by index. |
|---|
| 170 |
* @param slot streaming slot number. |
|---|
| 171 |
* @param volume volume (0:Minimum - 1:Maximum). |
|---|
| 172 |
*/ |
|---|
| 173 |
override public function setVolume(slot:int, volume:Number) : void |
|---|
| 174 |
{ |
|---|
| 175 |
_volumes[slot] = (volume<0) ? 0 : (volume>1) ? 128 : (volume * 128); |
|---|
| 176 |
for each (var sound:SoundObject in _soundList) sound.setVolume(slot, _volumes[slot]); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
/** Play all children sound. */ |
|---|
| 181 |
override public function play() : void |
|---|
| 182 |
{ |
|---|
| 183 |
_isPlaying = true; |
|---|
| 184 |
if (_effectChain && _effectChain.effectList.length > 0) { |
|---|
| 185 |
_effectChain._activateLocalEffect(_childDepth); |
|---|
| 186 |
_effectChain.setAllStreamSendLevels(_volumes); |
|---|
| 187 |
} |
|---|
| 188 |
for each (var sound:SoundObject in _soundList) sound.play(); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
/** Stop all children sound. */ |
|---|
| 193 |
override public function stop() : void |
|---|
| 194 |
{ |
|---|
| 195 |
_isPlaying = false; |
|---|
| 196 |
for each (var sound:SoundObject in _soundList) sound.stop(); |
|---|
| 197 |
if (_effectChain) { |
|---|
| 198 |
_effectChain._inactivateLocalEffect(); |
|---|
| 199 |
if (_effectChain.effectList.length == 0) { |
|---|
| 200 |
_effectChain.free(); |
|---|
| 201 |
_effectChain = null; |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
// operations for children |
|---|
| 210 |
//---------------------------------------- |
|---|
| 211 |
/** Adds a child SoundObject instance to this SoundObjectContainer instance. The added sound object will play sound during this container is playing. |
|---|
| 212 |
* The child is added to the end of all other children in this SoundObjectContainer instance. (To add a child to a specific index position, use the addChildAt() method.) |
|---|
| 213 |
* If you add a child object that already has a different sound object container as a parent, the object is removed from the child list of the other sound object container. |
|---|
| 214 |
* @param sound The SoundObject instance to add as a child of this SoundObjectContainer instance. |
|---|
| 215 |
* @return The SoundObject instance that you pass in the sound parameter |
|---|
| 216 |
*/ |
|---|
| 217 |
public function addChild(sound:SoundObject) : SoundObject |
|---|
| 218 |
{ |
|---|
| 219 |
sound.stop(); |
|---|
| 220 |
sound._setParent(this); |
|---|
| 221 |
_soundList.push(sound); |
|---|
| 222 |
if (_isPlaying) sound.play(); |
|---|
| 223 |
return sound; |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
/** Adds a child SoundObject instance to this SoundObjectContainer instance. The added sound object will play sound during this container is playing. |
|---|
| 228 |
* The child is added at the index position specified. An index of 0 represents the head of the sound list for this SoundObjectContainer object. |
|---|
| 229 |
* @param sound The SoundObject instance to add as a child of this SoundObjectContainer instance. |
|---|
| 230 |
* @param index The index position to which the child is added. If you specify a currently occupied index position, the child object that exists at that position and all higher positions are moved up one position in the child list. |
|---|
| 231 |
* @return The child sound object at the specified index position. |
|---|
| 232 |
*/ |
|---|
| 233 |
public function addChildAt(sound:SoundObject, index:int) : SoundObject |
|---|
| 234 |
{ |
|---|
| 235 |
sound.stop(); |
|---|
| 236 |
sound._setParent(this); |
|---|
| 237 |
if (index < _soundList.length) _soundList.splice(index, 0, sound); |
|---|
| 238 |
else _soundList.push(sound); |
|---|
| 239 |
if (_isPlaying) sound.play(); |
|---|
| 240 |
return sound; |
|---|
| 241 |
} |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
/** Removes the specified child SoundObject instance from the child list of the SoundObjectContainer instance. The removed sound object always stops. |
|---|
| 245 |
* The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist. |
|---|
| 246 |
* The index positions of any sound objects after the child in the SoundObjectContainer are decreased by 1. |
|---|
| 247 |
* @param sound The DisplayObject instance to remove |
|---|
| 248 |
* @return The SoundObject instance that you pass in the sound parameter. |
|---|
| 249 |
*/ |
|---|
| 250 |
public function removeChild(sound:SoundObject) : SoundObject |
|---|
| 251 |
{ |
|---|
| 252 |
var index:int = _soundList.indexOf(sound); |
|---|
| 253 |
if (index == -1) throw Error("SoundObjectContainer Error; Specifyed children is not in the children list."); |
|---|
| 254 |
_soundList.splice(index, 1); |
|---|
| 255 |
sound.stop(); |
|---|
| 256 |
sound._setParent(null); |
|---|
| 257 |
return sound; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
/** Removes a child SoundObject from the specified index position in the child list of the SoundObjectContainer. The removed sound object always stops. |
|---|
| 262 |
* The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist. |
|---|
| 263 |
* The index positions of any display objects above the child in the DisplayObjectContainer are decreased by 1. |
|---|
| 264 |
* @param The child index of the SoundObject to remove. |
|---|
| 265 |
* @return The SoundObject instance that was removed. |
|---|
| 266 |
*/ |
|---|
| 267 |
public function removeChildAt(index:int) : SoundObject |
|---|
| 268 |
{ |
|---|
| 269 |
if (index >= _soundList.length) throw Error("SoundObjectContainer Error; Specifyed index is not in the children list."); |
|---|
| 270 |
var sound:SoundObject = _soundList.splice(index, 1)[0]; |
|---|
| 271 |
sound.stop(); |
|---|
| 272 |
sound._setParent(null); |
|---|
| 273 |
return sound; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
/** Returns the child sound object instance that exists at the specified index. |
|---|
| 278 |
* @param The child index of the SoundObject to find. |
|---|
| 279 |
* @return founded SoundObject instance. |
|---|
| 280 |
*/ |
|---|
| 281 |
public function getChildAt(index:int) : SoundObject |
|---|
| 282 |
{ |
|---|
| 283 |
if (index >= _soundList.length) throw Error("SoundObjectContainer Error; Specifyed index is not in the children list."); |
|---|
| 284 |
return _soundList[index]; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
/** Returns the child sound object that exists with the specified name. |
|---|
| 289 |
* If more than one child sound object has the specified name, the method returns the first object in the child list. |
|---|
| 290 |
* @param The child name of the SoundObject to find. |
|---|
| 291 |
* @return founded SoundObject instance. Returns null if its not found. |
|---|
| 292 |
*/ |
|---|
| 293 |
public function getChildByName(name:String) : SoundObject |
|---|
| 294 |
{ |
|---|
| 295 |
for each (var sound:SoundObject in _soundList) { |
|---|
| 296 |
if (sound.name == name) return sound; |
|---|
| 297 |
} |
|---|
| 298 |
return null; |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
/** Returns the index position of a child SoundObject instance. |
|---|
| 303 |
* @param sound The SoundObject instance want to know. |
|---|
| 304 |
* @return index of specifyed SoundObject. Returns -1 if its not found. |
|---|
| 305 |
*/ |
|---|
| 306 |
public function getChildIndex(sound:SoundObject) : Number |
|---|
| 307 |
{ |
|---|
| 308 |
return _soundList.indexOf(sound); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
/** Changes the position of an existing child in the sound object container. This affects the processing order of child objects. |
|---|
| 313 |
* @param child The child SoundObject instance for which you want to change the index number. |
|---|
| 314 |
* @param index The resulting index number for the child sound object. |
|---|
| 315 |
* @param The SoundObject instance that you pass in the child parameter. |
|---|
| 316 |
*/ |
|---|
| 317 |
public function setChildIndex(child:SoundObject, index:int) : SoundObject |
|---|
| 318 |
{ |
|---|
| 319 |
return addChildAt(removeChild(child), index); |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
// oprate ancestor |
|---|
| 326 |
//---------------------------------------- |
|---|
| 327 |
/** @private [internal use] */ |
|---|
| 328 |
override internal function _updateChildDepth() : void |
|---|
| 329 |
{ |
|---|
| 330 |
_childDepth = (parent) ? (parent._childDepth + 1) : 0; |
|---|
| 331 |
for each (var sound:SoundObject in _soundList) sound._updateChildDepth(); |
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
/** @private [internal use] */ |
|---|
| 336 |
override internal function _updateMute() : void |
|---|
| 337 |
{ |
|---|
| 338 |
super._updateMute(); |
|---|
| 339 |
for each (var sound:SoundObject in _soundList) sound._updateMute(); |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
/** @private [internal use] */ |
|---|
| 344 |
override internal function _updateVolume() : void |
|---|
| 345 |
{ |
|---|
| 346 |
super._updateVolume(); |
|---|
| 347 |
for each (var sound:SoundObject in _soundList) sound._updateVolume(); |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
/** @private [internal use] */ |
|---|
| 352 |
override internal function _limitVolume() : void |
|---|
| 353 |
{ |
|---|
| 354 |
super._limitVolume(); |
|---|
| 355 |
for each (var sound:SoundObject in _soundList) sound._limitVolume(); |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
/** @private [internal use] */ |
|---|
| 360 |
override internal function _updatePan() : void |
|---|
| 361 |
{ |
|---|
| 362 |
super._updatePan(); |
|---|
| 363 |
for each (var sound:SoundObject in _soundList) sound._updatePan(); |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
/** @private [internal use] */ |
|---|
| 368 |
override internal function _limitPan() : void |
|---|
| 369 |
{ |
|---|
| 370 |
super._limitPan(); |
|---|
| 371 |
for each (var sound:SoundObject in _soundList) sound._limitPan(); |
|---|
| 372 |
} |
|---|
| 373 |
} |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
|
|---|