view src/com/aurellem/capture/examples/Advanced.java @ 63:df4a2d12ea13

added new timer for debugging purposes
author Robert McIntyre <rlm@mit.edu>
date Sun, 15 Jan 2012 04:10:37 -0700
parents f5e52169f056
children 23e3df41db3c
line wrap: on
line source
1 package com.aurellem.capture.examples;
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Field;
6 import java.nio.ByteBuffer;
8 import javax.sound.sampled.AudioFormat;
10 import org.tritonus.share.sampled.FloatSampleTools;
12 import com.aurellem.capture.AurellemSystemDelegate;
13 import com.aurellem.capture.Capture;
14 import com.aurellem.capture.IsoTimer;
15 import com.aurellem.capture.audio.CompositeSoundProcessor;
16 import com.aurellem.capture.audio.MultiListener;
17 import com.aurellem.capture.audio.SoundProcessor;
18 import com.aurellem.capture.audio.WaveFileWriter;
19 import com.jme3.app.SimpleApplication;
20 import com.jme3.audio.AudioNode;
21 import com.jme3.audio.Listener;
22 import com.jme3.cinematic.MotionPath;
23 import com.jme3.cinematic.events.AbstractCinematicEvent;
24 import com.jme3.cinematic.events.MotionTrack;
25 import com.jme3.material.Material;
26 import com.jme3.math.ColorRGBA;
27 import com.jme3.math.FastMath;
28 import com.jme3.math.Quaternion;
29 import com.jme3.math.Vector3f;
30 import com.jme3.scene.Geometry;
31 import com.jme3.scene.Node;
32 import com.jme3.scene.shape.Box;
33 import com.jme3.scene.shape.Sphere;
34 import com.jme3.system.AppSettings;
35 import com.jme3.system.JmeSystem;
37 /**
38 *
39 * Demonstrates advanced use of the audio capture and recording
40 * features. Multiple perspectives of the same scene are
41 * simultaneously rendered to different sound files.
42 *
43 * A key limitation of the way multiple listeners are implemented is
44 * that only 3D positioning effects are realized for listeners other
45 * than the main LWJGL listener. This means that audio effects such
46 * as environment settings will *not* be heard on any auxiliary
47 * listeners, though sound attenuation will work correctly.
48 *
49 * Multiple listeners as realized here might be used to make AI
50 * entities that can each hear the world from their own perspective.
51 *
52 * @author Robert McIntyre
53 */
55 public class Advanced extends SimpleApplication {
57 /**
58 * You will see three grey cubes, a blue sphere, and a path which
59 * circles each cube. The blue sphere is generating a constant
60 * monotone sound as it moves along the track. Each cube is
61 * listening for sound; when a cube hears sound whose intensity is
62 * greater than a certain threshold, it changes its color from
63 * grey to green.
64 *
65 * Each cube is also saving whatever it hears to a file. The
66 * scene from the perspective of the viewer is also saved to a
67 * video file. When you listen to each of the sound files
68 * alongside the video, the sound will get louder when the sphere
69 * approaches the cube that generated that sound file. This
70 * shows that each listener is hearing the world from its own
71 * perspective.
72 *
73 */
74 public static void main(String[] args) {
75 Advanced app = new Advanced();
76 AppSettings settings = new AppSettings(true);
77 settings.setAudioRenderer(AurellemSystemDelegate.SEND);
78 JmeSystem.setSystemDelegate(new AurellemSystemDelegate());
79 app.setSettings(settings);
80 app.setShowSettings(false);
81 app.setPauseOnLostFocus(false);
83 try {
84 //Capture.captureVideo(app, File.createTempFile("advanced",".avi"));
85 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));
86 }
87 catch (IOException e) {e.printStackTrace();}
89 app.start();
90 }
93 private Geometry bell;
94 private Geometry ear1;
95 private Geometry ear2;
96 private Geometry ear3;
97 private AudioNode music;
98 private MotionTrack motionControl;
99 private IsoTimer motionTimer = new IsoTimer(60);
101 private Geometry makeEar(Node root, Vector3f position){
102 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
103 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
104 ear.setLocalTranslation(position);
105 mat.setColor("Color", ColorRGBA.Green);
106 ear.setMaterial(mat);
107 root.attachChild(ear);
108 return ear;
109 }
111 private Vector3f[] path = new Vector3f[]{
112 // loop 1
113 new Vector3f(0, 0, 0),
114 new Vector3f(0, 0, -10),
115 new Vector3f(-2, 0, -14),
116 new Vector3f(-6, 0, -20),
117 new Vector3f(0, 0, -26),
118 new Vector3f(6, 0, -20),
119 new Vector3f(0, 0, -14),
120 new Vector3f(-6, 0, -20),
121 new Vector3f(0, 0, -26),
122 new Vector3f(6, 0, -20),
123 // loop 2
124 new Vector3f(5, 0, -5),
125 new Vector3f(7, 0, 1.5f),
126 new Vector3f(14, 0, 2),
127 new Vector3f(20, 0, 6),
128 new Vector3f(26, 0, 0),
129 new Vector3f(20, 0, -6),
130 new Vector3f(14, 0, 0),
131 new Vector3f(20, 0, 6),
132 new Vector3f(26, 0, 0),
133 new Vector3f(20, 0, -6),
134 new Vector3f(14, 0, 0),
135 // loop 3
136 new Vector3f(8, 0, 7.5f),
137 new Vector3f(7, 0, 10.5f),
138 new Vector3f(6, 0, 20),
139 new Vector3f(0, 0, 26),
140 new Vector3f(-6, 0, 20),
141 new Vector3f(0, 0, 14),
142 new Vector3f(6, 0, 20),
143 new Vector3f(0, 0, 26),
144 new Vector3f(-6, 0, 20),
145 new Vector3f(0, 0, 14),
146 // begin ellipse
147 new Vector3f(16, 5, 20),
148 new Vector3f(0, 0, 26),
149 new Vector3f(-16, -10, 20),
150 new Vector3f(0, 0, 14),
151 new Vector3f(16, 20, 20),
152 new Vector3f(0, 0, 26),
153 new Vector3f(-10, -25, 10),
154 new Vector3f(-10, 0, 0),
155 // come at me!
156 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
157 new Vector3f(0, 0 , -20),
158 };
160 private void createScene() {
161 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
162 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
163 mat.setColor("Color", ColorRGBA.Blue);
164 bell.setMaterial(mat);
165 rootNode.attachChild(bell);
167 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
168 ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20));
169 ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
171 MotionPath track = new MotionPath();
173 for (Vector3f v : path){
174 track.addWayPoint(v);
175 }
176 track.setCurveTension(0.80f);
178 motionControl = new MotionTrack(bell,track);
179 // for now, use reflection to change the timer...
180 // motionControl.setTimer(new IsoTimer(60));
182 try {
183 Field timerField;
184 timerField = AbstractCinematicEvent.class.getDeclaredField("timer");
185 timerField.setAccessible(true);
186 try {timerField.set(motionControl, motionTimer);}
187 catch (IllegalArgumentException e) {e.printStackTrace();}
188 catch (IllegalAccessException e) {e.printStackTrace();}
189 }
190 catch (SecurityException e) {e.printStackTrace();}
191 catch (NoSuchFieldException e) {e.printStackTrace();}
194 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
195 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
196 motionControl.setInitialDuration(20f);
197 motionControl.setSpeed(1f);
199 track.enableDebugShape(assetManager, rootNode);
200 positionCamera();
201 }
204 private void positionCamera(){
205 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
206 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
207 }
209 private void initAudio() {
210 org.lwjgl.input.Mouse.setGrabbed(false);
211 music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false);
213 rootNode.attachChild(music);
214 audioRenderer.playSource(music);
215 music.setPositional(true);
216 music.setVolume(1f);
217 music.setReverbEnabled(false);
218 music.setDirectional(false);
219 music.setMaxDistance(200.0f);
220 music.setRefDistance(1f);
221 //music.setRolloffFactor(1f);
222 music.setLooping(false);
223 audioRenderer.pauseSource(music);
224 }
226 public class Dancer implements SoundProcessor {
227 Geometry entity;
228 float scale = 2;
229 public Dancer(Geometry entity){
230 this.entity = entity;
231 }
233 /**
234 * this method is irrelevant since there is no state to cleanup.
235 */
236 public void cleanup() {}
239 /**
240 * Respond to sound! This is the brain of an AI entity that
241 * hears its surroundings and reacts to them.
242 */
243 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
244 audioSamples.clear();
245 byte[] data = new byte[numSamples];
246 float[] out = new float[numSamples];
247 audioSamples.get(data);
248 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
249 numSamples/format.getFrameSize(), format);
251 float max = Float.NEGATIVE_INFINITY;
252 for (float f : out){if (f > max) max = f;}
253 audioSamples.clear();
255 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}
256 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}
257 }
258 }
260 private void prepareEar(Geometry ear, int n){
261 if (this.audioRenderer instanceof MultiListener){
262 MultiListener rf = (MultiListener)this.audioRenderer;
264 Listener auxListener = new Listener();
265 auxListener.setLocation(ear.getLocalTranslation());
267 rf.addListener(auxListener);
268 WaveFileWriter aux = null;
270 try {aux = new WaveFileWriter(File.createTempFile("advanced-audio-" + n, ".wav"));}
271 catch (IOException e) {e.printStackTrace();}
273 rf.registerSoundProcessor(auxListener,
274 new CompositeSoundProcessor(new Dancer(ear), aux));
276 }
277 }
280 public void simpleInitApp() {
281 this.setTimer(new IsoTimer(60));
282 initAudio();
284 createScene();
286 prepareEar(ear1, 1);
287 prepareEar(ear2, 1);
288 prepareEar(ear3, 1);
290 motionControl.play();
292 }
294 public void simpleUpdate(float tpf) {
295 motionTimer.update();
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 }