1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Vector3 } from '../math/Vector3.js';
|
6 | import { Quaternion } from '../math/Quaternion.js';
|
7 | import { Object3D } from '../core/Object3D.js';
|
8 | import { AudioContext } from './AudioContext.js';
|
9 |
|
10 | function AudioListener() {
|
11 |
|
12 | Object3D.call( this );
|
13 |
|
14 | this.type = 'AudioListener';
|
15 |
|
16 | this.context = AudioContext.getContext();
|
17 |
|
18 | this.gain = this.context.createGain();
|
19 | this.gain.connect( this.context.destination );
|
20 |
|
21 | this.filter = null;
|
22 |
|
23 | }
|
24 |
|
25 | AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
|
26 |
|
27 | constructor: AudioListener,
|
28 |
|
29 | getInput: function () {
|
30 |
|
31 | return this.gain;
|
32 |
|
33 | },
|
34 |
|
35 | removeFilter: function ( ) {
|
36 |
|
37 | if ( this.filter !== null ) {
|
38 |
|
39 | this.gain.disconnect( this.filter );
|
40 | this.filter.disconnect( this.context.destination );
|
41 | this.gain.connect( this.context.destination );
|
42 | this.filter = null;
|
43 |
|
44 | }
|
45 |
|
46 | },
|
47 |
|
48 | getFilter: function () {
|
49 |
|
50 | return this.filter;
|
51 |
|
52 | },
|
53 |
|
54 | setFilter: function ( value ) {
|
55 |
|
56 | if ( this.filter !== null ) {
|
57 |
|
58 | this.gain.disconnect( this.filter );
|
59 | this.filter.disconnect( this.context.destination );
|
60 |
|
61 | } else {
|
62 |
|
63 | this.gain.disconnect( this.context.destination );
|
64 |
|
65 | }
|
66 |
|
67 | this.filter = value;
|
68 | this.gain.connect( this.filter );
|
69 | this.filter.connect( this.context.destination );
|
70 |
|
71 | },
|
72 |
|
73 | getMasterVolume: function () {
|
74 |
|
75 | return this.gain.gain.value;
|
76 |
|
77 | },
|
78 |
|
79 | setMasterVolume: function ( value ) {
|
80 |
|
81 | this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
|
82 |
|
83 | },
|
84 |
|
85 | updateMatrixWorld: ( function () {
|
86 |
|
87 | var position = new Vector3();
|
88 | var quaternion = new Quaternion();
|
89 | var scale = new Vector3();
|
90 |
|
91 | var orientation = new Vector3();
|
92 |
|
93 | return function updateMatrixWorld( force ) {
|
94 |
|
95 | Object3D.prototype.updateMatrixWorld.call( this, force );
|
96 |
|
97 | var listener = this.context.listener;
|
98 | var up = this.up;
|
99 |
|
100 | this.matrixWorld.decompose( position, quaternion, scale );
|
101 |
|
102 | orientation.set( 0, 0, - 1 ).applyQuaternion( quaternion );
|
103 |
|
104 | if ( listener.positionX ) {
|
105 |
|
106 | listener.positionX.setValueAtTime( position.x, this.context.currentTime );
|
107 | listener.positionY.setValueAtTime( position.y, this.context.currentTime );
|
108 | listener.positionZ.setValueAtTime( position.z, this.context.currentTime );
|
109 | listener.forwardX.setValueAtTime( orientation.x, this.context.currentTime );
|
110 | listener.forwardY.setValueAtTime( orientation.y, this.context.currentTime );
|
111 | listener.forwardZ.setValueAtTime( orientation.z, this.context.currentTime );
|
112 | listener.upX.setValueAtTime( up.x, this.context.currentTime );
|
113 | listener.upY.setValueAtTime( up.y, this.context.currentTime );
|
114 | listener.upZ.setValueAtTime( up.z, this.context.currentTime );
|
115 |
|
116 | } else {
|
117 |
|
118 | listener.setPosition( position.x, position.y, position.z );
|
119 | listener.setOrientation( orientation.x, orientation.y, orientation.z, up.x, up.y, up.z );
|
120 |
|
121 | }
|
122 |
|
123 | };
|
124 |
|
125 | } )()
|
126 |
|
127 | } );
|
128 |
|
129 | export { AudioListener };
|