annotate src/com/aurellem/capture/examples/Advanced.java @ 41:58386a64d019

renamed stuff
author Robert McIntyre <rlm@mit.edu>
date Thu, 03 Nov 2011 16:55:43 -0700
parents
children b1bc965a38d2
rev   line source
rlm@41 1 package com.aurellem.capture.examples;
rlm@41 2
rlm@41 3 import java.io.File;
rlm@41 4 import java.io.FileNotFoundException;
rlm@41 5 import java.io.IOException;
rlm@41 6 import java.nio.ByteBuffer;
rlm@41 7 import java.util.logging.Level;
rlm@41 8 import java.util.logging.Logger;
rlm@41 9
rlm@41 10 import javax.sound.sampled.AudioFormat;
rlm@41 11
rlm@41 12 import org.tritonus.share.sampled.FloatSampleTools;
rlm@41 13
rlm@41 14 import com.aurellem.capture.Capture;
rlm@41 15 import com.aurellem.capture.IsoTimer;
rlm@41 16 import com.aurellem.capture.audio.CompositeSoundProcessor;
rlm@41 17 import com.aurellem.capture.audio.MultiListener;
rlm@41 18 import com.aurellem.capture.audio.SoundProcessor;
rlm@41 19 import com.aurellem.capture.audio.WaveFileWriter;
rlm@41 20 import com.jme3.app.SimpleApplication;
rlm@41 21 import com.jme3.audio.AudioNode;
rlm@41 22 import com.jme3.audio.Listener;
rlm@41 23 import com.jme3.cinematic.MotionPath;
rlm@41 24 import com.jme3.cinematic.events.MotionTrack;
rlm@41 25 import com.jme3.material.Material;
rlm@41 26 import com.jme3.math.ColorRGBA;
rlm@41 27 import com.jme3.math.FastMath;
rlm@41 28 import com.jme3.math.Quaternion;
rlm@41 29 import com.jme3.math.Vector3f;
rlm@41 30 import com.jme3.scene.Geometry;
rlm@41 31 import com.jme3.scene.Node;
rlm@41 32 import com.jme3.scene.shape.Box;
rlm@41 33 import com.jme3.scene.shape.Sphere;
rlm@41 34 import com.jme3.system.AppSettings;
rlm@41 35
rlm@41 36
rlm@41 37 /**
rlm@41 38 *
rlm@41 39 * Demonstrates advanced use of the audio capture and recording features.
rlm@41 40 * Multiple perspectives of the same scene are simultaneously rendered to
rlm@41 41 * different sound files.
rlm@41 42 *
rlm@41 43 * A key limitation of the way multiple listeners are implemented is that
rlm@41 44 * only 3D positioning effects are realized for listeners other than the
rlm@41 45 * main LWJGL listener. This means that audio effects such as environment
rlm@41 46 * settings will *not* be heard on any auxiliary listeners, though sound
rlm@41 47 * attenuation will work correctly.
rlm@41 48 *
rlm@41 49 * Multiple listeners as realized here might be used to make AI entities
rlm@41 50 * that can each hear the world from their own perspective.
rlm@41 51 *
rlm@41 52 * @author Robert McIntyre
rlm@41 53 *
rlm@41 54 */
rlm@41 55
rlm@41 56 public class Advanced extends SimpleApplication {
rlm@41 57
rlm@41 58
rlm@41 59 private Geometry bell;
rlm@41 60 private Geometry ear1;
rlm@41 61 private Geometry ear2;
rlm@41 62 private Geometry ear3;
rlm@41 63 private AudioNode music;
rlm@41 64 private MotionTrack motionControl;
rlm@41 65
rlm@41 66 public static void main(String[] args) {
rlm@41 67 Logger.getLogger("com.jme3").setLevel(Level.OFF);
rlm@41 68 Advanced app = new Advanced();
rlm@41 69 AppSettings settings = new AppSettings(true);
rlm@41 70 settings.setAudioRenderer("Send");
rlm@41 71 app.setSettings(settings);
rlm@41 72 app.setShowSettings(false);
rlm@41 73 app.setPauseOnLostFocus(false);
rlm@41 74
rlm@41 75 try {
rlm@41 76 Capture.captureVideo(app, File.createTempFile("advanced",".avi"));
rlm@41 77 Capture.captureAudio(app, File.createTempFile("advanced", ".wav"));
rlm@41 78 }
rlm@41 79 catch (IOException e) {e.printStackTrace();}
rlm@41 80 app.start();
rlm@41 81 }
rlm@41 82
rlm@41 83 private Geometry makeEar(Node root, Vector3f position){
rlm@41 84 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@41 85 Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f));
rlm@41 86 ear.setLocalTranslation(position);
rlm@41 87 mat.setColor("Color", ColorRGBA.Green);
rlm@41 88 ear.setMaterial(mat);
rlm@41 89 root.attachChild(ear);
rlm@41 90 return ear;
rlm@41 91 }
rlm@41 92
rlm@41 93 private Vector3f[] path = new Vector3f[]{
rlm@41 94 // loop 1
rlm@41 95 new Vector3f(0, 0, 0),
rlm@41 96 new Vector3f(0, 0, -10),
rlm@41 97 new Vector3f(-2, 0, -14),
rlm@41 98 new Vector3f(-6, 0, -20),
rlm@41 99 new Vector3f(0, 0, -26),
rlm@41 100 new Vector3f(6, 0, -20),
rlm@41 101 new Vector3f(0, 0, -14),
rlm@41 102 new Vector3f(-6, 0, -20),
rlm@41 103 new Vector3f(0, 0, -26),
rlm@41 104 new Vector3f(6, 0, -20),
rlm@41 105 // loop 2
rlm@41 106 new Vector3f(5, 0, -5),
rlm@41 107 new Vector3f(7, 0, 1.5f),
rlm@41 108 new Vector3f(14, 0, 2),
rlm@41 109 new Vector3f(20, 0, 6),
rlm@41 110 new Vector3f(26, 0, 0),
rlm@41 111 new Vector3f(20, 0, -6),
rlm@41 112 new Vector3f(14, 0, 0),
rlm@41 113 new Vector3f(20, 0, 6),
rlm@41 114 new Vector3f(26, 0, 0),
rlm@41 115 new Vector3f(20, 0, -6),
rlm@41 116 new Vector3f(14, 0, 0),
rlm@41 117 // loop 3
rlm@41 118 new Vector3f(8, 0, 7.5f),
rlm@41 119 new Vector3f(7, 0, 10.5f),
rlm@41 120 new Vector3f(6, 0, 20),
rlm@41 121 new Vector3f(0, 0, 26),
rlm@41 122 new Vector3f(-6, 0, 20),
rlm@41 123 new Vector3f(0, 0, 14),
rlm@41 124 new Vector3f(6, 0, 20),
rlm@41 125 new Vector3f(0, 0, 26),
rlm@41 126 new Vector3f(-6, 0, 20),
rlm@41 127 new Vector3f(0, 0, 14),
rlm@41 128 // begin ellipse
rlm@41 129 new Vector3f(16, 5, 20),
rlm@41 130 new Vector3f(0, 0, 26),
rlm@41 131 new Vector3f(-16, -10, 20),
rlm@41 132 new Vector3f(0, 0, 14),
rlm@41 133 new Vector3f(16, 20, 20),
rlm@41 134 new Vector3f(0, 0, 26),
rlm@41 135 new Vector3f(-10, -25, 10),
rlm@41 136 new Vector3f(-10, 0, 0),
rlm@41 137 // come at me!
rlm@41 138 new Vector3f(-28.00242f, 48.005623f, -34.648228f),
rlm@41 139 new Vector3f(0, 0 , -20),
rlm@41 140 };
rlm@41 141
rlm@41 142 private void createScene() {
rlm@41 143 Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@41 144 bell = new Geometry( "sound-emitter" , new Sphere(15,15,1));
rlm@41 145 mat.setColor("Color", ColorRGBA.Blue);
rlm@41 146 bell.setMaterial(mat);
rlm@41 147 rootNode.attachChild(bell);
rlm@41 148
rlm@41 149 ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20));
rlm@41 150 ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20));
rlm@41 151 ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0));
rlm@41 152
rlm@41 153 MotionPath track = new MotionPath();
rlm@41 154
rlm@41 155 for (Vector3f v : path){
rlm@41 156 track.addWayPoint(v);
rlm@41 157 }
rlm@41 158 track.setCurveTension(0.80f);
rlm@41 159
rlm@41 160 motionControl = new MotionTrack(bell,track);
rlm@41 161 motionControl.setTimer(new IsoTimer(60));
rlm@41 162 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
rlm@41 163 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
rlm@41 164 motionControl.setInitialDuration(20f);
rlm@41 165 motionControl.setSpeed(1f);
rlm@41 166
rlm@41 167 track.enableDebugShape(assetManager, rootNode);
rlm@41 168 positionCamera();
rlm@41 169 }
rlm@41 170
rlm@41 171
rlm@41 172 private void positionCamera(){
rlm@41 173 this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f));
rlm@41 174 this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f));
rlm@41 175 }
rlm@41 176
rlm@41 177 private void initAudio() {
rlm@41 178 org.lwjgl.input.Mouse.setGrabbed(false);
rlm@41 179 music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false);
rlm@41 180
rlm@41 181 rootNode.attachChild(music);
rlm@41 182 audioRenderer.playSource(music);
rlm@41 183 music.setPositional(true);
rlm@41 184 music.setVolume(1f);
rlm@41 185 music.setReverbEnabled(false);
rlm@41 186 music.setDirectional(false);
rlm@41 187 music.setMaxDistance(200.0f);
rlm@41 188 music.setRefDistance(1f);
rlm@41 189 music.setRolloffFactor(1f);
rlm@41 190 music.setLooping(false);
rlm@41 191 audioRenderer.pauseSource(music);
rlm@41 192 }
rlm@41 193
rlm@41 194
rlm@41 195
rlm@41 196
rlm@41 197
rlm@41 198
rlm@41 199 public class Dancer implements SoundProcessor {
rlm@41 200 Geometry entity;
rlm@41 201 float scale = 2;
rlm@41 202 public Dancer(Geometry entity){
rlm@41 203 this.entity = entity;
rlm@41 204 }
rlm@41 205
rlm@41 206 /**
rlm@41 207 * this method is irrelevant since there is no state to cleanup.
rlm@41 208 */
rlm@41 209 public void cleanup() {}
rlm@41 210
rlm@41 211
rlm@41 212 /**
rlm@41 213 * Respond to sound! This is the brain of an AI entity that
rlm@41 214 * hears it's surroundings and reacts to them.
rlm@41 215 */
rlm@41 216 public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) {
rlm@41 217 audioSamples.clear();
rlm@41 218 byte[] data = new byte[numSamples];
rlm@41 219 float[] out = new float[numSamples];
rlm@41 220 audioSamples.get(data);
rlm@41 221 FloatSampleTools.byte2floatInterleaved(data, 0, out, 0,
rlm@41 222 numSamples/format.getFrameSize(), format);
rlm@41 223
rlm@41 224 float max = Float.NEGATIVE_INFINITY;
rlm@41 225 for (float f : out){if (f > max) max = f;}
rlm@41 226 audioSamples.clear();
rlm@41 227
rlm@41 228 if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);}
rlm@41 229 else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);}
rlm@41 230 }
rlm@41 231 }
rlm@41 232
rlm@41 233 private void prepareEar(Geometry ear, int n){
rlm@41 234 if (this.audioRenderer instanceof MultiListener){
rlm@41 235 MultiListener rf = (MultiListener)this.audioRenderer;
rlm@41 236
rlm@41 237 Listener auxListener = new Listener();
rlm@41 238 auxListener.setLocation(ear.getLocalTranslation());
rlm@41 239
rlm@41 240 rf.addListener(auxListener);
rlm@41 241 WaveFileWriter aux = null;
rlm@41 242
rlm@41 243 try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));}
rlm@41 244 catch (FileNotFoundException e) {e.printStackTrace();}
rlm@41 245
rlm@41 246 rf.registerSoundProcessor(auxListener,
rlm@41 247 new CompositeSoundProcessor(new Dancer(ear), aux));
rlm@41 248
rlm@41 249 }
rlm@41 250 }
rlm@41 251
rlm@41 252
rlm@41 253 public void simpleInitApp() {
rlm@41 254 this.setTimer(new IsoTimer(60));
rlm@41 255 initAudio();
rlm@41 256
rlm@41 257 createScene();
rlm@41 258
rlm@41 259 prepareEar(ear1, 1);
rlm@41 260 prepareEar(ear2, 1);
rlm@41 261 prepareEar(ear3, 1);
rlm@41 262
rlm@41 263 motionControl.play();
rlm@41 264 }
rlm@41 265
rlm@41 266 public void simpleUpdate(float tpf) {
rlm@41 267 if (music.getStatus() != AudioNode.Status.Playing){
rlm@41 268 music.play();
rlm@41 269 }
rlm@41 270 Vector3f loc = cam.getLocation();
rlm@41 271 Quaternion rot = cam.getRotation();
rlm@41 272 listener.setLocation(loc);
rlm@41 273 listener.setRotation(rot);
rlm@41 274 music.setLocalTranslation(bell.getLocalTranslation());
rlm@41 275 }
rlm@41 276
rlm@41 277 }