AudioListener.js 2.9 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

B
bentok 已提交
5 6 7 8
import { Vector3 } from '../math/Vector3.js';
import { Quaternion } from '../math/Quaternion.js';
import { Object3D } from '../core/Object3D.js';
import { AudioContext } from './AudioContext.js';
R
Rich Harris 已提交
9

M
Mr.doob 已提交
10
function AudioListener() {
11

R
Rich Harris 已提交
12
	Object3D.call( this );
13 14 15

	this.type = 'AudioListener';

16
	this.context = AudioContext.getContext();
M
Mr.doob 已提交
17

18 19
	this.gain = this.context.createGain();
	this.gain.connect( this.context.destination );
M
Mr.doob 已提交
20

21
	this.filter = null;
M
Mr.doob 已提交
22

M
Mr.doob 已提交
23
}
24

R
Rich Harris 已提交
25
AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
26

R
Rich Harris 已提交
27
	constructor: AudioListener,
M
Mr.doob 已提交
28

29
	getInput: function () {
M
Mr.doob 已提交
30

31
		return this.gain;
32

33
	},
M
Mr.doob 已提交
34

35
	removeFilter: function ( ) {
M
Mr.doob 已提交
36

37
		if ( this.filter !== null ) {
M
Mr.doob 已提交
38

39 40 41 42
			this.gain.disconnect( this.filter );
			this.filter.disconnect( this.context.destination );
			this.gain.connect( this.context.destination );
			this.filter = null;
M
Mr.doob 已提交
43

44
		}
45

46 47
		return this;

48
	},
49

50
	getFilter: function () {
M
Mr.doob 已提交
51

52
		return this.filter;
53

54
	},
M
Mr.doob 已提交
55

56
	setFilter: function ( value ) {
M
Mr.doob 已提交
57

58
		if ( this.filter !== null ) {
M
Mr.doob 已提交
59

60 61
			this.gain.disconnect( this.filter );
			this.filter.disconnect( this.context.destination );
M
Mr.doob 已提交
62

63
		} else {
64

65
			this.gain.disconnect( this.context.destination );
66

67
		}
68

69 70 71
		this.filter = value;
		this.gain.connect( this.filter );
		this.filter.connect( this.context.destination );
72

73 74
		return this;

75
	},
76

77
	getMasterVolume: function () {
78

79
		return this.gain.gain.value;
80

81
	},
82

83
	setMasterVolume: function ( value ) {
84

85
		this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
86

87 88
		return this;

89
	},
90

91
	updateMatrixWorld: ( function () {
92

R
Rich Harris 已提交
93 94 95
		var position = new Vector3();
		var quaternion = new Quaternion();
		var scale = new Vector3();
96

R
Rich Harris 已提交
97
		var orientation = new Vector3();
98

99
		return function updateMatrixWorld( force ) {
100

R
Rich Harris 已提交
101
			Object3D.prototype.updateMatrixWorld.call( this, force );
M
Mr.doob 已提交
102

103 104
			var listener = this.context.listener;
			var up = this.up;
105

106
			this.matrixWorld.decompose( position, quaternion, scale );
107

108
			orientation.set( 0, 0, - 1 ).applyQuaternion( quaternion );
109

110 111
			if ( listener.positionX ) {

112 113 114 115 116 117 118 119 120
				listener.positionX.setValueAtTime( position.x, this.context.currentTime );
				listener.positionY.setValueAtTime( position.y, this.context.currentTime );
				listener.positionZ.setValueAtTime( position.z, this.context.currentTime );
				listener.forwardX.setValueAtTime( orientation.x, this.context.currentTime );
				listener.forwardY.setValueAtTime( orientation.y, this.context.currentTime );
				listener.forwardZ.setValueAtTime( orientation.z, this.context.currentTime );
				listener.upX.setValueAtTime( up.x, this.context.currentTime );
				listener.upY.setValueAtTime( up.y, this.context.currentTime );
				listener.upZ.setValueAtTime( up.z, this.context.currentTime );
121

M
Mr.doob 已提交
122
			} else {
123 124 125 126 127

				listener.setPosition( position.x, position.y, position.z );
				listener.setOrientation( orientation.x, orientation.y, orientation.z, up.x, up.y, up.z );

			}
128

129
		};
130

131
	} )()
132

133
} );
R
Rich Harris 已提交
134

M
Mr.doob 已提交
135
export { AudioListener };