PageRenderTime 342ms CodeModel.GetById 85ms app.highlight 5ms RepoModel.GetById 118ms app.codeStats 1ms

/src/away3d/audio/drivers/SimplePanVolumeDriver.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 98 lines | 68 code | 19 blank | 11 comment | 3 complexity | bef926dc93d0a54e165ad0d1f5d17d93 MD5 | raw file
 1package away3d.audio.drivers
 2{
 3	import away3d.audio.SoundTransform3D;
 4	
 5	import flash.events.Event;
 6	import flash.geom.*;
 7	import flash.media.*;
 8	
 9	/**
10	 * The Simple pan/volume Sound3D driver will alter the pan and volume properties on the
11	 * sound transform object of a regular flash.media.Sound3D representation of the sound. This
12	 * is very efficient, but has the drawback that it can only reflect azimuth and distance,
13	 * and will disregard elevation. You'll be able to hear whether a
14	 */
15	public class SimplePanVolumeDriver extends AbstractSound3DDriver implements ISound3DDriver
16	{
17		private var _sound_chan:SoundChannel;
18		private var _pause_position:Number;
19		private var _st3D:SoundTransform3D;
20		
21		public function SimplePanVolumeDriver()
22		{
23			super();
24			
25			_ref_v = new Vector3D();
26			_st3D = new SoundTransform3D();
27		}
28		
29		public function play():void
30		{
31			var pos:Number;
32			
33			if (!_src)
34				throw new Error('SimplePanVolumeDriver.play(): No sound source to play.');
35			
36			_playing = true;
37			
38			// Update sound transform first. This has not happened while
39			// the sound was not playing, so needs to be done now.
40			_updateSoundTransform();
41			
42			// Start playing. If paused, resume from pause position. Else,
43			// start from beginning of file.
44			pos = _paused? _pause_position : 0;
45			_sound_chan = _src.play(pos, 0, _st3D.soundTransform);
46			_sound_chan.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
47		}
48		
49		public function pause():void
50		{
51			_paused = true;
52			_pause_position = _sound_chan.position;
53			_sound_chan.stop();
54			_sound_chan.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
55		}
56		
57		public function stop():void
58		{
59			_sound_chan.stop();
60			_sound_chan.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
61		}
62		
63		public override function set volume(val:Number):void
64		{
65			_volume = val;
66			_st3D.volume = val;
67		}
68		
69		public override function set scale(val:Number):void
70		{
71			_scale = val;
72			_st3D.scale = scale;
73		}
74		
75		public override function updateReferenceVector(v:Vector3D):void
76		{
77			super.updateReferenceVector(v);
78			
79			// Only update sound transform while playing
80			if (_playing)
81				_updateSoundTransform();
82		}
83		
84		private function _updateSoundTransform():void
85		{
86			
87			_st3D.updateFromVector3D(_ref_v);
88			
89			if (_sound_chan)
90				_sound_chan.soundTransform = _st3D.soundTransform;
91		}
92		
93		private function onSoundComplete(ev:Event):void
94		{
95			this.dispatchEvent(ev.clone());
96		}
97	}
98}