rlm@41: package com.aurellem.capture.examples; rlm@41: rlm@41: import java.io.File; 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@56: * Demonstrates advanced use of the audio capture and recording rlm@56: * features. Multiple perspectives of the same scene are rlm@56: * simultaneously rendered to different sound files. rlm@41: * rlm@56: * A key limitation of the way multiple listeners are implemented is rlm@56: * that only 3D positioning effects are realized for listeners other rlm@56: * than the main LWJGL listener. This means that audio effects such rlm@56: * as environment settings will *not* be heard on any auxiliary rlm@56: * listeners, though sound attenuation will work correctly. rlm@41: * rlm@56: * Multiple listeners as realized here might be used to make AI rlm@56: * entities that can each hear the world from their own perspective. rlm@41: * rlm@41: * @author Robert McIntyre rlm@41: */ rlm@41: rlm@41: public class Advanced extends SimpleApplication { rlm@41: rlm@65: /** rlm@65: * You will see three grey cubes, a blue sphere, and a path which rlm@65: * circles each cube. The blue sphere is generating a constant rlm@65: * monotone sound as it moves along the track. Each cube is rlm@65: * listening for sound; when a cube hears sound whose intensity is rlm@65: * greater than a certain threshold, it changes its color from rlm@65: * grey to green. rlm@65: * rlm@65: * Each cube is also saving whatever it hears to a file. The rlm@65: * scene from the perspective of the viewer is also saved to a rlm@65: * video file. When you listen to each of the sound files rlm@65: * alongside the video, the sound will get louder when the sphere rlm@65: * approaches the cube that generated that sound file. This rlm@65: * shows that each listener is hearing the world from its own rlm@65: * perspective. rlm@65: * rlm@65: */ rlm@65: public static void main(String[] args) { rlm@65: Advanced app = new Advanced(); rlm@65: AppSettings settings = new AppSettings(true); rlm@65: settings.setAudioRenderer(AurellemSystemDelegate.SEND); rlm@65: JmeSystem.setSystemDelegate(new AurellemSystemDelegate()); rlm@65: app.setSettings(settings); rlm@65: app.setShowSettings(false); rlm@65: app.setPauseOnLostFocus(false); rlm@43: rlm@65: try { rlm@65: //Capture.captureVideo(app, File.createTempFile("advanced",".avi")); rlm@65: Capture.captureAudio(app, File.createTempFile("advanced",".wav")); rlm@65: } rlm@65: catch (IOException e) {e.printStackTrace();} rlm@62: rlm@65: app.start(); rlm@65: } rlm@65: rlm@65: private Geometry bell; rlm@65: private Geometry ear1; rlm@65: private Geometry ear2; rlm@65: private Geometry ear3; rlm@65: private AudioNode music; rlm@65: private MotionTrack motionControl; rlm@65: private IsoTimer motionTimer = new IsoTimer(60); rlm@65: rlm@65: private Geometry makeEar(Node root, Vector3f position){ rlm@65: Material mat = new Material(assetManager, rlm@65: "Common/MatDefs/Misc/Unshaded.j3md"); rlm@65: Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f)); rlm@65: ear.setLocalTranslation(position); rlm@65: mat.setColor("Color", ColorRGBA.Green); rlm@65: ear.setMaterial(mat); rlm@65: root.attachChild(ear); rlm@65: return ear; rlm@65: } rlm@65: rlm@65: private Vector3f[] path = new Vector3f[]{ rlm@65: // loop 1 rlm@65: new Vector3f(0, 0, 0), rlm@65: new Vector3f(0, 0, -10), rlm@65: new Vector3f(-2, 0, -14), rlm@65: new Vector3f(-6, 0, -20), rlm@65: new Vector3f(0, 0, -26), rlm@65: new Vector3f(6, 0, -20), rlm@65: new Vector3f(0, 0, -14), rlm@65: new Vector3f(-6, 0, -20), rlm@65: new Vector3f(0, 0, -26), rlm@65: new Vector3f(6, 0, -20), rlm@65: // loop 2 rlm@65: new Vector3f(5, 0, -5), rlm@65: new Vector3f(7, 0, 1.5f), rlm@65: new Vector3f(14, 0, 2), rlm@65: new Vector3f(20, 0, 6), rlm@65: new Vector3f(26, 0, 0), rlm@65: new Vector3f(20, 0, -6), rlm@65: new Vector3f(14, 0, 0), rlm@65: new Vector3f(20, 0, 6), rlm@65: new Vector3f(26, 0, 0), rlm@65: new Vector3f(20, 0, -6), rlm@65: new Vector3f(14, 0, 0), rlm@65: // loop 3 rlm@65: new Vector3f(8, 0, 7.5f), rlm@65: new Vector3f(7, 0, 10.5f), rlm@65: new Vector3f(6, 0, 20), rlm@65: new Vector3f(0, 0, 26), rlm@65: new Vector3f(-6, 0, 20), rlm@65: new Vector3f(0, 0, 14), rlm@65: new Vector3f(6, 0, 20), rlm@65: new Vector3f(0, 0, 26), rlm@65: new Vector3f(-6, 0, 20), rlm@65: new Vector3f(0, 0, 14), rlm@65: // begin ellipse rlm@65: new Vector3f(16, 5, 20), rlm@65: new Vector3f(0, 0, 26), rlm@65: new Vector3f(-16, -10, 20), rlm@65: new Vector3f(0, 0, 14), rlm@65: new Vector3f(16, 20, 20), rlm@65: new Vector3f(0, 0, 26), rlm@65: new Vector3f(-10, -25, 10), rlm@65: new Vector3f(-10, 0, 0), rlm@65: // come at me! rlm@65: new Vector3f(-28.00242f, 48.005623f, -34.648228f), rlm@65: new Vector3f(0, 0 , -20), rlm@65: }; rlm@65: rlm@65: private void createScene() { rlm@65: Material mat = new Material(assetManager, rlm@65: "Common/MatDefs/Misc/Unshaded.j3md"); rlm@65: bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); rlm@65: mat.setColor("Color", ColorRGBA.Blue); rlm@65: bell.setMaterial(mat); rlm@65: rootNode.attachChild(bell); rlm@65: rlm@65: ear1 = makeEar(rootNode, new Vector3f(0, 0 ,-20)); rlm@65: ear2 = makeEar(rootNode, new Vector3f(0, 0 ,20)); rlm@65: ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0)); rlm@65: rlm@65: MotionPath track = new MotionPath(); rlm@65: rlm@65: for (Vector3f v : path){ rlm@65: track.addWayPoint(v); rlm@65: } rlm@65: track.setCurveTension(0.80f); rlm@65: rlm@65: motionControl = new MotionTrack(bell,track); rlm@65: // for now, use reflection to change the timer... rlm@65: // motionControl.setTimer(new IsoTimer(60)); rlm@65: rlm@65: try { rlm@65: Field timerField; rlm@65: timerField = rlm@65: AbstractCinematicEvent.class.getDeclaredField("timer"); rlm@65: timerField.setAccessible(true); rlm@65: try {timerField.set(motionControl, motionTimer);} rlm@65: catch (IllegalArgumentException e) {e.printStackTrace();} rlm@65: catch (IllegalAccessException e) {e.printStackTrace();} rlm@65: } rlm@65: catch (SecurityException e) {e.printStackTrace();} rlm@65: catch (NoSuchFieldException e) {e.printStackTrace();} rlm@65: rlm@65: rlm@65: motionControl.setDirectionType rlm@65: (MotionTrack.Direction.PathAndRotation); rlm@65: motionControl.setRotation rlm@65: (new Quaternion().fromAngleNormalAxis rlm@65: (-FastMath.HALF_PI, Vector3f.UNIT_Y)); rlm@65: motionControl.setInitialDuration(20f); rlm@65: motionControl.setSpeed(1f); rlm@65: rlm@65: track.enableDebugShape(assetManager, rootNode); rlm@65: positionCamera(); rlm@65: } rlm@65: rlm@65: private void positionCamera(){ rlm@65: this.cam.setLocation rlm@65: (new Vector3f(-28.00242f, 48.005623f, -34.648228f)); rlm@65: this.cam.setRotation rlm@65: (new Quaternion rlm@65: (0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f)); rlm@65: } rlm@65: rlm@65: private void initAudio() { rlm@65: org.lwjgl.input.Mouse.setGrabbed(false); rlm@65: music = new AudioNode(assetManager, rlm@65: "Sound/Effects/Beep.ogg", false); rlm@65: rootNode.attachChild(music); rlm@65: audioRenderer.playSource(music); rlm@65: music.setPositional(true); rlm@65: music.setVolume(1f); rlm@65: music.setReverbEnabled(false); rlm@65: music.setDirectional(false); rlm@65: music.setMaxDistance(200.0f); rlm@65: music.setRefDistance(1f); rlm@65: //music.setRolloffFactor(1f); rlm@65: music.setLooping(false); rlm@65: audioRenderer.pauseSource(music); rlm@65: } rlm@65: rlm@65: public class Dancer implements SoundProcessor { rlm@65: Geometry entity; rlm@65: float scale = 2; rlm@65: public Dancer(Geometry entity){ rlm@65: this.entity = entity; rlm@43: } rlm@41: rlm@65: /** rlm@65: * this method is irrelevant since there is no state to cleanup. rlm@65: */ rlm@65: public void cleanup() {} rlm@56: rlm@56: rlm@65: /** rlm@65: * Respond to sound! This is the brain of an AI entity that rlm@65: * hears its surroundings and reacts to them. rlm@65: */ rlm@65: public void process(ByteBuffer audioSamples, rlm@65: int numSamples, AudioFormat format) { rlm@65: audioSamples.clear(); rlm@65: byte[] data = new byte[numSamples]; rlm@65: float[] out = new float[numSamples]; rlm@65: audioSamples.get(data); rlm@65: FloatSampleTools. rlm@65: byte2floatInterleaved rlm@65: (data, 0, out, 0, numSamples/format.getFrameSize(), format); rlm@56: rlm@65: float max = Float.NEGATIVE_INFINITY; rlm@65: for (float f : out){if (f > max) max = f;} rlm@65: audioSamples.clear(); rlm@56: rlm@65: if (max > 0.1){ rlm@65: entity.getMaterial().setColor("Color", ColorRGBA.Green); rlm@65: } rlm@65: else { rlm@65: entity.getMaterial().setColor("Color", ColorRGBA.Gray); rlm@65: } rlm@65: } rlm@65: } rlm@62: rlm@65: private void prepareEar(Geometry ear, int n){ rlm@65: if (this.audioRenderer instanceof MultiListener){ rlm@65: MultiListener rf = (MultiListener)this.audioRenderer; rlm@62: rlm@65: Listener auxListener = new Listener(); rlm@65: auxListener.setLocation(ear.getLocalTranslation()); rlm@62: rlm@65: rf.addListener(auxListener); rlm@65: WaveFileWriter aux = null; rlm@62: rlm@65: try { rlm@65: aux = new WaveFileWriter rlm@65: (File.createTempFile("advanced-audio-" + n, ".wav"));} rlm@65: catch (IOException e) {e.printStackTrace();} rlm@62: rlm@65: rf.registerSoundProcessor rlm@65: (auxListener, rlm@65: new CompositeSoundProcessor(new Dancer(ear), aux)); rlm@65: } rlm@65: } rlm@62: rlm@65: public void simpleInitApp() { rlm@65: this.setTimer(new IsoTimer(60)); rlm@65: initAudio(); rlm@62: rlm@65: createScene(); rlm@65: rlm@65: prepareEar(ear1, 1); rlm@65: prepareEar(ear2, 1); rlm@65: prepareEar(ear3, 1); rlm@65: rlm@65: motionControl.play(); rlm@65: } rlm@65: rlm@65: public void simpleUpdate(float tpf) { rlm@65: motionTimer.update(); rlm@65: if (music.getStatus() != AudioNode.Status.Playing){ rlm@65: music.play(); rlm@56: } rlm@65: Vector3f loc = cam.getLocation(); rlm@65: Quaternion rot = cam.getRotation(); rlm@65: listener.setLocation(loc); rlm@65: listener.setRotation(rot); rlm@65: music.setLocalTranslation(bell.getLocalTranslation()); rlm@65: } rlm@41: }