view src/com/aurellem/capture/examples/AdvancedAudio.java @ 22:c7a07eefaeea

started work on audio AI
author Robert McIntyre <rlm@mit.edu>
date Sun, 30 Oct 2011 01:19:11 -0700
parents bc6fbfbbadd9
children 5f616cc420dd
line wrap: on
line source
1 package com.aurellem.capture.examples;
3 import java.io.File;
4 import java.nio.ByteBuffer;
6 import com.aurellem.capture.IsoTimer;
7 import com.aurellem.capture.audio.MultiListener;
8 import com.aurellem.capture.audio.SoundProcessor;
9 import com.aurellem.capture.audio.WaveFileWriter;
10 import com.jme3.app.SimpleApplication;
11 import com.jme3.audio.AudioNode;
12 import com.jme3.audio.Listener;
13 import com.jme3.cinematic.MotionPath;
14 import com.jme3.cinematic.events.MotionTrack;
15 import com.jme3.input.controls.ActionListener;
16 import com.jme3.input.controls.MouseButtonTrigger;
17 import com.jme3.light.DirectionalLight;
18 import com.jme3.material.Material;
19 import com.jme3.math.ColorRGBA;
20 import com.jme3.math.FastMath;
21 import com.jme3.math.Quaternion;
22 import com.jme3.math.Vector3f;
23 import com.jme3.scene.Geometry;
24 import com.jme3.scene.Node;
25 import com.jme3.scene.Spatial;
26 import com.jme3.scene.shape.Box;
27 import com.jme3.scene.shape.Sphere;
28 import com.jme3.system.AppSettings;
31 /**
32 *
33 * Demonstrates advanced use of the audio capture and recording features.
34 * Multiple perspectives of the same scene are simultaneously rendered to
35 * different sound files.
36 *
37 * A key limitation of the way multiple listeners are implemented is that
38 * only 3D positioning effects are realized for listeners other than the
39 * main LWJGL listener. This means that audio effects such as environment
40 * settings will *not* be heard on any auxiliary listeners, though sound
41 * attenuation will work correctly.
42 *
43 * Multiple listeners as realized here might be used to make AI entities
44 * that can each hear the world from their own perspective.
45 *
46 * @author Robert McIntyre
47 *
48 */
50 public class AdvancedAudio extends SimpleApplication {
52 public static void main(String[] args) {
54 AdvancedAudio app = new AdvancedAudio();
55 AppSettings settings = new AppSettings(true);
56 settings.setAudioRenderer("Send");
57 app.setSettings(settings);
58 app.setShowSettings(false);
59 app.setPauseOnLostFocus(false);
60 org.lwjgl.input.Mouse.setGrabbed(false);
61 app.start();
62 }
64 private MotionTrack motionControl;
67 private Spatial makeEar(Node root, Vector3f position){
68 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
69 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
70 ear.setLocalTranslation(position);
71 mat.setColor("Color", ColorRGBA.Green);
72 ear.setMaterial(mat);
73 root.attachChild(ear);
74 return ear;
75 }
77 private Geometry bell;
79 private Spatial ear1;
80 //private Spatial ear2;
81 //private Spatial ear3;
82 //private Spatial ear4;
85 private Vector3f[] path = new Vector3f[]{
86 // loop 1
87 new Vector3f(0, 0, 0),
88 new Vector3f(0, 0, -10),
89 new Vector3f(-2, 0, -14),
90 new Vector3f(-6, 0, -20),
91 new Vector3f(0, 0, -26),
92 new Vector3f(6, 0, -20),
93 new Vector3f(0, 0, -14),
94 new Vector3f(-6, 0, -20),
95 new Vector3f(0, 0, -26),
96 new Vector3f(6, 0, -20),
97 // loop 2
98 new Vector3f(5, 0, -5),
99 new Vector3f(7, 0, 1.5f),
100 new Vector3f(14, 0, 2),
101 new Vector3f(20, 0, 6),
102 new Vector3f(26, 0, 0),
103 new Vector3f(20, 0, -6),
104 new Vector3f(14, 0, 0),
105 new Vector3f(20, 0, 6),
106 new Vector3f(26, 0, 0),
107 new Vector3f(20, 0, -6),
108 new Vector3f(14, 0, 0),
109 // loop 3
110 new Vector3f(8, 0, 7.5f),
111 new Vector3f(7, 0, 10.5f),
112 new Vector3f(6, 0, 20),
113 new Vector3f(0, 0, 26),
114 new Vector3f(-6, 0, 20),
115 new Vector3f(0, 0, 14),
116 new Vector3f(6, 0, 20),
117 new Vector3f(0, 0, 26),
118 new Vector3f(-6, 0, 20),
119 new Vector3f(0, 0, 14),
120 // begin ellipse
121 new Vector3f(16, 5, 20),
122 new Vector3f(0, 0, 26),
123 new Vector3f(-16, -10, 20),
124 new Vector3f(0, 0, 14),
125 new Vector3f(16, 20, 20),
126 new Vector3f(0, 0, 26),
127 new Vector3f(-10, -25, 10),
128 new Vector3f(-10, 0, 0),
129 // come at me bro!
130 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
131 new Vector3f(0, 0 , -20),
132 };
136 private void createScene() {
137 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
138 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
139 mat.setColor("Color", ColorRGBA.Blue);
140 bell.setMaterial(mat);
141 rootNode.attachChild(bell);
143 DirectionalLight light = new DirectionalLight();
144 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
145 light.setColor(ColorRGBA.White.mult(1.5f));
146 rootNode.addLight(light);
148 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,20));
149 //ear2 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
150 //ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
151 //ear4 = makeEar(rootNode, new Vector3f(-20, 0 ,0));
153 MotionPath track = new MotionPath();
155 for (Vector3f v : path){
156 track.addWayPoint(v);
157 }
160 track.setCurveTension(0.80f);
163 motionControl = new MotionTrack(bell,track);
164 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
165 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
166 motionControl.setInitialDuration(10f);
167 motionControl.setSpeed(0.1f);
170 track.enableDebugShape(assetManager, rootNode);
173 positionCamera();
176 }
179 private void positionCamera(){
180 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
181 // cam.setLocation(new Vector3f(0,0,-20));
182 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
183 }
185 private AudioNode music;
191 private void initAudio() {
193 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
195 rootNode.attachChild(music);
196 audioRenderer.playSource(music);
198 music.setVolume(1f);
199 music.setPositional(true);
200 music.setMaxDistance(200.0f);
201 music.setRefDistance(0.1f);
202 music.setRolloffFactor(5f);
203 audioRenderer.pauseSource(music);
205 }
210 private Listener auxListener = new Listener();
211 public File data1 = new File("/home/r/tmp/data1.wav");
212 public File data2 = new File("/home/r/tmp/data2.wav");
213 public File data3 = new File("/home/r/tmp/data3.wav");
214 public File data4 = new File("/home/r/tmp/data4.wav");
215 public File data5 = new File("/home/r/tmp/data5.wav");
216 public File data6 = new File("/home/r/tmp/data6.wav");
219 public class Dancer implements SoundProcessor {
221 Spatial entity;
223 float scale = 2;
225 public Dancer(Spatial entity){
226 this.entity = entity;
227 }
229 /**
230 * this method is irrelevant since there is no state to cleanup.
231 */
232 public void cleanup() {}
235 /**
236 * Dance to the beat! This is the brain of an AI entity that
237 * hears it's surroundings and reacts to them.
238 */
239 public void process(ByteBuffer audioSamples, int numSamples) {
240 //System.out.println("I'm DANCING <3");
241 entity.scale(this.scale);
242 if (this.scale == 2f){this.scale = 0.5f;}
243 else {this.scale = 2;}
244 }
247 }
252 public void simpleInitApp() {
253 this.setTimer(new IsoTimer(60));
254 initAudio();
255 initKeys();
256 createScene();
257 if (this.audioRenderer instanceof MultiListener){
258 MultiListener rf = (MultiListener)this.audioRenderer;
261 rf.addListener(auxListener);
263 rf.registerSoundProcessor(new WaveFileWriter(data1));
264 rf.registerSoundProcessor(auxListener, new Dancer(ear1));
266 }
268 motionControl.play();
269 }
275 private void initKeys() {
276 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
277 inputManager.addListener(actionListener, "Shoot");
278 }
280 /** Defining the "Shoot" action: Play a gun sound. */
281 private ActionListener actionListener = new ActionListener() {
282 @Override
283 public void onAction(String name, boolean keyPressed, float tpf) {
284 if (name.equals("Shoot") && !keyPressed) {
285 System.out.println("I'm playing! <3");
286 System.out.println(bell.getLocalTranslation().subtract(cam.getLocation()).length());
288 audioRenderer.playSource(music);
289 System.out.println(music.getRefDistance());
291 }
292 }
293 };
295 /** Move the listener with the camera - for 3D audio. */
298 private Vector3f prevBellPos = Vector3f.ZERO;
299 public void simpleUpdate(float tpf) {
300 //Vector3f loc = cam.getLocation();
301 //Quaternion rot = cam.getRotation();
302 //listener.setLocation(loc);
303 //listener.setRotation(rot);
306 listener.setLocation(cam.getLocation());
307 listener.setRotation(cam.getRotation());
308 //auxListener.setLocation(loc);
309 //auxListener.setRotation(rot);
310 //if (music.getStatus() == AudioNode.Status.Stopped){
312 //music.playInstance();
313 //}
314 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
316 Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
317 prevBellPos = bell.getLocalTranslation();
319 music.setLocalTranslation(bell.getLocalTranslation());
320 music.setVelocity(bellVelocity);
322 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
323 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
325 }
327 }