Browse Source

Audio code clean up.

Mr.doob 9 years ago
parent
commit
0c51c8287d
4 changed files with 74 additions and 69 deletions
  1. 34 37
      src/audio/Audio.js
  2. 15 10
      src/audio/AudioBuffer.js
  3. 22 21
      src/audio/AudioListener.js
  4. 3 1
      src/audio/PositionalAudio.js

+ 34 - 37
src/audio/Audio.js

@@ -13,7 +13,7 @@ THREE.Audio = function ( listener ) {
 	this.source.onended = this.onEnded.bind( this );
 
 	this.gain = this.context.createGain();
-	this.gain.connect( listener.getOutputNode());
+	this.gain.connect( listener.getOutputNode() );
 
 	this.autoplay = false;
 
@@ -29,37 +29,38 @@ THREE.Audio = function ( listener ) {
 THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
 THREE.Audio.prototype.constructor = THREE.Audio;
 
-THREE.Audio.prototype.getOutput = function() {
+THREE.Audio.prototype.getOutput = function () {
+
 	return this.gain;
+
 };
 
-THREE.Audio.prototype.load = function ( fileName ) {
+THREE.Audio.prototype.load = function ( file ) {
+
+	var buffer = new THREE.AudioBuffer( this.context );
+	buffer.load( file );
+
+	this.setBuffer( buffer );
 
-	var audioBuffer = new THREE.AudioBuffer(this.context);
-	audioBuffer.load(fileName);
-	this.setBuffer(audioBuffer);
 	return this;
 
 };
 
-
 THREE.Audio.prototype.setNodeSource = function ( audioNode ) {
 
 	this.hasPlaybackControl = false;
 	this.sourceType = 'audioNode';
 	this.source = audioNode;
 	this.connect();
-	
+
 	return this;
 
 };
 
-
-
 THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
 
 	var scope = this;
-	
+
 	audioBuffer.onReady(function(buffer) {
 		scope.source.buffer = buffer;
 		scope.sourceType = 'buffer';
@@ -70,8 +71,6 @@ THREE.Audio.prototype.setBuffer = function ( audioBuffer ) {
 
 };
 
-
-
 THREE.Audio.prototype.play = function () {
 
 	if ( this.isPlaying === true ) {
@@ -80,7 +79,7 @@ THREE.Audio.prototype.play = function () {
 		return;
 
 	}
-	
+
 	if ( this.hasPlaybackControl === false ) {
 
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
@@ -88,7 +87,6 @@ THREE.Audio.prototype.play = function () {
 
 	}
 
-
 	var source = this.context.createBufferSource();
 
 	source.buffer = this.source.buffer;
@@ -107,13 +105,13 @@ THREE.Audio.prototype.play = function () {
 
 THREE.Audio.prototype.pause = function () {
 
-	if (this.hasPlaybackControl === false) {
-		
+	if ( this.hasPlaybackControl === false ) {
+
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
 		return;
-		
+
 	}
-	
+
 	this.source.stop();
 	this.startTime = this.context.currentTime;
 
@@ -121,11 +119,11 @@ THREE.Audio.prototype.pause = function () {
 
 THREE.Audio.prototype.stop = function () {
 
-	if (this.hasPlaybackControl === false) {
-		
+	if ( this.hasPlaybackControl === false ) {
+
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
 		return;
-		
+
 	}
 
 	this.source.stop();
@@ -138,11 +136,11 @@ THREE.Audio.prototype.connect = function () {
 	if ( this.filter !== null ) {
 
 		this.source.connect( this.filter );
-		this.filter.connect( this.getOutput());
+		this.filter.connect( this.getOutput() );
 
 	} else {
 
-		this.source.connect( this.getOutput());
+		this.source.connect( this.getOutput() );
 
 	}
 
@@ -153,11 +151,11 @@ THREE.Audio.prototype.disconnect = function () {
 	if ( this.filter !== null ) {
 
 		this.source.disconnect( this.filter );
-		this.filter.disconnect( this.getOutput());
+		this.filter.disconnect( this.getOutput() );
 
 	} else {
 
-		this.source.disconnect( this.getOutput());
+		this.source.disconnect( this.getOutput() );
 
 	}
 
@@ -187,11 +185,11 @@ THREE.Audio.prototype.getFilter = function () {
 
 THREE.Audio.prototype.setPlaybackRate = function ( value ) {
 
-	if (this.hasPlaybackControl === false) {
-		
+	if ( this.hasPlaybackControl === false ) {
+
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
 		return;
-		
+
 	}
 
 	this.playbackRate = value;
@@ -218,11 +216,11 @@ THREE.Audio.prototype.onEnded = function() {
 
 THREE.Audio.prototype.setLoop = function ( value ) {
 
-	if (this.hasPlaybackControl === false) {
-		
+	if ( this.hasPlaybackControl === false ) {
+
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
 		return;
-		
+
 	}
 
 	this.source.loop = value;
@@ -230,12 +228,12 @@ THREE.Audio.prototype.setLoop = function ( value ) {
 };
 
 THREE.Audio.prototype.getLoop = function () {
-	
-	if (this.hasPlaybackControl === false) {
-		
+
+	if ( this.hasPlaybackControl === false ) {
+
 		console.warn( 'THREE.Audio: this Audio has no playback control.' );
 		return false;
-		
+
 	}
 
 	return this.source.loop;
@@ -254,4 +252,3 @@ THREE.Audio.prototype.getVolume = function () {
 	return this.gain.gain.value;
 
 };
-

+ 15 - 10
src/audio/AudioBuffer.js

@@ -23,12 +23,15 @@ THREE.AudioBuffer.prototype.load = function ( file ) {
 
 			scope.buffer = buffer;
 			scope.ready = true;
-			
-			for (var i = 0; i < scope.readyCallbacks.length; i++) {
+
+			for ( var i = 0; i < scope.readyCallbacks.length; i ++ ) {
+
 				scope.readyCallbacks[i](scope.buffer);
+
 			}
+
 			scope.readyCallbacks = [];
-			
+
 		} );
 
 	};
@@ -39,13 +42,15 @@ THREE.AudioBuffer.prototype.load = function ( file ) {
 };
 
 THREE.AudioBuffer.prototype.onReady = function ( callback ) {
-	
-	if (this.ready) {
-		callback(this.buffer);
-	}
-	else {
-		this.readyCallbacks.push(callback);
+
+	if ( this.ready ) {
+
+		callback( this.buffer );
+
+	} else {
+
+		this.readyCallbacks.push( callback );
+
 	}
 
 };
-

+ 22 - 21
src/audio/AudioListener.js

@@ -12,47 +12,48 @@ THREE.AudioListener = function () {
 	this.masterGain =  this.context.createGain();
 	this.masterGain.connect(this.context.destination);
 	this.filter = null;
-	
+
 };
 
 THREE.AudioListener.prototype = Object.create( THREE.Object3D.prototype );
 THREE.AudioListener.prototype.constructor = THREE.AudioListener;
 
 THREE.AudioListener.prototype.getOutputNode = function () {
-	
+
 	return this.masterGain;
-	
+
 };
 
 THREE.AudioListener.prototype.removeFilter = function ( ) {
-	
-	if (this.filter !== null) {
-	
-		this.masterGain.disconnect(this.filter);
-		this.filter.disconnect(this.context.destination);
-		this.masterGain.connect(this.context.destination);
+
+	if ( this.filter !== null ) {
+
+		this.masterGain.disconnect( this.filter );
+		this.filter.disconnect( this.context.destination );
+		this.masterGain.connect( this.context.destination );
 		this.filter = null;
-		
+
 	}
-	
+
 }
 
 THREE.AudioListener.prototype.setFilter = function ( value ) {
 
-	if (this.filter !== null) {
-		
-		this.masterGain.disconnect(this.filter);
-		this.filter.disconnect(this.context.destination);
+	if ( this.filter !== null ) {
+
+		this.masterGain.disconnect( this.filter );
+		this.filter.disconnect( this.context.destination );
 
 	} else {
-		
-		this.masterGain.disconnect(this.context.destination);
-		
+
+		this.masterGain.disconnect( this.context.destination );
+
 	}
+
 	this.filter = value;
-	this.masterGain.connect(this.filter);
-	this.filter.connect(this.context.destination);	
-	
+	this.masterGain.connect( this.filter );
+	this.filter.connect( this.context.destination );
+
 };
 
 THREE.AudioListener.prototype.getFilter = function () {

+ 3 - 1
src/audio/PositionalAudio.js

@@ -14,8 +14,10 @@ THREE.PositionalAudio = function ( listener ) {
 THREE.PositionalAudio.prototype = Object.create( THREE.Audio.prototype );
 THREE.PositionalAudio.prototype.constructor = THREE.PositionalAudio;
 
-THREE.PositionalAudio.prototype.getOutput = function() {
+THREE.PositionalAudio.prototype.getOutput = function () {
+
 	return this.panner;
+
 };