Audio.js 4.2 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

	this.gain = this.context.createGain();
16
	this.gain.connect( listener.getOutputNode());
17

M
Mr.doob 已提交
18 19
	this.autoplay = false;

20
	this.startTime = 0;
21
	this.playbackRate = 1;
22
	this.isPlaying = false;
23 24
	this.hasPlaybackControl = true;
	this.sourceType = 'empty';
25
	this.filter = null;
26

27 28 29
};

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

32 33 34
THREE.Audio.prototype.getOutput = function() {
	return this.gain;
};
35

36 37 38 39 40 41 42 43 44 45
THREE.Audio.prototype.load = function ( fileName ) {

	var audioBuffer = new THREE.AudioBuffer(this.context);
	audioBuffer.load(fileName);
	this.setBuffer(audioBuffer);
	return this;

};


46 47 48 49 50 51 52 53 54 55 56 57 58
THREE.Audio.prototype.setNodeSource = function ( audioNode ) {

	this.hasPlaybackControl = false;
	this.sourceType = 'audioNode';
	this.source = audioNode;
	this.connect();
	
	return this;

};



59
THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
60 61

	var scope = this;
62 63 64
	
	audioBuffer.onReady(function(buffer) {
		scope.source.buffer = buffer;
65
		scope.sourceType = 'buffer';
66 67
		if ( scope.autoplay ) scope.play();
	});
68 69 70 71 72

	return this;

};

73 74


75 76
THREE.Audio.prototype.play = function () {

M
Mr.doob 已提交
77
	if ( this.isPlaying === true ) {
78

79
		console.warn( 'THREE.Audio: Audio is already playing.' );
M
Mr.doob 已提交
80
		return;
81 82

	}
83 84 85 86 87 88 89 90
	
	if ( this.hasPlaybackControl === false ) {

		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;

	}

91

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

M
Mr.doob 已提交
94 95 96 97
	source.buffer = this.source.buffer;
	source.loop = this.source.loop;
	source.onended = this.source.onended;
	source.start( 0, this.startTime );
98
	source.playbackRate.value = this.playbackRate;
M
Mr.doob 已提交
99

M
Mr.doob 已提交
100 101 102
	this.isPlaying = true;

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

104
	this.connect();
105 106 107 108 109

};

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

110 111 112 113 114 115 116
	if (this.hasPlaybackControl === false) {
		
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
		
	}
	
117 118 119 120 121 122 123
	this.source.stop();
	this.startTime = this.context.currentTime;

};

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

124 125 126 127 128 129 130
	if (this.hasPlaybackControl === false) {
		
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
		
	}

131 132
	this.source.stop();
	this.startTime = 0;
133 134 135

};

136 137
THREE.Audio.prototype.connect = function () {

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

140
		this.source.connect( this.filter );
141
		this.filter.connect( this.getOutput());
142 143 144

	} else {

145
		this.source.connect( this.getOutput());
146 147 148 149 150 151 152

	}

};

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

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

155
		this.source.disconnect( this.filter );
156
		this.filter.disconnect( this.getOutput());
157 158 159

	} else {

160
		this.source.disconnect( this.getOutput());
161 162 163 164 165 166 167

	}

};

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

M
Mr.doob 已提交
168 169 170
	if ( this.isPlaying === true ) {

		this.disconnect();
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		this.filter = value;
		this.connect();

	} else {

		this.filter = value;

	}

};

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

	return this.filter;

};

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

190 191 192 193 194 195 196
	if (this.hasPlaybackControl === false) {
		
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
		
	}

197 198
	this.playbackRate = value;

M
Mr.doob 已提交
199 200 201 202 203
	if ( this.isPlaying === true ) {

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

	}
204 205 206 207 208 209 210 211 212

};

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

	return this.playbackRate;

};

213 214
THREE.Audio.prototype.onEnded = function() {

215 216 217 218
	this.isPlaying = false;

};

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

221 222 223 224 225 226 227
	if (this.hasPlaybackControl === false) {
		
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
		
	}

M
Mr.doob 已提交
228 229 230 231
	this.source.loop = value;

};

232
THREE.Audio.prototype.getLoop = function () {
233 234 235 236 237 238 239
	
	if (this.hasPlaybackControl === false) {
		
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return false;
		
	}
240 241 242 243 244 245

	return this.source.loop;

};


M
Matthew Tung 已提交
246 247 248 249 250 251
THREE.Audio.prototype.setVolume = function ( value ) {

	this.gain.gain.value = value;

};

252 253 254 255 256 257
THREE.Audio.prototype.getVolume = function () {

	return this.gain.gain.value;

};