annotate src/com/aurellem/capture/examples/Advanced.java @ 59:5afa49c5a7d3

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