view src/com/aurellem/capture/examples/AdvancedAudio.java @ 38:adeb88787645

fixed all problems for 16 bit mono output
author Robert McIntyre <rlm@mit.edu>
date Mon, 31 Oct 2011 07:43:44 -0700
parents 094a92b556a2
children 784a3f4e6202
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.audio.ListenerParam;
25 import com.jme3.cinematic.MotionPath;
26 import com.jme3.cinematic.events.MotionTrack;
27 import com.jme3.input.controls.ActionListener;
28 import com.jme3.input.controls.MouseButtonTrigger;
29 import com.jme3.light.DirectionalLight;
30 import com.jme3.material.Material;
31 import com.jme3.math.ColorRGBA;
32 import com.jme3.math.FastMath;
33 import com.jme3.math.Quaternion;
34 import com.jme3.math.Vector3f;
35 import com.jme3.scene.Geometry;
36 import com.jme3.scene.Node;
37 import com.jme3.scene.Spatial;
38 import com.jme3.scene.shape.Box;
39 import com.jme3.scene.shape.Sphere;
40 import com.jme3.system.AppSettings;
43 /**
44 *
45 * Demonstrates advanced use of the audio capture and recording features.
46 * Multiple perspectives of the same scene are simultaneously rendered to
47 * different sound files.
48 *
49 * A key limitation of the way multiple listeners are implemented is that
50 * only 3D positioning effects are realized for listeners other than the
51 * main LWJGL listener. This means that audio effects such as environment
52 * settings will *not* be heard on any auxiliary listeners, though sound
53 * attenuation will work correctly.
54 *
55 * Multiple listeners as realized here might be used to make AI entities
56 * that can each hear the world from their own perspective.
57 *
58 * @author Robert McIntyre
59 *
60 */
62 public class AdvancedAudio extends SimpleApplication {
64 public static void main(String[] args) {
65 Logger.getLogger("com.jme3").setLevel(Level.OFF);
66 AdvancedAudio app = new AdvancedAudio();
67 AppSettings settings = new AppSettings(true);
68 settings.setAudioRenderer("Send");
69 app.setSettings(settings);
70 app.setShowSettings(false);
71 app.setPauseOnLostFocus(false);
72 org.lwjgl.input.Mouse.setGrabbed(false);
73 //try {Capture.captureVideo(app, new File("/home/r/tmp/out.avi"));}
74 //catch (IOException e) {e.printStackTrace();}
75 app.start();
76 }
78 private MotionTrack motionControl;
81 private Spatial makeEar(Node root, Vector3f position){
82 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
83 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
84 ear.setLocalTranslation(position);
85 mat.setColor("Color", ColorRGBA.Green);
86 ear.setMaterial(mat);
87 root.attachChild(ear);
88 return ear;
89 }
91 private Geometry bell;
93 private Spatial ear1;
94 //private Spatial ear2;
95 //private Spatial ear3;
96 //private Spatial ear4;
99 private Vector3f[] path = new Vector3f[]{
100 // loop 1
101 new Vector3f(0, 0, 0),
102 new Vector3f(0, 0, -10),
103 new Vector3f(-2, 0, -14),
104 new Vector3f(-6, 0, -20),
105 new Vector3f(0, 0, -26),
106 new Vector3f(6, 0, -20),
107 new Vector3f(0, 0, -14),
108 new Vector3f(-6, 0, -20),
109 new Vector3f(0, 0, -26),
110 new Vector3f(6, 0, -20),
111 // loop 2
112 new Vector3f(5, 0, -5),
113 new Vector3f(7, 0, 1.5f),
114 new Vector3f(14, 0, 2),
115 new Vector3f(20, 0, 6),
116 new Vector3f(26, 0, 0),
117 new Vector3f(20, 0, -6),
118 new Vector3f(14, 0, 0),
119 new Vector3f(20, 0, 6),
120 new Vector3f(26, 0, 0),
121 new Vector3f(20, 0, -6),
122 new Vector3f(14, 0, 0),
123 // loop 3
124 new Vector3f(8, 0, 7.5f),
125 new Vector3f(7, 0, 10.5f),
126 new Vector3f(6, 0, 20),
127 new Vector3f(0, 0, 26),
128 new Vector3f(-6, 0, 20),
129 new Vector3f(0, 0, 14),
130 new Vector3f(6, 0, 20),
131 new Vector3f(0, 0, 26),
132 new Vector3f(-6, 0, 20),
133 new Vector3f(0, 0, 14),
134 // begin ellipse
135 new Vector3f(16, 5, 20),
136 new Vector3f(0, 0, 26),
137 new Vector3f(-16, -10, 20),
138 new Vector3f(0, 0, 14),
139 new Vector3f(16, 20, 20),
140 new Vector3f(0, 0, 26),
141 new Vector3f(-10, -25, 10),
142 new Vector3f(-10, 0, 0),
143 // come at me bro!
144 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
145 new Vector3f(0, 0 , -20),
146 };
150 private void createScene() {
151 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
152 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
153 mat.setColor("Color", ColorRGBA.Blue);
154 bell.setMaterial(mat);
155 rootNode.attachChild(bell);
157 DirectionalLight light = new DirectionalLight();
158 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
159 light.setColor(ColorRGBA.White.mult(1.5f));
160 rootNode.addLight(light);
162 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
163 //ear2 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
164 //ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
165 //ear4 = makeEar(rootNode, new Vector3f(-20, 0 ,0));
167 MotionPath track = new MotionPath();
169 for (Vector3f v : path){
170 track.addWayPoint(v);
171 }
174 track.setCurveTension(0.80f);
177 motionControl = new MotionTrack(bell,track);
178 motionControl.setTimer(new IsoTimer(60));
179 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
180 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
181 motionControl.setInitialDuration(20f);
182 motionControl.setSpeed(1f);
185 track.enableDebugShape(assetManager, rootNode);
188 positionCamera();
191 }
194 private void positionCamera(){
195 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
196 // cam.setLocation(new Vector3f(0,0,-20));
197 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
198 }
200 private AudioNode music;
206 private void initAudio() {
208 music = new AudioNode(assetManager, "Sound/Environment/sqr-1kHz.wav", false);
210 rootNode.attachChild(music);
211 audioRenderer.playSource(music);
212 music.setPositional(true);
213 music.setVolume(1f);
214 music.setReverbEnabled(false);
215 music.setDirectional(false);
216 music.setMaxDistance(200.0f);
217 music.setRefDistance(1f);
218 music.setRolloffFactor(1f);
219 music.setLooping(false);
220 audioRenderer.pauseSource(music);
222 }
227 private Listener auxListener;
228 //public File data1 = new File("/home/r/tmp/data1.wav");
229 //public File data2 = new File("/home/r/tmp/data2.wav");
230 //public File data3 = new File("/home/r/tmp/data3.wav");
231 //public File data4 = new File("/home/r/tmp/data4.wav");
232 //public File data5 = new File("/home/r/tmp/data5.wav");
233 //public File data6 = new File("/home/r/tmp/data6.wav");
236 public class Dancer implements SoundProcessor {
238 Spatial entity;
240 float scale = 2;
241 String debug;
242 public Dancer(Spatial entity, String debug){
243 this.entity = entity;
244 this.debug = debug;
245 }
247 /**
248 * this method is irrelevant since there is no state to cleanup.
249 */
250 public void cleanup() {}
253 /**
254 * Dance to the beat! This is the brain of an AI entity that
255 * hears it's surroundings and reacts to them.
256 */
257 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
258 audioSamples.clear();
259 byte[] data = new byte[numSamples];
260 float[] out = new float[numSamples];
261 audioSamples.get(data);
262 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
263 numSamples/format.getFrameSize(), format);
265 float max = Float.NEGATIVE_INFINITY;
266 for (float f : out){if (f > max) max = f;}
267 audioSamples.clear();
268 System.out.println(debug);
269 System.out.println(max);
273 //entity.scale(this.scale);
274 //if (this.scale == 2f){this.scale = 0.5f;}
275 //else {this.scale = 2;}
276 }
279 }
284 public void simpleInitApp() {
285 this.setTimer(new IsoTimer(60));
286 initAudio();
287 initKeys();
288 createScene();
289 listener.setLocation(ear1.getLocalTranslation());
290 listener.setRotation(new Quaternion().fromAngleAxis(0, Vector3f.UNIT_Y));
291 if (this.audioRenderer instanceof MultiListener){
292 MultiListener rf = (MultiListener)this.audioRenderer;
296 auxListener = new Listener(listener);
298 rf.addListener(auxListener);
299 WaveFileWriter aux = null;
300 WaveFileWriter main = null;
303 try {aux = new WaveFileWriter(new File("/home/r/tmp/aux.wav"));}
304 catch (FileNotFoundException e) {e.printStackTrace();}
306 try {main = new WaveFileWriter(new File("/home/r/tmp/main.wav"));}
307 catch (FileNotFoundException e) {e.printStackTrace();}
309 rf.registerSoundProcessor(auxListener,
310 new CompositeSoundProcessor(new Dancer(ear1, "aux"), aux));
312 rf.registerSoundProcessor(
313 new CompositeSoundProcessor(new Dancer(ear1, "--------\nmain"), main));
314 }
316 motionControl.play();
317 }
323 private void initKeys() {
324 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
325 inputManager.addListener(actionListener, "Shoot");
326 }
328 /** Defining the "Shoot" action: Play a gun sound. */
329 private ActionListener actionListener = new ActionListener() {
330 @Override
331 public void onAction(String name, boolean keyPressed, float tpf) {
332 if (name.equals("Shoot") && !keyPressed) {
334 System.out.println(bell.getLocalTranslation().subtract(listener.getLocation()).length());
335 //bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
336 //audioRenderer.playSource(music);
337 System.out.println(music.getRefDistance());
339 }
340 }
341 };
343 /** Move the listener with the camera - for 3D audio. */
346 //private Vector3f prevBellPos = Vector3f.ZERO;
347 private int countdown = 0;
349 public void simpleUpdate(float tpf) {
350 if (countdown == 0){
351 music.play();
352 }
353 //Vector3f loc = cam.getLocation();
354 //Quaternion rot = cam.getRotation();
355 //listener.setLocation(loc);
356 listener.setRotation(new Quaternion().fromAngleAxis(0, music.getLocalTranslation().subtract(listener.getLocation())));
357 audioRenderer.updateListenerParam(listener, ListenerParam.Rotation);
359 System.out.println(countdown);
361 if (countdown++ == 300) { this.requestClose(false);}
363 //System.out.println("channel "+ music.getChannel());
364 //listener.setLocation(cam.getLocation());
365 //listener.setRotation(cam.getRotation());
366 //auxListener.setLocation(loc);
367 //auxListener.setRotation(rot);
368 //if (music.getStatus() != AudioNode.Status.Playing){
369 //audioRenderer.playSource(music);
370 //music.play();
371 // bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
372 //System.out.println("I'm playing! <3");
373 //}
374 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
376 //Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
377 //prevBellPos = bell.getLocalTranslation();
379 music.setLocalTranslation(bell.getLocalTranslation());
381 System.out.println("distance: " +
382 music.getLocalTranslation().subtract(listener.getLocation()).length());
384 //music.setVelocity(bellVelocity);
386 //audioRenderer.updateSourceParam(music, AudioParam.Position);
387 //audioRenderer.updateSourceParam(music, AudioParam.Velocity);
390 //System.out.println("main:" + listener.getVolume());
391 //System.out.println("aux:" + auxListener.getVolume());
392 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
393 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
395 }
397 }