view src/com/aurellem/capture/examples/AdvancedAudio.java @ 35:c969b04fa0dc

working on divergence
author Robert McIntyre <rlm@mit.edu>
date Mon, 31 Oct 2011 00:14:46 -0700
parents 13d354e1184b
children 2a525e937d86
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.nio.ByteBuffer;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
10 import javax.sound.sampled.AudioFormat;
12 import org.tritonus.share.sampled.FloatSampleTools;
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.AudioParam;
23 import com.jme3.audio.Listener;
24 import com.jme3.cinematic.MotionPath;
25 import com.jme3.cinematic.events.MotionTrack;
26 import com.jme3.input.controls.ActionListener;
27 import com.jme3.input.controls.MouseButtonTrigger;
28 import com.jme3.light.DirectionalLight;
29 import com.jme3.material.Material;
30 import com.jme3.math.ColorRGBA;
31 import com.jme3.math.FastMath;
32 import com.jme3.math.Quaternion;
33 import com.jme3.math.Vector3f;
34 import com.jme3.scene.Geometry;
35 import com.jme3.scene.Node;
36 import com.jme3.scene.Spatial;
37 import com.jme3.scene.shape.Box;
38 import com.jme3.scene.shape.Sphere;
39 import com.jme3.system.AppSettings;
42 /**
43 *
44 * Demonstrates advanced use of the audio capture and recording features.
45 * Multiple perspectives of the same scene are simultaneously rendered to
46 * different sound files.
47 *
48 * A key limitation of the way multiple listeners are implemented is that
49 * only 3D positioning effects are realized for listeners other than the
50 * main LWJGL listener. This means that audio effects such as environment
51 * settings will *not* be heard on any auxiliary listeners, though sound
52 * attenuation will work correctly.
53 *
54 * Multiple listeners as realized here might be used to make AI entities
55 * that can each hear the world from their own perspective.
56 *
57 * @author Robert McIntyre
58 *
59 */
61 public class AdvancedAudio extends SimpleApplication {
63 public static void main(String[] args) {
64 Logger.getLogger("com.jme3").setLevel(Level.OFF);
65 AdvancedAudio app = new AdvancedAudio();
66 AppSettings settings = new AppSettings(true);
67 settings.setAudioRenderer("Send");
68 app.setSettings(settings);
69 app.setShowSettings(false);
70 app.setPauseOnLostFocus(false);
71 org.lwjgl.input.Mouse.setGrabbed(false);
72 try {Capture.captureVideo(app, new File("/home/r/tmp/out.avi"));}
73 catch (IOException e) {e.printStackTrace();}
74 app.start();
75 }
77 private MotionTrack motionControl;
80 private Spatial makeEar(Node root, Vector3f position){
81 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
82 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
83 ear.setLocalTranslation(position);
84 mat.setColor("Color", ColorRGBA.Green);
85 ear.setMaterial(mat);
86 root.attachChild(ear);
87 return ear;
88 }
90 private Geometry bell;
92 private Spatial ear1;
93 //private Spatial ear2;
94 //private Spatial ear3;
95 //private Spatial ear4;
98 private Vector3f[] path = new Vector3f[]{
99 // loop 1
100 new Vector3f(0, 0, 0),
101 new Vector3f(0, 0, -10),
102 new Vector3f(-2, 0, -14),
103 new Vector3f(-6, 0, -20),
104 new Vector3f(0, 0, -26),
105 new Vector3f(6, 0, -20),
106 new Vector3f(0, 0, -14),
107 new Vector3f(-6, 0, -20),
108 new Vector3f(0, 0, -26),
109 new Vector3f(6, 0, -20),
110 // loop 2
111 new Vector3f(5, 0, -5),
112 new Vector3f(7, 0, 1.5f),
113 new Vector3f(14, 0, 2),
114 new Vector3f(20, 0, 6),
115 new Vector3f(26, 0, 0),
116 new Vector3f(20, 0, -6),
117 new Vector3f(14, 0, 0),
118 new Vector3f(20, 0, 6),
119 new Vector3f(26, 0, 0),
120 new Vector3f(20, 0, -6),
121 new Vector3f(14, 0, 0),
122 // loop 3
123 new Vector3f(8, 0, 7.5f),
124 new Vector3f(7, 0, 10.5f),
125 new Vector3f(6, 0, 20),
126 new Vector3f(0, 0, 26),
127 new Vector3f(-6, 0, 20),
128 new Vector3f(0, 0, 14),
129 new Vector3f(6, 0, 20),
130 new Vector3f(0, 0, 26),
131 new Vector3f(-6, 0, 20),
132 new Vector3f(0, 0, 14),
133 // begin ellipse
134 new Vector3f(16, 5, 20),
135 new Vector3f(0, 0, 26),
136 new Vector3f(-16, -10, 20),
137 new Vector3f(0, 0, 14),
138 new Vector3f(16, 20, 20),
139 new Vector3f(0, 0, 26),
140 new Vector3f(-10, -25, 10),
141 new Vector3f(-10, 0, 0),
142 // come at me bro!
143 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
144 new Vector3f(0, 0 , -20),
145 };
149 private void createScene() {
150 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
151 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
152 mat.setColor("Color", ColorRGBA.Blue);
153 bell.setMaterial(mat);
154 rootNode.attachChild(bell);
156 DirectionalLight light = new DirectionalLight();
157 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
158 light.setColor(ColorRGBA.White.mult(1.5f));
159 rootNode.addLight(light);
161 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,20));
162 //ear2 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
163 //ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
164 //ear4 = makeEar(rootNode, new Vector3f(-20, 0 ,0));
166 MotionPath track = new MotionPath();
168 for (Vector3f v : path){
169 track.addWayPoint(v);
170 }
173 track.setCurveTension(0.80f);
176 motionControl = new MotionTrack(bell,track);
177 motionControl.setTimer(new IsoTimer(60));
178 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
179 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
180 motionControl.setInitialDuration(20f);
181 motionControl.setSpeed(1f);
184 track.enableDebugShape(assetManager, rootNode);
187 positionCamera();
190 }
193 private void positionCamera(){
194 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
195 // cam.setLocation(new Vector3f(0,0,-20));
196 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
197 }
199 private AudioNode music;
205 private void initAudio() {
207 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
209 rootNode.attachChild(music);
210 //audioRenderer.playSource(music);
211 music.setPositional(true);
212 music.setVolume(1f);
213 music.setReverbEnabled(false);
214 music.setMaxDistance(200.0f);
215 music.setRefDistance(1f);
216 music.setRolloffFactor(5f);
219 //audioRenderer.pauseSource(music);
221 }
226 private Listener auxListener = new Listener();
227 //public File data1 = new File("/home/r/tmp/data1.wav");
228 //public File data2 = new File("/home/r/tmp/data2.wav");
229 //public File data3 = new File("/home/r/tmp/data3.wav");
230 //public File data4 = new File("/home/r/tmp/data4.wav");
231 //public File data5 = new File("/home/r/tmp/data5.wav");
232 //public File data6 = new File("/home/r/tmp/data6.wav");
235 public class Dancer implements SoundProcessor {
237 Spatial entity;
239 float scale = 2;
240 String debug;
241 public Dancer(Spatial entity, String debug){
242 this.entity = entity;
243 this.debug = debug;
244 }
246 /**
247 * this method is irrelevant since there is no state to cleanup.
248 */
249 public void cleanup() {}
252 /**
253 * Dance to the beat! This is the brain of an AI entity that
254 * hears it's surroundings and reacts to them.
255 */
256 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
257 audioSamples.clear();
258 byte[] data = new byte[numSamples];
259 float[] out = new float[numSamples];
260 audioSamples.get(data);
261 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
262 numSamples/format.getFrameSize(), format);
264 float max = Float.NEGATIVE_INFINITY;
265 for (float f : out){if (f > max) max = f;}
266 audioSamples.clear();
267 System.out.println(debug);
268 System.out.println(max);
272 //entity.scale(this.scale);
273 //if (this.scale == 2f){this.scale = 0.5f;}
274 //else {this.scale = 2;}
275 }
278 }
283 public void simpleInitApp() {
284 this.setTimer(new IsoTimer(60));
285 initAudio();
286 initKeys();
287 createScene();
288 listener.setLocation(ear1.getLocalTranslation());
289 if (this.audioRenderer instanceof MultiListener){
290 MultiListener rf = (MultiListener)this.audioRenderer;
294 auxListener = new Listener(listener);
296 rf.addListener(auxListener);
297 WaveFileWriter aux = null;
298 WaveFileWriter main = null;
301 try {aux = new WaveFileWriter(new File("/home/r/tmp/aux.wav"));}
302 catch (FileNotFoundException e) {e.printStackTrace();}
304 try {main = new WaveFileWriter(new File("/home/r/tmp/main.wav"));}
305 catch (FileNotFoundException e) {e.printStackTrace();}
307 rf.registerSoundProcessor(auxListener,
308 new CompositeSoundProcessor(new Dancer(ear1, "aux"), aux));
310 rf.registerSoundProcessor(
311 new CompositeSoundProcessor(new Dancer(ear1, "--------\nmain"), main));
312 }
314 motionControl.play();
315 }
321 private void initKeys() {
322 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
323 inputManager.addListener(actionListener, "Shoot");
324 }
326 /** Defining the "Shoot" action: Play a gun sound. */
327 private ActionListener actionListener = new ActionListener() {
328 @Override
329 public void onAction(String name, boolean keyPressed, float tpf) {
330 if (name.equals("Shoot") && !keyPressed) {
332 System.out.println(bell.getLocalTranslation().subtract(listener.getLocation()).length());
333 //bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
334 //audioRenderer.playSource(music);
335 System.out.println(music.getRefDistance());
337 }
338 }
339 };
341 /** Move the listener with the camera - for 3D audio. */
344 //private Vector3f prevBellPos = Vector3f.ZERO;
345 private int countdown = 0;
347 public void simpleUpdate(float tpf) {
348 //Vector3f loc = cam.getLocation();
349 //Quaternion rot = cam.getRotation();
350 //listener.setLocation(loc);
351 //listener.setRotation(rot);
352 System.out.println(countdown);
353 if (countdown++ == 14) { this.requestClose(false);}
355 System.out.println("channel "+ music.getChannel());
356 //listener.setLocation(cam.getLocation());
357 //listener.setRotation(cam.getRotation());
358 //auxListener.setLocation(loc);
359 //auxListener.setRotation(rot);
360 if (music.getStatus() != AudioNode.Status.Playing){
361 //audioRenderer.playSource(music);
362 music.play();
363 bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
364 System.out.println("I'm playing! <3");
365 }
366 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
368 //Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
369 //prevBellPos = bell.getLocalTranslation();
371 music.setLocalTranslation(bell.getLocalTranslation());
372 //music.setVelocity(bellVelocity);
374 //audioRenderer.updateSourceParam(music, AudioParam.Position);
375 //audioRenderer.updateSourceParam(music, AudioParam.Velocity);
378 //System.out.println("main:" + listener.getVolume());
379 //System.out.println("aux:" + auxListener.getVolume());
380 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
381 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
383 }
385 }