|
@@ -13,46 +13,61 @@ THREE.Audio = function ( listener ) {
|
|
|
this.source.onended = this.onEnded.bind( this );
|
|
|
|
|
|
this.gain = this.context.createGain();
|
|
|
- this.gain.connect( this.context.destination );
|
|
|
-
|
|
|
- this.panner = this.context.createPanner();
|
|
|
- this.panner.connect( this.gain );
|
|
|
+ this.gain.connect( listener.getOutputNode());
|
|
|
|
|
|
this.autoplay = false;
|
|
|
|
|
|
this.startTime = 0;
|
|
|
this.playbackRate = 1;
|
|
|
this.isPlaying = false;
|
|
|
+ this.hasPlaybackControl = true;
|
|
|
+ this.sourceType = 'empty';
|
|
|
|
|
|
};
|
|
|
|
|
|
THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
|
|
|
THREE.Audio.prototype.constructor = THREE.Audio;
|
|
|
|
|
|
-THREE.Audio.prototype.load = function ( file ) {
|
|
|
|
|
|
- var scope = this;
|
|
|
+THREE.Audio.prototype.load = function ( fileName ) {
|
|
|
+
|
|
|
+ var audioBuffer = new THREE.AudioBuffer(this.context);
|
|
|
+ audioBuffer.load(fileName);
|
|
|
+ this.setBuffer(audioBuffer);
|
|
|
+ return this;
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
|
|
|
- var request = new XMLHttpRequest();
|
|
|
- request.open( 'GET', file, true );
|
|
|
- request.responseType = 'arraybuffer';
|
|
|
- request.onload = function ( e ) {
|
|
|
+THREE.Audio.prototype.setNodeSource = function ( audioNode ) {
|
|
|
|
|
|
- scope.context.decodeAudioData( this.response, function ( buffer ) {
|
|
|
+ this.hasPlaybackControl = false;
|
|
|
+ this.sourceType = 'audioNode';
|
|
|
+ this.source = audioNode;
|
|
|
+ this.connect();
|
|
|
+
|
|
|
+ return this;
|
|
|
|
|
|
- scope.source.buffer = buffer;
|
|
|
+};
|
|
|
|
|
|
- if ( scope.autoplay ) scope.play();
|
|
|
|
|
|
- } );
|
|
|
|
|
|
- };
|
|
|
- request.send();
|
|
|
+THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
|
|
|
+
|
|
|
+ var scope = this;
|
|
|
+
|
|
|
+ audioBuffer.onReady(function(buffer) {
|
|
|
+ scope.source.buffer = buffer;
|
|
|
+ scope.sourceType = 'buffer';
|
|
|
+ if ( scope.autoplay ) scope.play();
|
|
|
+ });
|
|
|
|
|
|
return this;
|
|
|
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+
|
|
|
THREE.Audio.prototype.play = function () {
|
|
|
|
|
|
if ( this.isPlaying === true ) {
|
|
@@ -61,6 +76,14 @@ THREE.Audio.prototype.play = function () {
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ if ( this.hasPlaybackControl === false ) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
var source = this.context.createBufferSource();
|
|
|
|
|
@@ -80,6 +103,13 @@ THREE.Audio.prototype.play = function () {
|
|
|
|
|
|
THREE.Audio.prototype.pause = function () {
|
|
|
|
|
|
+ if (this.hasPlaybackControl === false) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
this.source.stop();
|
|
|
this.startTime = this.context.currentTime;
|
|
|
|
|
@@ -87,6 +117,13 @@ THREE.Audio.prototype.pause = function () {
|
|
|
|
|
|
THREE.Audio.prototype.stop = function () {
|
|
|
|
|
|
+ if (this.hasPlaybackControl === false) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
this.source.stop();
|
|
|
this.startTime = 0;
|
|
|
|
|
@@ -97,11 +134,11 @@ THREE.Audio.prototype.connect = function () {
|
|
|
if ( this.filter !== undefined ) {
|
|
|
|
|
|
this.source.connect( this.filter );
|
|
|
- this.filter.connect( this.panner );
|
|
|
+ this.filter.connect( this.gain );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
- this.source.connect( this.panner );
|
|
|
+ this.source.connect( this.gain );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -112,11 +149,11 @@ THREE.Audio.prototype.disconnect = function () {
|
|
|
if ( this.filter !== undefined ) {
|
|
|
|
|
|
this.source.disconnect( this.filter );
|
|
|
- this.filter.disconnect( this.panner );
|
|
|
+ this.filter.disconnect( this.gain );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
- this.source.disconnect( this.panner );
|
|
|
+ this.source.disconnect( this.gain );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -146,6 +183,13 @@ THREE.Audio.prototype.getFilter = function () {
|
|
|
|
|
|
THREE.Audio.prototype.setPlaybackRate = function ( value ) {
|
|
|
|
|
|
+ if (this.hasPlaybackControl === false) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
this.playbackRate = value;
|
|
|
|
|
|
if ( this.isPlaying === true ) {
|
|
@@ -170,39 +214,30 @@ THREE.Audio.prototype.onEnded = function() {
|
|
|
|
|
|
THREE.Audio.prototype.setLoop = function ( value ) {
|
|
|
|
|
|
+ if (this.hasPlaybackControl === false) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
this.source.loop = value;
|
|
|
|
|
|
};
|
|
|
|
|
|
THREE.Audio.prototype.getLoop = function () {
|
|
|
+
|
|
|
+ if (this.hasPlaybackControl === false) {
|
|
|
+
|
|
|
+ console.warn( 'THREE.Audio: this Audio has no playback control.' );
|
|
|
+ return false;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
return this.source.loop;
|
|
|
|
|
|
};
|
|
|
|
|
|
-THREE.Audio.prototype.setRefDistance = function ( value ) {
|
|
|
-
|
|
|
- this.panner.refDistance = value;
|
|
|
-
|
|
|
-};
|
|
|
-
|
|
|
-THREE.Audio.prototype.getRefDistance = function () {
|
|
|
-
|
|
|
- return this.panner.refDistance;
|
|
|
-
|
|
|
-};
|
|
|
-
|
|
|
-THREE.Audio.prototype.setRolloffFactor = function ( value ) {
|
|
|
-
|
|
|
- this.panner.rolloffFactor = value;
|
|
|
-
|
|
|
-};
|
|
|
-
|
|
|
-THREE.Audio.prototype.getRolloffFactor = function () {
|
|
|
-
|
|
|
- return this.panner.rolloffFactor;
|
|
|
-
|
|
|
-};
|
|
|
|
|
|
THREE.Audio.prototype.setVolume = function ( value ) {
|
|
|
|
|
@@ -216,18 +251,3 @@ THREE.Audio.prototype.getVolume = function () {
|
|
|
|
|
|
};
|
|
|
|
|
|
-THREE.Audio.prototype.updateMatrixWorld = ( function () {
|
|
|
-
|
|
|
- var position = new THREE.Vector3();
|
|
|
-
|
|
|
- return function updateMatrixWorld( force ) {
|
|
|
-
|
|
|
- THREE.Object3D.prototype.updateMatrixWorld.call( this, force );
|
|
|
-
|
|
|
- position.setFromMatrixPosition( this.matrixWorld );
|
|
|
-
|
|
|
- this.panner.setPosition( position.x, position.y, position.z );
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
-} )();
|