view src/com/aurellem/capture/hello/AdvancedAudio.java @ 12:d10f4d4ff15a

going to improve documentation
author Robert McIntyre <rlm@mit.edu>
date Thu, 27 Oct 2011 21:07:50 -0700
parents
children
line wrap: on
line source
1 package com.aurellem.capture.hello;
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 *
25 *
26 * @author Robert McIntyre
27 *
28 */
30 public class AdvancedAudio extends SimpleApplication {
34 public static void main(String[] args) {
36 // Logger.getLogger("com.jme3").setLevel(Level.OFF);
38 AdvancedAudio app = new AdvancedAudio();
39 AppSettings settings = new AppSettings(true);
41 settings.setAudioRenderer("Send");
42 app.setSettings(settings);
43 app.setShowSettings(false);
44 app.start();
45 app.setPauseOnLostFocus(false);
46 }
50 private AudioNode audio_gun;
51 private AudioNode audio_nature;
52 private Geometry player;
53 private Listener auxListener = new Listener();
54 public File data1 = new File("/home/r/tmp/data1.wav");
55 public File data2 = new File("/home/r/tmp/data2.wav");
56 public File data3 = new File("/home/r/tmp/data3.wav");
58 private File makeTarget(int n){
59 return new File("/home/r/tmp/assload-" + n + ".wav");
60 }
63 @Override
64 public void simpleInitApp() {
65 this.setTimer(new IsoTimer(60));
68 if (this.audioRenderer instanceof MultiListener){
69 MultiListener rf = (MultiListener)this.audioRenderer;
71 for (int n = 0; n < 0; n++){
72 Listener zzz = new Listener();
73 rf.addListener(zzz);
74 rf.registerSoundProcessor(
75 zzz, new WaveFileWriter(makeTarget(n)));
76 }
77 Listener listener3 = new Listener();
78 rf.addListener(auxListener);
79 rf.addListener(listener3);
80 rf.registerSoundProcessor(new WaveFileWriter(data1));
81 rf.registerSoundProcessor(auxListener, new WaveFileWriter(data2));
82 rf.registerSoundProcessor(listener3, new WaveFileWriter(data3));
83 }
84 flyCam.setMoveSpeed(40);
86 /** just a blue box floating in space */
87 Box box1 = new Box(Vector3f.ZERO, 1, 1, 1);
88 player = new Geometry("Player", box1);
89 Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
90 mat1.setColor("Color", ColorRGBA.Blue);
91 player.setMaterial(mat1);
92 rootNode.attachChild(player);
94 /** custom init methods, see below */
95 initKeys();
96 initAudio();
98 this.audioRenderer.playSource(audio_gun);
102 }
104 /** We create two audio nodes. */
105 private void initAudio() {
106 //audioRenderer.setEnvironment(Environment.Cavern);
107 /* gun shot sound is to be triggered by a mouse click. */
108 audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
109 //audio_gun = new AudioNode(assetManager, "Sound/Effects/dream.wav", false, false);
110 audio_gun.setLooping(false);
111 audio_gun.setVolume(2);
112 audio_gun.setPositional(true);
115 /* nature sound - keeps playing in a loop. */
116 audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", false, false);
117 audio_nature.setLooping(true);
118 audio_nature.setPositional(true);
119 audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
120 audio_nature.setVolume(3);
121 audio_nature.updateGeometricState();
122 }
124 /** Declaring the "Shoot" action, and
125 * mapping it to a trigger (mouse click). */
126 private void initKeys() {
127 inputManager.addMapping("Shoot", new MouseButtonTrigger(0));
128 inputManager.addListener(actionListener, "Shoot");
129 }
131 /** Defining the "Shoot" action: Play a gun sound. */
132 private ActionListener actionListener = new ActionListener() {
133 @Override
134 public void onAction(String name, boolean keyPressed, float tpf) {
135 if (name.equals("Shoot") && !keyPressed) {
136 audioRenderer.playSource(audio_gun); // play once!
137 }
138 }
139 };
141 /** Move the listener with the camera - for 3D audio. */
142 @Override
143 public void simpleUpdate(float tpf) {
144 Vector3f loc = cam.getLocation();
145 Quaternion rot = cam.getRotation();
146 listener.setLocation(loc);
147 listener.setRotation(rot);
148 auxListener.setLocation(loc);
149 auxListener.setRotation(rot);
150 if (audio_gun.getStatus() == AudioNode.Status.Stopped){
151 System.out.println("I'm Stopped!");
152 this.requestClose(false);
153 }
156 }
158 }