annotate src/com/aurellem/capture/examples/AdvancedAudio.java @ 14:e299cd89074d

creating Advanced Audio documentation.
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 21:55:51 -0700
parents
children be5ac56826be
rev   line source
rlm@14 1 package com.aurellem.capture.examples;
rlm@14 2
rlm@14 3 import java.io.File;
rlm@14 4
rlm@14 5 import com.aurellem.capture.IsoTimer;
rlm@14 6 import com.aurellem.capture.audio.MultiListener;
rlm@14 7 import com.aurellem.capture.audio.WaveFileWriter;
rlm@14 8 import com.jme3.app.SimpleApplication;
rlm@14 9 import com.jme3.audio.AudioNode;
rlm@14 10 import com.jme3.audio.Listener;
rlm@14 11 import com.jme3.input.controls.ActionListener;
rlm@14 12 import com.jme3.input.controls.MouseButtonTrigger;
rlm@14 13 import com.jme3.material.Material;
rlm@14 14 import com.jme3.math.ColorRGBA;
rlm@14 15 import com.jme3.math.Quaternion;
rlm@14 16 import com.jme3.math.Vector3f;
rlm@14 17 import com.jme3.scene.Geometry;
rlm@14 18 import com.jme3.scene.shape.Box;
rlm@14 19 import com.jme3.system.AppSettings;
rlm@14 20
rlm@14 21
rlm@14 22 /**
rlm@14 23 *
rlm@14 24 * Demonstrates advanced use of the audio capture and recording features.
rlm@14 25 * Multiple perspectives of the same scene are simultaneously rendered to
rlm@14 26 * different sound files.
rlm@14 27 *
rlm@14 28 * A key limitation of the way multiple listeners are implemented is that
rlm@14 29 * only 3D positioning effects are realized for listeners other than the
rlm@14 30 * main LWJGL listener. This means that audio effects such as environment
rlm@14 31 * settings will *not* be heard on any auxiliary listeners, though sound
rlm@14 32 * attenuation will work correctly.
rlm@14 33 *
rlm@14 34 * Multiple listeners as realized here might be used to make AI entities
rlm@14 35 * that can each hear the world from their own perspective.
rlm@14 36 *
rlm@14 37 * @author Robert McIntyre
rlm@14 38 *
rlm@14 39 */
rlm@14 40
rlm@14 41 public class AdvancedAudio extends SimpleApplication {
rlm@14 42
rlm@14 43 public static void main(String[] args) {
rlm@14 44
rlm@14 45 AdvancedAudio app = new AdvancedAudio();
rlm@14 46 AppSettings settings = new AppSettings(true);
rlm@14 47
rlm@14 48 settings.setAudioRenderer("Send");
rlm@14 49 app.setSettings(settings);
rlm@14 50 app.setShowSettings(false);
rlm@14 51 app.start();
rlm@14 52 app.setPauseOnLostFocus(false);
rlm@14 53 }
rlm@14 54
rlm@14 55 private AudioNode audio_gun;
rlm@14 56 private AudioNode audio_nature;
rlm@14 57 private Geometry player;
rlm@14 58 private Listener auxListener = new Listener();
rlm@14 59 public File data1 = new File("/home/r/tmp/data1.wav");
rlm@14 60 public File data2 = new File("/home/r/tmp/data2.wav");
rlm@14 61 public File data3 = new File("/home/r/tmp/data3.wav");
rlm@14 62
rlm@14 63 private File makeTarget(int n){
rlm@14 64 return new File("/home/r/tmp/assload-" + n + ".wav");
rlm@14 65 }
rlm@14 66
rlm@14 67
rlm@14 68 @Override
rlm@14 69 public void simpleInitApp() {
rlm@14 70 this.setTimer(new IsoTimer(60));
rlm@14 71
rlm@14 72
rlm@14 73 if (this.audioRenderer instanceof MultiListener){
rlm@14 74 MultiListener rf = (MultiListener)this.audioRenderer;
rlm@14 75
rlm@14 76 for (int n = 0; n < 0; n++){
rlm@14 77 Listener zzz = new Listener();
rlm@14 78 rf.addListener(zzz);
rlm@14 79 rf.registerSoundProcessor(
rlm@14 80 zzz, new WaveFileWriter(makeTarget(n)));
rlm@14 81 }
rlm@14 82 Listener listener3 = new Listener();
rlm@14 83 rf.addListener(auxListener);
rlm@14 84 rf.addListener(listener3);
rlm@14 85 rf.registerSoundProcessor(new WaveFileWriter(data1));
rlm@14 86 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
rlm@14 87 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
rlm@14 88 }
rlm@14 89 flyCam.setMoveSpeed(40);
rlm@14 90
rlm@14 91 /** just a blue box floating in space */
rlm@14 92 Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
rlm@14 93 player = new Geometry("Player", box1);
rlm@14 94 Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
rlm@14 95 mat1.setColor("Color", ColorRGBA.Blue);
rlm@14 96 player.setMaterial(mat1);
rlm@14 97 rootNode.attachChild(player);
rlm@14 98
rlm@14 99 /** custom init methods, see below */
rlm@14 100 initKeys();
rlm@14 101 initAudio();
rlm@14 102
rlm@14 103 this.audioRenderer.playSource(audio_gun);
rlm@14 104
rlm@14 105
rlm@14 106
rlm@14 107 }
rlm@14 108
rlm@14 109 /** We create two audio nodes. */
rlm@14 110 private void initAudio() {
rlm@14 111 //audioRenderer.setEnvironment(Environment.Cavern);
rlm@14 112 /* gun shot sound is to be triggered by a mouse click. */
rlm@14 113 audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
rlm@14 114 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
rlm@14 115 audio_gun.setLooping(false);
rlm@14 116 audio_gun.setVolume(2);
rlm@14 117 audio_gun.setPositional(true);
rlm@14 118
rlm@14 119
rlm@14 120 /* nature sound - keeps playing in a loop. */
rlm@14 121 audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
rlm@14 122 audio_nature.setLooping(true);
rlm@14 123 audio_nature.setPositional(true);
rlm@14 124 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
rlm@14 125 audio_nature.setVolume(3);
rlm@14 126 audio_nature.updateGeometricState();
rlm@14 127 }
rlm@14 128
rlm@14 129 /** Declaring the "Shoot" action, and
rlm@14 130 * mapping it to a trigger (mouse click). */
rlm@14 131 private void initKeys() {
rlm@14 132 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
rlm@14 133 inputManager.addListener(actionListener, "Shoot");
rlm@14 134 }
rlm@14 135
rlm@14 136 /** Defining the "Shoot" action: Play a gun sound. */
rlm@14 137 private ActionListener actionListener = new ActionListener() {
rlm@14 138 @Override
rlm@14 139 public void onAction(String name, boolean keyPressed, float tpf) {
rlm@14 140 if (name.equals("Shoot") && !keyPressed) {
rlm@14 141 audioRenderer.playSource(audio_gun); // play once!
rlm@14 142 }
rlm@14 143 }
rlm@14 144 };
rlm@14 145
rlm@14 146 /** Move the listener with the camera - for 3D audio. */
rlm@14 147 @Override
rlm@14 148 public void simpleUpdate(float tpf) {
rlm@14 149 Vector3f loc = cam.getLocation();
rlm@14 150 Quaternion rot = cam.getRotation();
rlm@14 151 listener.setLocation(loc);
rlm@14 152 listener.setRotation(rot);
rlm@14 153 auxListener.setLocation(loc);
rlm@14 154 auxListener.setRotation(rot);
rlm@14 155 if (audio_gun.getStatus() == AudioNode.Status.Stopped){
rlm@14 156 System.out.println("I'm Stopped!");
rlm@14 157 this.requestClose(false);
rlm@14 158 }
rlm@14 159
rlm@14 160
rlm@14 161 }
rlm@14 162
rlm@14 163 }