view src/com/aurellem/capture/examples/AdvancedAudio.java @ 36:2a525e937d86

still debuging.
author Robert McIntyre <rlm@mit.edu>
date Mon, 31 Oct 2011 01:21:30 -0700
parents c969b04fa0dc
children 094a92b556a2
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(false);
212 music.setVolume(1f);
213 music.setReverbEnabled(false);
214 music.setMaxDistance(200.0f);
215 music.setRefDistance(1f);
216 music.setRolloffFactor(5f);
217 audioRenderer.pauseSource(music);
219 }
224 private Listener auxListener = new Listener();
225 //public File data1 = new File("/home/r/tmp/data1.wav");
226 //public File data2 = new File("/home/r/tmp/data2.wav");
227 //public File data3 = new File("/home/r/tmp/data3.wav");
228 //public File data4 = new File("/home/r/tmp/data4.wav");
229 //public File data5 = new File("/home/r/tmp/data5.wav");
230 //public File data6 = new File("/home/r/tmp/data6.wav");
233 public class Dancer implements SoundProcessor {
235 Spatial entity;
237 float scale = 2;
238 String debug;
239 public Dancer(Spatial entity, String debug){
240 this.entity = entity;
241 this.debug = debug;
242 }
244 /**
245 * this method is irrelevant since there is no state to cleanup.
246 */
247 public void cleanup() {}
250 /**
251 * Dance to the beat! This is the brain of an AI entity that
252 * hears it's surroundings and reacts to them.
253 */
254 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
255 audioSamples.clear();
256 byte[] data = new byte[numSamples];
257 float[] out = new float[numSamples];
258 audioSamples.get(data);
259 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
260 numSamples/format.getFrameSize(), format);
262 float max = Float.NEGATIVE_INFINITY;
263 for (float f : out){if (f > max) max = f;}
264 audioSamples.clear();
265 System.out.println(debug);
266 System.out.println(max);
270 //entity.scale(this.scale);
271 //if (this.scale == 2f){this.scale = 0.5f;}
272 //else {this.scale = 2;}
273 }
276 }
281 public void simpleInitApp() {
282 this.setTimer(new IsoTimer(60));
283 initAudio();
284 initKeys();
285 createScene();
286 listener.setLocation(ear1.getLocalTranslation());
287 if (this.audioRenderer instanceof MultiListener){
288 MultiListener rf = (MultiListener)this.audioRenderer;
292 auxListener = new Listener(listener);
294 rf.addListener(auxListener);
295 WaveFileWriter aux = null;
296 WaveFileWriter main = null;
299 try {aux = new WaveFileWriter(new File("/home/r/tmp/aux.wav"));}
300 catch (FileNotFoundException e) {e.printStackTrace();}
302 try {main = new WaveFileWriter(new File("/home/r/tmp/main.wav"));}
303 catch (FileNotFoundException e) {e.printStackTrace();}
305 rf.registerSoundProcessor(auxListener,
306 new CompositeSoundProcessor(new Dancer(ear1, "aux"), aux));
308 rf.registerSoundProcessor(
309 new CompositeSoundProcessor(new Dancer(ear1, "--------\nmain"), main));
310 }
312 motionControl.play();
313 }
319 private void initKeys() {
320 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
321 inputManager.addListener(actionListener, "Shoot");
322 }
324 /** Defining the "Shoot" action: Play a gun sound. */
325 private ActionListener actionListener = new ActionListener() {
326 @Override
327 public void onAction(String name, boolean keyPressed, float tpf) {
328 if (name.equals("Shoot") && !keyPressed) {
330 System.out.println(bell.getLocalTranslation().subtract(listener.getLocation()).length());
331 //bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
332 //audioRenderer.playSource(music);
333 System.out.println(music.getRefDistance());
335 }
336 }
337 };
339 /** Move the listener with the camera - for 3D audio. */
342 //private Vector3f prevBellPos = Vector3f.ZERO;
343 private int countdown = 0;
345 public void simpleUpdate(float tpf) {
346 //Vector3f loc = cam.getLocation();
347 //Quaternion rot = cam.getRotation();
348 //listener.setLocation(loc);
349 //listener.setRotation(rot);
350 System.out.println(countdown);
351 if (countdown++ == 400) { this.requestClose(false);}
353 System.out.println("channel "+ music.getChannel());
354 //listener.setLocation(cam.getLocation());
355 //listener.setRotation(cam.getRotation());
356 //auxListener.setLocation(loc);
357 //auxListener.setRotation(rot);
358 if (music.getStatus() != AudioNode.Status.Playing){
359 //audioRenderer.playSource(music);
360 music.play();
361 bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
362 System.out.println("I'm playing! <3");
363 }
364 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
366 //Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
367 //prevBellPos = bell.getLocalTranslation();
369 music.setLocalTranslation(bell.getLocalTranslation());
370 //music.setVelocity(bellVelocity);
372 //audioRenderer.updateSourceParam(music, AudioParam.Position);
373 //audioRenderer.updateSourceParam(music, AudioParam.Velocity);
376 //System.out.println("main:" + listener.getVolume());
377 //System.out.println("aux:" + auxListener.getVolume());
378 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
379 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
381 }
383 }