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