annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 33:c4bfbf5d090e

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