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