Audio.js 4.1 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();
M
Mr.doob 已提交
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

M
Mr.doob 已提交
32 33
THREE.Audio.prototype.getOutput = function () {

34
	return this.gain;
M
Mr.doob 已提交
35

36
};
37

M
Mr.doob 已提交
38 39 40 41 42 43
THREE.Audio.prototype.load = function ( file ) {

	var buffer = new THREE.AudioBuffer( this.context );
	buffer.load( file );

	this.setBuffer( buffer );
44 45 46 47 48

	return this;

};

49 50 51 52 53 54
THREE.Audio.prototype.setNodeSource = function ( audioNode ) {

	this.hasPlaybackControl = false;
	this.sourceType = 'audioNode';
	this.source = audioNode;
	this.connect();
M
Mr.doob 已提交
55

56 57 58 59
	return this;

};

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

	var scope = this;
M
Mr.doob 已提交
63

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

	return this;

};

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

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

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

	}
M
Mr.doob 已提交
82

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

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

	}

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

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

M
Mr.doob 已提交
98 99 100
	this.isPlaying = true;

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

102
	this.connect();
103 104 105 106 107

};

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

M
Mr.doob 已提交
108 109
	if ( this.hasPlaybackControl === false ) {

110 111
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
M
Mr.doob 已提交
112

113
	}
M
Mr.doob 已提交
114

115 116 117 118 119 120 121
	this.source.stop();
	this.startTime = this.context.currentTime;

};

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

M
Mr.doob 已提交
122 123
	if ( this.hasPlaybackControl === false ) {

124 125
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
M
Mr.doob 已提交
126

127 128
	}

129 130
	this.source.stop();
	this.startTime = 0;
131 132 133

};

134 135
THREE.Audio.prototype.connect = function () {

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

138
		this.source.connect( this.filter );
M
Mr.doob 已提交
139
		this.filter.connect( this.getOutput() );
140 141 142

	} else {

M
Mr.doob 已提交
143
		this.source.connect( this.getOutput() );
144 145 146 147 148 149 150

	}

};

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

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

153
		this.source.disconnect( this.filter );
M
Mr.doob 已提交
154
		this.filter.disconnect( this.getOutput() );
155 156 157

	} else {

M
Mr.doob 已提交
158
		this.source.disconnect( this.getOutput() );
159 160 161 162 163 164 165

	}

};

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

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

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

	} else {

		this.filter = value;

	}

};

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

	return this.filter;

};

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

M
Mr.doob 已提交
188 189
	if ( this.hasPlaybackControl === false ) {

190 191
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
M
Mr.doob 已提交
192

193 194
	}

195 196
	this.playbackRate = value;

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

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

	}
202 203 204 205 206 207 208 209 210

};

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

	return this.playbackRate;

};

211 212
THREE.Audio.prototype.onEnded = function() {

213 214 215 216
	this.isPlaying = false;

};

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

M
Mr.doob 已提交
219 220
	if ( this.hasPlaybackControl === false ) {

221 222
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return;
M
Mr.doob 已提交
223

224 225
	}

M
Mr.doob 已提交
226 227 228 229
	this.source.loop = value;

};

230
THREE.Audio.prototype.getLoop = function () {
M
Mr.doob 已提交
231 232 233

	if ( this.hasPlaybackControl === false ) {

234 235
		console.warn( 'THREE.Audio: this Audio has no playback control.' );
		return false;
M
Mr.doob 已提交
236

237
	}
238 239 240 241 242 243

	return this.source.loop;

};


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

	this.gain.gain.value = value;

};

250 251 252 253 254
THREE.Audio.prototype.getVolume = function () {

	return this.gain.gain.value;

};