view src/com/aurellem/capture/examples/AdvancedAudio.java @ 26:95648b0c12bc

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