annotate src/com/aurellem/capture/examples/Advanced.java @ 48:6ecfef90e9eb

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