view src/com/aurellem/capture/examples/AdvancedAudio.java @ 33:c4bfbf5d090e

starting to get something that can percieve sound
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 13:57:16 -0700
parents be37291c62b8
children 13d354e1184b
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;
8 import javax.sound.sampled.AudioFormat;
10 import org.tritonus.share.sampled.FloatSampleTools;
12 import com.aurellem.capture.Capture;
13 import com.aurellem.capture.IsoTimer;
14 import com.aurellem.capture.audio.CompositeSoundProcessor;
15 import com.aurellem.capture.audio.MultiListener;
16 import com.aurellem.capture.audio.SoundProcessor;
17 import com.aurellem.capture.audio.WaveFileWriter;
18 import com.jme3.app.SimpleApplication;
19 import com.jme3.audio.AudioNode;
20 import com.jme3.audio.Listener;
21 import com.jme3.cinematic.MotionPath;
22 import com.jme3.cinematic.events.MotionTrack;
23 import com.jme3.input.controls.ActionListener;
24 import com.jme3.input.controls.MouseButtonTrigger;
25 import com.jme3.light.DirectionalLight;
26 import com.jme3.material.Material;
27 import com.jme3.math.ColorRGBA;
28 import com.jme3.math.FastMath;
29 import com.jme3.math.Quaternion;
30 import com.jme3.math.Vector3f;
31 import com.jme3.scene.Geometry;
32 import com.jme3.scene.Node;
33 import com.jme3.scene.Spatial;
34 import com.jme3.scene.shape.Box;
35 import com.jme3.scene.shape.Sphere;
36 import com.jme3.system.AppSettings;
39 /**
40 *
41 * Demonstrates advanced use of the audio capture and recording features.
42 * Multiple perspectives of the same scene are simultaneously rendered to
43 * different sound files.
44 *
45 * A key limitation of the way multiple listeners are implemented is that
46 * only 3D positioning effects are realized for listeners other than the
47 * main LWJGL listener. This means that audio effects such as environment
48 * settings will *not* be heard on any auxiliary listeners, though sound
49 * attenuation will work correctly.
50 *
51 * Multiple listeners as realized here might be used to make AI entities
52 * that can each hear the world from their own perspective.
53 *
54 * @author Robert McIntyre
55 *
56 */
58 public class AdvancedAudio extends SimpleApplication {
60 public static void main(String[] args) {
62 AdvancedAudio app = new AdvancedAudio();
63 AppSettings settings = new AppSettings(true);
64 settings.setAudioRenderer("Send");
65 app.setSettings(settings);
66 app.setShowSettings(false);
67 app.setPauseOnLostFocus(false);
68 org.lwjgl.input.Mouse.setGrabbed(false);
69 try {Capture.captureVideo(app, new File("/home/r/tmp/out.avi"));}
70 catch (IOException e) {e.printStackTrace();}
71 app.start();
72 }
74 private MotionTrack motionControl;
77 private Spatial makeEar(Node root, Vector3f position){
78 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
79 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
80 ear.setLocalTranslation(position);
81 mat.setColor("Color", ColorRGBA.Green);
82 ear.setMaterial(mat);
83 root.attachChild(ear);
84 return ear;
85 }
87 private Geometry bell;
89 private Spatial ear1;
90 //private Spatial ear2;
91 //private Spatial ear3;
92 //private Spatial ear4;
95 private Vector3f[] path = new Vector3f[]{
96 // loop 1
97 new Vector3f(0, 0, 0),
98 new Vector3f(0, 0, -10),
99 new Vector3f(-2, 0, -14),
100 new Vector3f(-6, 0, -20),
101 new Vector3f(0, 0, -26),
102 new Vector3f(6, 0, -20),
103 new Vector3f(0, 0, -14),
104 new Vector3f(-6, 0, -20),
105 new Vector3f(0, 0, -26),
106 new Vector3f(6, 0, -20),
107 // loop 2
108 new Vector3f(5, 0, -5),
109 new Vector3f(7, 0, 1.5f),
110 new Vector3f(14, 0, 2),
111 new Vector3f(20, 0, 6),
112 new Vector3f(26, 0, 0),
113 new Vector3f(20, 0, -6),
114 new Vector3f(14, 0, 0),
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 // loop 3
120 new Vector3f(8, 0, 7.5f),
121 new Vector3f(7, 0, 10.5f),
122 new Vector3f(6, 0, 20),
123 new Vector3f(0, 0, 26),
124 new Vector3f(-6, 0, 20),
125 new Vector3f(0, 0, 14),
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 // begin ellipse
131 new Vector3f(16, 5, 20),
132 new Vector3f(0, 0, 26),
133 new Vector3f(-16, -10, 20),
134 new Vector3f(0, 0, 14),
135 new Vector3f(16, 20, 20),
136 new Vector3f(0, 0, 26),
137 new Vector3f(-10, -25, 10),
138 new Vector3f(-10, 0, 0),
139 // come at me bro!
140 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
141 new Vector3f(0, 0 , -20),
142 };
146 private void createScene() {
147 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
148 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
149 mat.setColor("Color", ColorRGBA.Blue);
150 bell.setMaterial(mat);
151 rootNode.attachChild(bell);
153 DirectionalLight light = new DirectionalLight();
154 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
155 light.setColor(ColorRGBA.White.mult(1.5f));
156 rootNode.addLight(light);
158 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,20));
159 //ear2 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
160 //ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
161 //ear4 = makeEar(rootNode, new Vector3f(-20, 0 ,0));
163 MotionPath track = new MotionPath();
165 for (Vector3f v : path){
166 track.addWayPoint(v);
167 }
170 track.setCurveTension(0.80f);
173 motionControl = new MotionTrack(bell,track);
174 motionControl.setTimer(new IsoTimer(60));
175 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
176 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
177 motionControl.setInitialDuration(20f);
178 motionControl.setSpeed(1f);
181 track.enableDebugShape(assetManager, rootNode);
184 positionCamera();
187 }
190 private void positionCamera(){
191 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
192 // cam.setLocation(new Vector3f(0,0,-20));
193 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
194 }
196 private AudioNode music;
202 private void initAudio() {
204 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
206 rootNode.attachChild(music);
207 audioRenderer.playSource(music);
208 music.setPositional(true);
209 //music.setVolume(1f);
211 //music.setMaxDistance(200.0f);
212 //music.setRefDistance(0.1f);
213 //music.setRolloffFactor(5f);
214 audioRenderer.pauseSource(music);
216 }
221 private Listener auxListener = new Listener();
222 public File data1 = new File("/home/r/tmp/data1.wav");
223 public File data2 = new File("/home/r/tmp/data2.wav");
224 public File data3 = new File("/home/r/tmp/data3.wav");
225 public File data4 = new File("/home/r/tmp/data4.wav");
226 public File data5 = new File("/home/r/tmp/data5.wav");
227 public File data6 = new File("/home/r/tmp/data6.wav");
230 public class Dancer implements SoundProcessor {
232 Spatial entity;
234 float scale = 2;
235 String debug;
236 public Dancer(Spatial entity, String debug){
237 this.entity = entity;
238 this.debug = debug;
239 }
241 /**
242 * this method is irrelevant since there is no state to cleanup.
243 */
244 public void cleanup() {}
247 /**
248 * Dance to the beat! This is the brain of an AI entity that
249 * hears it's surroundings and reacts to them.
250 */
251 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
252 audioSamples.clear();
253 byte[] data = new byte[numSamples];
254 float[] out = new float[numSamples];
255 audioSamples.get(data);
256 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
257 numSamples/format.getFrameSize(), format);
259 float max = Float.NEGATIVE_INFINITY;
260 for (float f : out){if (f > max) max = f;}
262 System.out.println(debug);
263 System.out.println(max);
267 //entity.scale(this.scale);
268 //if (this.scale == 2f){this.scale = 0.5f;}
269 //else {this.scale = 2;}
270 }
273 }
278 public void simpleInitApp() {
279 this.setTimer(new IsoTimer(60));
280 initAudio();
281 initKeys();
282 createScene();
283 if (this.audioRenderer instanceof MultiListener){
284 MultiListener rf = (MultiListener)this.audioRenderer;
287 rf.addListener(auxListener);
288 WaveFileWriter writer = null;
289 WaveFileWriter writer2 = null;
290 auxListener.setLocation(ear1.getLocalTranslation());
291 listener.setLocation(ear1.getLocalTranslation());
292 try {writer = new WaveFileWriter(new File("/home/r/tmp/out.wav"));}
293 catch (FileNotFoundException e) {e.printStackTrace();}
295 try {writer2 = new WaveFileWriter(new File("/home/r/tmp/outmain.wav"));}
296 catch (FileNotFoundException e) {e.printStackTrace();}
298 rf.registerSoundProcessor(auxListener,
299 new CompositeSoundProcessor(new Dancer(ear1, "aux"), writer));
301 rf.registerSoundProcessor(
302 new CompositeSoundProcessor(new Dancer(ear1, "main"), writer2));
303 }
305 motionControl.play();
306 }
312 private void initKeys() {
313 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
314 inputManager.addListener(actionListener, "Shoot");
315 }
317 /** Defining the "Shoot" action: Play a gun sound. */
318 private ActionListener actionListener = new ActionListener() {
319 @Override
320 public void onAction(String name, boolean keyPressed, float tpf) {
321 if (name.equals("Shoot") && !keyPressed) {
322 System.out.println("I'm playing! <3");
323 System.out.println(bell.getLocalTranslation().subtract(cam.getLocation()).length());
324 bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
325 audioRenderer.playSource(music);
326 System.out.println(music.getRefDistance());
328 }
329 }
330 };
332 /** Move the listener with the camera - for 3D audio. */
335 private Vector3f prevBellPos = Vector3f.ZERO;
338 public void simpleUpdate(float tpf) {
339 //Vector3f loc = cam.getLocation();
340 //Quaternion rot = cam.getRotation();
341 //listener.setLocation(loc);
342 //listener.setRotation(rot);
345 //listener.setLocation(cam.getLocation());
346 //listener.setRotation(cam.getRotation());
347 //auxListener.setLocation(loc);
348 //auxListener.setRotation(rot);
349 if (music.getStatus() != AudioNode.Status.Playing){
350 audioRenderer.playSource(music);
351 bell.getMaterial().setColor("Color", ColorRGBA.randomColor());
352 }
353 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
355 Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
356 prevBellPos = bell.getLocalTranslation();
358 music.setLocalTranslation(bell.getLocalTranslation());
359 music.setVelocity(bellVelocity);
361 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
362 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
364 }
366 }