Browse Source

adding support for connecting any AudioNode to THREE.Audio as a source

vincent courtalon 9 years ago
parent
commit
c5e9eefc1c
2 changed files with 109 additions and 6 deletions
  1. 49 6
      examples/misc_sound.html
  2. 60 0
      src/audio/Audio.js

+ 49 - 6
examples/misc_sound.html

@@ -88,6 +88,7 @@
 
 				material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
 				material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
+				material_sphere3 = new THREE.MeshLambertMaterial( { color: 0x6622aa, shading: THREE.FlatShading } );
 
 				// sound spheres
 
@@ -116,15 +117,37 @@
 				sound2.setRefDistance( 20 );
 				sound2.autoplay = true;
 				mesh2.add( sound2 );
+				
+				
+				var mesh3 = new THREE.Mesh( sphere, material_sphere3 );
+				mesh3.position.set( 0, 30, -250 );
+				scene.add( mesh3 );
+				
+				var sound3 = new THREE.PositionalAudio( listener );
+				var oscillator = listener.context.createOscillator();
+				oscillator.type = 'sine';
+				oscillator.frequency.value = 144;
+				oscillator.start(0);
+				sound3.setNodeSource(oscillator);
+				sound3.setRefDistance( 20 );
+				sound3.setVolume(0.5);
+				mesh3.add(sound3);
+				
+		/*		buffer2.load( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' );
+				sound2.setBuffer(buffer2);
+				sound2.setRefDistance( 20 );
+				sound2.autoplay = true;
+				mesh2.add( sound2 );
+		*/		
 
 				// global ambient audio
 	
-				var sound3 = new THREE.Audio( listener );
+				var sound4 = new THREE.Audio( listener );
 				var buffer3 = new THREE.AudioBuffer( listener );
 				buffer3.load( 'sounds/Project_Utopia.ogg' );
-				sound3.setBuffer(buffer3);
-				sound3.autoplay = true;
-				sound3.setLoop(true);
+				sound4.setBuffer(buffer3);
+				sound4.autoplay = true;
+				sound4.setLoop(true);
 				sound3.setVolume(0.5);
 				
 				// ground
@@ -140,11 +163,18 @@
 					   this.master = listener.getMasterVolume();
 					   this.firstSphere =  sound1.getVolume();
 					   this.secondSphere =  sound2.getVolume();
-					   this.Ambient =  sound3.getVolume();
+					   this.thirdSphere = sound3.getVolume();
+					   this.Ambient =  sound4.getVolume();
 				};
+				var GeneratorControls = function() {
+					   this.frequency = oscillator.frequency.value;
+					   this.wavetype = oscillator.type;
+				}
       			var gui = new dat.GUI();
 				var soundControls = new SoundControls();
+				var generatorControls = new GeneratorControls();
 				var volumeFolder = gui.addFolder('sound volume');
+				var generatorFolder = gui.addFolder('sound generator');
 				volumeFolder.add(soundControls, 'master').min(0.0).max(1.0).step(0.01).onChange(function() {
 					listener.setMasterVolume(soundControls.master);
 				});
@@ -154,10 +184,22 @@
 				volumeFolder.add(soundControls, 'secondSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
 					sound2.setVolume(soundControls.secondSphere);
 				});
+				
+				volumeFolder.add(soundControls, 'thirdSphere').min(0.0).max(1.0).step(0.01).onChange(function() {
+					sound3.setVolume(soundControls.thirdSphere);
+				});
 				volumeFolder.add(soundControls, 'Ambient').min(0.0).max(1.0).step(0.01).onChange(function() {
-					sound3.setVolume(soundControls.Ambient);
+					sound4.setVolume(soundControls.Ambient);
 				});
 				volumeFolder.open();
+				generatorFolder.add(generatorControls, 'frequency').min(50.0).max(5000.0).step(1.0).onChange(function() {
+					oscillator.frequency.value = generatorControls.frequency;
+				});
+				generatorFolder.add(generatorControls, 'wavetype', ['sine', 'square', 'sawtooth', 'triangle']).onChange(function() {
+					oscillator.type = generatorControls.wavetype;
+				});
+				generatorFolder.open();
+				
 
 
 
@@ -210,6 +252,7 @@
 
 				material_sphere1.color.setHSL( 0.0, 0.3 + 0.7 * ( 1 + Math.cos( time ) ) / 2, 0.5 );
 				material_sphere2.color.setHSL( 0.1, 0.3 + 0.7 * ( 1 + Math.sin( time ) ) / 2, 0.5 );
+				material_sphere3.color.setHSL( 0.1, 0.3 + 0.7 * ( 1 + Math.sin( time ) ) / 2, 0.5 );
 
 				renderer.render( scene, camera );
 

+ 60 - 0
src/audio/Audio.js

@@ -20,18 +20,35 @@ THREE.Audio = function ( listener ) {
 	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.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';
 		if ( scope.autoplay ) scope.play();
 	});
 
@@ -49,6 +66,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();
 
@@ -68,6 +93,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;
 
@@ -75,6 +107,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;
 
@@ -134,6 +173,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 ) {
@@ -158,11 +204,25 @@ 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;