AudioListener.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Vector3 } from '../math/Vector3.js';
  5. import { Quaternion } from '../math/Quaternion.js';
  6. import { Clock } from '../core/Clock.js';
  7. import { Object3D } from '../core/Object3D.js';
  8. import { AudioContext } from './AudioContext.js';
  9. function AudioListener() {
  10. Object3D.call( this );
  11. this.type = 'AudioListener';
  12. this.context = AudioContext.getContext();
  13. this.gain = this.context.createGain();
  14. this.gain.connect( this.context.destination );
  15. this.filter = null;
  16. }
  17. AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
  18. constructor: AudioListener,
  19. getInput: function () {
  20. return this.gain;
  21. },
  22. removeFilter: function ( ) {
  23. if ( this.filter !== null ) {
  24. this.gain.disconnect( this.filter );
  25. this.filter.disconnect( this.context.destination );
  26. this.gain.connect( this.context.destination );
  27. this.filter = null;
  28. }
  29. return this;
  30. },
  31. getFilter: function () {
  32. return this.filter;
  33. },
  34. setFilter: function ( value ) {
  35. if ( this.filter !== null ) {
  36. this.gain.disconnect( this.filter );
  37. this.filter.disconnect( this.context.destination );
  38. } else {
  39. this.gain.disconnect( this.context.destination );
  40. }
  41. this.filter = value;
  42. this.gain.connect( this.filter );
  43. this.filter.connect( this.context.destination );
  44. return this;
  45. },
  46. getMasterVolume: function () {
  47. return this.gain.gain.value;
  48. },
  49. setMasterVolume: function ( value ) {
  50. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  51. return this;
  52. },
  53. updateMatrixWorld: ( function () {
  54. var position = new Vector3();
  55. var quaternion = new Quaternion();
  56. var scale = new Vector3();
  57. var orientation = new Vector3();
  58. var clock = new Clock();
  59. return function updateMatrixWorld( force ) {
  60. Object3D.prototype.updateMatrixWorld.call( this, force );
  61. var listener = this.context.listener;
  62. var up = this.up;
  63. this.matrixWorld.decompose( position, quaternion, scale );
  64. orientation.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  65. if ( listener.positionX ) {
  66. var delta = clock.getDelta();
  67. var endTime = this.context.currentTime + delta;
  68. listener.positionX.linearRampToValueAtTime( position.x, endTime );
  69. listener.positionY.linearRampToValueAtTime( position.y, endTime );
  70. listener.positionZ.linearRampToValueAtTime( position.z, endTime );
  71. listener.forwardX.linearRampToValueAtTime( orientation.x, endTime );
  72. listener.forwardY.linearRampToValueAtTime( orientation.y, endTime );
  73. listener.forwardZ.linearRampToValueAtTime( orientation.z, endTime );
  74. listener.upX.linearRampToValueAtTime( up.x, endTime );
  75. listener.upY.linearRampToValueAtTime( up.y, endTime );
  76. listener.upZ.linearRampToValueAtTime( up.z, endTime );
  77. } else {
  78. listener.setPosition( position.x, position.y, position.z );
  79. listener.setOrientation( orientation.x, orientation.y, orientation.z, up.x, up.y, up.z );
  80. }
  81. };
  82. } )()
  83. } );
  84. export { AudioListener };