Mercurial > jmeCapture
view src/com/aurellem/capture/examples/AdvancedAudio.java @ 16:87f818f58975
more work on Advanced documentation
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 28 Oct 2011 22:24:10 -0700 |
parents | be5ac56826be |
children | dae169178d11 |
line wrap: on
line source
1 package com.aurellem.capture.examples;3 import java.io.File;5 import com.aurellem.capture.IsoTimer;6 import com.aurellem.capture.audio.MultiListener;7 import com.aurellem.capture.audio.WaveFileWriter;8 import com.jme3.app.SimpleApplication;9 import com.jme3.audio.AudioNode;10 import com.jme3.audio.Listener;11 import com.jme3.cinematic.MotionPath;12 import com.jme3.cinematic.events.MotionTrack;13 import com.jme3.input.KeyInput;14 import com.jme3.input.controls.ActionListener;15 import com.jme3.input.controls.KeyTrigger;16 import com.jme3.input.controls.MouseButtonTrigger;17 import com.jme3.light.DirectionalLight;18 import com.jme3.material.Material;19 import com.jme3.math.ColorRGBA;20 import com.jme3.math.FastMath;21 import com.jme3.math.Quaternion;22 import com.jme3.math.Vector3f;23 import com.jme3.scene.Geometry;24 import com.jme3.scene.Node;25 import com.jme3.scene.Spatial;26 import com.jme3.scene.shape.Box;27 import com.jme3.scene.shape.Sphere;28 import com.jme3.system.AppSettings;31 /**32 *33 * Demonstrates advanced use of the audio capture and recording features.34 * Multiple perspectives of the same scene are simultaneously rendered to35 * different sound files.36 *37 * A key limitation of the way multiple listeners are implemented is that38 * only 3D positioning effects are realized for listeners other than the39 * main LWJGL listener. This means that audio effects such as environment40 * settings will *not* be heard on any auxiliary listeners, though sound41 * attenuation will work correctly.42 *43 * Multiple listeners as realized here might be used to make AI entities44 * that can each hear the world from their own perspective.45 *46 * @author Robert McIntyre47 *48 */50 public class AdvancedAudio extends SimpleApplication {52 public static void main(String[] args) {54 AdvancedAudio app = new AdvancedAudio();55 AppSettings settings = new AppSettings(true);56 //settings.setAudioRenderer("Send");57 app.setSettings(settings);58 app.setShowSettings(false);59 app.setPauseOnLostFocus(false);60 org.lwjgl.input.Mouse.setGrabbed(false);61 app.start();62 }67 private MotionTrack motionControl;70 private void makeEar(Node root, Vector3f position){71 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");72 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));73 ear.setLocalTranslation(position);74 mat.setColor("Color", ColorRGBA.Green);75 ear.setMaterial(mat);76 root.attachChild(ear);77 }79 private void createScene() {80 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");81 Geometry bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));82 mat.setColor("Color", ColorRGBA.Blue);83 bell.setMaterial(mat);84 rootNode.attachChild(bell);86 DirectionalLight light = new DirectionalLight();87 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());88 light.setColor(ColorRGBA.White.mult(1.5f));89 rootNode.addLight(light);91 makeEar(rootNode, new Vector3f(0, 0 ,20));92 makeEar(rootNode, new Vector3f(0, 0 ,-20));93 makeEar(rootNode, new Vector3f(20, 0 ,0));94 makeEar(rootNode, new Vector3f(-20, 0 ,0));96 MotionPath path = new MotionPath();98 // loop 199 path.addWayPoint(new Vector3f(0, 0, 0));100 path.addWayPoint(new Vector3f(0, 0, -10));101 path.addWayPoint(new Vector3f(-2, 0, -14));102 path.addWayPoint(new Vector3f(-6, 0, -20));103 path.addWayPoint(new Vector3f(0, 0, -26));104 path.addWayPoint(new Vector3f(6, 0, -20));105 path.addWayPoint(new Vector3f(0, 0, -14));106 path.addWayPoint(new Vector3f(-6, 0, -20));107 path.addWayPoint(new Vector3f(0, 0, -26));108 path.addWayPoint(new Vector3f(6, 0, -20));111 // loop 2112 path.addWayPoint(new Vector3f(5, 0, -5));113 path.addWayPoint(new Vector3f(7, 0, 1.5f));114 path.addWayPoint(new Vector3f(14, 0, 2));115 path.addWayPoint(new Vector3f(20, 0, 6));116 path.addWayPoint(new Vector3f(26, 0, 0));117 path.addWayPoint(new Vector3f(20, 0, -6));118 path.addWayPoint(new Vector3f(14, 0, 0));119 path.addWayPoint(new Vector3f(20, 0, 6));120 path.addWayPoint(new Vector3f(26, 0, 0));121 path.addWayPoint(new Vector3f(20, 0, -6));122 path.addWayPoint(new Vector3f(14, 0, 0));126 // loop 3127 path.addWayPoint(new Vector3f(8, 0, 7.5f));128 path.addWayPoint(new Vector3f(7, 0, 10.5f));129 path.addWayPoint(new Vector3f(6, 0, 20));130 path.addWayPoint(new Vector3f(0, 0, 26));131 path.addWayPoint(new Vector3f(-6, 0, 20));132 path.addWayPoint(new Vector3f(0, 0, 14));133 path.addWayPoint(new Vector3f(6, 0, 20));134 path.addWayPoint(new Vector3f(0, 0, 26));135 path.addWayPoint(new Vector3f(-6, 0, 20));136 path.addWayPoint(new Vector3f(0, 0, 14));139 // begin elipse141 path.addWayPoint(new Vector3f(16, 5, 20));142 path.addWayPoint(new Vector3f(0, 0, 26));143 path.addWayPoint(new Vector3f(-16, -10, 20));144 path.addWayPoint(new Vector3f(0, 0, 14));145 path.addWayPoint(new Vector3f(16, 20, 20));146 path.addWayPoint(new Vector3f(0, 0, 26));147 path.addWayPoint(new Vector3f(-10, -25, 10));148 path.addWayPoint(new Vector3f(-10, 0, 0));150 // come at me bro!151 path.addWayPoint(new Vector3f(-28.00242f, 48.005623f, -34.648228f));152 path.setCurveTension(0.80f);155 motionControl = new MotionTrack(bell,path);156 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);157 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));158 motionControl.setInitialDuration(10f);159 motionControl.setSpeed(0.1f);162 //path.enableDebugShape(assetManager, rootNode);165 positionCamera();168 }171 private void positionCamera(){172 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));173 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));176 }182 private AudioNode audio_gun;183 private AudioNode audio_nature;184 private Geometry player;185 private Listener auxListener = new Listener();186 public File data1 = new File("/home/r/tmp/data1.wav");187 public File data2 = new File("/home/r/tmp/data2.wav");188 public File data3 = new File("/home/r/tmp/data3.wav");191 @Override192 public void simpleInitApp() {193 //this.setTimer(new IsoTimer(60));195 if (this.audioRenderer instanceof MultiListener){196 MultiListener rf = (MultiListener)this.audioRenderer;198 for (int n = 0; n < 0; n++){199 Listener zzz = new Listener();200 rf.addListener(zzz);201 }202 Listener listener3 = new Listener();203 rf.addListener(auxListener);204 rf.addListener(listener3);205 rf.registerSoundProcessor(new WaveFileWriter(data1));206 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));207 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));208 }214 initKeys();215 initAudio();216 createScene();219 motionControl.play();221 this.audioRenderer.playSource(audio_gun);225 }227 /** We create two audio nodes. */228 private void initAudio() {229 //audioRenderer.setEnvironment(Environment.Cavern);230 /* gun shot sound is to be triggered by a mouse click. */231 audio_gun = new AudioNode(assetManager, "Sound/Environment/haydn.wav", false);232 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);233 audio_gun.setLooping(false);234 audio_gun.setVolume(2);235 audio_gun.setPositional(true);238 /* nature sound - keeps playing in a loop. */239 audio_nature = new AudioNode(assetManager, "Sound/Environment/haydn.wav", false, false);240 audio_nature.setLooping(true);241 audio_nature.setPositional(true);242 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());243 audio_nature.setVolume(3);244 audio_nature.updateGeometricState();245 }247 /** Declaring the "Shoot" action, and248 * mapping it to a trigger (mouse click). */249 private void initKeys() {250 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));251 inputManager.addListener(actionListener, "Shoot");252 }254 /** Defining the "Shoot" action: Play a gun sound. */255 private ActionListener actionListener = new ActionListener() {256 @Override257 public void onAction(String name, boolean keyPressed, float tpf) {258 if (name.equals("Shoot") && !keyPressed) {259 audioRenderer.playSource(audio_gun); // play once!260 }261 }262 };264 /** Move the listener with the camera - for 3D audio. */265 @Override266 public void simpleUpdate(float tpf) {267 Vector3f loc = cam.getLocation();268 Quaternion rot = cam.getRotation();269 listener.setLocation(loc);270 listener.setRotation(rot);271 auxListener.setLocation(loc);272 auxListener.setRotation(rot);276 }278 }