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