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