rlm@14: package com.aurellem.capture.examples; rlm@14: rlm@14: import java.io.File; rlm@33: import java.io.FileNotFoundException; rlm@33: import java.io.IOException; rlm@20: import java.nio.ByteBuffer; rlm@35: import java.util.logging.Level; rlm@35: import java.util.logging.Logger; rlm@14: rlm@30: import javax.sound.sampled.AudioFormat; rlm@30: rlm@33: import org.tritonus.share.sampled.FloatSampleTools; rlm@33: rlm@33: import com.aurellem.capture.Capture; rlm@14: import com.aurellem.capture.IsoTimer; rlm@33: import com.aurellem.capture.audio.CompositeSoundProcessor; rlm@14: import com.aurellem.capture.audio.MultiListener; rlm@20: import com.aurellem.capture.audio.SoundProcessor; rlm@33: import com.aurellem.capture.audio.WaveFileWriter; rlm@14: import com.jme3.app.SimpleApplication; rlm@14: import com.jme3.audio.AudioNode; rlm@34: import com.jme3.audio.AudioParam; rlm@14: import com.jme3.audio.Listener; rlm@15: import com.jme3.cinematic.MotionPath; rlm@15: import com.jme3.cinematic.events.MotionTrack; rlm@18: import com.jme3.input.controls.ActionListener; rlm@18: import com.jme3.input.controls.MouseButtonTrigger; rlm@15: import com.jme3.light.DirectionalLight; rlm@14: import com.jme3.material.Material; rlm@14: import com.jme3.math.ColorRGBA; rlm@15: import com.jme3.math.FastMath; rlm@14: import com.jme3.math.Quaternion; rlm@14: import com.jme3.math.Vector3f; rlm@14: import com.jme3.scene.Geometry; rlm@15: import com.jme3.scene.Node; rlm@20: import com.jme3.scene.Spatial; rlm@14: import com.jme3.scene.shape.Box; rlm@15: import com.jme3.scene.shape.Sphere; rlm@14: import com.jme3.system.AppSettings; rlm@14: rlm@14: rlm@14: /** rlm@14: * rlm@14: * Demonstrates advanced use of the audio capture and recording features. rlm@14: * Multiple perspectives of the same scene are simultaneously rendered to rlm@14: * different sound files. rlm@14: * rlm@14: * A key limitation of the way multiple listeners are implemented is that rlm@14: * only 3D positioning effects are realized for listeners other than the rlm@14: * main LWJGL listener. This means that audio effects such as environment rlm@14: * settings will *not* be heard on any auxiliary listeners, though sound rlm@14: * attenuation will work correctly. rlm@14: * rlm@14: * Multiple listeners as realized here might be used to make AI entities rlm@14: * that can each hear the world from their own perspective. rlm@14: * rlm@14: * @author Robert McIntyre rlm@14: * rlm@14: */ rlm@14: rlm@14: public class AdvancedAudio extends SimpleApplication { rlm@15: rlm@15: public static void main(String[] args) { rlm@35: Logger.getLogger("com.jme3").setLevel(Level.OFF); rlm@15: AdvancedAudio app = new AdvancedAudio(); rlm@15: AppSettings settings = new AppSettings(true); rlm@20: settings.setAudioRenderer("Send"); rlm@15: app.setSettings(settings); rlm@15: app.setShowSettings(false); rlm@15: app.setPauseOnLostFocus(false); rlm@15: org.lwjgl.input.Mouse.setGrabbed(false); rlm@33: try {Capture.captureVideo(app, new File("/home/r/tmp/out.avi"));} rlm@33: catch (IOException e) {e.printStackTrace();} rlm@15: app.start(); rlm@15: } rlm@16: rlm@15: private MotionTrack motionControl; rlm@15: rlm@15: rlm@20: private Spatial makeEar(Node root, Vector3f position){ rlm@15: Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); rlm@15: Geometry ear = new Geometry("ear", new Box(1.0f, 1.0f, 1.0f)); rlm@15: ear.setLocalTranslation(position); rlm@15: mat.setColor("Color", ColorRGBA.Green); rlm@15: ear.setMaterial(mat); rlm@15: root.attachChild(ear); rlm@20: return ear; rlm@16: } rlm@15: rlm@17: private Geometry bell; rlm@17: rlm@20: private Spatial ear1; rlm@22: //private Spatial ear2; rlm@22: //private Spatial ear3; rlm@22: //private Spatial ear4; rlm@20: rlm@19: rlm@19: private Vector3f[] path = new Vector3f[]{ rlm@19: // loop 1 rlm@19: new Vector3f(0, 0, 0), rlm@19: new Vector3f(0, 0, -10), rlm@19: new Vector3f(-2, 0, -14), rlm@19: new Vector3f(-6, 0, -20), rlm@19: new Vector3f(0, 0, -26), rlm@19: new Vector3f(6, 0, -20), rlm@19: new Vector3f(0, 0, -14), rlm@19: new Vector3f(-6, 0, -20), rlm@19: new Vector3f(0, 0, -26), rlm@19: new Vector3f(6, 0, -20), rlm@19: // loop 2 rlm@19: new Vector3f(5, 0, -5), rlm@19: new Vector3f(7, 0, 1.5f), rlm@19: new Vector3f(14, 0, 2), rlm@19: new Vector3f(20, 0, 6), rlm@19: new Vector3f(26, 0, 0), rlm@19: new Vector3f(20, 0, -6), rlm@19: new Vector3f(14, 0, 0), rlm@19: new Vector3f(20, 0, 6), rlm@19: new Vector3f(26, 0, 0), rlm@19: new Vector3f(20, 0, -6), rlm@19: new Vector3f(14, 0, 0), rlm@19: // loop 3 rlm@19: new Vector3f(8, 0, 7.5f), rlm@19: new Vector3f(7, 0, 10.5f), rlm@19: new Vector3f(6, 0, 20), rlm@19: new Vector3f(0, 0, 26), rlm@19: new Vector3f(-6, 0, 20), rlm@19: new Vector3f(0, 0, 14), rlm@19: new Vector3f(6, 0, 20), rlm@19: new Vector3f(0, 0, 26), rlm@19: new Vector3f(-6, 0, 20), rlm@19: new Vector3f(0, 0, 14), rlm@19: // begin ellipse rlm@19: new Vector3f(16, 5, 20), rlm@19: new Vector3f(0, 0, 26), rlm@19: new Vector3f(-16, -10, 20), rlm@19: new Vector3f(0, 0, 14), rlm@19: new Vector3f(16, 20, 20), rlm@19: new Vector3f(0, 0, 26), rlm@19: new Vector3f(-10, -25, 10), rlm@19: new Vector3f(-10, 0, 0), rlm@19: // come at me bro! rlm@19: new Vector3f(-28.00242f, 48.005623f, -34.648228f), rlm@19: new Vector3f(0, 0 , -20), rlm@19: }; rlm@19: rlm@19: rlm@19: rlm@15: private void createScene() { rlm@15: Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); rlm@17: bell = new Geometry( "sound-emitter" , new Sphere(15,15,1)); rlm@15: mat.setColor("Color", ColorRGBA.Blue); rlm@15: bell.setMaterial(mat); rlm@15: rootNode.attachChild(bell); rlm@15: rlm@15: DirectionalLight light = new DirectionalLight(); rlm@15: light.setDirection(new Vector3f(0, -1, 0).normalizeLocal()); rlm@15: light.setColor(ColorRGBA.White.mult(1.5f)); rlm@15: rootNode.addLight(light); rlm@15: rlm@20: ear1 = makeEar(rootNode, new Vector3f(0, 0 ,20)); rlm@22: //ear2 = makeEar(rootNode, new Vector3f(0, 0 ,-20)); rlm@22: //ear3 = makeEar(rootNode, new Vector3f(20, 0 ,0)); rlm@22: //ear4 = makeEar(rootNode, new Vector3f(-20, 0 ,0)); rlm@15: rlm@19: MotionPath track = new MotionPath(); rlm@15: rlm@19: for (Vector3f v : path){ rlm@19: track.addWayPoint(v); rlm@19: } rlm@19: rlm@19: rlm@19: track.setCurveTension(0.80f); rlm@15: rlm@15: rlm@19: motionControl = new MotionTrack(bell,track); rlm@33: motionControl.setTimer(new IsoTimer(60)); rlm@15: motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation); rlm@15: motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y)); rlm@33: motionControl.setInitialDuration(20f); rlm@33: motionControl.setSpeed(1f); rlm@15: rlm@15: rlm@19: track.enableDebugShape(assetManager, rootNode); rlm@15: rlm@15: rlm@15: positionCamera(); rlm@15: rlm@15: rlm@15: } rlm@15: rlm@15: rlm@15: private void positionCamera(){ rlm@19: this.cam.setLocation(new Vector3f(-28.00242f, 48.005623f, -34.648228f)); rlm@19: // cam.setLocation(new Vector3f(0,0,-20)); rlm@15: this.cam.setRotation(new Quaternion(0.3359635f, 0.34280345f, -0.13281013f, 0.8671653f)); rlm@15: } rlm@14: rlm@17: private AudioNode music; rlm@17: rlm@17: rlm@19: rlm@19: rlm@19: rlm@19: private void initAudio() { rlm@19: rlm@19: music = new AudioNode(assetManager, "Sound/Environment/pure.wav", false); rlm@19: rlm@19: rootNode.attachChild(music); rlm@35: //audioRenderer.playSource(music); rlm@33: music.setPositional(true); rlm@34: music.setVolume(1f); rlm@34: music.setReverbEnabled(false); rlm@34: music.setMaxDistance(200.0f); rlm@34: music.setRefDistance(1f); rlm@34: music.setRolloffFactor(5f); rlm@35: rlm@35: rlm@35: //audioRenderer.pauseSource(music); rlm@19: rlm@19: } rlm@19: rlm@19: rlm@19: rlm@19: rlm@14: private Listener auxListener = new Listener(); rlm@34: //public File data1 = new File("/home/r/tmp/data1.wav"); rlm@34: //public File data2 = new File("/home/r/tmp/data2.wav"); rlm@34: //public File data3 = new File("/home/r/tmp/data3.wav"); rlm@34: //public File data4 = new File("/home/r/tmp/data4.wav"); rlm@34: //public File data5 = new File("/home/r/tmp/data5.wav"); rlm@34: //public File data6 = new File("/home/r/tmp/data6.wav"); rlm@20: rlm@20: rlm@20: public class Dancer implements SoundProcessor { rlm@20: rlm@20: Spatial entity; rlm@20: rlm@20: float scale = 2; rlm@33: String debug; rlm@33: public Dancer(Spatial entity, String debug){ rlm@20: this.entity = entity; rlm@33: this.debug = debug; rlm@20: } rlm@20: rlm@20: /** rlm@20: * this method is irrelevant since there is no state to cleanup. rlm@20: */ rlm@20: public void cleanup() {} rlm@20: rlm@20: rlm@20: /** rlm@20: * Dance to the beat! This is the brain of an AI entity that rlm@20: * hears it's surroundings and reacts to them. rlm@20: */ rlm@30: public void process(ByteBuffer audioSamples, int numSamples, AudioFormat format) { rlm@33: audioSamples.clear(); rlm@33: byte[] data = new byte[numSamples]; rlm@33: float[] out = new float[numSamples]; rlm@33: audioSamples.get(data); rlm@33: FloatSampleTools.byte2floatInterleaved(data, 0, out, 0, rlm@33: numSamples/format.getFrameSize(), format); rlm@33: rlm@33: float max = Float.NEGATIVE_INFINITY; rlm@33: for (float f : out){if (f > max) max = f;} rlm@34: audioSamples.clear(); rlm@33: System.out.println(debug); rlm@33: System.out.println(max); rlm@33: rlm@33: rlm@33: rlm@33: //entity.scale(this.scale); rlm@33: //if (this.scale == 2f){this.scale = 0.5f;} rlm@33: //else {this.scale = 2;} rlm@20: } rlm@20: rlm@20: rlm@20: } rlm@20: rlm@20: rlm@14: rlm@18: rlm@14: public void simpleInitApp() { rlm@17: this.setTimer(new IsoTimer(60)); rlm@20: initAudio(); rlm@20: initKeys(); rlm@20: createScene(); rlm@35: listener.setLocation(ear1.getLocalTranslation()); rlm@14: if (this.audioRenderer instanceof MultiListener){ rlm@14: MultiListener rf = (MultiListener)this.audioRenderer; rlm@14: rlm@20: rlm@35: rlm@34: auxListener = new Listener(listener); rlm@34: rlm@14: rf.addListener(auxListener); rlm@34: WaveFileWriter aux = null; rlm@34: WaveFileWriter main = null; rlm@34: rlm@34: rlm@34: try {aux = new WaveFileWriter(new File("/home/r/tmp/aux.wav"));} rlm@33: catch (FileNotFoundException e) {e.printStackTrace();} rlm@20: rlm@34: try {main = new WaveFileWriter(new File("/home/r/tmp/main.wav"));} rlm@33: catch (FileNotFoundException e) {e.printStackTrace();} rlm@33: rlm@33: rf.registerSoundProcessor(auxListener, rlm@34: new CompositeSoundProcessor(new Dancer(ear1, "aux"), aux)); rlm@20: rlm@33: rf.registerSoundProcessor( rlm@34: new CompositeSoundProcessor(new Dancer(ear1, "--------\nmain"), main)); rlm@19: } rlm@20: rlm@15: motionControl.play(); rlm@14: } rlm@14: rlm@19: rlm@14: rlm@14: rlm@17: rlm@18: private void initKeys() { rlm@18: inputManager.addMapping("Shoot", new MouseButtonTrigger(0)); rlm@18: inputManager.addListener(actionListener, "Shoot"); rlm@18: } rlm@18: rlm@18: /** Defining the "Shoot" action: Play a gun sound. */ rlm@18: private ActionListener actionListener = new ActionListener() { rlm@18: @Override rlm@18: public void onAction(String name, boolean keyPressed, float tpf) { rlm@18: if (name.equals("Shoot") && !keyPressed) { rlm@35: rlm@35: System.out.println(bell.getLocalTranslation().subtract(listener.getLocation()).length()); rlm@35: //bell.getMaterial().setColor("Color", ColorRGBA.randomColor()); rlm@35: //audioRenderer.playSource(music); rlm@18: System.out.println(music.getRefDistance()); rlm@18: rlm@18: } rlm@18: } rlm@18: }; rlm@14: rlm@14: /** Move the listener with the camera - for 3D audio. */ rlm@19: rlm@19: rlm@35: //private Vector3f prevBellPos = Vector3f.ZERO; rlm@35: private int countdown = 0; rlm@33: rlm@14: public void simpleUpdate(float tpf) { rlm@18: //Vector3f loc = cam.getLocation(); rlm@18: //Quaternion rot = cam.getRotation(); rlm@18: //listener.setLocation(loc); rlm@18: //listener.setRotation(rot); rlm@35: System.out.println(countdown); rlm@35: if (countdown++ == 14) { this.requestClose(false);} rlm@18: rlm@35: System.out.println("channel "+ music.getChannel()); rlm@33: //listener.setLocation(cam.getLocation()); rlm@33: //listener.setRotation(cam.getRotation()); rlm@18: //auxListener.setLocation(loc); rlm@18: //auxListener.setRotation(rot); rlm@33: if (music.getStatus() != AudioNode.Status.Playing){ rlm@35: //audioRenderer.playSource(music); rlm@35: music.play(); rlm@33: bell.getMaterial().setColor("Color", ColorRGBA.randomColor()); rlm@35: System.out.println("I'm playing! <3"); rlm@33: } rlm@18: //audioRenderer.updateSourceParam(music, AudioParam.Direction); rlm@15: rlm@35: //Vector3f bellVelocity = bell.getLocalTranslation().subtract(prevBellPos).mult(1.0f/tpf); rlm@35: //prevBellPos = bell.getLocalTranslation(); rlm@19: rlm@17: music.setLocalTranslation(bell.getLocalTranslation()); rlm@35: //music.setVelocity(bellVelocity); rlm@17: rlm@34: //audioRenderer.updateSourceParam(music, AudioParam.Position); rlm@34: //audioRenderer.updateSourceParam(music, AudioParam.Velocity); rlm@34: rlm@34: rlm@34: //System.out.println("main:" + listener.getVolume()); rlm@34: //System.out.println("aux:" + auxListener.getVolume()); rlm@18: //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_MIN_GAIN, 0f); rlm@18: //org.lwjgl.openal.AL10.alSourcef(1, org.lwjgl.openal.AL10.AL_ROLLOFF_FACTOR, 5f); rlm@14: rlm@14: } rlm@14: rlm@14: }