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