view src/com/aurellem/capture/examples/Advanced.java @ 54:6484a820e27d

wrote main documentation.
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Dec 2011 19:15:43 -0600
parents d799a0278cc9
children afc437f637bd
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;
38 /**
39 *
40 * Demonstrates advanced use of the audio capture and recording features.
41 * Multiple perspectives of the same scene are simultaneously rendered to
42 * different sound files.
43 *
44 * A key limitation of the way multiple listeners are implemented is that
45 * only 3D positioning effects are realized for listeners other than the
46 * main LWJGL listener. This means that audio effects such as environment
47 * settings will *not* be heard on any auxiliary listeners, though sound
48 * attenuation will work correctly.
49 *
50 * Multiple listeners as realized here might be used to make AI entities
51 * that can each hear the world from their own perspective.
52 *
53 * @author Robert McIntyre
54 */
56 public class Advanced extends SimpleApplication {
58 /**
59 * You will see three grey cubes, a blue sphere, and a path
60 * which circles each cube. The blue sphere is generating a
61 * constant monotone sound as it moves along the track. Each
62 * cube is listening for sound; when a cube hears sound whose
63 * intensity is greater than a certain threshold, it changes
64 * its color from grey to green.
65 *
66 * Each cube is also saving whatever it hears to a file. The
67 * scene from the perspective of the viewer is also saved to
68 * a video file. When you listen to each of the sound files
69 * alongside the video, the sound will get louder when the
70 * sphere approaches the cube that generated that sound file.
71 * This shows that each listener is hearing the world from
72 * its own perspective.
73 *
74 */
75 public static void main(String[] args) {
76 Advanced app = new Advanced();
77 AppSettings settings = new AppSettings(true);
78 settings.setAudioRenderer(AurellemSystemDelegate.SEND);
79 JmeSystem.setSystemDelegate(new AurellemSystemDelegate());
80 app.setSettings(settings);
81 app.setShowSettings(false);
82 app.setPauseOnLostFocus(false);
84 try {
85 Capture.captureVideo(app, File.createTempFile("advanced",".avi"));
86 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));
87 }
88 catch (IOException e) {e.printStackTrace();}
90 app.start();
91 }
94 private Geometry bell;
95 private Geometry ear1;
96 private Geometry ear2;
97 private Geometry ear3;
98 private AudioNode music;
99 private MotionTrack motionControl;
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);
180 // for now, use reflection to change the timer...
181 // 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, new IsoTimer(60));}
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();}
193 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
194 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
195 motionControl.setInitialDuration(20f);
196 motionControl.setSpeed(1f);
198 track.enableDebugShape(assetManager, rootNode);
199 positionCamera();
200 }
203 private void positionCamera(){
204 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
205 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
206 }
208 private void initAudio() {
209 org.lwjgl.input.Mouse.setGrabbed(false);
210 music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false);
212 rootNode.attachChild(music);
213 audioRenderer.playSource(music);
214 music.setPositional(true);
215 music.setVolume(1f);
216 music.setReverbEnabled(false);
217 music.setDirectional(false);
218 music.setMaxDistance(200.0f);
219 music.setRefDistance(1f);
220 //music.setRolloffFactor(1f);
221 music.setLooping(false);
222 audioRenderer.pauseSource(music);
223 }
225 public class Dancer implements SoundProcessor {
226 Geometry entity;
227 float scale = 2;
228 public Dancer(Geometry entity){
229 this.entity = entity;
230 }
232 /**
233 * this method is irrelevant since there is no state to cleanup.
234 */
235 public void cleanup() {}
238 /**
239 * Respond to sound! This is the brain of an AI entity that
240 * hears it's surroundings and reacts to them.
241 */
242 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
243 audioSamples.clear();
244 byte[] data = new byte[numSamples];
245 float[] out = new float[numSamples];
246 audioSamples.get(data);
247 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
248 numSamples/format.getFrameSize(), format);
250 float max = Float.NEGATIVE_INFINITY;
251 for (float f : out){if (f > max) max = f;}
252 audioSamples.clear();
254 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}
255 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}
256 }
257 }
259 private void prepareEar(Geometry ear, int n){
260 if (this.audioRenderer instanceof MultiListener){
261 MultiListener rf = (MultiListener)this.audioRenderer;
263 Listener auxListener = new Listener();
264 auxListener.setLocation(ear.getLocalTranslation());
266 rf.addListener(auxListener);
267 WaveFileWriter aux = null;
269 try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));}
270 catch (FileNotFoundException e) {e.printStackTrace();}
272 rf.registerSoundProcessor(auxListener,
273 new CompositeSoundProcessor(new Dancer(ear), aux));
274 }
275 }
278 public void simpleInitApp() {
279 this.setTimer(new IsoTimer(60));
280 initAudio();
282 createScene();
284 prepareEar(ear1, 1);
285 prepareEar(ear2, 1);
286 prepareEar(ear3, 1);
288 motionControl.play();
289 }
291 public void simpleUpdate(float tpf) {
292 if (music.getStatus() != AudioNode.Status.Playing){
293 music.play();
294 }
295 Vector3f loc = cam.getLocation();
296 Quaternion rot = cam.getRotation();
297 listener.setLocation(loc);
298 listener.setRotation(rot);
299 music.setLocalTranslation(bell.getLocalTranslation());
300 }
302 }