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