AudioListener.js 2.8 KB

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