comparison src/com/aurellem/capture/examples/AdvancedAudio.java @ 17:dae169178d11

investigate sound positioning problem
author Robert McIntyre <rlm@mit.edu>
date Sat, 29 Oct 2011 00:33:07 -0700
parents 87f818f58975
children 2543c95a0fd2
comparison
equal deleted inserted replaced
16:87f818f58975 17:dae169178d11
8 import com.jme3.app.SimpleApplication; 8 import com.jme3.app.SimpleApplication;
9 import com.jme3.audio.AudioNode; 9 import com.jme3.audio.AudioNode;
10 import com.jme3.audio.Listener; 10 import com.jme3.audio.Listener;
11 import com.jme3.cinematic.MotionPath; 11 import com.jme3.cinematic.MotionPath;
12 import com.jme3.cinematic.events.MotionTrack; 12 import com.jme3.cinematic.events.MotionTrack;
13 import com.jme3.input.KeyInput;
14 import com.jme3.input.controls.ActionListener;
15 import com.jme3.input.controls.KeyTrigger;
16 import com.jme3.input.controls.MouseButtonTrigger;
17 import com.jme3.light.DirectionalLight; 13 import com.jme3.light.DirectionalLight;
18 import com.jme3.material.Material; 14 import com.jme3.material.Material;
19 import com.jme3.math.ColorRGBA; 15 import com.jme3.math.ColorRGBA;
20 import com.jme3.math.FastMath; 16 import com.jme3.math.FastMath;
21 import com.jme3.math.Quaternion; 17 import com.jme3.math.Quaternion;
22 import com.jme3.math.Vector3f; 18 import com.jme3.math.Vector3f;
23 import com.jme3.scene.Geometry; 19 import com.jme3.scene.Geometry;
24 import com.jme3.scene.Node; 20 import com.jme3.scene.Node;
25 import com.jme3.scene.Spatial;
26 import com.jme3.scene.shape.Box; 21 import com.jme3.scene.shape.Box;
27 import com.jme3.scene.shape.Sphere; 22 import com.jme3.scene.shape.Sphere;
28 import com.jme3.system.AppSettings; 23 import com.jme3.system.AppSettings;
29 24
30 25
74 mat.setColor("Color", ColorRGBA.Green); 69 mat.setColor("Color", ColorRGBA.Green);
75 ear.setMaterial(mat); 70 ear.setMaterial(mat);
76 root.attachChild(ear); 71 root.attachChild(ear);
77 } 72 }
78 73
74 private Geometry bell;
75
79 private void createScene() { 76 private void createScene() {
80 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 77 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
81 Geometry bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); 78 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
82 mat.setColor("Color", ColorRGBA.Blue); 79 mat.setColor("Color", ColorRGBA.Blue);
83 bell.setMaterial(mat); 80 bell.setMaterial(mat);
84 rootNode.attachChild(bell); 81 rootNode.attachChild(bell);
85 82
86 DirectionalLight light = new DirectionalLight(); 83 DirectionalLight light = new DirectionalLight();
177 174
178 175
179 176
180 177
181 178
182 private AudioNode audio_gun; 179 private AudioNode music;
183 private AudioNode audio_nature; 180
184 private Geometry player; 181
185 private Listener auxListener = new Listener(); 182 private Listener auxListener = new Listener();
186 public File data1 = new File("/home/r/tmp/data1.wav"); 183 public File data1 = new File("/home/r/tmp/data1.wav");
187 public File data2 = new File("/home/r/tmp/data2.wav"); 184 public File data2 = new File("/home/r/tmp/data2.wav");
188 public File data3 = new File("/home/r/tmp/data3.wav"); 185 public File data3 = new File("/home/r/tmp/data3.wav");
189 186
190 187
191 @Override 188 @Override
192 public void simpleInitApp() { 189 public void simpleInitApp() {
193 //this.setTimer(new IsoTimer(60)); 190 this.setTimer(new IsoTimer(60));
194 191
195 if (this.audioRenderer instanceof MultiListener){ 192 if (this.audioRenderer instanceof MultiListener){
196 MultiListener rf = (MultiListener)this.audioRenderer; 193 MultiListener rf = (MultiListener)this.audioRenderer;
197 194
198 for (int n = 0; n < 0; n++){ 195 for (int n = 0; n < 0; n++){
209 206
210 207
211 208
212 209
213 210
214 initKeys(); 211
215 initAudio(); 212 initAudio();
216 createScene(); 213 createScene();
217 214
218 215
219 motionControl.play(); 216 motionControl.play();
220 217
221 this.audioRenderer.playSource(audio_gun); 218 this.audioRenderer.playSource(music);
222 219
223 220
224 221
225 } 222 }
226 223
227 /** We create two audio nodes. */ 224 /** We create two audio nodes. */
228 private void initAudio() { 225 private void initAudio() {
229 //audioRenderer.setEnvironment(Environment.Cavern); 226 //audioRenderer.setEnvironment(Environment.Cavern);
230 /* gun shot sound is to be triggered by a mouse click. */ 227
231 audio_gun = new AudioNode(assetManager, "Sound/Environment/haydn.wav", false); 228 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
232 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false); 229 music.setLooping(true);
233 audio_gun.setLooping(false); 230 music.setVolume(2);
234 audio_gun.setVolume(2); 231 music.setRefDistance(0.1f);
235 audio_gun.setPositional(true); 232 music.setMaxDistance(200);
236 233 music.setPositional(true);
237
238 /* nature sound - keeps playing in a loop. */
239 audio_nature = new AudioNode(assetManager, "Sound/Environment/haydn.wav", false, false);
240 audio_nature.setLooping(true);
241 audio_nature.setPositional(true);
242 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
243 audio_nature.setVolume(3);
244 audio_nature.updateGeometricState();
245 } 234 }
246 235
247 /** Declaring the "Shoot" action, and 236 /** Declaring the "Shoot" action, and
248 * mapping it to a trigger (mouse click). */ 237 * mapping it to a trigger (mouse click). */
249 private void initKeys() { 238
250 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
251 inputManager.addListener(actionListener, "Shoot");
252 }
253 239
254 /** Defining the "Shoot" action: Play a gun sound. */ 240 /** Defining the "Shoot" action: Play a gun sound. */
255 private ActionListener actionListener = new ActionListener() { 241
256 @Override
257 public void onAction(String name, boolean keyPressed, float tpf) {
258 if (name.equals("Shoot") && !keyPressed) {
259 audioRenderer.playSource(audio_gun); // play once!
260 }
261 }
262 };
263 242
264 /** Move the listener with the camera - for 3D audio. */ 243 /** Move the listener with the camera - for 3D audio. */
265 @Override 244 @Override
266 public void simpleUpdate(float tpf) { 245 public void simpleUpdate(float tpf) {
267 Vector3f loc = cam.getLocation(); 246 Vector3f loc = cam.getLocation();
269 listener.setLocation(loc); 248 listener.setLocation(loc);
270 listener.setRotation(rot); 249 listener.setRotation(rot);
271 auxListener.setLocation(loc); 250 auxListener.setLocation(loc);
272 auxListener.setRotation(rot); 251 auxListener.setRotation(rot);
273 252
274 253 music.setLocalTranslation(bell.getLocalTranslation());
254
275 255
276 } 256 }
277 257
278 } 258 }