view 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 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 }
92 private Geometry bell;
93 private Geometry ear1;
94 private Geometry ear2;
95 private Geometry ear3;
96 private AudioNode music;
97 private MotionTrack motionControl;
98 private IsoTimer motionTimer = new IsoTimer(60);
100 private Geometry makeEar(Node root, Vector3f position){
101 Material mat = new Material(assetManager,
102 "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,
162 "Common/MatDefs/Misc/Unshaded.j3md");
163 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
164 mat.setColor("Color", ColorRGBA.Blue);
165 bell.setMaterial(mat);
166 rootNode.attachChild(bell);
168 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
169 ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20));
170 ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
172 MotionPath track = new MotionPath();
174 for (Vector3f v : path){
175 track.addWayPoint(v);
176 }
177 track.setCurveTension(0.80f);
179 motionControl = new MotionTrack(bell,track);
180 // for now, use reflection to change the timer...
181 // motionControl.setTimer(new IsoTimer(60));
183 try {
184 Field timerField;
185 timerField =
186 AbstractCinematicEvent.class.getDeclaredField("timer");
187 timerField.setAccessible(true);
188 try {timerField.set(motionControl, motionTimer);}
189 catch (IllegalArgumentException e) {e.printStackTrace();}
190 catch (IllegalAccessException e) {e.printStackTrace();}
191 }
192 catch (SecurityException e) {e.printStackTrace();}
193 catch (NoSuchFieldException e) {e.printStackTrace();}
196 motionControl.setDirectionType
197 (MotionTrack.Direction.PathAndRotation);
198 motionControl.setRotation
199 (new Quaternion().fromAngleNormalAxis
200 (-FastMath.HALF_PI, Vector3f.UNIT_Y));
201 motionControl.setInitialDuration(20f);
202 motionControl.setSpeed(1f);
204 track.enableDebugShape(assetManager, rootNode);
205 positionCamera();
206 }
208 private void positionCamera(){
209 this.cam.setLocation
210 (new Vector3f(-28.00242f, 48.005623f, -34.648228f));
211 this.cam.setRotation
212 (new Quaternion
213 (0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
214 }
216 private void initAudio() {
217 org.lwjgl.input.Mouse.setGrabbed(false);
218 music = new AudioNode(assetManager,
219 "Sound/Effects/Beep.ogg", false);
220 rootNode.attachChild(music);
221 audioRenderer.playSource(music);
222 music.setPositional(true);
223 music.setVolume(1f);
224 music.setReverbEnabled(false);
225 music.setDirectional(false);
226 music.setMaxDistance(200.0f);
227 music.setRefDistance(1f);
228 //music.setRolloffFactor(1f);
229 music.setLooping(false);
230 audioRenderer.pauseSource(music);
231 }
233 public class Dancer implements SoundProcessor {
234 Geometry entity;
235 float scale = 2;
236 public Dancer(Geometry entity){
237 this.entity = entity;
238 }
240 /**
241 * this method is irrelevant since there is no state to cleanup.
242 */
243 public void cleanup() {}
246 /**
247 * Respond to sound! This is the brain of an AI entity that
248 * hears its surroundings and reacts to them.
249 */
250 public void process(ByteBuffer audioSamples,
251 int numSamples, AudioFormat format) {
252 audioSamples.clear();
253 byte[] data = new byte[numSamples];
254 float[] out = new float[numSamples];
255 audioSamples.get(data);
256 FloatSampleTools.
257 byte2floatInterleaved
258 (data, 0, out, 0, numSamples/format.getFrameSize(), format);
260 float max = Float.NEGATIVE_INFINITY;
261 for (float f : out){if (f > max) max = f;}
262 audioSamples.clear();
264 if (max > 0.1){
265 entity.getMaterial().setColor("Color", ColorRGBA.Green);
266 }
267 else {
268 entity.getMaterial().setColor("Color", ColorRGBA.Gray);
269 }
270 }
271 }
273 private void prepareEar(Geometry ear, int n){
274 if (this.audioRenderer instanceof MultiListener){
275 MultiListener rf = (MultiListener)this.audioRenderer;
277 Listener auxListener = new Listener();
278 auxListener.setLocation(ear.getLocalTranslation());
280 rf.addListener(auxListener);
281 WaveFileWriter aux = null;
283 try {
284 aux = new WaveFileWriter
285 (File.createTempFile("advanced-audio-" + n, ".wav"));}
286 catch (IOException e) {e.printStackTrace();}
288 rf.registerSoundProcessor
289 (auxListener,
290 new CompositeSoundProcessor(new Dancer(ear), aux));
291 }
292 }
294 public void simpleInitApp() {
295 this.setTimer(new IsoTimer(60));
296 initAudio();
298 createScene();
300 prepareEar(ear1, 1);
301 prepareEar(ear2, 1);
302 prepareEar(ear3, 1);
304 motionControl.play();
305 }
307 public void simpleUpdate(float tpf) {
308 motionTimer.update();
309 if (music.getStatus() != AudioNode.Status.Playing){
310 music.play();
311 }
312 Vector3f loc = cam.getLocation();
313 Quaternion rot = cam.getRotation();
314 listener.setLocation(loc);
315 listener.setRotation(rot);
316 music.setLocalTranslation(bell.getLocalTranslation());
317 }
318 }