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