view src/com/aurellem/capture/examples/AdvancedAudio.java @ 19:4de7988407ef

added CompositeSoundProcessor for combining SoundProcessors
author Robert McIntyre <rlm@mit.edu>
date Sat, 29 Oct 2011 15:16:06 -0700
parents 2543c95a0fd2
children bc6fbfbbadd9
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.controls.ActionListener;
14 import com.jme3.input.controls.MouseButtonTrigger;
15 import com.jme3.light.DirectionalLight;
16 import com.jme3.material.Material;
17 import com.jme3.math.ColorRGBA;
18 import com.jme3.math.FastMath;
19 import com.jme3.math.Quaternion;
20 import com.jme3.math.Vector3f;
21 import com.jme3.scene.Geometry;
22 import com.jme3.scene.Node;
23 import com.jme3.scene.shape.Box;
24 import com.jme3.scene.shape.Sphere;
25 import com.jme3.system.AppSettings;
28 /**
29 *
30 * Demonstrates advanced use of the audio capture and recording features.
31 * Multiple perspectives of the same scene are simultaneously rendered to
32 * different sound files.
33 *
34 * A key limitation of the way multiple listeners are implemented is that
35 * only 3D positioning effects are realized for listeners other than the
36 * main LWJGL listener. This means that audio effects such as environment
37 * settings will *not* be heard on any auxiliary listeners, though sound
38 * attenuation will work correctly.
39 *
40 * Multiple listeners as realized here might be used to make AI entities
41 * that can each hear the world from their own perspective.
42 *
43 * @author Robert McIntyre
44 *
45 */
47 public class AdvancedAudio extends SimpleApplication {
49 public static void main(String[] args) {
51 AdvancedAudio app = new AdvancedAudio();
52 AppSettings settings = new AppSettings(true);
53 //settings.setAudioRenderer("Send");
54 app.setSettings(settings);
55 app.setShowSettings(false);
56 app.setPauseOnLostFocus(false);
57 org.lwjgl.input.Mouse.setGrabbed(false);
58 app.start();
59 }
61 private MotionTrack motionControl;
64 private void makeEar(Node root, Vector3f position){
65 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
66 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
67 ear.setLocalTranslation(position);
68 mat.setColor("Color", ColorRGBA.Green);
69 ear.setMaterial(mat);
70 root.attachChild(ear);
71 }
73 private Geometry bell;
76 private Vector3f[] path = new Vector3f[]{
77 // loop 1
78 new Vector3f(0, 0, 0),
79 new Vector3f(0, 0, -10),
80 new Vector3f(-2, 0, -14),
81 new Vector3f(-6, 0, -20),
82 new Vector3f(0, 0, -26),
83 new Vector3f(6, 0, -20),
84 new Vector3f(0, 0, -14),
85 new Vector3f(-6, 0, -20),
86 new Vector3f(0, 0, -26),
87 new Vector3f(6, 0, -20),
88 // loop 2
89 new Vector3f(5, 0, -5),
90 new Vector3f(7, 0, 1.5f),
91 new Vector3f(14, 0, 2),
92 new Vector3f(20, 0, 6),
93 new Vector3f(26, 0, 0),
94 new Vector3f(20, 0, -6),
95 new Vector3f(14, 0, 0),
96 new Vector3f(20, 0, 6),
97 new Vector3f(26, 0, 0),
98 new Vector3f(20, 0, -6),
99 new Vector3f(14, 0, 0),
100 // loop 3
101 new Vector3f(8, 0, 7.5f),
102 new Vector3f(7, 0, 10.5f),
103 new Vector3f(6, 0, 20),
104 new Vector3f(0, 0, 26),
105 new Vector3f(-6, 0, 20),
106 new Vector3f(0, 0, 14),
107 new Vector3f(6, 0, 20),
108 new Vector3f(0, 0, 26),
109 new Vector3f(-6, 0, 20),
110 new Vector3f(0, 0, 14),
111 // begin ellipse
112 new Vector3f(16, 5, 20),
113 new Vector3f(0, 0, 26),
114 new Vector3f(-16, -10, 20),
115 new Vector3f(0, 0, 14),
116 new Vector3f(16, 20, 20),
117 new Vector3f(0, 0, 26),
118 new Vector3f(-10, -25, 10),
119 new Vector3f(-10, 0, 0),
120 // come at me bro!
121 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
122 new Vector3f(0, 0 , -20),
123 };
127 private void createScene() {
128 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
129 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
130 mat.setColor("Color", ColorRGBA.Blue);
131 bell.setMaterial(mat);
132 rootNode.attachChild(bell);
134 DirectionalLight light = new DirectionalLight();
135 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal());
136 light.setColor(ColorRGBA.White.mult(1.5f));
137 rootNode.addLight(light);
139 makeEar(rootNode, new Vector3f(0, 0 ,20));
140 makeEar(rootNode, new Vector3f(0, 0 ,-20));
141 makeEar(rootNode, new Vector3f(20, 0 ,0));
142 makeEar(rootNode, new Vector3f(-20, 0 ,0));
144 MotionPath track = new MotionPath();
146 for (Vector3f v : path){
147 track.addWayPoint(v);
148 }
151 track.setCurveTension(0.80f);
154 motionControl = new MotionTrack(bell,track);
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 track.enableDebugShape(assetManager, rootNode);
164 positionCamera();
167 }
170 private void positionCamera(){
171 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
172 // cam.setLocation(new Vector3f(0,0,-20));
173 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
174 }
176 private AudioNode music;
182 private void initAudio() {
184 music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false);
186 rootNode.attachChild(music);
187 audioRenderer.playSource(music);
189 music.setVolume(1f);
190 music.setPositional(true);
191 music.setMaxDistance(200.0f);
192 music.setRefDistance(0.1f);
193 music.setRolloffFactor(5f);
194 audioRenderer.pauseSource(music);
196 }
201 private Listener auxListener = new Listener();
202 public File data1 = new File("/home/r/tmp/data1.wav");
203 public File data2 = new File("/home/r/tmp/data2.wav");
204 public File data3 = new File("/home/r/tmp/data3.wav");
205 public File data4 = new File("/home/r/tmp/data4.wav");
206 public File data5 = new File("/home/r/tmp/data5.wav");
207 public File data6 = new File("/home/r/tmp/data6.wav");
210 public void simpleInitApp() {
211 this.setTimer(new IsoTimer(60));
213 if (this.audioRenderer instanceof MultiListener){
214 MultiListener rf = (MultiListener)this.audioRenderer;
216 for (int n = 0; n < 0; n++){
217 Listener zzz = new Listener();
218 rf.addListener(zzz);
219 }
220 Listener listener3 = new Listener();
221 rf.addListener(auxListener);
222 rf.addListener(listener3);
223 rf.registerSoundProcessor(new WaveFileWriter(data1));
224 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
225 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
226 }
227 initAudio();
228 initKeys();
229 createScene();
230 motionControl.play();
231 }
237 private void initKeys() {
238 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
239 inputManager.addListener(actionListener, "Shoot");
240 }
242 /** Defining the "Shoot" action: Play a gun sound. */
243 private ActionListener actionListener = new ActionListener() {
244 @Override
245 public void onAction(String name, boolean keyPressed, float tpf) {
246 if (name.equals("Shoot") && !keyPressed) {
247 System.out.println("I'm playing! <3");
248 System.out.println(bell.getLocalTranslation().subtract(cam.getLocation()).length());
250 audioRenderer.playSource(music);
251 System.out.println(music.getRefDistance());
253 }
254 }
255 };
257 /** Move the listener with the camera - for 3D audio. */
260 private Vector3f prevBellPos = Vector3f.ZERO;
261 public void simpleUpdate(float tpf) {
262 //Vector3f loc = cam.getLocation();
263 //Quaternion rot = cam.getRotation();
264 //listener.setLocation(loc);
265 //listener.setRotation(rot);
268 listener.setLocation(cam.getLocation());
269 listener.setRotation(cam.getRotation());
270 //auxListener.setLocation(loc);
271 //auxListener.setRotation(rot);
272 //if (music.getStatus() == AudioNode.Status.Stopped){
274 //music.playInstance();
275 //}
276 //audioRenderer.updateSourceParam(music, AudioParam.Direction);
278 Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf);
279 prevBellPos = bell.getLocalTranslation();
281 music.setLocalTranslation(bell.getLocalTranslation());
282 music.setVelocity(bellVelocity);
284 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f);
285 //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f);
287 }
289 }