Mercurial > jmeCapture
view src/com/aurellem/capture/examples/Advanced.java @ 45:5cd9e6c65a93
system is shaping up
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Dec 2011 12:44:23 -0600 |
parents | 388f9d062012 |
children | 6ecfef90e9eb |
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("Send");73 app.setSettings(settings);74 app.setShowSettings(false);75 app.setPauseOnLostFocus(false);79 try {80 Capture.captureVideo(app, File.createTempFile("advanced",".avi"));81 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));82 }83 catch (IOException e) {e.printStackTrace();}85 app.start();86 }88 public static void test(){89 Advanced app = new Advanced();90 AppSettings settings = new AppSettings(true);91 settings.setAudioRenderer("Send");92 JmeSystem.setSystemDelegate(new AurellemSystemDelegate());93 app.setSettings(settings);94 app.setShowSettings(false);95 app.setPauseOnLostFocus(false);96 app.start();97 }99 private Geometry makeEar(Node root, Vector3f position){100 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");101 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));102 ear.setLocalTranslation(position);103 mat.setColor("Color", ColorRGBA.Green);104 ear.setMaterial(mat);105 root.attachChild(ear);106 return ear;107 }109 private Vector3f[] path = new Vector3f[]{110 // loop 1111 new Vector3f(0, 0, 0),112 new Vector3f(0, 0, -10),113 new Vector3f(-2, 0, -14),114 new Vector3f(-6, 0, -20),115 new Vector3f(0, 0, -26),116 new Vector3f(6, 0, -20),117 new Vector3f(0, 0, -14),118 new Vector3f(-6, 0, -20),119 new Vector3f(0, 0, -26),120 new Vector3f(6, 0, -20),121 // loop 2122 new Vector3f(5, 0, -5),123 new Vector3f(7, 0, 1.5f),124 new Vector3f(14, 0, 2),125 new Vector3f(20, 0, 6),126 new Vector3f(26, 0, 0),127 new Vector3f(20, 0, -6),128 new Vector3f(14, 0, 0),129 new Vector3f(20, 0, 6),130 new Vector3f(26, 0, 0),131 new Vector3f(20, 0, -6),132 new Vector3f(14, 0, 0),133 // loop 3134 new Vector3f(8, 0, 7.5f),135 new Vector3f(7, 0, 10.5f),136 new Vector3f(6, 0, 20),137 new Vector3f(0, 0, 26),138 new Vector3f(-6, 0, 20),139 new Vector3f(0, 0, 14),140 new Vector3f(6, 0, 20),141 new Vector3f(0, 0, 26),142 new Vector3f(-6, 0, 20),143 new Vector3f(0, 0, 14),144 // begin ellipse145 new Vector3f(16, 5, 20),146 new Vector3f(0, 0, 26),147 new Vector3f(-16, -10, 20),148 new Vector3f(0, 0, 14),149 new Vector3f(16, 20, 20),150 new Vector3f(0, 0, 26),151 new Vector3f(-10, -25, 10),152 new Vector3f(-10, 0, 0),153 // come at me!154 new Vector3f(-28.00242f, 48.005623f, -34.648228f),155 new Vector3f(0, 0 , -20),156 };158 private void createScene() {159 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");160 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));161 mat.setColor("Color", ColorRGBA.Blue);162 bell.setMaterial(mat);163 rootNode.attachChild(bell);165 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));166 ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20));167 ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));169 MotionPath track = new MotionPath();171 for (Vector3f v : path){172 track.addWayPoint(v);173 }174 track.setCurveTension(0.80f);176 motionControl = new MotionTrack(bell,track);178 // for now, use reflection to change the timer...179 //motionControl.setTimer(new IsoTimer(60));180 try {181 Field timerField;182 timerField = AbstractCinematicEvent.class.getDeclaredField("timer");183 timerField.setAccessible(true);184 try {timerField.set(motionControl, new IsoTimer(60));}185 catch (IllegalArgumentException e) {e.printStackTrace();}186 catch (IllegalAccessException e) {e.printStackTrace();}187 }188 catch (SecurityException e) {e.printStackTrace();}189 catch (NoSuchFieldException e) {e.printStackTrace();}191 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);192 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));193 motionControl.setInitialDuration(20f);194 motionControl.setSpeed(1f);196 track.enableDebugShape(assetManager, rootNode);197 positionCamera();198 }201 private void positionCamera(){202 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));203 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));204 }206 private void initAudio() {207 org.lwjgl.input.Mouse.setGrabbed(false);208 music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false);210 rootNode.attachChild(music);211 audioRenderer.playSource(music);212 music.setPositional(true);213 music.setVolume(1f);214 music.setReverbEnabled(false);215 music.setDirectional(false);216 music.setMaxDistance(200.0f);217 music.setRefDistance(1f);218 //music.setRolloffFactor(1f);219 music.setLooping(false);220 audioRenderer.pauseSource(music);221 }223 public class Dancer implements SoundProcessor {224 Geometry entity;225 float scale = 2;226 public Dancer(Geometry entity){227 this.entity = entity;228 }230 /**231 * this method is irrelevant since there is no state to cleanup.232 */233 public void cleanup() {}236 /**237 * Respond to sound! This is the brain of an AI entity that238 * hears it's surroundings and reacts to them.239 */240 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {241 audioSamples.clear();242 byte[] data = new byte[numSamples];243 float[] out = new float[numSamples];244 audioSamples.get(data);245 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,246 numSamples/format.getFrameSize(), format);248 float max = Float.NEGATIVE_INFINITY;249 for (float f : out){if (f > max) max = f;}250 audioSamples.clear();252 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}253 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}254 }255 }257 private void prepareEar(Geometry ear, int n){258 if (this.audioRenderer instanceof MultiListener){259 MultiListener rf = (MultiListener)this.audioRenderer;261 Listener auxListener = new Listener();262 auxListener.setLocation(ear.getLocalTranslation());264 rf.addListener(auxListener);265 WaveFileWriter aux = null;267 try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));}268 catch (FileNotFoundException e) {e.printStackTrace();}270 rf.registerSoundProcessor(auxListener,271 new CompositeSoundProcessor(new Dancer(ear), aux));272 }273 }276 public void simpleInitApp() {277 this.setTimer(new IsoTimer(60));278 initAudio();280 createScene();282 prepareEar(ear1, 1);283 prepareEar(ear2, 1);284 prepareEar(ear3, 1);286 motionControl.play();287 }289 public void simpleUpdate(float tpf) {290 if (music.getStatus() != AudioNode.Status.Playing){291 music.play();292 }293 Vector3f loc = cam.getLocation();294 Quaternion rot = cam.getRotation();295 listener.setLocation(loc);296 listener.setRotation(rot);297 music.setLocalTranslation(bell.getLocalTranslation());298 }300 }