annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 26:95648b0c12bc

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