annotate src/com/aurellem/capture/examples/Advanced.java @ 52:d799a0278cc9

advanced documentation.
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 13:51:28 -0600
parents 8a091a5f48fa
children afc437f637bd
rev   line source
rlm@41 1 package com.aurellem.capture.examples;
rlm@41 2
rlm@41 3 import java.io.File;
rlm@41 4 import java.io.FileNotFoundException;
rlm@41 5 import java.io.IOException;
rlm@42 6 import java.lang.reflect.Field;
rlm@41 7 import java.nio.ByteBuffer;
rlm@41 8
rlm@41 9 import javax.sound.sampled.AudioFormat;
rlm@41 10
rlm@41 11 import org.tritonus.share.sampled.FloatSampleTools;
rlm@41 12
rlm@45 13 import com.aurellem.capture.AurellemSystemDelegate;
rlm@41 14 import com.aurellem.capture.Capture;
rlm@41 15 import com.aurellem.capture.IsoTimer;
rlm@41 16 import com.aurellem.capture.audio.CompositeSoundProcessor;
rlm@41 17 import com.aurellem.capture.audio.MultiListener;
rlm@41 18 import com.aurellem.capture.audio.SoundProcessor;
rlm@41 19 import com.aurellem.capture.audio.WaveFileWriter;
rlm@41 20 import com.jme3.app.SimpleApplication;
rlm@41 21 import com.jme3.audio.AudioNode;
rlm@41 22 import com.jme3.audio.Listener;
rlm@41 23 import com.jme3.cinematic.MotionPath;
rlm@42 24 import com.jme3.cinematic.events.AbstractCinematicEvent;
rlm@41 25 import com.jme3.cinematic.events.MotionTrack;
rlm@41 26 import com.jme3.material.Material;
rlm@41 27 import com.jme3.math.ColorRGBA;
rlm@41 28 import com.jme3.math.FastMath;
rlm@41 29 import com.jme3.math.Quaternion;
rlm@41 30 import com.jme3.math.Vector3f;
rlm@41 31 import com.jme3.scene.Geometry;
rlm@41 32 import com.jme3.scene.Node;
rlm@41 33 import com.jme3.scene.shape.Box;
rlm@41 34 import com.jme3.scene.shape.Sphere;
rlm@41 35 import com.jme3.system.AppSettings;
rlm@45 36 import com.jme3.system.JmeSystem;
rlm@41 37
rlm@41 38 /**
rlm@41 39 *
rlm@41 40 * Demonstrates advanced use of the audio capture and recording features.
rlm@41 41 * Multiple perspectives of the same scene are simultaneously rendered to
rlm@41 42 * different sound files.
rlm@41 43 *
rlm@41 44 * A key limitation of the way multiple listeners are implemented is that
rlm@41 45 * only 3D positioning effects are realized for listeners other than the
rlm@41 46 * main LWJGL listener. This means that audio effects such as environment
rlm@41 47 * settings will *not* be heard on any auxiliary listeners, though sound
rlm@41 48 * attenuation will work correctly.
rlm@41 49 *
rlm@41 50 * Multiple listeners as realized here might be used to make AI entities
rlm@41 51 * that can each hear the world from their own perspective.
rlm@41 52 *
rlm@41 53 * @author Robert McIntyre
rlm@41 54 */
rlm@41 55
rlm@41 56 public class Advanced extends SimpleApplication {
rlm@41 57
rlm@52 58 /**
rlm@52 59 * You will see three grey cubes, a blue sphere, and a path
rlm@52 60 * which circles each cube. The blue sphere is generating a
rlm@52 61 * constant monotone sound as it moves along the track. Each
rlm@52 62 * cube is listening for sound; when a cube hears sound whose
rlm@52 63 * intensity is greater than a certain threshold, it changes
rlm@52 64 * its color from grey to green.
rlm@52 65 *
rlm@52 66 * Each cube is also saving whatever it hears to a file. The
rlm@52 67 * scene from the perspective of the viewer is also saved to
rlm@52 68 * a video file. When you listen to each of the sound files
rlm@52 69 * alongside the video, the sound will get louder when the
rlm@52 70 * sphere approaches the cube that generated that sound file.
rlm@52 71 * This shows that each listener is hearing the world from
rlm@52 72 * its own perspective.
rlm@52 73 *
rlm@52 74 */
rlm@41 75 public static void main(String[] args) {
rlm@41 76 Advanced app = new Advanced();
rlm@41 77 AppSettings settings = new AppSettings(true);
rlm@49 78 settings.setAudioRenderer(AurellemSystemDelegate.SEND);
rlm@48 79 JmeSystem.setSystemDelegate(new AurellemSystemDelegate());
rlm@41 80 app.setSettings(settings);
rlm@41 81 app.setShowSettings(false);
rlm@41 82 app.setPauseOnLostFocus(false);
rlm@43 83
rlm@41 84 try {
rlm@41 85 Capture.captureVideo(app, File.createTempFile("advanced",".avi"));
rlm@41 86 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));
rlm@41 87 }
rlm@41 88 catch (IOException e) {e.printStackTrace();}
rlm@43 89
rlm@43 90 app.start();
rlm@43 91 }
rlm@41 92
rlm@52 93
rlm@52 94 private Geometry bell;
rlm@52 95 private Geometry ear1;
rlm@52 96 private Geometry ear2;
rlm@52 97 private Geometry ear3;
rlm@52 98 private AudioNode music;
rlm@52 99 private MotionTrack motionControl;
rlm@52 100
rlm@41 101 private Geometry makeEar(Node root, Vector3f position){
rlm@41 102 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@41 103 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
rlm@41 104 ear.setLocalTranslation(position);
rlm@41 105 mat.setColor("Color", ColorRGBA.Green);
rlm@41 106 ear.setMaterial(mat);
rlm@41 107 root.attachChild(ear);
rlm@41 108 return ear;
rlm@41 109 }
rlm@41 110
rlm@41 111 private Vector3f[] path = new Vector3f[]{
rlm@41 112 // loop 1
rlm@41 113 new Vector3f(0, 0, 0),
rlm@41 114 new Vector3f(0, 0, -10),
rlm@41 115 new Vector3f(-2, 0, -14),
rlm@41 116 new Vector3f(-6, 0, -20),
rlm@41 117 new Vector3f(0, 0, -26),
rlm@41 118 new Vector3f(6, 0, -20),
rlm@41 119 new Vector3f(0, 0, -14),
rlm@41 120 new Vector3f(-6, 0, -20),
rlm@41 121 new Vector3f(0, 0, -26),
rlm@41 122 new Vector3f(6, 0, -20),
rlm@41 123 // loop 2
rlm@41 124 new Vector3f(5, 0, -5),
rlm@41 125 new Vector3f(7, 0, 1.5f),
rlm@41 126 new Vector3f(14, 0, 2),
rlm@41 127 new Vector3f(20, 0, 6),
rlm@41 128 new Vector3f(26, 0, 0),
rlm@41 129 new Vector3f(20, 0, -6),
rlm@41 130 new Vector3f(14, 0, 0),
rlm@41 131 new Vector3f(20, 0, 6),
rlm@41 132 new Vector3f(26, 0, 0),
rlm@41 133 new Vector3f(20, 0, -6),
rlm@41 134 new Vector3f(14, 0, 0),
rlm@41 135 // loop 3
rlm@41 136 new Vector3f(8, 0, 7.5f),
rlm@41 137 new Vector3f(7, 0, 10.5f),
rlm@41 138 new Vector3f(6, 0, 20),
rlm@41 139 new Vector3f(0, 0, 26),
rlm@41 140 new Vector3f(-6, 0, 20),
rlm@41 141 new Vector3f(0, 0, 14),
rlm@41 142 new Vector3f(6, 0, 20),
rlm@41 143 new Vector3f(0, 0, 26),
rlm@41 144 new Vector3f(-6, 0, 20),
rlm@41 145 new Vector3f(0, 0, 14),
rlm@41 146 // begin ellipse
rlm@41 147 new Vector3f(16, 5, 20),
rlm@41 148 new Vector3f(0, 0, 26),
rlm@41 149 new Vector3f(-16, -10, 20),
rlm@41 150 new Vector3f(0, 0, 14),
rlm@41 151 new Vector3f(16, 20, 20),
rlm@41 152 new Vector3f(0, 0, 26),
rlm@41 153 new Vector3f(-10, -25, 10),
rlm@41 154 new Vector3f(-10, 0, 0),
rlm@41 155 // come at me!
rlm@41 156 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
rlm@41 157 new Vector3f(0, 0 , -20),
rlm@41 158 };
rlm@41 159
rlm@41 160 private void createScene() {
rlm@41 161 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@41 162 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
rlm@41 163 mat.setColor("Color", ColorRGBA.Blue);
rlm@41 164 bell.setMaterial(mat);
rlm@41 165 rootNode.attachChild(bell);
rlm@41 166
rlm@41 167 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
rlm@41 168 ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20));
rlm@41 169 ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
rlm@41 170
rlm@41 171 MotionPath track = new MotionPath();
rlm@41 172
rlm@41 173 for (Vector3f v : path){
rlm@41 174 track.addWayPoint(v);
rlm@41 175 }
rlm@41 176 track.setCurveTension(0.80f);
rlm@41 177
rlm@41 178 motionControl = new MotionTrack(bell,track);
rlm@42 179
rlm@42 180 // for now, use reflection to change the timer...
rlm@49 181 // motionControl.setTimer(new IsoTimer(60));
rlm@42 182 try {
rlm@42 183 Field timerField;
rlm@42 184 timerField = AbstractCinematicEvent.class.getDeclaredField("timer");
rlm@42 185 timerField.setAccessible(true);
rlm@42 186 try {timerField.set(motionControl, new IsoTimer(60));}
rlm@42 187 catch (IllegalArgumentException e) {e.printStackTrace();}
rlm@42 188 catch (IllegalAccessException e) {e.printStackTrace();}
rlm@42 189 }
rlm@42 190 catch (SecurityException e) {e.printStackTrace();}
rlm@42 191 catch (NoSuchFieldException e) {e.printStackTrace();}
rlm@42 192
rlm@41 193 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
rlm@41 194 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
rlm@41 195 motionControl.setInitialDuration(20f);
rlm@41 196 motionControl.setSpeed(1f);
rlm@41 197
rlm@41 198 track.enableDebugShape(assetManager, rootNode);
rlm@41 199 positionCamera();
rlm@41 200 }
rlm@41 201
rlm@41 202
rlm@41 203 private void positionCamera(){
rlm@41 204 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
rlm@41 205 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
rlm@41 206 }
rlm@41 207
rlm@41 208 private void initAudio() {
rlm@41 209 org.lwjgl.input.Mouse.setGrabbed(false);
rlm@41 210 music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false);
rlm@41 211
rlm@41 212 rootNode.attachChild(music);
rlm@41 213 audioRenderer.playSource(music);
rlm@41 214 music.setPositional(true);
rlm@41 215 music.setVolume(1f);
rlm@41 216 music.setReverbEnabled(false);
rlm@41 217 music.setDirectional(false);
rlm@41 218 music.setMaxDistance(200.0f);
rlm@41 219 music.setRefDistance(1f);
rlm@44 220 //music.setRolloffFactor(1f);
rlm@41 221 music.setLooping(false);
rlm@41 222 audioRenderer.pauseSource(music);
rlm@41 223 }
rlm@41 224
rlm@41 225 public class Dancer implements SoundProcessor {
rlm@41 226 Geometry entity;
rlm@41 227 float scale = 2;
rlm@41 228 public Dancer(Geometry entity){
rlm@41 229 this.entity = entity;
rlm@41 230 }
rlm@41 231
rlm@41 232 /**
rlm@41 233 * this method is irrelevant since there is no state to cleanup.
rlm@41 234 */
rlm@41 235 public void cleanup() {}
rlm@41 236
rlm@41 237
rlm@41 238 /**
rlm@41 239 * Respond to sound! This is the brain of an AI entity that
rlm@41 240 * hears it's surroundings and reacts to them.
rlm@41 241 */
rlm@41 242 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@41 243 audioSamples.clear();
rlm@41 244 byte[] data = new byte[numSamples];
rlm@41 245 float[] out = new float[numSamples];
rlm@41 246 audioSamples.get(data);
rlm@41 247 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
rlm@41 248 numSamples/format.getFrameSize(), format);
rlm@41 249
rlm@41 250 float max = Float.NEGATIVE_INFINITY;
rlm@41 251 for (float f : out){if (f > max) max = f;}
rlm@41 252 audioSamples.clear();
rlm@41 253
rlm@41 254 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}
rlm@41 255 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}
rlm@41 256 }
rlm@41 257 }
rlm@41 258
rlm@41 259 private void prepareEar(Geometry ear, int n){
rlm@41 260 if (this.audioRenderer instanceof MultiListener){
rlm@41 261 MultiListener rf = (MultiListener)this.audioRenderer;
rlm@41 262
rlm@41 263 Listener auxListener = new Listener();
rlm@41 264 auxListener.setLocation(ear.getLocalTranslation());
rlm@41 265
rlm@41 266 rf.addListener(auxListener);
rlm@41 267 WaveFileWriter aux = null;
rlm@41 268
rlm@41 269 try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));}
rlm@41 270 catch (FileNotFoundException e) {e.printStackTrace();}
rlm@41 271
rlm@41 272 rf.registerSoundProcessor(auxListener,
rlm@41 273 new CompositeSoundProcessor(new Dancer(ear), aux));
rlm@41 274 }
rlm@41 275 }
rlm@41 276
rlm@41 277
rlm@41 278 public void simpleInitApp() {
rlm@41 279 this.setTimer(new IsoTimer(60));
rlm@41 280 initAudio();
rlm@41 281
rlm@41 282 createScene();
rlm@41 283
rlm@41 284 prepareEar(ear1, 1);
rlm@41 285 prepareEar(ear2, 1);
rlm@41 286 prepareEar(ear3, 1);
rlm@41 287
rlm@41 288 motionControl.play();
rlm@41 289 }
rlm@41 290
rlm@41 291 public void simpleUpdate(float tpf) {
rlm@41 292 if (music.getStatus() != AudioNode.Status.Playing){
rlm@41 293 music.play();
rlm@41 294 }
rlm@41 295 Vector3f loc = cam.getLocation();
rlm@41 296 Quaternion rot = cam.getRotation();
rlm@41 297 listener.setLocation(loc);
rlm@41 298 listener.setRotation(rot);
rlm@41 299 music.setLocalTranslation(bell.getLocalTranslation());
rlm@41 300 }
rlm@41 301
rlm@41 302 }