-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
AudioListener.js
137 lines (81 loc) · 2.83 KB
/
AudioListener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Vector3 } from '../math/Vector3.js';
import { Quaternion } from '../math/Quaternion.js';
import { Clock } from '../core/Clock.js';
import { Object3D } from '../core/Object3D.js';
import { AudioContext } from './AudioContext.js';
const _position = /*@__PURE__*/ new Vector3();
const _quaternion = /*@__PURE__*/ new Quaternion();
const _scale = /*@__PURE__*/ new Vector3();
const _orientation = /*@__PURE__*/ new Vector3();
class AudioListener extends Object3D {
constructor() {
super();
this.type = 'AudioListener';
this.context = AudioContext.getContext();
this.gain = this.context.createGain();
this.gain.connect( this.context.destination );
this.filter = null;
this.timeDelta = 0;
// private
this._clock = new Clock();
}
getInput() {
return this.gain;
}
removeFilter() {
if ( this.filter !== null ) {
this.gain.disconnect( this.filter );
this.filter.disconnect( this.context.destination );
this.gain.connect( this.context.destination );
this.filter = null;
}
return this;
}
getFilter() {
return this.filter;
}
setFilter( value ) {
if ( this.filter !== null ) {
this.gain.disconnect( this.filter );
this.filter.disconnect( this.context.destination );
} else {
this.gain.disconnect( this.context.destination );
}
this.filter = value;
this.gain.connect( this.filter );
this.filter.connect( this.context.destination );
return this;
}
getMasterVolume() {
return this.gain.gain.value;
}
setMasterVolume( value ) {
this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
return this;
}
updateMatrixWorld( force ) {
super.updateMatrixWorld( force );
const listener = this.context.listener;
const up = this.up;
this.timeDelta = this._clock.getDelta();
this.matrixWorld.decompose( _position, _quaternion, _scale );
_orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion );
if ( listener.positionX ) {
// code path for Chrome (see #14393)
const endTime = this.context.currentTime + this.timeDelta;
listener.positionX.linearRampToValueAtTime( _position.x, endTime );
listener.positionY.linearRampToValueAtTime( _position.y, endTime );
listener.positionZ.linearRampToValueAtTime( _position.z, endTime );
listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime );
listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime );
listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime );
listener.upX.linearRampToValueAtTime( up.x, endTime );
listener.upY.linearRampToValueAtTime( up.y, endTime );
listener.upZ.linearRampToValueAtTime( up.z, endTime );
} else {
listener.setPosition( _position.x, _position.y, _position.z );
listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z );
}
}
}
export { AudioListener };