Mercurial > jmeCapture
diff src/com/aurellem/capture/examples/Advanced.java @ 65:23e3df41db3c
reformatting for web
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 11 Feb 2012 12:25:26 -0700 |
parents | f5e52169f056 |
children |
line wrap: on
line diff
1.1 --- a/src/com/aurellem/capture/examples/Advanced.java Fri Feb 10 04:07:05 2012 -0700 1.2 +++ b/src/com/aurellem/capture/examples/Advanced.java Sat Feb 11 12:25:26 2012 -0700 1.3 @@ -54,253 +54,265 @@ 1.4 1.5 public class Advanced extends SimpleApplication { 1.6 1.7 - /** 1.8 - * You will see three grey cubes, a blue sphere, and a path which 1.9 - * circles each cube. The blue sphere is generating a constant 1.10 - * monotone sound as it moves along the track. Each cube is 1.11 - * listening for sound; when a cube hears sound whose intensity is 1.12 - * greater than a certain threshold, it changes its color from 1.13 - * grey to green. 1.14 - * 1.15 - * Each cube is also saving whatever it hears to a file. The 1.16 - * scene from the perspective of the viewer is also saved to a 1.17 - * video file. When you listen to each of the sound files 1.18 - * alongside the video, the sound will get louder when the sphere 1.19 - * approaches the cube that generated that sound file. This 1.20 - * shows that each listener is hearing the world from its own 1.21 - * perspective. 1.22 - * 1.23 - */ 1.24 - public static void main(String[] args) { 1.25 - Advanced app = new Advanced(); 1.26 - AppSettings settings = new AppSettings(true); 1.27 - settings.setAudioRenderer(AurellemSystemDelegate.SEND); 1.28 - JmeSystem.setSystemDelegate(new AurellemSystemDelegate()); 1.29 - app.setSettings(settings); 1.30 - app.setShowSettings(false); 1.31 - app.setPauseOnLostFocus(false); 1.32 + /** 1.33 + * You will see three grey cubes, a blue sphere, and a path which 1.34 + * circles each cube. The blue sphere is generating a constant 1.35 + * monotone sound as it moves along the track. Each cube is 1.36 + * listening for sound; when a cube hears sound whose intensity is 1.37 + * greater than a certain threshold, it changes its color from 1.38 + * grey to green. 1.39 + * 1.40 + * Each cube is also saving whatever it hears to a file. The 1.41 + * scene from the perspective of the viewer is also saved to a 1.42 + * video file. When you listen to each of the sound files 1.43 + * alongside the video, the sound will get louder when the sphere 1.44 + * approaches the cube that generated that sound file. This 1.45 + * shows that each listener is hearing the world from its own 1.46 + * perspective. 1.47 + * 1.48 + */ 1.49 + public static void main(String[] args) { 1.50 + Advanced app = new Advanced(); 1.51 + AppSettings settings = new AppSettings(true); 1.52 + settings.setAudioRenderer(AurellemSystemDelegate.SEND); 1.53 + JmeSystem.setSystemDelegate(new AurellemSystemDelegate()); 1.54 + app.setSettings(settings); 1.55 + app.setShowSettings(false); 1.56 + app.setPauseOnLostFocus(false); 1.57 1.58 - try { 1.59 - //Capture.captureVideo(app, File.createTempFile("advanced",".avi")); 1.60 - Capture.captureAudio(app, File.createTempFile("advanced", ".wav")); 1.61 - } 1.62 - catch (IOException e) {e.printStackTrace();} 1.63 + try { 1.64 + //Capture.captureVideo(app, File.createTempFile("advanced",".avi")); 1.65 + Capture.captureAudio(app, File.createTempFile("advanced",".wav")); 1.66 + } 1.67 + catch (IOException e) {e.printStackTrace();} 1.68 1.69 - app.start(); 1.70 + app.start(); 1.71 + } 1.72 + 1.73 + private Geometry bell; 1.74 + private Geometry ear1; 1.75 + private Geometry ear2; 1.76 + private Geometry ear3; 1.77 + private AudioNode music; 1.78 + private MotionTrack motionControl; 1.79 + private IsoTimer motionTimer = new IsoTimer(60); 1.80 + 1.81 + private Geometry makeEar(Node root, Vector3f position){ 1.82 + Material mat = new Material(assetManager, 1.83 + "Common/MatDefs/Misc/Unshaded.j3md"); 1.84 + Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f)); 1.85 + ear.setLocalTranslation(position); 1.86 + mat.setColor("Color", ColorRGBA.Green); 1.87 + ear.setMaterial(mat); 1.88 + root.attachChild(ear); 1.89 + return ear; 1.90 + } 1.91 + 1.92 + private Vector3f[] path = new Vector3f[]{ 1.93 + // loop 1 1.94 + new Vector3f(0, 0, 0), 1.95 + new Vector3f(0, 0, -10), 1.96 + new Vector3f(-2, 0, -14), 1.97 + new Vector3f(-6, 0, -20), 1.98 + new Vector3f(0, 0, -26), 1.99 + new Vector3f(6, 0, -20), 1.100 + new Vector3f(0, 0, -14), 1.101 + new Vector3f(-6, 0, -20), 1.102 + new Vector3f(0, 0, -26), 1.103 + new Vector3f(6, 0, -20), 1.104 + // loop 2 1.105 + new Vector3f(5, 0, -5), 1.106 + new Vector3f(7, 0, 1.5f), 1.107 + new Vector3f(14, 0, 2), 1.108 + new Vector3f(20, 0, 6), 1.109 + new Vector3f(26, 0, 0), 1.110 + new Vector3f(20, 0, -6), 1.111 + new Vector3f(14, 0, 0), 1.112 + new Vector3f(20, 0, 6), 1.113 + new Vector3f(26, 0, 0), 1.114 + new Vector3f(20, 0, -6), 1.115 + new Vector3f(14, 0, 0), 1.116 + // loop 3 1.117 + new Vector3f(8, 0, 7.5f), 1.118 + new Vector3f(7, 0, 10.5f), 1.119 + new Vector3f(6, 0, 20), 1.120 + new Vector3f(0, 0, 26), 1.121 + new Vector3f(-6, 0, 20), 1.122 + new Vector3f(0, 0, 14), 1.123 + new Vector3f(6, 0, 20), 1.124 + new Vector3f(0, 0, 26), 1.125 + new Vector3f(-6, 0, 20), 1.126 + new Vector3f(0, 0, 14), 1.127 + // begin ellipse 1.128 + new Vector3f(16, 5, 20), 1.129 + new Vector3f(0, 0, 26), 1.130 + new Vector3f(-16, -10, 20), 1.131 + new Vector3f(0, 0, 14), 1.132 + new Vector3f(16, 20, 20), 1.133 + new Vector3f(0, 0, 26), 1.134 + new Vector3f(-10, -25, 10), 1.135 + new Vector3f(-10, 0, 0), 1.136 + // come at me! 1.137 + new Vector3f(-28.00242f, 48.005623f, -34.648228f), 1.138 + new Vector3f(0, 0 , -20), 1.139 + }; 1.140 + 1.141 + private void createScene() { 1.142 + Material mat = new Material(assetManager, 1.143 + "Common/MatDefs/Misc/Unshaded.j3md"); 1.144 + bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); 1.145 + mat.setColor("Color", ColorRGBA.Blue); 1.146 + bell.setMaterial(mat); 1.147 + rootNode.attachChild(bell); 1.148 + 1.149 + ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20)); 1.150 + ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20)); 1.151 + ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0)); 1.152 + 1.153 + MotionPath track = new MotionPath(); 1.154 + 1.155 + for (Vector3f v : path){ 1.156 + track.addWayPoint(v); 1.157 + } 1.158 + track.setCurveTension(0.80f); 1.159 + 1.160 + motionControl = new MotionTrack(bell,track); 1.161 + // for now, use reflection to change the timer... 1.162 + // motionControl.setTimer(new IsoTimer(60)); 1.163 + 1.164 + try { 1.165 + Field timerField; 1.166 + timerField = 1.167 + AbstractCinematicEvent.class.getDeclaredField("timer"); 1.168 + timerField.setAccessible(true); 1.169 + try {timerField.set(motionControl, motionTimer);} 1.170 + catch (IllegalArgumentException e) {e.printStackTrace();} 1.171 + catch (IllegalAccessException e) {e.printStackTrace();} 1.172 + } 1.173 + catch (SecurityException e) {e.printStackTrace();} 1.174 + catch (NoSuchFieldException e) {e.printStackTrace();} 1.175 + 1.176 + 1.177 + motionControl.setDirectionType 1.178 + (MotionTrack.Direction.PathAndRotation); 1.179 + motionControl.setRotation 1.180 + (new Quaternion().fromAngleNormalAxis 1.181 + (-FastMath.HALF_PI, Vector3f.UNIT_Y)); 1.182 + motionControl.setInitialDuration(20f); 1.183 + motionControl.setSpeed(1f); 1.184 + 1.185 + track.enableDebugShape(assetManager, rootNode); 1.186 + positionCamera(); 1.187 + } 1.188 + 1.189 + private void positionCamera(){ 1.190 + this.cam.setLocation 1.191 + (new Vector3f(-28.00242f, 48.005623f, -34.648228f)); 1.192 + this.cam.setRotation 1.193 + (new Quaternion 1.194 + (0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f)); 1.195 + } 1.196 + 1.197 + private void initAudio() { 1.198 + org.lwjgl.input.Mouse.setGrabbed(false); 1.199 + music = new AudioNode(assetManager, 1.200 + "Sound/Effects/Beep.ogg", false); 1.201 + rootNode.attachChild(music); 1.202 + audioRenderer.playSource(music); 1.203 + music.setPositional(true); 1.204 + music.setVolume(1f); 1.205 + music.setReverbEnabled(false); 1.206 + music.setDirectional(false); 1.207 + music.setMaxDistance(200.0f); 1.208 + music.setRefDistance(1f); 1.209 + //music.setRolloffFactor(1f); 1.210 + music.setLooping(false); 1.211 + audioRenderer.pauseSource(music); 1.212 + } 1.213 + 1.214 + public class Dancer implements SoundProcessor { 1.215 + Geometry entity; 1.216 + float scale = 2; 1.217 + public Dancer(Geometry entity){ 1.218 + this.entity = entity; 1.219 } 1.220 1.221 + /** 1.222 + * this method is irrelevant since there is no state to cleanup. 1.223 + */ 1.224 + public void cleanup() {} 1.225 1.226 - private Geometry bell; 1.227 - private Geometry ear1; 1.228 - private Geometry ear2; 1.229 - private Geometry ear3; 1.230 - private AudioNode music; 1.231 - private MotionTrack motionControl; 1.232 - private IsoTimer motionTimer = new IsoTimer(60); 1.233 1.234 - private Geometry makeEar(Node root, Vector3f position){ 1.235 - Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 1.236 - Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f)); 1.237 - ear.setLocalTranslation(position); 1.238 - mat.setColor("Color", ColorRGBA.Green); 1.239 - ear.setMaterial(mat); 1.240 - root.attachChild(ear); 1.241 - return ear; 1.242 - } 1.243 + /** 1.244 + * Respond to sound! This is the brain of an AI entity that 1.245 + * hears its surroundings and reacts to them. 1.246 + */ 1.247 + public void process(ByteBuffer audioSamples, 1.248 + int numSamples, AudioFormat format) { 1.249 + audioSamples.clear(); 1.250 + byte[] data = new byte[numSamples]; 1.251 + float[] out = new float[numSamples]; 1.252 + audioSamples.get(data); 1.253 + FloatSampleTools. 1.254 + byte2floatInterleaved 1.255 + (data, 0, out, 0, numSamples/format.getFrameSize(), format); 1.256 1.257 - private Vector3f[] path = new Vector3f[]{ 1.258 - // loop 1 1.259 - new Vector3f(0, 0, 0), 1.260 - new Vector3f(0, 0, -10), 1.261 - new Vector3f(-2, 0, -14), 1.262 - new Vector3f(-6, 0, -20), 1.263 - new Vector3f(0, 0, -26), 1.264 - new Vector3f(6, 0, -20), 1.265 - new Vector3f(0, 0, -14), 1.266 - new Vector3f(-6, 0, -20), 1.267 - new Vector3f(0, 0, -26), 1.268 - new Vector3f(6, 0, -20), 1.269 - // loop 2 1.270 - new Vector3f(5, 0, -5), 1.271 - new Vector3f(7, 0, 1.5f), 1.272 - new Vector3f(14, 0, 2), 1.273 - new Vector3f(20, 0, 6), 1.274 - new Vector3f(26, 0, 0), 1.275 - new Vector3f(20, 0, -6), 1.276 - new Vector3f(14, 0, 0), 1.277 - new Vector3f(20, 0, 6), 1.278 - new Vector3f(26, 0, 0), 1.279 - new Vector3f(20, 0, -6), 1.280 - new Vector3f(14, 0, 0), 1.281 - // loop 3 1.282 - new Vector3f(8, 0, 7.5f), 1.283 - new Vector3f(7, 0, 10.5f), 1.284 - new Vector3f(6, 0, 20), 1.285 - new Vector3f(0, 0, 26), 1.286 - new Vector3f(-6, 0, 20), 1.287 - new Vector3f(0, 0, 14), 1.288 - new Vector3f(6, 0, 20), 1.289 - new Vector3f(0, 0, 26), 1.290 - new Vector3f(-6, 0, 20), 1.291 - new Vector3f(0, 0, 14), 1.292 - // begin ellipse 1.293 - new Vector3f(16, 5, 20), 1.294 - new Vector3f(0, 0, 26), 1.295 - new Vector3f(-16, -10, 20), 1.296 - new Vector3f(0, 0, 14), 1.297 - new Vector3f(16, 20, 20), 1.298 - new Vector3f(0, 0, 26), 1.299 - new Vector3f(-10, -25, 10), 1.300 - new Vector3f(-10, 0, 0), 1.301 - // come at me! 1.302 - new Vector3f(-28.00242f, 48.005623f, -34.648228f), 1.303 - new Vector3f(0, 0 , -20), 1.304 - }; 1.305 + float max = Float.NEGATIVE_INFINITY; 1.306 + for (float f : out){if (f > max) max = f;} 1.307 + audioSamples.clear(); 1.308 1.309 - private void createScene() { 1.310 - Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 1.311 - bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); 1.312 - mat.setColor("Color", ColorRGBA.Blue); 1.313 - bell.setMaterial(mat); 1.314 - rootNode.attachChild(bell); 1.315 + if (max > 0.1){ 1.316 + entity.getMaterial().setColor("Color", ColorRGBA.Green); 1.317 + } 1.318 + else { 1.319 + entity.getMaterial().setColor("Color", ColorRGBA.Gray); 1.320 + } 1.321 + } 1.322 + } 1.323 1.324 - ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20)); 1.325 - ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20)); 1.326 - ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0)); 1.327 + private void prepareEar(Geometry ear, int n){ 1.328 + if (this.audioRenderer instanceof MultiListener){ 1.329 + MultiListener rf = (MultiListener)this.audioRenderer; 1.330 1.331 - MotionPath track = new MotionPath(); 1.332 + Listener auxListener = new Listener(); 1.333 + auxListener.setLocation(ear.getLocalTranslation()); 1.334 1.335 - for (Vector3f v : path){ 1.336 - track.addWayPoint(v); 1.337 - } 1.338 - track.setCurveTension(0.80f); 1.339 + rf.addListener(auxListener); 1.340 + WaveFileWriter aux = null; 1.341 1.342 - motionControl = new MotionTrack(bell,track); 1.343 - // for now, use reflection to change the timer... 1.344 - // motionControl.setTimer(new IsoTimer(60)); 1.345 - 1.346 - try { 1.347 - Field timerField; 1.348 - timerField = AbstractCinematicEvent.class.getDeclaredField("timer"); 1.349 - timerField.setAccessible(true); 1.350 - try {timerField.set(motionControl, motionTimer);} 1.351 - catch (IllegalArgumentException e) {e.printStackTrace();} 1.352 - catch (IllegalAccessException e) {e.printStackTrace();} 1.353 - } 1.354 - catch (SecurityException e) {e.printStackTrace();} 1.355 - catch (NoSuchFieldException e) {e.printStackTrace();} 1.356 + try { 1.357 + aux = new WaveFileWriter 1.358 + (File.createTempFile("advanced-audio-" + n, ".wav"));} 1.359 + catch (IOException e) {e.printStackTrace();} 1.360 1.361 + rf.registerSoundProcessor 1.362 + (auxListener, 1.363 + new CompositeSoundProcessor(new Dancer(ear), aux)); 1.364 + } 1.365 + } 1.366 1.367 - motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation); 1.368 - motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y)); 1.369 - motionControl.setInitialDuration(20f); 1.370 - motionControl.setSpeed(1f); 1.371 + public void simpleInitApp() { 1.372 + this.setTimer(new IsoTimer(60)); 1.373 + initAudio(); 1.374 1.375 - track.enableDebugShape(assetManager, rootNode); 1.376 - positionCamera(); 1.377 + createScene(); 1.378 + 1.379 + prepareEar(ear1, 1); 1.380 + prepareEar(ear2, 1); 1.381 + prepareEar(ear3, 1); 1.382 + 1.383 + motionControl.play(); 1.384 + } 1.385 + 1.386 + public void simpleUpdate(float tpf) { 1.387 + motionTimer.update(); 1.388 + if (music.getStatus() != AudioNode.Status.Playing){ 1.389 + music.play(); 1.390 } 1.391 - 1.392 - 1.393 - private void positionCamera(){ 1.394 - this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f)); 1.395 - this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f)); 1.396 - } 1.397 - 1.398 - private void initAudio() { 1.399 - org.lwjgl.input.Mouse.setGrabbed(false); 1.400 - music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false); 1.401 - 1.402 - rootNode.attachChild(music); 1.403 - audioRenderer.playSource(music); 1.404 - music.setPositional(true); 1.405 - music.setVolume(1f); 1.406 - music.setReverbEnabled(false); 1.407 - music.setDirectional(false); 1.408 - music.setMaxDistance(200.0f); 1.409 - music.setRefDistance(1f); 1.410 - //music.setRolloffFactor(1f); 1.411 - music.setLooping(false); 1.412 - audioRenderer.pauseSource(music); 1.413 - } 1.414 - 1.415 - public class Dancer implements SoundProcessor { 1.416 - Geometry entity; 1.417 - float scale = 2; 1.418 - public Dancer(Geometry entity){ 1.419 - this.entity = entity; 1.420 - } 1.421 - 1.422 - /** 1.423 - * this method is irrelevant since there is no state to cleanup. 1.424 - */ 1.425 - public void cleanup() {} 1.426 - 1.427 - 1.428 - /** 1.429 - * Respond to sound! This is the brain of an AI entity that 1.430 - * hears its surroundings and reacts to them. 1.431 - */ 1.432 - public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) { 1.433 - audioSamples.clear(); 1.434 - byte[] data = new byte[numSamples]; 1.435 - float[] out = new float[numSamples]; 1.436 - audioSamples.get(data); 1.437 - FloatSampleTools.byte2floatInterleaved(data, 0, out, 0, 1.438 - numSamples/format.getFrameSize(), format); 1.439 - 1.440 - float max = Float.NEGATIVE_INFINITY; 1.441 - for (float f : out){if (f > max) max = f;} 1.442 - audioSamples.clear(); 1.443 - 1.444 - if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);} 1.445 - else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);} 1.446 - } 1.447 - } 1.448 - 1.449 - private void prepareEar(Geometry ear, int n){ 1.450 - if (this.audioRenderer instanceof MultiListener){ 1.451 - MultiListener rf = (MultiListener)this.audioRenderer; 1.452 - 1.453 - Listener auxListener = new Listener(); 1.454 - auxListener.setLocation(ear.getLocalTranslation()); 1.455 - 1.456 - rf.addListener(auxListener); 1.457 - WaveFileWriter aux = null; 1.458 - 1.459 - try {aux = new WaveFileWriter(File.createTempFile("advanced-audio-" + n, ".wav"));} 1.460 - catch (IOException e) {e.printStackTrace();} 1.461 - 1.462 - rf.registerSoundProcessor(auxListener, 1.463 - new CompositeSoundProcessor(new Dancer(ear), aux)); 1.464 - 1.465 - } 1.466 - } 1.467 - 1.468 - 1.469 - public void simpleInitApp() { 1.470 - this.setTimer(new IsoTimer(60)); 1.471 - initAudio(); 1.472 - 1.473 - createScene(); 1.474 - 1.475 - prepareEar(ear1, 1); 1.476 - prepareEar(ear2, 1); 1.477 - prepareEar(ear3, 1); 1.478 - 1.479 - motionControl.play(); 1.480 - 1.481 - } 1.482 - 1.483 - public void simpleUpdate(float tpf) { 1.484 - motionTimer.update(); 1.485 - if (music.getStatus() != AudioNode.Status.Playing){ 1.486 - music.play(); 1.487 - } 1.488 - Vector3f loc = cam.getLocation(); 1.489 - Quaternion rot = cam.getRotation(); 1.490 - listener.setLocation(loc); 1.491 - listener.setRotation(rot); 1.492 - music.setLocalTranslation(bell.getLocalTranslation()); 1.493 - } 1.494 - 1.495 + Vector3f loc = cam.getLocation(); 1.496 + Quaternion rot = cam.getRotation(); 1.497 + listener.setLocation(loc); 1.498 + listener.setRotation(rot); 1.499 + music.setLocalTranslation(bell.getLocalTranslation()); 1.500 + } 1.501 }