rlm@41: package com.aurellem.capture.examples; rlm@41: rlm@41: import java.io.File; rlm@41: import java.io.FileNotFoundException; rlm@41: import java.io.IOException; rlm@42: import java.lang.reflect.Field; rlm@41: import java.nio.ByteBuffer; rlm@41: rlm@41: import javax.sound.sampled.AudioFormat; rlm@41: rlm@41: import org.tritonus.share.sampled.FloatSampleTools; rlm@41: rlm@45: import com.aurellem.capture.AurellemSystemDelegate; rlm@41: import com.aurellem.capture.Capture; rlm@41: import com.aurellem.capture.IsoTimer; rlm@41: import com.aurellem.capture.audio.CompositeSoundProcessor; rlm@41: import com.aurellem.capture.audio.MultiListener; rlm@41: import com.aurellem.capture.audio.SoundProcessor; rlm@41: import com.aurellem.capture.audio.WaveFileWriter; rlm@41: import com.jme3.app.SimpleApplication; rlm@41: import com.jme3.audio.AudioNode; rlm@41: import com.jme3.audio.Listener; rlm@41: import com.jme3.cinematic.MotionPath; rlm@42: import com.jme3.cinematic.events.AbstractCinematicEvent; rlm@41: import com.jme3.cinematic.events.MotionTrack; rlm@41: import com.jme3.material.Material; rlm@41: import com.jme3.math.ColorRGBA; rlm@41: import com.jme3.math.FastMath; rlm@41: import com.jme3.math.Quaternion; rlm@41: import com.jme3.math.Vector3f; rlm@41: import com.jme3.scene.Geometry; rlm@41: import com.jme3.scene.Node; rlm@41: import com.jme3.scene.shape.Box; rlm@41: import com.jme3.scene.shape.Sphere; rlm@41: import com.jme3.system.AppSettings; rlm@45: import com.jme3.system.JmeSystem; rlm@41: rlm@41: rlm@41: /** rlm@41: * rlm@41: * Demonstrates advanced use of the audio capture and recording features. rlm@41: * Multiple perspectives of the same scene are simultaneously rendered to rlm@41: * different sound files. rlm@41: * rlm@41: * A key limitation of the way multiple listeners are implemented is that rlm@41: * only 3D positioning effects are realized for listeners other than the rlm@41: * main LWJGL listener. This means that audio effects such as environment rlm@41: * settings will *not* be heard on any auxiliary listeners, though sound rlm@41: * attenuation will work correctly. rlm@41: * rlm@41: * Multiple listeners as realized here might be used to make AI entities rlm@41: * that can each hear the world from their own perspective. rlm@41: * rlm@41: * @author Robert McIntyre rlm@41: * rlm@41: */ rlm@41: rlm@41: public class Advanced extends SimpleApplication { rlm@41: rlm@41: rlm@41: private Geometry bell; rlm@41: private Geometry ear1; rlm@41: private Geometry ear2; rlm@41: private Geometry ear3; rlm@41: private AudioNode music; rlm@41: private MotionTrack motionControl; rlm@41: rlm@41: public static void main(String[] args) { rlm@44: //Logger.getLogger("com.jme3").setLevel(Level.OFF); rlm@41: Advanced app = new Advanced(); rlm@41: AppSettings settings = new AppSettings(true); rlm@41: settings.setAudioRenderer("Send"); rlm@41: app.setSettings(settings); rlm@41: app.setShowSettings(false); rlm@41: app.setPauseOnLostFocus(false); rlm@43: rlm@43: rlm@43: rlm@41: try { rlm@41: Capture.captureVideo(app, File.createTempFile("advanced",".avi")); rlm@41: Capture.captureAudio(app, File.createTempFile("advanced", ".wav")); rlm@41: } rlm@41: catch (IOException e) {e.printStackTrace();} rlm@43: rlm@43: app.start(); rlm@43: } rlm@43: rlm@43: public static void test(){ rlm@43: Advanced app = new Advanced(); rlm@43: AppSettings settings = new AppSettings(true); rlm@43: settings.setAudioRenderer("Send"); rlm@45: JmeSystem.setSystemDelegate(new AurellemSystemDelegate()); rlm@43: app.setSettings(settings); rlm@43: app.setShowSettings(false); rlm@43: app.setPauseOnLostFocus(false); rlm@41: app.start(); rlm@41: } rlm@41: rlm@41: private Geometry makeEar(Node root, Vector3f position){ rlm@41: Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); rlm@41: Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f)); rlm@41: ear.setLocalTranslation(position); rlm@41: mat.setColor("Color", ColorRGBA.Green); rlm@41: ear.setMaterial(mat); rlm@41: root.attachChild(ear); rlm@41: return ear; rlm@41: } rlm@41: rlm@41: private Vector3f[] path = new Vector3f[]{ rlm@41: // loop 1 rlm@41: new Vector3f(0, 0, 0), rlm@41: new Vector3f(0, 0, -10), rlm@41: new Vector3f(-2, 0, -14), rlm@41: new Vector3f(-6, 0, -20), rlm@41: new Vector3f(0, 0, -26), rlm@41: new Vector3f(6, 0, -20), rlm@41: new Vector3f(0, 0, -14), rlm@41: new Vector3f(-6, 0, -20), rlm@41: new Vector3f(0, 0, -26), rlm@41: new Vector3f(6, 0, -20), rlm@41: // loop 2 rlm@41: new Vector3f(5, 0, -5), rlm@41: new Vector3f(7, 0, 1.5f), rlm@41: new Vector3f(14, 0, 2), rlm@41: new Vector3f(20, 0, 6), rlm@41: new Vector3f(26, 0, 0), rlm@41: new Vector3f(20, 0, -6), rlm@41: new Vector3f(14, 0, 0), rlm@41: new Vector3f(20, 0, 6), rlm@41: new Vector3f(26, 0, 0), rlm@41: new Vector3f(20, 0, -6), rlm@41: new Vector3f(14, 0, 0), rlm@41: // loop 3 rlm@41: new Vector3f(8, 0, 7.5f), rlm@41: new Vector3f(7, 0, 10.5f), rlm@41: new Vector3f(6, 0, 20), rlm@41: new Vector3f(0, 0, 26), rlm@41: new Vector3f(-6, 0, 20), rlm@41: new Vector3f(0, 0, 14), rlm@41: new Vector3f(6, 0, 20), rlm@41: new Vector3f(0, 0, 26), rlm@41: new Vector3f(-6, 0, 20), rlm@41: new Vector3f(0, 0, 14), rlm@41: // begin ellipse rlm@41: new Vector3f(16, 5, 20), rlm@41: new Vector3f(0, 0, 26), rlm@41: new Vector3f(-16, -10, 20), rlm@41: new Vector3f(0, 0, 14), rlm@41: new Vector3f(16, 20, 20), rlm@41: new Vector3f(0, 0, 26), rlm@41: new Vector3f(-10, -25, 10), rlm@41: new Vector3f(-10, 0, 0), rlm@41: // come at me! rlm@41: new Vector3f(-28.00242f, 48.005623f, -34.648228f), rlm@41: new Vector3f(0, 0 , -20), rlm@41: }; rlm@41: rlm@41: private void createScene() { rlm@41: Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); rlm@41: bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); rlm@41: mat.setColor("Color", ColorRGBA.Blue); rlm@41: bell.setMaterial(mat); rlm@41: rootNode.attachChild(bell); rlm@41: rlm@41: ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20)); rlm@41: ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20)); rlm@41: ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0)); rlm@41: rlm@41: MotionPath track = new MotionPath(); rlm@41: rlm@41: for (Vector3f v : path){ rlm@41: track.addWayPoint(v); rlm@41: } rlm@41: track.setCurveTension(0.80f); rlm@41: rlm@41: motionControl = new MotionTrack(bell,track); rlm@42: rlm@42: // for now, use reflection to change the timer... rlm@42: //motionControl.setTimer(new IsoTimer(60)); rlm@42: try { rlm@42: Field timerField; rlm@42: timerField = AbstractCinematicEvent.class.getDeclaredField("timer"); rlm@42: timerField.setAccessible(true); rlm@42: try {timerField.set(motionControl, new IsoTimer(60));} rlm@42: catch (IllegalArgumentException e) {e.printStackTrace();} rlm@42: catch (IllegalAccessException e) {e.printStackTrace();} rlm@42: } rlm@42: catch (SecurityException e) {e.printStackTrace();} rlm@42: catch (NoSuchFieldException e) {e.printStackTrace();} rlm@42: rlm@41: motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation); rlm@41: motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y)); rlm@41: motionControl.setInitialDuration(20f); rlm@41: motionControl.setSpeed(1f); rlm@41: rlm@41: track.enableDebugShape(assetManager, rootNode); rlm@41: positionCamera(); rlm@41: } rlm@41: rlm@41: rlm@41: private void positionCamera(){ rlm@41: this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f)); rlm@41: this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f)); rlm@41: } rlm@41: rlm@41: private void initAudio() { rlm@41: org.lwjgl.input.Mouse.setGrabbed(false); rlm@41: music = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", false); rlm@41: rlm@41: rootNode.attachChild(music); rlm@41: audioRenderer.playSource(music); rlm@41: music.setPositional(true); rlm@41: music.setVolume(1f); rlm@41: music.setReverbEnabled(false); rlm@41: music.setDirectional(false); rlm@41: music.setMaxDistance(200.0f); rlm@41: music.setRefDistance(1f); rlm@44: //music.setRolloffFactor(1f); rlm@41: music.setLooping(false); rlm@41: audioRenderer.pauseSource(music); rlm@41: } rlm@41: rlm@41: public class Dancer implements SoundProcessor { rlm@41: Geometry entity; rlm@41: float scale = 2; rlm@41: public Dancer(Geometry entity){ rlm@41: this.entity = entity; rlm@41: } rlm@41: rlm@41: /** rlm@41: * this method is irrelevant since there is no state to cleanup. rlm@41: */ rlm@41: public void cleanup() {} rlm@41: rlm@41: rlm@41: /** rlm@41: * Respond to sound! This is the brain of an AI entity that rlm@41: * hears it's surroundings and reacts to them. rlm@41: */ rlm@41: public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) { rlm@41: audioSamples.clear(); rlm@41: byte[] data = new byte[numSamples]; rlm@41: float[] out = new float[numSamples]; rlm@41: audioSamples.get(data); rlm@41: FloatSampleTools.byte2floatInterleaved(data, 0, out, 0, rlm@41: numSamples/format.getFrameSize(), format); rlm@41: rlm@41: float max = Float.NEGATIVE_INFINITY; rlm@41: for (float f : out){if (f > max) max = f;} rlm@41: audioSamples.clear(); rlm@41: rlm@41: if (max > 0.1){entity.getMaterial().setColor("Color", ColorRGBA.Green);} rlm@41: else {entity.getMaterial().setColor("Color", ColorRGBA.Gray);} rlm@41: } rlm@41: } rlm@41: rlm@41: private void prepareEar(Geometry ear, int n){ rlm@41: if (this.audioRenderer instanceof MultiListener){ rlm@41: MultiListener rf = (MultiListener)this.audioRenderer; rlm@41: rlm@41: Listener auxListener = new Listener(); rlm@41: auxListener.setLocation(ear.getLocalTranslation()); rlm@41: rlm@41: rf.addListener(auxListener); rlm@41: WaveFileWriter aux = null; rlm@41: rlm@41: try {aux = new WaveFileWriter(new File("/home/r/tmp/ear"+n+".wav"));} rlm@41: catch (FileNotFoundException e) {e.printStackTrace();} rlm@41: rlm@41: rf.registerSoundProcessor(auxListener, rlm@41: new CompositeSoundProcessor(new Dancer(ear), aux)); rlm@41: } rlm@41: } rlm@41: rlm@41: rlm@41: public void simpleInitApp() { rlm@41: this.setTimer(new IsoTimer(60)); rlm@41: initAudio(); rlm@41: rlm@41: createScene(); rlm@41: rlm@41: prepareEar(ear1, 1); rlm@41: prepareEar(ear2, 1); rlm@41: prepareEar(ear3, 1); rlm@41: rlm@41: motionControl.play(); rlm@41: } rlm@41: rlm@41: public void simpleUpdate(float tpf) { rlm@41: if (music.getStatus() != AudioNode.Status.Playing){ rlm@41: music.play(); rlm@41: } rlm@41: Vector3f loc = cam.getLocation(); rlm@41: Quaternion rot = cam.getRotation(); rlm@41: listener.setLocation(loc); rlm@41: listener.setRotation(rot); rlm@41: music.setLocalTranslation(bell.getLocalTranslation()); rlm@41: } rlm@41: rlm@41: }