rlm@12: package com.aurellem.capture.hello; rlm@12: rlm@12: import java.io.File; rlm@12: rlm@12: import com.aurellem.capture.IsoTimer; rlm@12: import com.aurellem.capture.audio.MultiListener; rlm@12: import com.aurellem.capture.audio.WaveFileWriter; rlm@12: import com.jme3.app.SimpleApplication; rlm@12: import com.jme3.audio.AudioNode; rlm@12: import com.jme3.audio.Listener; rlm@12: import com.jme3.input.controls.ActionListener; rlm@12: import com.jme3.input.controls.MouseButtonTrigger; rlm@12: import com.jme3.material.Material; rlm@12: import com.jme3.math.ColorRGBA; rlm@12: import com.jme3.math.Quaternion; rlm@12: import com.jme3.math.Vector3f; rlm@12: import com.jme3.scene.Geometry; rlm@12: import com.jme3.scene.shape.Box; rlm@12: import com.jme3.system.AppSettings; rlm@12: rlm@12: rlm@12: /** rlm@12: * rlm@12: * rlm@12: * rlm@12: * @author Robert McIntyre rlm@12: * rlm@12: */ rlm@12: rlm@12: public class AdvancedAudio extends SimpleApplication { rlm@12: rlm@12: rlm@12: rlm@12: public static void main(String[] args) { rlm@12: rlm@12: // Logger.getLogger("com.jme3").setLevel(Level.OFF); rlm@12: rlm@12: AdvancedAudio app = new AdvancedAudio(); rlm@12: AppSettings settings = new AppSettings(true); rlm@12: rlm@12: settings.setAudioRenderer("Send"); rlm@12: app.setSettings(settings); rlm@12: app.setShowSettings(false); rlm@12: app.start(); rlm@12: app.setPauseOnLostFocus(false); rlm@12: } rlm@12: rlm@12: rlm@12: rlm@12: private AudioNode audio_gun; rlm@12: private AudioNode audio_nature; rlm@12: private Geometry player; rlm@12: private Listener auxListener = new Listener(); rlm@12: public File data1 = new File("/home/r/tmp/data1.wav"); rlm@12: public File data2 = new File("/home/r/tmp/data2.wav"); rlm@12: public File data3 = new File("/home/r/tmp/data3.wav"); rlm@12: rlm@12: private File makeTarget(int n){ rlm@12: return new File("/home/r/tmp/assload-" + n + ".wav"); rlm@12: } rlm@12: rlm@12: rlm@12: @Override rlm@12: public void simpleInitApp() { rlm@12: this.setTimer(new IsoTimer(60)); rlm@12: rlm@12: rlm@12: if (this.audioRenderer instanceof MultiListener){ rlm@12: MultiListener rf = (MultiListener)this.audioRenderer; rlm@12: rlm@12: for (int n = 0; n < 0; n++){ rlm@12: Listener zzz = new Listener(); rlm@12: rf.addListener(zzz); rlm@12: rf.registerSoundProcessor( rlm@12: zzz, new WaveFileWriter(makeTarget(n))); rlm@12: } rlm@12: Listener listener3 = new Listener(); rlm@12: rf.addListener(auxListener); rlm@12: rf.addListener(listener3); rlm@12: rf.registerSoundProcessor(new WaveFileWriter(data1)); rlm@12: rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2)); rlm@12: rf.registerSoundProcessor(listener3, new WaveFileWriter(data3)); rlm@12: } rlm@12: flyCam.setMoveSpeed(40); rlm@12: rlm@12: /** just a blue box floating in space */ rlm@12: Box box1 = new Box(Vector3f.ZERO, 1, 1, 1); rlm@12: player = new Geometry("Player", box1); rlm@12: Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); rlm@12: mat1.setColor("Color", ColorRGBA.Blue); rlm@12: player.setMaterial(mat1); rlm@12: rootNode.attachChild(player); rlm@12: rlm@12: /** custom init methods, see below */ rlm@12: initKeys(); rlm@12: initAudio(); rlm@12: rlm@12: this.audioRenderer.playSource(audio_gun); rlm@12: rlm@12: rlm@12: rlm@12: } rlm@12: rlm@12: /** We create two audio nodes. */ rlm@12: private void initAudio() { rlm@12: //audioRenderer.setEnvironment(Environment.Cavern); rlm@12: /* gun shot sound is to be triggered by a mouse click. */ rlm@12: audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false); rlm@12: //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false); rlm@12: audio_gun.setLooping(false); rlm@12: audio_gun.setVolume(2); rlm@12: audio_gun.setPositional(true); rlm@12: rlm@12: rlm@12: /* nature sound - keeps playing in a loop. */ rlm@12: audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false); rlm@12: audio_nature.setLooping(true); rlm@12: audio_nature.setPositional(true); rlm@12: audio_nature.setLocalTranslation(Vector3f.ZERO.clone()); rlm@12: audio_nature.setVolume(3); rlm@12: audio_nature.updateGeometricState(); rlm@12: } rlm@12: rlm@12: /** Declaring the "Shoot" action, and rlm@12: * mapping it to a trigger (mouse click). */ rlm@12: private void initKeys() { rlm@12: inputManager.addMapping("Shoot", new MouseButtonTrigger(0)); rlm@12: inputManager.addListener(actionListener, "Shoot"); rlm@12: } rlm@12: rlm@12: /** Defining the "Shoot" action: Play a gun sound. */ rlm@12: private ActionListener actionListener = new ActionListener() { rlm@12: @Override rlm@12: public void onAction(String name, boolean keyPressed, float tpf) { rlm@12: if (name.equals("Shoot") && !keyPressed) { rlm@12: audioRenderer.playSource(audio_gun); // play once! rlm@12: } rlm@12: } rlm@12: }; rlm@12: rlm@12: /** Move the listener with the camera - for 3D audio. */ rlm@12: @Override rlm@12: public void simpleUpdate(float tpf) { rlm@12: Vector3f loc = cam.getLocation(); rlm@12: Quaternion rot = cam.getRotation(); rlm@12: listener.setLocation(loc); rlm@12: listener.setRotation(rot); rlm@12: auxListener.setLocation(loc); rlm@12: auxListener.setRotation(rot); rlm@12: if (audio_gun.getStatus() == AudioNode.Status.Stopped){ rlm@12: System.out.println("I'm Stopped!"); rlm@12: this.requestClose(false); rlm@12: } rlm@12: rlm@12: rlm@12: } rlm@12: rlm@12: }