annotate src/com/aurellem/capture/hello/HelloAudio.java @ 0:9c4438349e88

added Hello sample programs. Will slowly move all the capture code to here
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 10:42:36 -0700
parents
children a92de00f0414
rev   line source
rlm@0 1 package com.aurellem.capture.hello;
rlm@0 2
rlm@0 3 import java.io.File;
rlm@0 4 import java.util.logging.Level;
rlm@0 5 import java.util.logging.Logger;
rlm@0 6
rlm@0 7 import com.jme3.app.SimpleApplication;
rlm@0 8 import com.jme3.audio.AudioNode;
rlm@0 9 import com.jme3.audio.Listener;
rlm@0 10 import com.jme3.capture.MultiListener;
rlm@0 11 import com.jme3.capture.WaveFileWriter;
rlm@0 12 import com.jme3.input.controls.ActionListener;
rlm@0 13 import com.jme3.input.controls.MouseButtonTrigger;
rlm@0 14 import com.jme3.material.Material;
rlm@0 15 import com.jme3.math.ColorRGBA;
rlm@0 16 import com.jme3.math.Quaternion;
rlm@0 17 import com.jme3.math.Vector3f;
rlm@0 18 import com.jme3.scene.Geometry;
rlm@0 19 import com.jme3.scene.shape.Box;
rlm@0 20 import com.jme3.system.AppSettings;
rlm@0 21 import com.jme3.system.IsoTimer;
rlm@0 22
rlm@0 23 /** Sample 11 - playing 3D audio. */
rlm@0 24 public class HelloAudio extends SimpleApplication {
rlm@0 25
rlm@0 26 private AudioNode audio_gun;
rlm@0 27 private AudioNode audio_nature;
rlm@0 28 private Geometry player;
rlm@0 29 private Listener auxListener = new Listener();
rlm@0 30 public File data1 = new File("/home/r/tmp/data1.wav");
rlm@0 31 public File data2 = new File("/home/r/tmp/data2.wav");
rlm@0 32 public File data3 = new File("/home/r/tmp/data3.wav");
rlm@0 33
rlm@0 34
rlm@0 35
rlm@0 36
rlm@0 37 private File makeTarget(int n){
rlm@0 38 return new File("/home/r/tmp/assload-" + n + ".wav");
rlm@0 39 }
rlm@0 40
rlm@0 41
rlm@0 42 public static void main(String[] args) {
rlm@0 43
rlm@0 44 // Logger.getLogger("com.jme3").setLevel(Level.OFF);
rlm@0 45
rlm@0 46 HelloAudio app = new HelloAudio();
rlm@0 47 AppSettings settings = new AppSettings(true);
rlm@0 48 settings.setAudioRenderer("Send");
rlm@0 49 app.setSettings(settings);
rlm@0 50 app.setShowSettings(false);
rlm@0 51 app.start();
rlm@0 52 app.setPauseOnLostFocus(false);
rlm@0 53 }
rlm@0 54
rlm@0 55 @Override
rlm@0 56 public void simpleInitApp() {
rlm@0 57 this.setTimer(new IsoTimer(60));
rlm@0 58
rlm@0 59
rlm@0 60 if (this.audioRenderer instanceof MultiListener){
rlm@0 61 MultiListener rf = (MultiListener)this.audioRenderer;
rlm@0 62
rlm@0 63 for (int n = 0; n < 0; n++){
rlm@0 64 Listener zzz = new Listener();
rlm@0 65 rf.addListener(zzz);
rlm@0 66 rf.registerSoundProcessor(
rlm@0 67 zzz, new WaveFileWriter(makeTarget(n)));
rlm@0 68 }
rlm@0 69 Listener listener3 = new Listener();
rlm@0 70 rf.addListener(auxListener);
rlm@0 71 rf.addListener(listener3);
rlm@0 72 rf.registerSoundProcessor(new WaveFileWriter(data1));
rlm@0 73 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
rlm@0 74 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
rlm@0 75 }
rlm@0 76 flyCam.setMoveSpeed(40);
rlm@0 77
rlm@0 78 /** just a blue box floating in space */
rlm@0 79 Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
rlm@0 80 player = new Geometry("Player", box1);
rlm@0 81 Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@0 82 mat1.setColor("Color", ColorRGBA.Blue);
rlm@0 83 player.setMaterial(mat1);
rlm@0 84 rootNode.attachChild(player);
rlm@0 85
rlm@0 86 /** custom init methods, see below */
rlm@0 87 initKeys();
rlm@0 88 initAudio();
rlm@0 89
rlm@0 90 this.audioRenderer.playSource(audio_gun);
rlm@0 91
rlm@0 92
rlm@0 93
rlm@0 94 }
rlm@0 95
rlm@0 96 /** We create two audio nodes. */
rlm@0 97 private void initAudio() {
rlm@0 98 //audioRenderer.setEnvironment(Environment.Cavern);
rlm@0 99 /* gun shot sound is to be triggered by a mouse click. */
rlm@0 100 //audio_gun = new AudioNode(audioRenderer, assetManager, "Sound/Effects/Gun.wav", false);
rlm@0 101 audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
rlm@0 102 audio_gun.setLooping(false);
rlm@0 103 audio_gun.setVolume(2);
rlm@0 104 audio_gun.setPositional(true);
rlm@0 105
rlm@0 106
rlm@0 107 /* nature sound - keeps playing in a loop. */
rlm@0 108 audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
rlm@0 109 audio_nature.setLooping(true);
rlm@0 110 audio_nature.setPositional(true);
rlm@0 111 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
rlm@0 112 audio_nature.setVolume(3);
rlm@0 113 audio_nature.updateGeometricState();
rlm@0 114 }
rlm@0 115
rlm@0 116 /** Declaring the "Shoot" action, and
rlm@0 117 * mapping it to a trigger (mouse click). */
rlm@0 118 private void initKeys() {
rlm@0 119 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
rlm@0 120 inputManager.addListener(actionListener, "Shoot");
rlm@0 121 }
rlm@0 122
rlm@0 123 /** Defining the "Shoot" action: Play a gun sound. */
rlm@0 124 private ActionListener actionListener = new ActionListener() {
rlm@0 125 @Override
rlm@0 126 public void onAction(String name, boolean keyPressed, float tpf) {
rlm@0 127 if (name.equals("Shoot") && !keyPressed) {
rlm@0 128 audioRenderer.playSource(audio_gun); // play once!
rlm@0 129 }
rlm@0 130 }
rlm@0 131 };
rlm@0 132
rlm@0 133 /** Move the listener with the camera - for 3D audio. */
rlm@0 134 @Override
rlm@0 135 public void simpleUpdate(float tpf) {
rlm@0 136 Vector3f loc = cam.getLocation();
rlm@0 137 Quaternion rot = cam.getRotation();
rlm@0 138 listener.setLocation(loc);
rlm@0 139 listener.setRotation(rot);
rlm@0 140 auxListener.setLocation(loc);
rlm@0 141 auxListener.setRotation(rot);
rlm@0 142 }
rlm@0 143
rlm@0 144 }