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