annotate src/com/aurellem/capture/examples/Advanced.java @ 67:30cea3f0f30a

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