Skip to content

Commit a74be4a

Browse files
authored
Update TestOgg.java
1 parent d128d96 commit a74be4a

1 file changed

Lines changed: 148 additions & 14 deletions

File tree

jme3-examples/src/main/java/jme3test/audio/TestOgg.java

Lines changed: 148 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2012 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -37,34 +37,168 @@
3737
import com.jme3.audio.AudioNode;
3838
import com.jme3.audio.AudioSource;
3939
import com.jme3.audio.LowPassFilter;
40+
import com.jme3.font.BitmapText;
41+
import com.jme3.input.KeyInput;
42+
import com.jme3.input.controls.ActionListener;
43+
import com.jme3.input.controls.KeyTrigger;
44+
import com.jme3.input.controls.Trigger;
45+
import com.jme3.material.Material;
46+
import com.jme3.math.ColorRGBA;
47+
import com.jme3.math.FastMath;
48+
import com.jme3.math.Vector3f;
49+
import com.jme3.scene.Geometry;
50+
import com.jme3.scene.Mesh;
51+
import com.jme3.scene.debug.Grid;
52+
import com.jme3.scene.shape.Sphere;
4053

41-
public class TestOgg extends SimpleApplication {
54+
/**
55+
*
56+
* @author capdevon
57+
*/
58+
public class TestOgg extends SimpleApplication implements ActionListener {
4259

60+
private final StringBuilder sb = new StringBuilder();
61+
private int frameCount = 0;
62+
private BitmapText bmp;
4363
private AudioNode audioSource;
64+
private float volume = 1.0f;
65+
private float pitch = 1.0f;
66+
67+
/**
68+
* ### Filters ###
69+
* Changing a parameter value in the Filter Object after it has been attached to a Source will not
70+
* affect the Source. To update the filter(s) used on a Source, an application must update the
71+
* parameters of a Filter object and then re-attach it to the Source.
72+
*/
73+
private final LowPassFilter dryFilter = new LowPassFilter(1f, .1f);
4474

45-
public static void main(String[] args){
75+
public static void main(String[] args) {
4676
TestOgg test = new TestOgg();
4777
test.start();
4878
}
4979

5080
@Override
51-
public void simpleInitApp(){
52-
System.out.println("Playing without filter");
81+
public void simpleInitApp() {
82+
configureCamera();
83+
84+
bmp = createLabelText(10, 20, "<placeholder>");
85+
86+
// just a blue sphere to mark the spot
87+
Geometry geo = makeShape("Beeper", new Sphere(16, 16, 1f), ColorRGBA.Blue);
88+
rootNode.attachChild(geo);
89+
90+
Geometry grid = makeShape("DebugGrid", new Grid(21, 21, 2), ColorRGBA.Gray);
91+
grid.center().move(0, 0, 0);
92+
rootNode.attachChild(grid);
93+
5394
audioSource = new AudioNode(assetManager, "Sound/Effects/Foot steps.ogg", DataType.Buffer);
95+
audioSource.setName("Foot steps");
96+
audioSource.setLooping(true);
97+
audioSource.setVolume(volume);
98+
audioSource.setPitch(pitch);
99+
audioSource.setMaxDistance(100);
100+
audioSource.setRefDistance(5);
54101
audioSource.play();
102+
rootNode.attachChild(audioSource);
103+
104+
registerInputMappings();
105+
}
106+
107+
private void configureCamera() {
108+
flyCam.setMoveSpeed(25f);
109+
flyCam.setDragToRotate(true);
110+
111+
cam.setLocation(Vector3f.UNIT_XYZ.mult(20f));
112+
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
55113
}
56114

57115
@Override
58-
public void simpleUpdate(float tpf){
59-
if (audioSource.getStatus() != AudioSource.Status.Playing){
60-
audioRenderer.deleteAudioData(audioSource.getAudioData());
61-
62-
System.out.println("Playing with low pass filter");
63-
audioSource = new AudioNode(assetManager, "Sound/Effects/Foot steps.ogg", DataType.Buffer);
64-
audioSource.setDryFilter(new LowPassFilter(1f, .1f));
65-
audioSource.setVolume(3);
66-
audioSource.play();
116+
public void simpleUpdate(float tpf) {
117+
frameCount++;
118+
if (frameCount % 10 == 0) {
119+
frameCount = 0;
120+
121+
sb.append("Audio: ").append(audioSource.getName()).append("\n");
122+
sb.append(audioSource.getAudioData()).append("\n");
123+
sb.append("Looping: ").append(audioSource.isLooping()).append("\n");
124+
sb.append("Volume: ").append(String.format("%.2f", audioSource.getVolume())).append("\n");
125+
sb.append("Pitch: ").append(String.format("%.2f", audioSource.getPitch())).append("\n");
126+
sb.append("Positional: ").append(audioSource.isPositional()).append("\n");
127+
sb.append("MaxDistance: ").append(audioSource.getMaxDistance()).append("\n");
128+
sb.append("RefDistance: ").append(audioSource.getRefDistance()).append("\n");
129+
sb.append("DryFilter: ").append(audioSource.getDryFilter() != null).append("\n");
130+
sb.append("Status: ").append(audioSource.getStatus()).append("\n");
131+
132+
bmp.setText(sb.toString());
133+
sb.setLength(0);
67134
}
68135
}
69136

137+
@Override
138+
public void onAction(String name, boolean isPressed, float tpf) {
139+
if (!isPressed) return;
140+
141+
if (name.equals("togglePlayPause")) {
142+
if (audioSource.getStatus() != AudioSource.Status.Playing) {
143+
audioSource.play();
144+
} else {
145+
audioSource.pause();
146+
}
147+
} else if (name.equals("togglePositional")) {
148+
boolean positional = audioSource.isPositional();
149+
audioSource.setPositional(!positional);
150+
151+
} else if (name.equals("dryFilter")) {
152+
boolean hasFilter = audioSource.getDryFilter() != null;
153+
audioSource.setDryFilter(hasFilter ? null : dryFilter);
154+
155+
} else if (name.equals("Volume+")) {
156+
volume = FastMath.clamp(volume + 0.1f, 0, 5f);
157+
audioSource.setVolume(volume);
158+
159+
} else if (name.equals("Volume-")) {
160+
volume = FastMath.clamp(volume - 0.1f, 0, 5f);
161+
audioSource.setVolume(volume);
162+
163+
} else if (name.equals("Pitch+")) {
164+
pitch = FastMath.clamp(pitch + 0.1f, 0.5f, 2f);
165+
audioSource.setPitch(pitch);
166+
167+
} else if (name.equals("Pitch-")) {
168+
pitch = FastMath.clamp(pitch - 0.1f, 0.5f, 2f);
169+
audioSource.setPitch(pitch);
170+
}
171+
}
172+
173+
private void registerInputMappings() {
174+
addMapping("togglePlayPause", new KeyTrigger(KeyInput.KEY_P));
175+
addMapping("togglePositional", new KeyTrigger(KeyInput.KEY_RETURN));
176+
addMapping("dryFilter", new KeyTrigger(KeyInput.KEY_SPACE));
177+
addMapping("Volume+", new KeyTrigger(KeyInput.KEY_I));
178+
addMapping("Volume-", new KeyTrigger(KeyInput.KEY_K));
179+
addMapping("Pitch+", new KeyTrigger(KeyInput.KEY_J));
180+
addMapping("Pitch-", new KeyTrigger(KeyInput.KEY_L));
181+
}
182+
183+
private void addMapping(String mappingName, Trigger... triggers) {
184+
inputManager.addMapping(mappingName, triggers);
185+
inputManager.addListener(this, mappingName);
186+
}
187+
188+
private BitmapText createLabelText(int x, int y, String text) {
189+
BitmapText bmp = new BitmapText(guiFont);
190+
bmp.setText(text);
191+
bmp.setLocalTranslation(x, settings.getHeight() - y, 0);
192+
bmp.setColor(ColorRGBA.Red);
193+
guiNode.attachChild(bmp);
194+
return bmp;
195+
}
196+
197+
private Geometry makeShape(String name, Mesh mesh, ColorRGBA color) {
198+
Geometry geo = new Geometry(name, mesh);
199+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
200+
mat.setColor("Color", color);
201+
geo.setMaterial(mat);
202+
return geo;
203+
}
70204
}

0 commit comments

Comments
 (0)