| 1 |
//---------------------------------------------------------------------------------------------------- |
|---|
| 2 |
// Sound object |
|---|
| 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.EventDispatcher; |
|---|
| 10 |
import org.si.sion.*; |
|---|
| 11 |
import org.si.sion.utils.Translator; |
|---|
| 12 |
import org.si.sion.utils.Fader; |
|---|
| 13 |
import org.si.sion.namespaces._sion_internal; |
|---|
| 14 |
import org.si.sion.events.SiONEvent; |
|---|
| 15 |
import org.si.sion.events.SiONTrackEvent; |
|---|
| 16 |
import org.si.sion.effector.SiEffectBase; |
|---|
| 17 |
import org.si.sion.module.SiOPMModule; |
|---|
| 18 |
import org.si.sion.sequencer.SiMMLTrack; |
|---|
| 19 |
import org.si.sound.namespaces._sound_object_internal; |
|---|
| 20 |
import org.si.sound.core.EffectChain; |
|---|
| 21 |
import org.si.sound.patterns.Sequencer; |
|---|
| 22 |
import org.si.sound.events.SoundObjectEvent; |
|---|
| 23 |
import org.si.sound.synthesizers.VoiceReference; |
|---|
| 24 |
import org.si.sound.synthesizers.BasicSynth; |
|---|
| 25 |
import org.si.sound.synthesizers._synthesizer_internal; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
/** @eventType org.si.sound.events.SoundObjectEvent.NOTE_ON_STREAM */ |
|---|
| 29 |
[Event(name="noteOnStream", type="org.si.sound.events.SoundObjectEvent")] |
|---|
| 30 |
/** @eventType org.si.sound.events.SoundObjectEvent.NOTE_OFF_STREAM */ |
|---|
| 31 |
[Event(name="noteOffStream", type="org.si.sound.events.SoundObjectEvent")] |
|---|
| 32 |
/** @eventType org.si.sound.events.SoundObjectEvent.NOTE_ON_FRAME */ |
|---|
| 33 |
[Event(name="noteOnFrame", type="org.si.sound.events.SoundObjectEvent")] |
|---|
| 34 |
/** @eventType org.si.sound.events.SoundObjectEvent.NOTE_OFF_FRAME */ |
|---|
| 35 |
[Event(name="noteOffFrame", type="org.si.sound.events.SoundObjectEvent")] |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
/** The SoundObject class is the base class of all objects that operates sounds by SiONDriver. |
|---|
| 39 |
*/ |
|---|
| 40 |
public class SoundObject extends EventDispatcher |
|---|
| 41 |
{ |
|---|
| 42 |
// namespace |
|---|
| 43 |
//---------------------------------------- |
|---|
| 44 |
use namespace _synthesizer_internal; |
|---|
| 45 |
use namespace _sound_object_internal; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
// valiables |
|---|
| 51 |
//---------------------------------------- |
|---|
| 52 |
/** Name. */ |
|---|
| 53 |
public var name:String; |
|---|
| 54 |
|
|---|
| 55 |
/** @private [protected] Base note of this sound */ |
|---|
| 56 |
protected var _note:int; |
|---|
| 57 |
/** @private [protected] Synthesizer instance */ |
|---|
| 58 |
protected var _synthesizer:VoiceReference; |
|---|
| 59 |
/** @private [protected] Synthesizer instance to use SiONVoice */ |
|---|
| 60 |
protected var _voiceReference:VoiceReference; |
|---|
| 61 |
/** @private [protected] Effect chain instance */ |
|---|
| 62 |
protected var _effectChain:EffectChain; |
|---|
| 63 |
/** @private [protected] track for noteOn() */ |
|---|
| 64 |
protected var _track:SiMMLTrack; |
|---|
| 65 |
/** @private [protected] tracks for sequenceOn() */ |
|---|
| 66 |
protected var _tracks:Vector.<SiMMLTrack>; |
|---|
| 67 |
/** @private [protected] Auto-fader to fade in/out. */ |
|---|
| 68 |
protected var _fader:Fader; |
|---|
| 69 |
/** @private [protected] Fader volume. */ |
|---|
| 70 |
protected var _faderVolume:Number; |
|---|
| 71 |
|
|---|
| 72 |
/** @private [protected] Sound length uint in 16th beat, 0 sets inifinity length. @default 0. */ |
|---|
| 73 |
protected var _length:Number; |
|---|
| 74 |
/** @private [protected] Sound delay uint in 16th beat. @default 0. */ |
|---|
| 75 |
protected var _delay:Number; |
|---|
| 76 |
/** @private [protected] Synchronizing uint in 16th beat. (0:No synchronization, 1:sync.with 16th, 4:sync.with 4th). @default 0. */ |
|---|
| 77 |
protected var _quantize:Number; |
|---|
| 78 |
|
|---|
| 79 |
/** @private [protected] Note shift in half-tone unit. */ |
|---|
| 80 |
protected var _noteShift:int; |
|---|
| 81 |
/** @private [protected] Pitch shift in half-tone unit. */ |
|---|
| 82 |
protected var _pitchShift:Number; |
|---|
| 83 |
/** @private [protected] gate ratio (value of 'q' command * 0.125) */ |
|---|
| 84 |
protected var _gateTime:Number; |
|---|
| 85 |
/** @private [protected] Event mask (value of '@mask' command) */ |
|---|
| 86 |
protected var _eventMask:Number; |
|---|
| 87 |
/** @private [protected] Event trigger ID */ |
|---|
| 88 |
protected var _eventTriggerID:int; |
|---|
| 89 |
/** @private [protected] note on trigger | (note off trigger << 2) trigger type */ |
|---|
| 90 |
protected var _noteTriggerFlags:int; |
|---|
| 91 |
/** @private [protected] listening note event trigger */ |
|---|
| 92 |
protected var _listeningFlags:int; |
|---|
| 93 |
|
|---|
| 94 |
/** @private [protected] volumes for all streams */ |
|---|
| 95 |
protected var _volumes:Vector.<int>; |
|---|
| 96 |
/** @private [protected] total panning of all ancestors */ |
|---|
| 97 |
protected var _pan:Number; |
|---|
| 98 |
/** @private [protected] total mute flag of all ancestors */ |
|---|
| 99 |
protected var _mute:Boolean; |
|---|
| 100 |
/** @private [protected] Pitch bend in half-tone unit. */ |
|---|
| 101 |
protected var _pitchBend:Number; |
|---|
| 102 |
|
|---|
| 103 |
/** @private [protected] parent container */ |
|---|
| 104 |
protected var _parent:SoundObjectContainer; |
|---|
| 105 |
/** @private [protected] the depth of parent-child chain */ |
|---|
| 106 |
protected var _childDepth:int; |
|---|
| 107 |
/** @private [protected] volume of this sound object */ |
|---|
| 108 |
protected var _thisVolume:Number; |
|---|
| 109 |
/** @private [protected] panning of this sound object */ |
|---|
| 110 |
protected var _thisPan:Number; |
|---|
| 111 |
/** @private [protected] mute flag of this sound object */ |
|---|
| 112 |
protected var _thisMute:Boolean; |
|---|
| 113 |
|
|---|
| 114 |
/** @private [protected] track id. This value is asigned when its created. */ |
|---|
| 115 |
protected var _trackID:int; |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
// properties |
|---|
| 121 |
//---------------------------------------- |
|---|
| 122 |
/** SiONDriver instrance to operate. this returns null when driver is not created. */ |
|---|
| 123 |
public function get driver() : SiONDriver { return SiONDriver.mutex; } |
|---|
| 124 |
|
|---|
| 125 |
/** parent container. */ |
|---|
| 126 |
public function get parent() : SoundObjectContainer { return _parent; } |
|---|
| 127 |
|
|---|
| 128 |
/** is playing ? */ |
|---|
| 129 |
public function get isPlaying() : Boolean { return (_track != null); } |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
/** Base note of this sound */ |
|---|
| 133 |
public function get note() : int { return _note; } |
|---|
| 134 |
public function set note(n:int) : void { _note = n; } |
|---|
| 135 |
|
|---|
| 136 |
/** Voice data to play */ |
|---|
| 137 |
public function get voice() : SiONVoice { |
|---|
| 138 |
return _synthesizer._synthesizer_internal::_voice; |
|---|
| 139 |
} |
|---|
| 140 |
public function set voice(v:SiONVoice) : void { |
|---|
| 141 |
_voiceReference.voice = v; |
|---|
| 142 |
if (!isPlaying) _synthesizer = _voiceReference; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
/** Synthesizer to generate sound */ |
|---|
| 146 |
public function get synthesizer() : VoiceReference { |
|---|
| 147 |
return _synthesizer; |
|---|
| 148 |
} |
|---|
| 149 |
public function set synthesizer(s:VoiceReference) : void { |
|---|
| 150 |
if (isPlaying) throw new Error("SoundObject: Synthesizer should not be changed during playing."); |
|---|
| 151 |
_synthesizer = s || _voiceReference; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
/** Sound length in 16th beat, 0 sets inifinity length. @default 0. */ |
|---|
| 155 |
public function get length() : Number { return _length; } |
|---|
| 156 |
public function set length(l:Number) : void { _length = l; } |
|---|
| 157 |
|
|---|
| 158 |
/** Synchronizing quantizing, uint in 16th beat. (0:No synchronization, 1:sync.with 16th, 4:sync.with 4th). @default 0. */ |
|---|
| 159 |
public function get quantize() : Number { return _quantize; } |
|---|
| 160 |
public function set quantize(q:Number) : void { _quantize = q; } |
|---|
| 161 |
|
|---|
| 162 |
/** Sound delay, uint in 16th beat. @default 0. */ |
|---|
| 163 |
public function get delay() : Number { return _delay; } |
|---|
| 164 |
public function set delay(d:Number) : void { _delay = d; } |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
/** Master coarse tuning, 1 for half-tone. */ |
|---|
| 169 |
public function get coarseTune() : int { return _noteShift; } |
|---|
| 170 |
public function set coarseTune(n:int) : void { |
|---|
| 171 |
_noteShift = n; |
|---|
| 172 |
if (_track) _track.noteShift = _noteShift; |
|---|
| 173 |
} |
|---|
| 174 |
/** Master fine tuning, 1 for half-tone, you can specify fineTune<-1 or fineTune>1. */ |
|---|
| 175 |
public function get fineTune() : Number { return _pitchShift * 0.015625; } |
|---|
| 176 |
public function set fineTune(p:Number) : void { |
|---|
| 177 |
_pitchShift = p; |
|---|
| 178 |
if (_track) _track.pitchShift = _pitchShift * 64; |
|---|
| 179 |
} |
|---|
| 180 |
/** Track gate time (0:Minimum - 1:Maximum). (value of 'q' command * 0.125) */ |
|---|
| 181 |
public function get gateTime() : Number { return _gateTime; } |
|---|
| 182 |
public function set gateTime(g:Number) : void { |
|---|
| 183 |
_gateTime = (g<0) ? 0 : (g>1) ? 1 : g; |
|---|
| 184 |
if (_track) _track.quantRatio = _gateTime; |
|---|
| 185 |
} |
|---|
| 186 |
/** Track event mask. (value of '@mask' command) */ |
|---|
| 187 |
public function get eventMask() : int { return _eventMask; } |
|---|
| 188 |
public function set eventMask(m:int) : void { |
|---|
| 189 |
_eventMask = m; |
|---|
| 190 |
if (_track) _track.eventMask = _eventMask; |
|---|
| 191 |
} |
|---|
| 192 |
/** Track id */ |
|---|
| 193 |
public function get trackID() : int { return _trackID; } |
|---|
| 194 |
/** Track event trigger ID */ |
|---|
| 195 |
public function get eventTriggerID() : int { return _eventTriggerID; } |
|---|
| 196 |
public function set eventTriggerID(id:int) : void { _eventTriggerID = id; } |
|---|
| 197 |
/** Track note on trigger type */ |
|---|
| 198 |
public function get noteOnTriggerType() : int { return _noteTriggerFlags & 3; } |
|---|
| 199 |
/** Track note off trigger type */ |
|---|
| 200 |
public function get noteOffTriggerType() : int { return _noteTriggerFlags >> 2; } |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
/** Channel mute, this property can control track after play(). */ |
|---|
| 204 |
public function get mute() : Boolean { return _thisMute; } |
|---|
| 205 |
public function set mute(m:Boolean) : void { |
|---|
| 206 |
_thisMute = m; |
|---|
| 207 |
_updateMute(); |
|---|
| 208 |
if (_track) _track.channel.mute = _mute; |
|---|
| 209 |
} |
|---|
| 210 |
/** Channel volume (0:Minimum - 1:Maximum), this property can control track after play(). */ |
|---|
| 211 |
public function get volume() : Number { return _thisVolume; } |
|---|
| 212 |
public function set volume(v:Number) : void { |
|---|
| 213 |
_thisVolume = v; |
|---|
| 214 |
_updateVolume(); |
|---|
| 215 |
_limitVolume(); |
|---|
| 216 |
_updateStreamSend(0, _volumes[0] * 0.0078125); |
|---|
| 217 |
} |
|---|
| 218 |
/** Channel panning (-1:Left - 0:Center - +1:Right), this property can control track after play(). */ |
|---|
| 219 |
public function get pan() : Number { return _thisPan; } |
|---|
| 220 |
public function set pan(p:Number) : void { |
|---|
| 221 |
_thisPan = p; |
|---|
| 222 |
_updatePan(); |
|---|
| 223 |
_limitPan(); |
|---|
| 224 |
if (_track) _track.channel.pan = _pan*64; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
/** Channel effect send level for slot 1 (0:Minimum - 1:Maximum), this property can control track after play(). */ |
|---|
| 229 |
public function get effectSend1() : Number { return _volumes[1] * 0.0078125; } |
|---|
| 230 |
public function set effectSend1(v:Number) : void { |
|---|
| 231 |
v = (v<0) ? 0 : (v>1) ? 1 : v; |
|---|
| 232 |
_volumes[1] = v * 128; |
|---|
| 233 |
_updateStreamSend(1, v); |
|---|
| 234 |
} |
|---|
| 235 |
/** Channel effect send level for slot 2 (0:Minimum - 1:Maximum), this property can control track after play(). */ |
|---|
| 236 |
public function get effectSend2() : Number { return _volumes[2] * 0.0078125; } |
|---|
| 237 |
public function set effectSend2(v:Number) : void { |
|---|
| 238 |
v = (v<0) ? 0 : (v>1) ? 1 : v; |
|---|
| 239 |
_volumes[2] = v * 128; |
|---|
| 240 |
_updateStreamSend(2, v); |
|---|
| 241 |
} |
|---|
| 242 |
/** Channel effect send level for slot 3 (0:Minimum - 1:Maximum), this property can control track after play(). */ |
|---|
| 243 |
public function get effectSend3() : Number { return _volumes[3] * 0.0078125; } |
|---|
| 244 |
public function set effectSend3(v:Number) : void { |
|---|
| 245 |
v = (v<0) ? 0 : (v>1) ? 1 : v; |
|---|
| 246 |
_volumes[3] = v * 128; |
|---|
| 247 |
_updateStreamSend(3, v); |
|---|
| 248 |
} |
|---|
| 249 |
/** Channel effect send level for slot 4 (0:Minimum - 1:Maximum), this property can control track after play(). */ |
|---|
| 250 |
public function get effectSend4() : Number { return _volumes[4] * 0.0078125; } |
|---|
| 251 |
public function set effectSend4(v:Number) : void { |
|---|
| 252 |
v = (v<0) ? 0 : (v>1) ? 1 : v; |
|---|
| 253 |
_volumes[4] = v * 128; |
|---|
| 254 |
_updateStreamSend(4, v); |
|---|
| 255 |
} |
|---|
| 256 |
/** Channel pitch bend, 1 for halftone, this property can control track after play(). */ |
|---|
| 257 |
public function get pitchBend() : Number { return _pitchBend; } |
|---|
| 258 |
public function set pitchBend(p:Number) : void { |
|---|
| 259 |
_pitchBend = p; |
|---|
| 260 |
if (_track) _track.pitchBend = p * 64; |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
/** Array of SiEffectBase to modify this sound object's output. */ |
|---|
| 265 |
public function get effectors() : Array { |
|---|
| 266 |
return (_effectChain) ? _effectChain.effectList : null; |
|---|
| 267 |
} |
|---|
| 268 |
public function set effectors(effectList:Array) :void { |
|---|
| 269 |
if (_effectChain) { |
|---|
| 270 |
_effectChain.effectList = effectList; |
|---|
| 271 |
} else { |
|---|
| 272 |
if (effectList && effectList.length>0) { |
|---|
| 273 |
_effectChain = EffectChain.alloc(effectList); |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
// counter to asign unique track id |
|---|
| 280 |
static private var _uniqueTrackID:int = 0; |
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
// constructor |
|---|
| 286 |
//---------------------------------------- |
|---|
| 287 |
/** constructor. */ |
|---|
| 288 |
function SoundObject(name:String = null, synth:VoiceReference = null) |
|---|
| 289 |
{ |
|---|
| 290 |
this.name = name || ""; |
|---|
| 291 |
_parent = null; |
|---|
| 292 |
_childDepth = 0; |
|---|
| 293 |
_voiceReference = new VoiceReference(); |
|---|
| 294 |
_synthesizer = synth || _voiceReference; |
|---|
| 295 |
_effectChain = null; |
|---|
| 296 |
_track = null; |
|---|
| 297 |
_tracks = null; |
|---|
| 298 |
_fader = new Fader(null, 1); |
|---|
| 299 |
_volumes = new Vector.<int>(SiOPMModule.STREAM_SEND_SIZE); |
|---|
| 300 |
_faderVolume = 1; |
|---|
| 301 |
|
|---|
| 302 |
_note = 60; |
|---|
| 303 |
_length = 0; |
|---|
| 304 |
_delay = 0; |
|---|
| 305 |
_quantize = 1; |
|---|
| 306 |
|
|---|
| 307 |
_volumes[0] = 64; |
|---|
| 308 |
for (var i:int=1; i<SiOPMModule.STREAM_SEND_SIZE; i++) _volumes[i] = 0; |
|---|
| 309 |
_pan = 0; |
|---|
| 310 |
_mute = false; |
|---|
| 311 |
_pitchBend = 0; |
|---|
| 312 |
|
|---|
| 313 |
_gateTime = 0.75; |
|---|
| 314 |
_noteShift = 0; |
|---|
| 315 |
_pitchShift = 0; |
|---|
| 316 |
_eventMask = 0; |
|---|
| 317 |
_eventTriggerID = 0; |
|---|
| 318 |
_noteTriggerFlags = 0; |
|---|
| 319 |
_listeningFlags = 0; |
|---|
| 320 |
|
|---|
| 321 |
_thisVolume = 0.5; |
|---|
| 322 |
_thisPan = 0; |
|---|
| 323 |
_thisMute = false; |
|---|
| 324 |
|
|---|
| 325 |
_trackID = (_uniqueTrackID & 0x7fff) | 0x8000; |
|---|
| 326 |
_uniqueTrackID++; |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
// settings |
|---|
| 333 |
//---------------------------------------- |
|---|
| 334 |
/** Reset */ |
|---|
| 335 |
public function reset() : void |
|---|
| 336 |
{ |
|---|
| 337 |
stop(); |
|---|
| 338 |
|
|---|
| 339 |
_note = 60; |
|---|
| 340 |
_length = 0; |
|---|
| 341 |
_delay = 0; |
|---|
| 342 |
_quantize = 1; |
|---|
| 343 |
|
|---|
| 344 |
_fader.setFade(null, 1); |
|---|
| 345 |
_effectChain = null; |
|---|
| 346 |
_volumes[0] = 64; |
|---|
| 347 |
for (var i:int=1; i<SiOPMModule.STREAM_SEND_SIZE; i++) _volumes[i] = 0; |
|---|
| 348 |
_faderVolume = 1; |
|---|
| 349 |
_pan = 0; |
|---|
| 350 |
_mute = false; |
|---|
| 351 |
_pitchBend = 0; |
|---|
| 352 |
|
|---|
| 353 |
_gateTime = 0.75; |
|---|
| 354 |
_noteShift = 0; |
|---|
| 355 |
_pitchShift = 0; |
|---|
| 356 |
_eventMask = 0; |
|---|
| 357 |
_eventTriggerID = 0; |
|---|
| 358 |
_noteTriggerFlags = 0; |
|---|
| 359 |
_listeningFlags = 0; |
|---|
| 360 |
|
|---|
| 361 |
_thisVolume = 0.5; |
|---|
| 362 |
_thisPan = 0; |
|---|
| 363 |
_thisMute = false; |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
/** Set volume by index. |
|---|
| 368 |
* @param slot streaming slot number. |
|---|
| 369 |
* @param volume volume (0:Minimum - 1:Maximum). |
|---|
| 370 |
*/ |
|---|
| 371 |
public function setVolume(slot:int, volume:Number) : void |
|---|
| 372 |
{ |
|---|
| 373 |
_volumes[slot] = (volume<0) ? 0 : (volume>1) ? 128 : (volume * 128); |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
/** Set fading in. |
|---|
| 378 |
* @param time fading time[sec]. |
|---|
| 379 |
*/ |
|---|
| 380 |
public function fadeIn(time:Number) : void |
|---|
| 381 |
{ |
|---|
| 382 |
var drv:SiONDriver = driver; |
|---|
| 383 |
if (drv) { |
|---|
| 384 |
if (!_fader.isActive) { |
|---|
| 385 |
drv.addEventListener(SiONEvent.STREAM, _onStream); |
|---|
| 386 |
drv._sion_internal::forceDispatchStreamEvent(); |
|---|
| 387 |
} |
|---|
| 388 |
_fader.setFade(_fadeVolume, 0, 1, time * drv.sampleRate / drv.bufferLength); |
|---|
| 389 |
} |
|---|
| 390 |
} |
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
/** Set fading out. |
|---|
| 394 |
* @param time fading time[sec]. |
|---|
| 395 |
*/ |
|---|
| 396 |
public function fadeOut(time:Number) : void |
|---|
| 397 |
{ |
|---|
| 398 |
var drv:SiONDriver = driver; |
|---|
| 399 |
if (drv) { |
|---|
| 400 |
if (!_fader.isActive) { |
|---|
| 401 |
drv.addEventListener(SiONEvent.STREAM, _onStream); |
|---|
| 402 |
drv._sion_internal::forceDispatchStreamEvent(); |
|---|
| 403 |
} |
|---|
| 404 |
_fader.setFade(_fadeVolume, 1, 0, time * drv.sampleRate / drv.bufferLength); |
|---|
| 405 |
} |
|---|
| 406 |
} |
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
// operations |
|---|
| 412 |
//---------------------------------------- |
|---|
| 413 |
/** Play sound. */ |
|---|
| 414 |
public function play() : void |
|---|
| 415 |
{ |
|---|
| 416 |
stop(); |
|---|
| 417 |
_track = _noteOn(_note, false); |
|---|
| 418 |
if (_track) _synthesizer._registerTrack(_track); |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
/** Stop sound. */ |
|---|
| 423 |
public function stop() : void |
|---|
| 424 |
{ |
|---|
| 425 |
if (_track) { |
|---|
| 426 |
_synthesizer._unregisterTracks(_track); |
|---|
| 427 |
_track.setDisposable(); |
|---|
| 428 |
_track = null; |
|---|
| 429 |
_noteOff(-1, false); |
|---|
| 430 |
} |
|---|
| 431 |
_stopEffect(); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
// operations |
|---|
| 438 |
//---------------------------------------- |
|---|
| 439 |
/** @private [protected] driver.noteOn. |
|---|
| 440 |
* @param note playing note |
|---|
| 441 |
* @param isDisposable disposable flag. |
|---|
| 442 |
* @return playing track |
|---|
| 443 |
*/ |
|---|
| 444 |
protected function _noteOn(note:int, isDisposable:Boolean) : SiMMLTrack |
|---|
| 445 |
{ |
|---|
| 446 |
if (!driver) return null; |
|---|
| 447 |
var voice:SiONVoice = _synthesizer._synthesizer_internal::_voice, |
|---|
| 448 |
topEC:EffectChain = _topEffectChain(), |
|---|
| 449 |
track:SiMMLTrack = driver.noteOn(note, voice, _length, _delay, _quantize, _trackID, isDisposable); |
|---|
| 450 |
_addNoteEventListeners(); |
|---|
| 451 |
if (_effectChain) { |
|---|
| 452 |
_effectChain._activateLocalEffect(_childDepth); |
|---|
| 453 |
_effectChain.setAllStreamSendLevels(_volumes); |
|---|
| 454 |
} |
|---|
| 455 |
if (topEC) { |
|---|
| 456 |
track.channel.masterVolume = 128; |
|---|
| 457 |
track.channel.setStreamBuffer(0, topEC.streamingBuffer); |
|---|
| 458 |
} else { |
|---|
| 459 |
track.channel.setAllStreamSendLevels(_volumes); |
|---|
| 460 |
} |
|---|
| 461 |
track.channel.pan = _pan*64; |
|---|
| 462 |
track.channel.mute = _mute; |
|---|
| 463 |
track.pitchBend = _pitchBend * 64; |
|---|
| 464 |
track.noteShift = _noteShift; |
|---|
| 465 |
track.pitchShift = _pitchShift * 64; |
|---|
| 466 |
if (voice && isNaN(voice.defaultGateTime)) track.quantRatio = _gateTime; |
|---|
| 467 |
return track; |
|---|
| 468 |
} |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
/** @private [protected] driver.noteOff() |
|---|
| 472 |
* @param stopWithReset stop sound wit resetting channels process |
|---|
| 473 |
* @return stopped track list |
|---|
| 474 |
*/ |
|---|
| 475 |
protected function _noteOff(note:int, stopWithReset:Boolean = true) : Vector.<SiMMLTrack> |
|---|
| 476 |
{ |
|---|
| 477 |
if (!driver) return null; |
|---|
| 478 |
_removeNoteEventListeners(); |
|---|
| 479 |
//if (_effectChain) _effectChain._inactivateLocalEffect(); |
|---|
| 480 |
return driver.noteOff(note, _trackID, _delay, _quantize, stopWithReset); |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
/** @private [protected] driver.sequenceOn() |
|---|
| 485 |
* @param data sequence data |
|---|
| 486 |
* @param isDisposable disposable flag |
|---|
| 487 |
* @param applyLength |
|---|
| 488 |
* @return vector of playing tracks |
|---|
| 489 |
*/ |
|---|
| 490 |
protected function _sequenceOn(data:SiONData, isDisposable:Boolean, applyLength:Boolean=true) : Vector.<SiMMLTrack> |
|---|
| 491 |
{ |
|---|
| 492 |
if (!driver) return null; |
|---|
| 493 |
var len:Number = (applyLength) ? _length : 0; |
|---|
| 494 |
var voice:SiONVoice = _synthesizer._synthesizer_internal::_voice, |
|---|
| 495 |
topEC:EffectChain = _topEffectChain(), |
|---|
| 496 |
list:Vector.<SiMMLTrack> = driver.sequenceOn(data, voice, len, _delay, _quantize, _trackID, isDisposable), |
|---|
| 497 |
track:SiMMLTrack, ps:int = _pitchShift * 64, pb:int = _pitchBend * 64; |
|---|
| 498 |
_addNoteEventListeners(); |
|---|
| 499 |
if (_effectChain) { |
|---|
| 500 |
_effectChain._activateLocalEffect(_childDepth); |
|---|
| 501 |
_effectChain.setAllStreamSendLevels(_volumes); |
|---|
| 502 |
} |
|---|
| 503 |
for each (track in list) { |
|---|
| 504 |
if (topEC) { |
|---|
| 505 |
track.channel.masterVolume = 128; |
|---|
| 506 |
track.channel.setStreamBuffer(0, topEC.streamingBuffer); |
|---|
| 507 |
} else { |
|---|
| 508 |
track.channel.setAllStreamSendLevels(_volumes); |
|---|
| 509 |
} |
|---|
| 510 |
track.channel.pan = _pan*64; |
|---|
| 511 |
track.channel.mute = _mute; |
|---|
| 512 |
track.pitchBend = pb; |
|---|
| 513 |
track.noteShift = _noteShift; |
|---|
| 514 |
track.pitchShift = ps; |
|---|
| 515 |
track.setEventTrigger(_eventTriggerID, _noteTriggerFlags&3, _noteTriggerFlags>>2); |
|---|
| 516 |
if (voice && isNaN(voice.defaultGateTime)) track.quantRatio = _gateTime; |
|---|
| 517 |
} |
|---|
| 518 |
return list; |
|---|
| 519 |
} |
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
/** @private [protected] driver.sequenceOff() |
|---|
| 523 |
* @param stopWithReset stop sound wit resetting channels process |
|---|
| 524 |
* @return stopped track list |
|---|
| 525 |
*/ |
|---|
| 526 |
protected function _sequenceOff(stopWithReset:Boolean = true) : Vector.<SiMMLTrack> |
|---|
| 527 |
{ |
|---|
| 528 |
if (!driver) return null; |
|---|
| 529 |
_removeNoteEventListeners(); |
|---|
| 530 |
//if (_effectChain) _effectChain._inactivateLocalEffect(); |
|---|
| 531 |
return driver.sequenceOff(_trackID, 0, _quantize, stopWithReset); |
|---|
| 532 |
} |
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
/** @private [protected] free effect chain if the effect list is empty */ |
|---|
| 536 |
protected function _stopEffect() : void |
|---|
| 537 |
{ |
|---|
| 538 |
if (_effectChain && _effectChain.effectList.length == 0) { |
|---|
| 539 |
_effectChain.free(); |
|---|
| 540 |
_effectChain = null; |
|---|
| 541 |
} |
|---|
| 542 |
} |
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
/** @private [protected] update stream send level */ |
|---|
| 546 |
protected function _updateStreamSend(streamNum:int, level:Number) : void { |
|---|
| 547 |
if (_track) { |
|---|
| 548 |
if (_effectChain) _effectChain.setStreamSend(streamNum, level); |
|---|
| 549 |
else _track.channel.setStreamSend(streamNum, level); |
|---|
| 550 |
} |
|---|
| 551 |
} |
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
/** @private [protected] add event trigger listeners */ |
|---|
| 555 |
protected function _addNoteEventListeners(): void { |
|---|
| 556 |
if (_listeningFlags != 0) return; |
|---|
| 557 |
var drv:SiONDriver = driver; |
|---|
| 558 |
_noteTriggerFlags = 0; |
|---|
| 559 |
if (hasEventListener(SoundObjectEvent.NOTE_ON_FRAME)) { |
|---|
| 560 |
drv.addEventListener(SiONTrackEvent.NOTE_ON_FRAME, _onTrackEvent); |
|---|
| 561 |
_noteTriggerFlags |= 1; |
|---|
| 562 |
} |
|---|
| 563 |
if (hasEventListener(SoundObjectEvent.NOTE_ON_STREAM)) { |
|---|
| 564 |
drv.addEventListener(SiONTrackEvent.NOTE_ON_STREAM, _onTrackEvent); |
|---|
| 565 |
_noteTriggerFlags |= 2; |
|---|
| 566 |
} |
|---|
| 567 |
if (hasEventListener(SoundObjectEvent.NOTE_OFF_FRAME)) { |
|---|
| 568 |
drv.addEventListener(SiONTrackEvent.NOTE_OFF_FRAME, _onTrackEvent); |
|---|
| 569 |
_noteTriggerFlags |= 4; |
|---|
| 570 |
} |
|---|
| 571 |
if (hasEventListener(SoundObjectEvent.NOTE_OFF_STREAM)) { |
|---|
| 572 |
drv.addEventListener(SiONTrackEvent.NOTE_OFF_STREAM, _onTrackEvent); |
|---|
| 573 |
_noteTriggerFlags |= 8; |
|---|
| 574 |
} |
|---|
| 575 |
_listeningFlags = _noteTriggerFlags; |
|---|
| 576 |
} |
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
/** @private [protected] remove event trigger listeners */ |
|---|
| 580 |
protected function _removeNoteEventListeners() : void { |
|---|
| 581 |
if (_listeningFlags == 0) return; |
|---|
| 582 |
var drv:SiONDriver = driver; |
|---|
| 583 |
if ((_listeningFlags & 1) != 0) drv.removeEventListener(SiONTrackEvent.NOTE_ON_FRAME, _onTrackEvent); |
|---|
| 584 |
if ((_listeningFlags & 2) != 0) drv.removeEventListener(SiONTrackEvent.NOTE_ON_STREAM, _onTrackEvent); |
|---|
| 585 |
if ((_listeningFlags & 4) != 0) drv.removeEventListener(SiONTrackEvent.NOTE_OFF_FRAME, _onTrackEvent); |
|---|
| 586 |
if ((_listeningFlags & 8) != 0) drv.removeEventListener(SiONTrackEvent.NOTE_OFF_STREAM, _onTrackEvent); |
|---|
| 587 |
_listeningFlags = 0; |
|---|
| 588 |
} |
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
/** @private [protected] handler for note event */ |
|---|
| 592 |
protected function _onTrackEvent(e:SiONTrackEvent) : void { |
|---|
| 593 |
if (e.track.trackID == _trackID) { |
|---|
| 594 |
dispatchEvent(new SoundObjectEvent(e.type, this, e)); |
|---|
| 595 |
} |
|---|
| 596 |
} |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
// internals |
|---|
| 601 |
//---------------------------------------- |
|---|
| 602 |
// top effect chain |
|---|
| 603 |
private function _topEffectChain() : EffectChain |
|---|
| 604 |
{ |
|---|
| 605 |
return _effectChain || ((_parent) ? _parent._topEffectChain() : null); |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
/** @private [internal use] */ |
|---|
| 610 |
internal function _setParent(parent:SoundObjectContainer) : void |
|---|
| 611 |
{ |
|---|
| 612 |
if (_parent != null) _parent.removeChild(this); |
|---|
| 613 |
_parent = parent; |
|---|
| 614 |
_updateChildDepth(); |
|---|
| 615 |
_updateMute(); |
|---|
| 616 |
_updateVolume(); |
|---|
| 617 |
_limitVolume(); |
|---|
| 618 |
_updatePan(); |
|---|
| 619 |
_limitPan(); |
|---|
| 620 |
} |
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
/** @private [internal use] */ |
|---|
| 624 |
internal function _updateChildDepth() : void |
|---|
| 625 |
{ |
|---|
| 626 |
_childDepth = (parent) ? (parent._childDepth + 1) : 0; |
|---|
| 627 |
} |
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
/** @private [internal use] */ |
|---|
| 631 |
internal function _updateMute() : void |
|---|
| 632 |
{ |
|---|
| 633 |
if (_parent) _mute = _parent._mute || _thisMute; |
|---|
| 634 |
else _mute = _thisMute; |
|---|
| 635 |
} |
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
/** @private [internal use] */ |
|---|
| 639 |
internal function _updateVolume() : void |
|---|
| 640 |
{ |
|---|
| 641 |
if (_parent) _volumes[0] = _parent._volumes[0] * _thisVolume * _faderVolume; |
|---|
| 642 |
else _volumes[0] = _thisVolume * _faderVolume * 128; |
|---|
| 643 |
} |
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
/** @private [internal use] */ |
|---|
| 647 |
internal function _limitVolume() : void |
|---|
| 648 |
{ |
|---|
| 649 |
if (_volumes[0] < 0) _volumes[0] = 0; |
|---|
| 650 |
else if (_volumes[0] > 128) _volumes[0] = 128; |
|---|
| 651 |
} |
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
/** @private [internal use] */ |
|---|
| 655 |
internal function _updatePan() : void |
|---|
| 656 |
{ |
|---|
| 657 |
if (_parent) _pan = (_parent._pan + _thisPan) * 0.5; |
|---|
| 658 |
else _pan = _thisPan; |
|---|
| 659 |
} |
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
/** @private [internal use] */ |
|---|
| 663 |
internal function _limitPan() : void |
|---|
| 664 |
{ |
|---|
| 665 |
if (_pan < -1) _pan = -1; |
|---|
| 666 |
else if (_pan > 1) _pan = 1; |
|---|
| 667 |
} |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
/** @private [protected] Handler for SiONEvent.STREAM */ |
|---|
| 671 |
protected function _onStream(e:SiONEvent) : void |
|---|
| 672 |
{ |
|---|
| 673 |
if (_fader.execute()) { |
|---|
| 674 |
driver.removeEventListener(SiONEvent.STREAM, _onStream); |
|---|
| 675 |
driver._sion_internal::forceDispatchStreamEvent(false); |
|---|
| 676 |
} |
|---|
| 677 |
} |
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
/** @private [protected] call from fader */ |
|---|
| 681 |
protected function _fadeVolume(v:Number) : void |
|---|
| 682 |
{ |
|---|
| 683 |
_faderVolume = v; |
|---|
| 684 |
_updateVolume(); |
|---|
| 685 |
_updateStreamSend(0, _volumes[0] * 0.0078125); |
|---|
| 686 |
} |
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
/** @private [protected] on enter frame */ |
|---|
| 690 |
protected function _onEnterFrame(seq:Sequencer) : void |
|---|
| 691 |
{ |
|---|
| 692 |
if (hasEventListener("soundObjectEnterFrame")) { |
|---|
| 693 |
var event:SoundObjectEvent = new SoundObjectEvent("soundObjectEnterFrame", this, null); |
|---|
| 694 |
event._note = note; |
|---|
| 695 |
event._eventTriggerID = _eventTriggerID; |
|---|
| 696 |
dispatchEvent(event); |
|---|
| 697 |
} |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
/** @private [protected] on enter segment */ |
|---|
| 702 |
protected function _onEnterSegment(seq:Sequencer) : void |
|---|
| 703 |
{ |
|---|
| 704 |
if (hasEventListener("soundObjectEnterSegment")) { |
|---|
| 705 |
var event:SoundObjectEvent = new SoundObjectEvent("soundObjectEnterSegment", this, null); |
|---|
| 706 |
event._eventTriggerID = _eventTriggerID; |
|---|
| 707 |
dispatchEvent(event); |
|---|
| 708 |
} |
|---|
| 709 |
} |
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
// errors |
|---|
| 715 |
//---------------------------------------- |
|---|
| 716 |
/** @private [protected] not available */ |
|---|
| 717 |
protected function _errorNotAvailable(str:String) : Error |
|---|
| 718 |
{ |
|---|
| 719 |
return new Error("SoundObject; " + str + " method is not available in this object."); |
|---|
| 720 |
} |
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
/** @private [protected] Cannot change */ |
|---|
| 724 |
protected function _errorCannotChange(str:String) : Error |
|---|
| 725 |
{ |
|---|
| 726 |
return new Error("SoundObject; You can not change " + str + " property in this object."); |
|---|
| 727 |
} |
|---|
| 728 |
} |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
|
|---|