annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 18:2543c95a0fd2

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