view src/com/aurellem/capture/examples/AdvancedAudio.java @ 18:2543c95a0fd2

fixed sound problem
author Robert McIntyre <rlm@mit.edu>
date Sat, 29 Oct 2011 14:39:19 -0700
parents dae169178d11
children 4de7988407ef
line wrap: on
line source
1 package com.aurellem.capture.examples;
3 import java.io.File;
5 import com.aurellem.capture.IsoTimer;
6 import com.aurellem.capture.audio.MultiListener;
7 import com.aurellem.capture.audio.WaveFileWriter;
8 import com.jme3.app.SimpleApplication;
9 import com.jme3.audio.AudioNode;
10 import com.jme3.audio.AudioParam;
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.shape.Box;
25 import com.jme3.scene.shape.Sphere;
26 import com.jme3.system.AppSettings;
29 /**
30 *
31 * Demonstrates advanced use of the audio capture and recording features.
32 * Multiple perspectives of the same scene are simultaneously rendered to
33 * different sound files.
34 *
35 * A key limitation of the way multiple listeners are implemented is that
36 * only 3D positioning effects are realized for listeners other than the
37 * main LWJGL listener. This means that audio effects such as environment
38 * settings will *not* be heard on any auxiliary listeners, though sound
39 * attenuation will work correctly.
40 *
41 * Multiple listeners as realized here might be used to make AI entities
42 * that can each hear the world from their own perspective.
43 *
44 * @author Robert McIntyre
45 *
46 */
48 public class AdvancedAudio extends SimpleApplication {
50 public static void main(String[] args) {
52 AdvancedAudio app = new AdvancedAudio();
53 AppSettings settings = new AppSettings(true);
54 //settings.setAudioRenderer("Send");
55 app.setSettings(settings);
56 app.setShowSettings(false);
57 app.setPauseOnLostFocus(false);
58 org.lwjgl.input.Mouse.setGrabbed(false);
59 app.start();
60 }
62 private MotionTrack motionControl;
65 private void makeEar(Node root, Vector3f position){
66 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
67 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
68 ear.setLocalTranslation(position);
69 mat.setColor("Color", ColorRGBA.Green);
70 ear.setMaterial(mat);
71 root.attachChild(ear);
72 }
74 private Geometry bell;
76 private void createScene() {
77 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
78 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
79 mat.setColor("Color", ColorRGBA.Blue);
80 bell.setMaterial(mat);
81 rootNode.attachChild(bell);
83 DirectionalLight light = new DirectionalLight();
84 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
85 light.setColor(ColorRGBA.White.mult(1.5f));
86 rootNode.addLight(light);
88 makeEar(rootNode, new Vector3f(0, 0 ,20));
89 makeEar(rootNode, new Vector3f(0, 0 ,-20));
90 makeEar(rootNode, new Vector3f(20, 0 ,0));
91 makeEar(rootNode, new Vector3f(-20, 0 ,0));
93 MotionPath path = new MotionPath();
95 // loop 1
96 path.addWayPoint(new Vector3f(0, 0, 0));
97 path.addWayPoint(new Vector3f(0, 0, -10));
98 path.addWayPoint(new Vector3f(-2, 0, -14));
99 path.addWayPoint(new Vector3f(-6, 0, -20));
100 path.addWayPoint(new Vector3f(0, 0, -26));
101 path.addWayPoint(new Vector3f(6, 0, -20));
102 path.addWayPoint(new Vector3f(0, 0, -14));
103 path.addWayPoint(new Vector3f(-6, 0, -20));
104 path.addWayPoint(new Vector3f(0, 0, -26));
105 path.addWayPoint(new Vector3f(6, 0, -20));
108 // loop 2
109 path.addWayPoint(new Vector3f(5, 0, -5));
110 path.addWayPoint(new Vector3f(7, 0, 1.5f));
111 path.addWayPoint(new Vector3f(14, 0, 2));
112 path.addWayPoint(new Vector3f(20, 0, 6));
113 path.addWayPoint(new Vector3f(26, 0, 0));
114 path.addWayPoint(new Vector3f(20, 0, -6));
115 path.addWayPoint(new Vector3f(14, 0, 0));
116 path.addWayPoint(new Vector3f(20, 0, 6));
117 path.addWayPoint(new Vector3f(26, 0, 0));
118 path.addWayPoint(new Vector3f(20, 0, -6));
119 path.addWayPoint(new Vector3f(14, 0, 0));
123 // loop 3
124 path.addWayPoint(new Vector3f(8, 0, 7.5f));
125 path.addWayPoint(new Vector3f(7, 0, 10.5f));
126 path.addWayPoint(new Vector3f(6, 0, 20));
127 path.addWayPoint(new Vector3f(0, 0, 26));
128 path.addWayPoint(new Vector3f(-6, 0, 20));
129 path.addWayPoint(new Vector3f(0, 0, 14));
130 path.addWayPoint(new Vector3f(6, 0, 20));
131 path.addWayPoint(new Vector3f(0, 0, 26));
132 path.addWayPoint(new Vector3f(-6, 0, 20));
133 path.addWayPoint(new Vector3f(0, 0, 14));
136 // begin elipse
138 path.addWayPoint(new Vector3f(16, 5, 20));
139 path.addWayPoint(new Vector3f(0, 0, 26));
140 path.addWayPoint(new Vector3f(-16, -10, 20));
141 path.addWayPoint(new Vector3f(0, 0, 14));
142 path.addWayPoint(new Vector3f(16, 20, 20));
143 path.addWayPoint(new Vector3f(0, 0, 26));
144 path.addWayPoint(new Vector3f(-10, -25, 10));
145 path.addWayPoint(new Vector3f(-10, 0, 0));
147 // come at me bro!
148 path.addWayPoint(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
149 //path.addWayPoint(new Vector3f(0, 0 , -20));
151 path.setCurveTension(0.80f);
154 motionControl = new MotionTrack(bell,path);
155 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
156 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
157 motionControl.setInitialDuration(10f);
158 motionControl.setSpeed(0.1f);
161 path.enableDebugShape(assetManager, rootNode);
164 positionCamera();
167 }
170 private void positionCamera(){
171 //this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
172 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
173 }
179 private AudioNode music;
182 private Listener auxListener = new Listener();
183 public File data1 = new File("/home/r/tmp/data1.wav");
184 public File data2 = new File("/home/r/tmp/data2.wav");
185 public File data3 = new File("/home/r/tmp/data3.wav");
189 public void simpleInitApp() {
190 this.setTimer(new IsoTimer(60));
192 if (this.audioRenderer instanceof MultiListener){
193 MultiListener rf = (MultiListener)this.audioRenderer;
195 for (int n = 0; n < 0; n++){
196 Listener zzz = new Listener();
197 rf.addListener(zzz);
198 }
199 Listener listener3 = new Listener();
200 rf.addListener(auxListener);
201 rf.addListener(listener3);
202 rf.registerSoundProcessor(new WaveFileWriter(data1));
203 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
204 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
205 }
212 initAudio();
213 initKeys();
214 createScene();
217 motionControl.play();
219 //this.audioRenderer.playSource(music);
223 }
225 /** We create two audio nodes. */
226 private void initAudio() {
227 //audioRenderer.setEnvironment(Environment.Cavern);
229 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
231 //music.setLooping(true);
237 rootNode.attachChild(music);
238 audioRenderer.playSource(music);
240 music.setVolume(1f);
241 music.setPositional(true);
242 music.setMaxDistance(200.0f);
243 music.setRefDistance(0.1f);
244 music.setRolloffFactor(5f);
245 audioRenderer.pauseSource(music);
249 }
251 /** Declaring the "Shoot" action, and
252 * mapping it to a trigger (mouse click). */
255 /** Defining the "Shoot" action: Play a gun sound. */
257 private void initKeys() {
258 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
259 inputManager.addListener(actionListener, "Shoot");
260 }
262 /** Defining the "Shoot" action: Play a gun sound. */
263 private ActionListener actionListener = new ActionListener() {
264 @Override
265 public void onAction(String name, boolean keyPressed, float tpf) {
266 if (name.equals("Shoot") && !keyPressed) {
267 System.out.println("I'm playing! <3");
268 System.out.println(bell.getLocalTranslation().subtract(cam.getLocation()).length());
270 audioRenderer.playSource(music);
271 System.out.println(music.getRefDistance());
273 }
274 }
275 };
277 /** Move the listener with the camera - for 3D audio. */
278 @Override
279 public void simpleUpdate(float tpf) {
280 //Vector3f loc = cam.getLocation();
281 //Quaternion rot = cam.getRotation();
282 //listener.setLocation(loc);
283 //listener.setRotation(rot);
286 listener.setLocation(cam.getLocation());
287 listener.setRotation(cam.getRotation());
288 //auxListener.setLocation(loc);
289 //auxListener.setRotation(rot);
290 //if (music.getStatus() == AudioNode.Status.Stopped){
292 //music.playInstance();
293 //}
294 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
296 music.setLocalTranslation(bell.getLocalTranslation());
298 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
299 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
301 }
303 }