3535import com .jme3 .audio .AudioData ;
3636import com .jme3 .audio .AudioNode ;
3737import com .jme3 .audio .Environment ;
38+ import com .jme3 .audio .LowPassFilter ;
39+ import com .jme3 .input .KeyInput ;
40+ import com .jme3 .input .controls .ActionListener ;
41+ import com .jme3 .input .controls .KeyTrigger ;
42+ import com .jme3 .input .controls .Trigger ;
3843import com .jme3 .material .Material ;
3944import com .jme3 .math .ColorRGBA ;
4045import com .jme3 .math .FastMath ;
4449import com .jme3 .scene .debug .Grid ;
4550import com .jme3 .scene .shape .Sphere ;
4651
47- public class TestReverb extends SimpleApplication {
52+ public class TestReverb extends SimpleApplication implements ActionListener {
4853
4954 public static void main (String [] args ) {
5055 TestReverb app = new TestReverb ();
5156 app .start ();
5257 }
5358
54- private AudioNode audio ;
59+ private AudioNode audioSource ;
5560 private float time = 0 ;
5661 private float nextTime = 1 ;
5762
63+ /**
64+ * ### Effects ###
65+ * Changing a parameter value in the Effect Object after it has been attached to the Auxiliary Effect
66+ * Slot will not affect the effect in the effect slot. To update the parameters of the effect in the effect
67+ * slot, an application must update the parameters of an Effect object and then re-attach it to the
68+ * Auxiliary Effect Slot.
69+ */
70+ private int index = 0 ;
71+ private final Environment [] environments = {
72+ Environment .Cavern ,
73+ Environment .AcousticLab ,
74+ Environment .Closet ,
75+ Environment .Dungeon ,
76+ Environment .Garage
77+ };
78+
5879 @ Override
5980 public void simpleInitApp () {
6081
61- flyCam .setMoveSpeed (50f );
62- flyCam .setDragToRotate (true );
63-
64- cam .setLocation (Vector3f .UNIT_XYZ .mult (50f ));
65- cam .lookAt (Vector3f .ZERO , Vector3f .UNIT_Y );
82+ configureCamera ();
6683
67- audioRenderer .setEnvironment (Environment . Cavern );
84+ audioRenderer .setEnvironment (environments [ index ] );
6885
69- audio = new AudioNode (assetManager , "Sound/Effects/Bang.wav" , AudioData .DataType .Buffer );
70- rootNode .attachChild (audio );
86+ audioSource = new AudioNode (assetManager , "Sound/Effects/Bang.wav" , AudioData .DataType .Buffer );
87+ audioSource .setLooping (false );
88+ audioSource .setPositional (true );
89+ audioSource .setMaxDistance (100 );
90+ audioSource .setRefDistance (5 );
91+ audioSource .setReverbFilter (new LowPassFilter (1f , 1f ));
92+ audioSource .setVolume (1f );
93+ rootNode .attachChild (audioSource );
7194
7295 Geometry marker = makeShape ("Marker" , new Sphere (16 , 16 , .5f ), ColorRGBA .Red );
73- audio .attachChild (marker );
96+ audioSource .attachChild (marker );
7497
7598 Geometry grid = makeShape ("DebugGrid" , new Grid (21 , 21 , 4 ), ColorRGBA .Blue );
7699 grid .center ().move (0 , 0 , 0 );
77100 rootNode .attachChild (grid );
101+
102+ registerInputMappings ();
103+ }
104+
105+ private void configureCamera () {
106+ flyCam .setMoveSpeed (50f );
107+ flyCam .setDragToRotate (true );
108+
109+ cam .setLocation (Vector3f .UNIT_XYZ .mult (50f ));
110+ cam .lookAt (Vector3f .ZERO , Vector3f .UNIT_Y );
78111 }
79112
80113 private Geometry makeShape (String name , Mesh mesh , ColorRGBA color ) {
@@ -94,8 +127,8 @@ public void simpleUpdate(float tpf) {
94127 nextTime = FastMath .nextRandomFloat () * 2 + 0.5f ;
95128
96129 Vector3f position = getRandomPosition ();
97- audio .setLocalTranslation (position );
98- audio .playInstance ();
130+ audioSource .setLocalTranslation (position );
131+ audioSource .playInstance ();
99132 }
100133 }
101134
@@ -109,4 +142,30 @@ private Vector3f getRandomPosition() {
109142 return vec ;
110143 }
111144
145+ @ Override
146+ public void onAction (String name , boolean isPressed , float tpf ) {
147+ if (!isPressed ) return ;
148+
149+ if (name .equals ("toggleReverbEnabled" )) {
150+ boolean reverbEnabled = audioSource .isReverbEnabled ();
151+ audioSource .setReverbEnabled (!reverbEnabled );
152+ System .out .println ("reverbEnabled: " + audioSource .isReverbEnabled ());
153+
154+ } else if (name .equals ("nextEnvironment" )) {
155+ index = (index + 1 ) % environments .length ;
156+ audioRenderer .setEnvironment (environments [index ]);
157+ System .out .println ("Next Environment Index: " + index );
158+ }
159+ }
160+
161+ private void registerInputMappings () {
162+ addMapping ("toggleReverbEnabled" , new KeyTrigger (KeyInput .KEY_SPACE ));
163+ addMapping ("nextEnvironment" , new KeyTrigger (KeyInput .KEY_N ));
164+ }
165+
166+ private void addMapping (String mappingName , Trigger ... triggers ) {
167+ inputManager .addMapping (mappingName , triggers );
168+ inputManager .addListener (this , mappingName );
169+ }
170+
112171}
0 commit comments