annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 36:2a525e937d86

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