Mercurial > jmeCapture
view src/com/aurellem/capture/examples/Advanced.java @ 44:388f9d062012
adapting to execellent new jmeSystem changes
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Dec 2011 12:18:55 -0600 |
parents | 2f129118e2d6 |
children | 5cd9e6c65a93 |
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;8 import java.util.logging.Level;9 import java.util.logging.Logger;11 import javax.sound.sampled.AudioFormat;13 import org.tritonus.share.sampled.FloatSampleTools;15 import com.aurellem.capture.Capture;16 import com.aurellem.capture.IsoTimer;17 import com.aurellem.capture.audio.CompositeSoundProcessor;18 import com.aurellem.capture.audio.MultiListener;19 import com.aurellem.capture.audio.SoundProcessor;20 import com.aurellem.capture.audio.WaveFileWriter;21 import com.jme3.app.SimpleApplication;22 import com.jme3.audio.AudioNode;23 import com.jme3.audio.Listener;24 import com.jme3.cinematic.MotionPath;25 import com.jme3.cinematic.events.AbstractCinematicEvent;26 import com.jme3.cinematic.events.MotionTrack;27 import com.jme3.material.Material;28 import com.jme3.math.ColorRGBA;29 import com.jme3.math.FastMath;30 import com.jme3.math.Quaternion;31 import com.jme3.math.Vector3f;32 import com.jme3.scene.Geometry;33 import com.jme3.scene.Node;34 import com.jme3.scene.shape.Box;35 import com.jme3.scene.shape.Sphere;36 import com.jme3.system.AppSettings;37 import com.jme3.system.Natives;40 /**41 *42 * Demonstrates advanced use of the audio capture and recording features.43 * Multiple perspectives of the same scene are simultaneously rendered to44 * different sound files.45 *46 * A key limitation of the way multiple listeners are implemented is that47 * only 3D positioning effects are realized for listeners other than the48 * main LWJGL listener. This means that audio effects such as environment49 * settings will *not* be heard on any auxiliary listeners, though sound50 * attenuation will work correctly.51 *52 * Multiple listeners as realized here might be used to make AI entities53 * that can each hear the world from their own perspective.54 *55 * @author Robert McIntyre56 *57 */59 public class Advanced extends SimpleApplication {62 private Geometry bell;63 private Geometry ear1;64 private Geometry ear2;65 private Geometry ear3;66 private AudioNode music;67 private MotionTrack motionControl;69 public static void main(String[] args) {70 //Logger.getLogger("com.jme3").setLevel(Level.OFF);71 Advanced app = new Advanced();72 AppSettings settings = new AppSettings(true);73 settings.setAudioRenderer("Send");74 app.setSettings(settings);75 app.setShowSettings(false);76 app.setPauseOnLostFocus(false);80 try {81 Capture.captureVideo(app, File.createTempFile("advanced",".avi"));82 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));83 }84 catch (IOException e) {e.printStackTrace();}86 app.start();87 }89 public static void test(){90 Advanced app = new Advanced();91 AppSettings settings = new AppSettings(true);92 settings.setAudioRenderer("Send");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 }228 public class Dancer implements SoundProcessor {229 Geometry entity;230 float scale = 2;231 public Dancer(Geometry entity){232 this.entity = entity;233 }235 /**236 * this method is irrelevant since there is no state to cleanup.237 */238 public void cleanup() {}241 /**242 * Respond to sound! This is the brain of an AI entity that243 * hears it's surroundings and reacts to them.244 */245 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {246 audioSamples.clear();247 byte[] data = new byte[numSamples];248 float[] out = new float[numSamples];249 audioSamples.get(data);250 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,251 numSamples/format.getFrameSize(), format);253 float max = Float.NEGATIVE_INFINITY;254 for (float f : out){if (f > max) max = f;}255 audioSamples.clear();257 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}258 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}259 }260 }262 private void prepareEar(Geometry ear, int n){263 if (this.audioRenderer instanceof MultiListener){264 MultiListener rf = (MultiListener)this.audioRenderer;266 Listener auxListener = new Listener();267 auxListener.setLocation(ear.getLocalTranslation());269 rf.addListener(auxListener);270 WaveFileWriter aux = null;272 try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));}273 catch (FileNotFoundException e) {e.printStackTrace();}275 rf.registerSoundProcessor(auxListener,276 new CompositeSoundProcessor(new Dancer(ear), aux));278 }279 }282 public void simpleInitApp() {283 this.setTimer(new IsoTimer(60));284 initAudio();286 createScene();288 prepareEar(ear1, 1);289 prepareEar(ear2, 1);290 prepareEar(ear3, 1);292 motionControl.play();293 }295 public void simpleUpdate(float tpf) {296 if (music.getStatus() != AudioNode.Status.Playing){297 music.play();298 }299 Vector3f loc = cam.getLocation();300 Quaternion rot = cam.getRotation();301 listener.setLocation(loc);302 listener.setRotation(rot);303 music.setLocalTranslation(bell.getLocalTranslation());304 }306 }