annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 28:7184bc09a92e

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