Audio.js 3.8 KB
Newer Older
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

M
Mr.doob 已提交
5
THREE.Audio = function ( listener ) {
6 7 8 9 10 11 12

	THREE.Object3D.call( this );

	this.type = 'Audio';

	this.context = listener.context;
	this.source = this.context.createBufferSource();
13
	this.source.onended = this.onEnded.bind( this );
14 15 16 17 18 19 20

	this.gain = this.context.createGain();
	this.gain.connect( this.context.destination );

	this.panner = this.context.createPanner();
	this.panner.connect( this.gain );

M
Mr.doob 已提交
21 22
	this.autoplay = false;

23
	this.startTime = 0;
24
	this.playbackRate = 1;
25 26
	this.isPlaying = false;

27 28 29
};

THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
30
THREE.Audio.prototype.constructor = THREE.Audio;
31

32
THREE.Audio.prototype.load = function ( file ) {
33 34 35 36 37 38 39 40 41 42 43

	var scope = this;

	var request = new XMLHttpRequest();
	request.open( 'GET', file, true );
	request.responseType = 'arraybuffer';
	request.onload = function ( e ) {

		scope.context.decodeAudioData( this.response, function ( buffer ) {

			scope.source.buffer = buffer;
44

45
			if ( scope.autoplay ) scope.play();
46 47 48 49 50 51 52 53 54 55

		} );

	};
	request.send();

	return this;

};

56 57
THREE.Audio.prototype.play = function () {

M
Mr.doob 已提交
58
	if ( this.isPlaying === true ) {
59

60
		console.warn( 'THREE.Audio: Audio is already playing.' );
M
Mr.doob 已提交
61
		return;
62 63 64

	}

M
Mr.doob 已提交
65
	var source = this.context.createBufferSource();
66

M
Mr.doob 已提交
67 68 69 70
	source.buffer = this.source.buffer;
	source.loop = this.source.loop;
	source.onended = this.source.onended;
	source.start( 0, this.startTime );
71
	source.playbackRate.value = this.playbackRate;
M
Mr.doob 已提交
72

M
Mr.doob 已提交
73 74 75
	this.isPlaying = true;

	this.source = source;
M
Mr.doob 已提交
76

77
	this.connect();
78 79 80 81 82 83 84 85 86 87 88 89 90 91

};

THREE.Audio.prototype.pause = function () {

	this.source.stop();
	this.startTime = this.context.currentTime;

};

THREE.Audio.prototype.stop = function () {

	this.source.stop();
	this.startTime = 0;
92 93 94

};

95 96 97
THREE.Audio.prototype.connect = function () {

	if ( this.filter !== undefined ) {
M
Mr.doob 已提交
98

99 100 101 102 103
		this.source.connect( this.filter );
		this.filter.connect( this.panner );

	} else {

M
Mr.doob 已提交
104
		this.source.connect( this.panner );
105 106 107 108 109 110 111 112

	}

};

THREE.Audio.prototype.disconnect = function () {

	if ( this.filter !== undefined ) {
M
Mr.doob 已提交
113

114 115 116 117 118
		this.source.disconnect( this.filter );
		this.filter.disconnect( this.panner );

	} else {

M
Mr.doob 已提交
119
		this.source.disconnect( this.panner );
120 121 122 123 124 125 126

	}

};

THREE.Audio.prototype.setFilter = function ( value ) {

M
Mr.doob 已提交
127 128 129
	if ( this.isPlaying === true ) {

		this.disconnect();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		this.filter = value;
		this.connect();

	} else {

		this.filter = value;

	}

};

THREE.Audio.prototype.getFilter = function () {

	return this.filter;

};

THREE.Audio.prototype.setPlaybackRate = function ( value ) {

	this.playbackRate = value;

M
Mr.doob 已提交
151 152 153 154 155
	if ( this.isPlaying === true ) {

		this.source.playbackRate.value = this.playbackRate;

	}
156 157 158 159 160 161 162 163 164

};

THREE.Audio.prototype.getPlaybackRate = function () {

	return this.playbackRate;

};

165 166
THREE.Audio.prototype.onEnded = function() {

167 168 169 170
	this.isPlaying = false;

};

M
Mr.doob 已提交
171 172 173 174 175 176
THREE.Audio.prototype.setLoop = function ( value ) {

	this.source.loop = value;

};

177 178 179 180 181 182
THREE.Audio.prototype.getLoop = function () {

	return this.source.loop;

};

183 184 185 186 187 188
THREE.Audio.prototype.setRefDistance = function ( value ) {

	this.panner.refDistance = value;

};

189 190 191 192 193 194
THREE.Audio.prototype.getRefDistance = function () {

	return this.panner.refDistance;

};

195 196 197 198 199 200
THREE.Audio.prototype.setRolloffFactor = function ( value ) {

	this.panner.rolloffFactor = value;

};

201 202 203 204 205 206
THREE.Audio.prototype.getRolloffFactor = function () {

	return this.panner.rolloffFactor;

};

M
Matthew Tung 已提交
207 208 209 210 211 212
THREE.Audio.prototype.setVolume = function ( value ) {

	this.gain.gain.value = value;

};

213 214 215 216 217 218
THREE.Audio.prototype.getVolume = function () {

	return this.gain.gain.value;

};

219
THREE.Audio.prototype.updateMatrixWorld = ( function () {
M
Mr.doob 已提交
220

221 222
	var position = new THREE.Vector3();

223
	return function updateMatrixWorld( force ) {
224 225 226 227 228 229 230 231 232

		THREE.Object3D.prototype.updateMatrixWorld.call( this, force );

		position.setFromMatrixPosition( this.matrixWorld );

		this.panner.setPosition( position.x, position.y, position.z );

	};

M
Mr.doob 已提交
233
} )();