1  /**
     2   * com.sekati.data.SoundCenter
     3   * @version 1.1.0
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   * 
     8   * extended from on ca.nectere.dllFactory.SoundFactory
     9   */
    10  
    11  import com.sekati.core.CoreObject;
    12  import com.sekati.core.FWDepth;
    13  import com.sekati.math.MathBase;
    14  import com.sekati.utils.ClassUtils;
    15  /**
    16   * Centralized Sound management solves issues calling linked sounds from dll's
    17   */
    18  class com.sekati.data.SoundCenter extends CoreObject {
    19  	private static var _instance:SoundCenter;
    20  	private var _holder:MovieClip;
    21  	private var _gSound:Sound;
    22  	private var _gMute:Boolean;
    23  	private var _gVolume:Number;
    24  	private var _gPan:Number;
    25  	/**
    26  	 * Singleton Private Constructor
    27  	 */
    28  	private function SoundCenter() {
    29  		super( );
    30  		_holder = ClassUtils.createEmptyMovieClip( com.sekati.display.BaseClip, _root, "__SoundCenter__", {_depth: FWDepth.SoundCenter} );
    31  		//no param means it'll affect the whole application
    32  		_gSound = new Sound( );
    33  		_gMute = false;
    34  	}
    35  	/**
    36  	 * Singleton Accessor
    37  	 * @return SoundManager
    38  	 */
    39  	public static function getInstance():SoundCenter {
    40  		if (!_instance) _instance = new SoundCenter( );
    41  		return _instance;
    42  	}
    43  	/**
    44  	 * shorthand singleton accessor getter
    45  	 */
    46  	public static function get $():SoundCenter {
    47  		return SoundCenter.getInstance( );	
    48  	}	
    49  	/**
    50  	 * returns the holder clip so we can load the DLL swf in
    51  	 * @return MovieClip
    52  	 */
    53  	public function getHolder():MovieClip {
    54  		return _holder;
    55  	}
    56  	/**
    57  	 * returns the global volume setting
    58  	 * @return Number
    59  	 */
    60  	public function getVolume():Number {
    61  		return _gVolume;
    62  	}
    63  	/**
    64  	 * sets the global volume
    65  	 * @param n (Number) 0 to 100
    66  	 * @return Void
    67  	 */
    68  	public function setVolume(n:Number):Void {
    69  		_gVolume = MathBase.constrain( n, 0, 100 );
    70  		_gSound.setVolume( _gVolume );
    71  	}
    72  	/**
    73  	 * returns the global pan settings
    74  	 * @return Number
    75  	 */
    76  	public function getPan():Number {
    77  		return _gPan;
    78  	}
    79  	/**
    80  	 * sets the global pan
    81  	 * @param n (Number) -100 to 100
    82  	 */
    83  	public function setPan(n:Number):Void {
    84  		_gPan = MathBase.constrain( n, -100, 100 );
    85  		_gSound.setPan( _gPan );
    86  	}
    87  	/**
    88  	 * play a sound
    89  	 * @param linkage (String) linkage id for sound
    90  	 * @param v (Number) optional sound-specific volume (else global volume from setVolume is used)
    91  	 * @param p (Number) optional sound-specific pan (else global pan from setPan is used)
    92  	 * @param loops (Number) number of times to play sound
    93  	 * @param msOffset (Number) miliseconds offset
    94  	 * @return Void
    95  	 */
    96  	public function play(linkage:String, v:Number, p:Number, loops:Number, msOffset:Number):Void {
    97  		if (loops == null) {
    98  			loops = 0;
    99  		}
   100  		if (msOffset == null) {
   101  			msOffset = 0;
   102  		}
   103  		var s:Sound = new Sound( _holder );
   104  		s.attachSound( linkage );
   105  		if (v) {
   106  			s.setVolume( v );
   107  		}
   108  		if (p) {
   109  			s.setPan( p );
   110  		}
   111  		s.start( msOffset / 1000, loops );
   112  	}
   113  	/**
   114  	 * toggle mute global application audio
   115  	 * @param isMute (Boolean) optional force state
   116  	 * @return Void
   117  	 */
   118  	public function mute(isMute:Boolean):Void {
   119  		if (!_gMute || isMute) {
   120  			_gSound.setVolume( 0 );
   121  			_gMute = true;
   122  		} else if (_gMute || !isMute) {
   123  			_gSound.setVolume( _gVolume );
   124  			_gMute = false;
   125  		}
   126  	}
   127  	/**
   128  	 * Destroy singleton instance.
   129  	 * @return Void
   130  	 */
   131  	public function destroy():Void {
   132  		_holder.destroy( );
   133  		delete _instance;
   134  		super.destroy( );
   135  	}
   136  }