view src/com/aurellem/capture/hello/HelloAudio.java @ 4:edaa7e7806e4

migrated IsoTimer
author Robert McIntyre <rlm@mit.edu>
date Tue, 25 Oct 2011 12:03:01 -0700
parents a92de00f0414
children dde12be02029
line wrap: on
line source
1 package com.aurellem.capture.hello;
3 import java.io.File;
4 import java.util.logging.Level;
5 import java.util.logging.Logger;
7 import com.aurellem.capture.IsoTimer;
8 import com.aurellem.capture.MultiListener;
9 import com.aurellem.capture.WaveFileWriter;
10 import com.jme3.app.SimpleApplication;
11 import com.jme3.audio.AudioNode;
12 import com.jme3.audio.Listener;
13 import com.jme3.input.controls.ActionListener;
14 import com.jme3.input.controls.MouseButtonTrigger;
15 import com.jme3.material.Material;
16 import com.jme3.math.ColorRGBA;
17 import com.jme3.math.Quaternion;
18 import com.jme3.math.Vector3f;
19 import com.jme3.scene.Geometry;
20 import com.jme3.scene.shape.Box;
21 import com.jme3.system.AppSettings;
23 /** Sample 11 - playing 3D audio. */
24 public class HelloAudio extends SimpleApplication {
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");
37 private File makeTarget(int n){
38 return new File("/home/r/tmp/assload-" + n + ".wav");
39 }
42 public static void main(String[] args) {
44 // Logger.getLogger("com.jme3").setLevel(Level.OFF);
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 }
55 @Override
56 public void simpleInitApp() {
57 this.setTimer(new IsoTimer(60));
60 if (this.audioRenderer instanceof MultiListener){
61 MultiListener rf = (MultiListener)this.audioRenderer;
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);
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);
86 /** custom init methods, see below */
87 initKeys();
88 initAudio();
90 this.audioRenderer.playSource(audio_gun);
94 }
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);
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 }
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 }
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 };
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 }
144 }