class SoundObject extends Sound {
// Variable declaration
private var intVolumeMax:Number;
private var intVolumeMin:Number;
private var intFadeTime:Number;
private var intFadeID:Number;
private var intQueueID:Number;
private var intQueueTrack:Number;
private var intID:Number;
private var bolFX:Boolean;
private var bolMute:Boolean;
private var bolFadeOngoing:Boolean;
private var arrQueue:Array;
public function onFadeComplete():Void { }
// FUNCTION: SoundObject
// constructor method -> setting default Values
function SoundObject() {
this.bolFX = true;
this.arrQueue = new Array();
this.intQueueTrack = 1;
this.intVolumeMax = 100;
this.intVolumeMin = 0;
this.intFadeTime = 3000;
this.bolFadeOngoing = false;
this.bolMute = false;
}
// FUNCTION: regID
// register SoundID in SoundObject for Access over Soundmanager
function regID(ID) {
this.intID = ID;
}
// FUNCTION: getID
// get SoundID for Access over Soundmanager
function getID() {
return this.intID;
}
// FUNCTION: setFX
// turn soundFX on or off
function setFX(FX) {
this.bolFX = FX;
}
// FUNCTION: getFX
// get soundFX status
function getFX() {
return this.bolFX;
}
// FUNCTION: observeQueue
// observe playing sound and fadeout before track ends
function observeQueue() {
if (this.arrQueue.length > 1) {
if (this.position > this.duration - this.intFadeTime - 500 && this.position != this.duration) {
clearInterval(this.intQueueID);
this.fade(false);
}
} else {
clearInterval(this.intQueueID);
}
}
// FUNCTION: setVolumeMax
// set the max Volume (default 100)
function setVolumeMax(intVolume) {
this.intVolumeMax = intVolume;
}
// FUNCTION: setVolumeMin
// set the min volume (default 0)
function setVolumeMin(intVolume) {
this.intVolumeMin = intVolume;
}
// FUNCTION: getVolumeMax()
// returns intVolumeMax
function getVolumeMax() {
return this.intVolumeMax;
}
// FUNCTION: getVolumeMin()
// returns intVolumeMin
function getVolumeMin() {
return this.intVolumeMin;
}
// FUNCTION: newSound
// adds a new sound or soundqueue (depending
// on input tpye) to the soundobject
// WARNING: overwrites old sounds in same
// object!
function newSound(objFiles) {
this.arrQueue = new Array();
this.intQueueTrack = 1;
if (objFiles instanceof Array) {
this.arrQueue = objFiles;
this.onSoundComplete = function() {
this.nextSound();
}
} else {
this.arrQueue[0] = objFiles;
this.onSoundComplete = function() { }
}
}
// FUNCTION: prevSound
// toggle previous soundtrack in queue
function prevSound() {
if (0 < this.intQueueTrack) {
this.intQueueTrack--;
} else {
this.intQueueTrack = this.arrQueue.length;
}
this.play();
if (this.bolFX == true) {
this.setVolume(0);
this.fade(true);
}
}
// FUNCTION: nextSound
// toggle next soundtrack in queue
function nextSound() {
if (this.arrQueue.length > this.intQueueTrack) {
this.intQueueTrack++;
} else {
this.intQueueTrack = 1;
}
this.play();
if (this.bolFX == true) {
this.setVolume(0);
this.fade(true);
}
}
// FUNCTION: play
// extended sound playing function
function play(intOffset:Number, intRepeat:Number) {
if (intOffset == null) { intOffset = 0; }
if (intRepeat == null) { intRepeat = 1; }
this.start(0);
this.stop();
clearInterval(this.intQueueID);
this.intQueueID = setInterval(this, "observeQueue", 500);
this.loadSound(this.arrQueue[intQueueTrack-1], true);
this.start(intOffset, intRepeat);
}
// FUNCTION: mute
// mute soundobject
function mute(bolMute:Boolean) {
if (bolMute == null) {
if (this.bolMute == true && bolFadeOngoing != true) {
this.bolMute = false;
this.setVolume(this.intVolumeMax);
} else {
this.bolMute = true;
this.setVolume(0);
}
} else {
this.bolMute = bolMute;
if (this.bolFadeOngoing != true) {
if (bolMute == true) {
this.setVolume(0);
} else {
this.setVolume(this.intVolumeMax);
}
}
}
}
// FUNCTION: fadeIn
// fade in sound dynamically from zero
function fadeIn() {
this.setVolume(0);
this.fade(true);
}
// FUNCTION: fadeOut
// fade out sound dynamically from intVolumeMax
function fadeOut() {
this.setVolume(this.intVolumeMax);
this.fade(false);
}
// FUNCTION: setFadeTime
// set fading time
function setFadeTime(intFadeTime:Number) {
this.intFadeTime = intFadeTime;
}
// FUNCTION: getFadeTime
// get fading time back
function getFadeTime() {
return this.intFadeTime;
}
// FUNCTION: fade
// initialise fading process
function fade(bolMode:Boolean, bolMute:Boolean, intTime:Number) {
if (this.bolMute != true || bolMute == false) {
this.bolFadeOngoing = true;
if (bolMute != null) {
this.mute(bolMute);
}
if (intTime == null) {
intTime = this.intFadeTime;
}
var intSteps = Math.round(intTime / 50);
clearInterval(this.intFadeID);
this.intFadeID = setInterval(this, "aniFade", intSteps, bolMode);
}
}
// FUNCTION: aniFade
// animates fading process by drawing seperate steps to output channel
function aniFade(bolMode) {
if (this == null) {
clearInterval(this.intFadeID);
} else if (bolMode == true && this.bolMute != true) {
this.setVolume(this.getVolume() + 2);
if (this.getVolume() >= this.intVolumeMax) {
this.setVolume(this.intVolumeMax);
clearInterval(this.intFadeID);
this.bolFadeOngoing = false;
this.onFadeComplete();
}
} else {
this.setVolume(this.getVolume() - 2);
if (this.getVolume() <= this.intVolumeMin) {
this.setVolume(this.intVolumeMin);
clearInterval(this.intFadeID);
this.bolFadeOngoing = false;
this.onFadeComplete();
}
}
}
}