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