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