annotate src/com/aurellem/capture/examples/Advanced.java @ 64:155c70b7e6de

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