view src/com/aurellem/capture/examples/AdvancedAudio.java @ 15:be5ac56826be

created part of the advanced documentation
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 06:52:46 -0700
parents e299cd89074d
children 87f818f58975
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.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;
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 }
65 private Spatial teapot;
66 private boolean active = true;
67 private boolean playing = false;
68 private MotionPath path;
69 private MotionTrack motionControl;
72 private void makeEar(Node root, Vector3f position){
73 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
74 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
75 ear.setLocalTranslation(position);
76 mat.setColor("Color", ColorRGBA.Green);
77 ear.setMaterial(mat);
78 root.attachChild(ear);
79 }
82 private void createScene() {
83 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
84 Geometry bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
85 mat.setColor("Color", ColorRGBA.Blue);
86 bell.setMaterial(mat);
87 rootNode.attachChild(bell);
92 DirectionalLight light = new DirectionalLight();
93 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
94 light.setColor(ColorRGBA.White.mult(1.5f));
95 rootNode.addLight(light);
97 makeEar(rootNode, new Vector3f(0, 0 ,20));
98 makeEar(rootNode, new Vector3f(0, 0 ,-20));
99 makeEar(rootNode, new Vector3f(20, 0 ,0));
100 makeEar(rootNode, new Vector3f(-20, 0 ,0));
102 MotionPath path = new MotionPath();
104 // loop 1
105 path.addWayPoint(new Vector3f(0, 0, 0));
106 path.addWayPoint(new Vector3f(0, 0, -10));
107 path.addWayPoint(new Vector3f(-2, 0, -14));
108 path.addWayPoint(new Vector3f(-6, 0, -20));
109 path.addWayPoint(new Vector3f(0, 0, -26));
110 path.addWayPoint(new Vector3f(6, 0, -20));
111 path.addWayPoint(new Vector3f(0, 0, -14));
112 path.addWayPoint(new Vector3f(-6, 0, -20));
113 path.addWayPoint(new Vector3f(0, 0, -26));
114 path.addWayPoint(new Vector3f(6, 0, -20));
117 // loop 2
118 path.addWayPoint(new Vector3f(5, 0, -5));
119 path.addWayPoint(new Vector3f(7, 0, 1.5f));
120 path.addWayPoint(new Vector3f(14, 0, 2));
121 path.addWayPoint(new Vector3f(20, 0, 6));
122 path.addWayPoint(new Vector3f(26, 0, 0));
123 path.addWayPoint(new Vector3f(20, 0, -6));
124 path.addWayPoint(new Vector3f(14, 0, 0));
125 path.addWayPoint(new Vector3f(20, 0, 6));
126 path.addWayPoint(new Vector3f(26, 0, 0));
127 path.addWayPoint(new Vector3f(20, 0, -6));
128 path.addWayPoint(new Vector3f(14, 0, 0));
132 // loop 3
133 path.addWayPoint(new Vector3f(8, 0, 7.5f));
134 path.addWayPoint(new Vector3f(7, 0, 10.5f));
135 path.addWayPoint(new Vector3f(6, 0, 20));
136 path.addWayPoint(new Vector3f(0, 0, 26));
137 path.addWayPoint(new Vector3f(-6, 0, 20));
138 path.addWayPoint(new Vector3f(0, 0, 14));
139 path.addWayPoint(new Vector3f(6, 0, 20));
140 path.addWayPoint(new Vector3f(0, 0, 26));
141 path.addWayPoint(new Vector3f(-6, 0, 20));
142 path.addWayPoint(new Vector3f(0, 0, 14));
145 // begin elipse
147 path.addWayPoint(new Vector3f(16, 5, 20));
148 path.addWayPoint(new Vector3f(0, 0, 26));
149 path.addWayPoint(new Vector3f(-16, -10, 20));
150 path.addWayPoint(new Vector3f(0, 0, 14));
151 path.addWayPoint(new Vector3f(16, 20, 20));
152 path.addWayPoint(new Vector3f(0, 0, 26));
153 path.addWayPoint(new Vector3f(-10, -25, 10));
154 path.addWayPoint(new Vector3f(-10, 0, 0));
156 // come at me bro!
157 path.addWayPoint(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
158 path.setCurveTension(0.80f);
161 motionControl = new MotionTrack(bell,path);
162 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
163 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
164 motionControl.setInitialDuration(10f);
165 motionControl.setSpeed(0.1f);
168 //path.enableDebugShape(assetManager, rootNode);
171 positionCamera();
174 }
177 private void positionCamera(){
178 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
179 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
182 }
188 private AudioNode audio_gun;
189 private AudioNode audio_nature;
190 private Geometry player;
191 private Listener auxListener = new Listener();
192 public File data1 = new File("/home/r/tmp/data1.wav");
193 public File data2 = new File("/home/r/tmp/data2.wav");
194 public File data3 = new File("/home/r/tmp/data3.wav");
197 @Override
198 public void simpleInitApp() {
199 //this.setTimer(new IsoTimer(60));
201 if (this.audioRenderer instanceof MultiListener){
202 MultiListener rf = (MultiListener)this.audioRenderer;
204 for (int n = 0; n < 0; n++){
205 Listener zzz = new Listener();
206 rf.addListener(zzz);
207 }
208 Listener listener3 = new Listener();
209 rf.addListener(auxListener);
210 rf.addListener(listener3);
211 rf.registerSoundProcessor(new WaveFileWriter(data1));
212 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
213 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
214 }
220 initKeys();
221 initAudio();
222 createScene();
225 motionControl.play();
227 this.audioRenderer.playSource(audio_gun);
231 }
233 /** We create two audio nodes. */
234 private void initAudio() {
235 //audioRenderer.setEnvironment(Environment.Cavern);
236 /* gun shot sound is to be triggered by a mouse click. */
237 audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
238 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
239 audio_gun.setLooping(false);
240 audio_gun.setVolume(2);
241 audio_gun.setPositional(true);
244 /* nature sound - keeps playing in a loop. */
245 audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
246 audio_nature.setLooping(true);
247 audio_nature.setPositional(true);
248 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
249 audio_nature.setVolume(3);
250 audio_nature.updateGeometricState();
251 }
253 /** Declaring the "Shoot" action, and
254 * mapping it to a trigger (mouse click). */
255 private void initKeys() {
256 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
257 inputManager.addListener(actionListener, "Shoot");
258 }
260 /** Defining the "Shoot" action: Play a gun sound. */
261 private ActionListener actionListener = new ActionListener() {
262 @Override
263 public void onAction(String name, boolean keyPressed, float tpf) {
264 if (name.equals("Shoot") && !keyPressed) {
265 audioRenderer.playSource(audio_gun); // play once!
266 }
267 }
268 };
270 /** Move the listener with the camera - for 3D audio. */
271 @Override
272 public void simpleUpdate(float tpf) {
273 Vector3f loc = cam.getLocation();
274 Quaternion rot = cam.getRotation();
275 listener.setLocation(loc);
276 listener.setRotation(rot);
277 auxListener.setLocation(loc);
278 auxListener.setRotation(rot);
282 }
284 }