/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

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