annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 22:c7a07eefaeea

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