annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 15:be5ac56826be

created part of the advanced documentation
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 06:52:46 -0700
parents e299cd89074d
children 87f818f58975
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@15 65 private Spatial teapot;
rlm@15 66 private boolean active = true;
rlm@15 67 private boolean playing = false;
rlm@15 68 private MotionPath path;
rlm@15 69 private MotionTrack motionControl;
rlm@15 70
rlm@15 71
rlm@15 72 private void makeEar(Node root, Vector3f position){
rlm@15 73 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@15 74 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
rlm@15 75 ear.setLocalTranslation(position);
rlm@15 76 mat.setColor("Color", ColorRGBA.Green);
rlm@15 77 ear.setMaterial(mat);
rlm@15 78 root.attachChild(ear);
rlm@15 79 }
rlm@15 80
rlm@15 81
rlm@15 82 private void createScene() {
rlm@15 83 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@15 84 Geometry bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
rlm@15 85 mat.setColor("Color", ColorRGBA.Blue);
rlm@15 86 bell.setMaterial(mat);
rlm@15 87 rootNode.attachChild(bell);
rlm@15 88
rlm@15 89
rlm@15 90
rlm@15 91
rlm@15 92 DirectionalLight light = new DirectionalLight();
rlm@15 93 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
rlm@15 94 light.setColor(ColorRGBA.White.mult(1.5f));
rlm@15 95 rootNode.addLight(light);
rlm@15 96
rlm@15 97 makeEar(rootNode, new Vector3f(0, 0 ,20));
rlm@15 98 makeEar(rootNode, new Vector3f(0, 0 ,-20));
rlm@15 99 makeEar(rootNode, new Vector3f(20, 0 ,0));
rlm@15 100 makeEar(rootNode, new Vector3f(-20, 0 ,0));
rlm@15 101
rlm@15 102 MotionPath path = new MotionPath();
rlm@15 103
rlm@15 104 // loop 1
rlm@15 105 path.addWayPoint(new Vector3f(0, 0, 0));
rlm@15 106 path.addWayPoint(new Vector3f(0, 0, -10));
rlm@15 107 path.addWayPoint(new Vector3f(-2, 0, -14));
rlm@15 108 path.addWayPoint(new Vector3f(-6, 0, -20));
rlm@15 109 path.addWayPoint(new Vector3f(0, 0, -26));
rlm@15 110 path.addWayPoint(new Vector3f(6, 0, -20));
rlm@15 111 path.addWayPoint(new Vector3f(0, 0, -14));
rlm@15 112 path.addWayPoint(new Vector3f(-6, 0, -20));
rlm@15 113 path.addWayPoint(new Vector3f(0, 0, -26));
rlm@15 114 path.addWayPoint(new Vector3f(6, 0, -20));
rlm@15 115
rlm@15 116
rlm@15 117 // loop 2
rlm@15 118 path.addWayPoint(new Vector3f(5, 0, -5));
rlm@15 119 path.addWayPoint(new Vector3f(7, 0, 1.5f));
rlm@15 120 path.addWayPoint(new Vector3f(14, 0, 2));
rlm@15 121 path.addWayPoint(new Vector3f(20, 0, 6));
rlm@15 122 path.addWayPoint(new Vector3f(26, 0, 0));
rlm@15 123 path.addWayPoint(new Vector3f(20, 0, -6));
rlm@15 124 path.addWayPoint(new Vector3f(14, 0, 0));
rlm@15 125 path.addWayPoint(new Vector3f(20, 0, 6));
rlm@15 126 path.addWayPoint(new Vector3f(26, 0, 0));
rlm@15 127 path.addWayPoint(new Vector3f(20, 0, -6));
rlm@15 128 path.addWayPoint(new Vector3f(14, 0, 0));
rlm@15 129
rlm@15 130
rlm@15 131
rlm@15 132 // loop 3
rlm@15 133 path.addWayPoint(new Vector3f(8, 0, 7.5f));
rlm@15 134 path.addWayPoint(new Vector3f(7, 0, 10.5f));
rlm@15 135 path.addWayPoint(new Vector3f(6, 0, 20));
rlm@15 136 path.addWayPoint(new Vector3f(0, 0, 26));
rlm@15 137 path.addWayPoint(new Vector3f(-6, 0, 20));
rlm@15 138 path.addWayPoint(new Vector3f(0, 0, 14));
rlm@15 139 path.addWayPoint(new Vector3f(6, 0, 20));
rlm@15 140 path.addWayPoint(new Vector3f(0, 0, 26));
rlm@15 141 path.addWayPoint(new Vector3f(-6, 0, 20));
rlm@15 142 path.addWayPoint(new Vector3f(0, 0, 14));
rlm@15 143
rlm@15 144
rlm@15 145 // begin elipse
rlm@15 146
rlm@15 147 path.addWayPoint(new Vector3f(16, 5, 20));
rlm@15 148 path.addWayPoint(new Vector3f(0, 0, 26));
rlm@15 149 path.addWayPoint(new Vector3f(-16, -10, 20));
rlm@15 150 path.addWayPoint(new Vector3f(0, 0, 14));
rlm@15 151 path.addWayPoint(new Vector3f(16, 20, 20));
rlm@15 152 path.addWayPoint(new Vector3f(0, 0, 26));
rlm@15 153 path.addWayPoint(new Vector3f(-10, -25, 10));
rlm@15 154 path.addWayPoint(new Vector3f(-10, 0, 0));
rlm@15 155
rlm@15 156 // come at me bro!
rlm@15 157 path.addWayPoint(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
rlm@15 158 path.setCurveTension(0.80f);
rlm@15 159
rlm@15 160
rlm@15 161 motionControl = new MotionTrack(bell,path);
rlm@15 162 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
rlm@15 163 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
rlm@15 164 motionControl.setInitialDuration(10f);
rlm@15 165 motionControl.setSpeed(0.1f);
rlm@15 166
rlm@15 167
rlm@15 168 //path.enableDebugShape(assetManager, rootNode);
rlm@15 169
rlm@15 170
rlm@15 171 positionCamera();
rlm@15 172
rlm@15 173
rlm@15 174 }
rlm@15 175
rlm@15 176
rlm@15 177 private void positionCamera(){
rlm@15 178 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
rlm@15 179 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
rlm@15 180
rlm@15 181
rlm@15 182 }
rlm@15 183
rlm@15 184
rlm@15 185
rlm@15 186
rlm@14 187
rlm@14 188 private AudioNode audio_gun;
rlm@14 189 private AudioNode audio_nature;
rlm@14 190 private Geometry player;
rlm@14 191 private Listener auxListener = new Listener();
rlm@14 192 public File data1 = new File("/home/r/tmp/data1.wav");
rlm@14 193 public File data2 = new File("/home/r/tmp/data2.wav");
rlm@14 194 public File data3 = new File("/home/r/tmp/data3.wav");
rlm@14 195
rlm@14 196
rlm@14 197 @Override
rlm@14 198 public void simpleInitApp() {
rlm@15 199 //this.setTimer(new IsoTimer(60));
rlm@14 200
rlm@14 201 if (this.audioRenderer instanceof MultiListener){
rlm@14 202 MultiListener rf = (MultiListener)this.audioRenderer;
rlm@14 203
rlm@14 204 for (int n = 0; n < 0; n++){
rlm@14 205 Listener zzz = new Listener();
rlm@14 206 rf.addListener(zzz);
rlm@14 207 }
rlm@14 208 Listener listener3 = new Listener();
rlm@14 209 rf.addListener(auxListener);
rlm@14 210 rf.addListener(listener3);
rlm@14 211 rf.registerSoundProcessor(new WaveFileWriter(data1));
rlm@14 212 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
rlm@14 213 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
rlm@14 214 }
rlm@14 215
rlm@14 216
rlm@15 217
rlm@15 218
rlm@15 219
rlm@14 220 initKeys();
rlm@14 221 initAudio();
rlm@15 222 createScene();
rlm@15 223
rlm@15 224
rlm@15 225 motionControl.play();
rlm@14 226
rlm@14 227 this.audioRenderer.playSource(audio_gun);
rlm@14 228
rlm@14 229
rlm@14 230
rlm@14 231 }
rlm@14 232
rlm@14 233 /** We create two audio nodes. */
rlm@14 234 private void initAudio() {
rlm@14 235 //audioRenderer.setEnvironment(Environment.Cavern);
rlm@14 236 /* gun shot sound is to be triggered by a mouse click. */
rlm@14 237 audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
rlm@14 238 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
rlm@14 239 audio_gun.setLooping(false);
rlm@14 240 audio_gun.setVolume(2);
rlm@14 241 audio_gun.setPositional(true);
rlm@14 242
rlm@14 243
rlm@14 244 /* nature sound - keeps playing in a loop. */
rlm@14 245 audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
rlm@14 246 audio_nature.setLooping(true);
rlm@14 247 audio_nature.setPositional(true);
rlm@14 248 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
rlm@14 249 audio_nature.setVolume(3);
rlm@14 250 audio_nature.updateGeometricState();
rlm@14 251 }
rlm@14 252
rlm@14 253 /** Declaring the "Shoot" action, and
rlm@14 254 * mapping it to a trigger (mouse click). */
rlm@14 255 private void initKeys() {
rlm@14 256 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
rlm@14 257 inputManager.addListener(actionListener, "Shoot");
rlm@14 258 }
rlm@14 259
rlm@14 260 /** Defining the "Shoot" action: Play a gun sound. */
rlm@14 261 private ActionListener actionListener = new ActionListener() {
rlm@14 262 @Override
rlm@14 263 public void onAction(String name, boolean keyPressed, float tpf) {
rlm@14 264 if (name.equals("Shoot") && !keyPressed) {
rlm@14 265 audioRenderer.playSource(audio_gun); // play once!
rlm@14 266 }
rlm@14 267 }
rlm@14 268 };
rlm@14 269
rlm@14 270 /** Move the listener with the camera - for 3D audio. */
rlm@14 271 @Override
rlm@14 272 public void simpleUpdate(float tpf) {
rlm@14 273 Vector3f loc = cam.getLocation();
rlm@14 274 Quaternion rot = cam.getRotation();
rlm@14 275 listener.setLocation(loc);
rlm@14 276 listener.setRotation(rot);
rlm@14 277 auxListener.setLocation(loc);
rlm@14 278 auxListener.setRotation(rot);
rlm@15 279
rlm@14 280
rlm@14 281
rlm@14 282 }
rlm@14 283
rlm@14 284 }