annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 37:094a92b556a2

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