view 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
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.Listener;
11 import com.jme3.cinematic.MotionPath;
12 import com.jme3.cinematic.events.MotionTrack;
13 import com.jme3.light.DirectionalLight;
14 import com.jme3.material.Material;
15 import com.jme3.math.ColorRGBA;
16 import com.jme3.math.FastMath;
17 import com.jme3.math.Quaternion;
18 import com.jme3.math.Vector3f;
19 import com.jme3.scene.Geometry;
20 import com.jme3.scene.Node;
21 import com.jme3.scene.shape.Box;
22 import com.jme3.scene.shape.Sphere;
23 import com.jme3.system.AppSettings;
26 /**
27 *
28 * Demonstrates advanced use of the audio capture and recording features.
29 * Multiple perspectives of the same scene are simultaneously rendered to
30 * different sound files.
31 *
32 * A key limitation of the way multiple listeners are implemented is that
33 * only 3D positioning effects are realized for listeners other than the
34 * main LWJGL listener. This means that audio effects such as environment
35 * settings will *not* be heard on any auxiliary listeners, though sound
36 * attenuation will work correctly.
37 *
38 * Multiple listeners as realized here might be used to make AI entities
39 * that can each hear the world from their own perspective.
40 *
41 * @author Robert McIntyre
42 *
43 */
45 public class AdvancedAudio extends SimpleApplication {
47 public static void main(String[] args) {
49 AdvancedAudio app = new AdvancedAudio();
50 AppSettings settings = new AppSettings(true);
51 //settings.setAudioRenderer("Send");
52 app.setSettings(settings);
53 app.setShowSettings(false);
54 app.setPauseOnLostFocus(false);
55 org.lwjgl.input.Mouse.setGrabbed(false);
56 app.start();
57 }
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.setCurveTension(0.80f);
152 motionControl = new MotionTrack(bell,path);
153 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
154 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
155 motionControl.setInitialDuration(10f);
156 motionControl.setSpeed(0.1f);
159 //path.enableDebugShape(assetManager, rootNode);
162 positionCamera();
165 }
168 private void positionCamera(){
169 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
170 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");
188 @Override
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 createScene();
216 motionControl.play();
218 this.audioRenderer.playSource(music);
222 }
224 /** We create two audio nodes. */
225 private void initAudio() {
226 //audioRenderer.setEnvironment(Environment.Cavern);
228 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
229 music.setLooping(true);
230 music.setVolume(2);
231 music.setRefDistance(0.1f);
232 music.setMaxDistance(200);
233 music.setPositional(true);
234 }
236 /** Declaring the "Shoot" action, and
237 * mapping it to a trigger (mouse click). */
240 /** Defining the "Shoot" action: Play a gun sound. */
243 /** Move the listener with the camera - for 3D audio. */
244 @Override
245 public void simpleUpdate(float tpf) {
246 Vector3f loc = cam.getLocation();
247 Quaternion rot = cam.getRotation();
248 listener.setLocation(loc);
249 listener.setRotation(rot);
250 auxListener.setLocation(loc);
251 auxListener.setRotation(rot);
253 music.setLocalTranslation(bell.getLocalTranslation());
256 }
258 }