Skip to content

Commit 1c2195e

Browse files
authored
Merge pull request #2425 from capdevon/capdevon-TestOgg
jme3-examples: TestOgg - added additional parameter tests
2 parents 3daabf2 + 9dd1165 commit 1c2195e

File tree

1 file changed

+150
-14
lines changed

1 file changed

+150
-14
lines changed

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

Lines changed: 150 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,170 @@
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;
4466

45-
public static void main(String[] args){
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);
74+
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 marker = makeShape("Marker", new Sphere(16, 16, 1f), ColorRGBA.Blue);
88+
rootNode.attachChild(marker);
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);
113+
}
114+
115+
@Override
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("Status: ").append(audioSource.getStatus()).append("\n");
130+
sb.append("SourceId: ").append(audioSource.getChannel()).append("\n");
131+
sb.append("DryFilter: ").append(audioSource.getDryFilter() != null).append("\n");
132+
sb.append("FilterId: ").append(dryFilter.getId()).append("\n");
133+
134+
bmp.setText(sb.toString());
135+
sb.setLength(0);
136+
}
55137
}
56138

57139
@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();
140+
public void onAction(String name, boolean isPressed, float tpf) {
141+
if (!isPressed) return;
142+
143+
if (name.equals("togglePlayPause")) {
144+
if (audioSource.getStatus() != AudioSource.Status.Playing) {
145+
audioSource.play();
146+
} else {
147+
audioSource.stop();
148+
}
149+
} else if (name.equals("togglePositional")) {
150+
boolean positional = audioSource.isPositional();
151+
audioSource.setPositional(!positional);
152+
153+
} else if (name.equals("dryFilter")) {
154+
boolean hasFilter = audioSource.getDryFilter() != null;
155+
audioSource.setDryFilter(hasFilter ? null : dryFilter);
156+
157+
} else if (name.equals("Volume+")) {
158+
volume = FastMath.clamp(volume + 0.1f, 0, 5f);
159+
audioSource.setVolume(volume);
160+
161+
} else if (name.equals("Volume-")) {
162+
volume = FastMath.clamp(volume - 0.1f, 0, 5f);
163+
audioSource.setVolume(volume);
164+
165+
} else if (name.equals("Pitch+")) {
166+
pitch = FastMath.clamp(pitch + 0.1f, 0.5f, 2f);
167+
audioSource.setPitch(pitch);
168+
169+
} else if (name.equals("Pitch-")) {
170+
pitch = FastMath.clamp(pitch - 0.1f, 0.5f, 2f);
171+
audioSource.setPitch(pitch);
67172
}
68173
}
69174

175+
private void registerInputMappings() {
176+
addMapping("togglePlayPause", new KeyTrigger(KeyInput.KEY_P));
177+
addMapping("togglePositional", new KeyTrigger(KeyInput.KEY_RETURN));
178+
addMapping("dryFilter", new KeyTrigger(KeyInput.KEY_SPACE));
179+
addMapping("Volume+", new KeyTrigger(KeyInput.KEY_I));
180+
addMapping("Volume-", new KeyTrigger(KeyInput.KEY_K));
181+
addMapping("Pitch+", new KeyTrigger(KeyInput.KEY_J));
182+
addMapping("Pitch-", new KeyTrigger(KeyInput.KEY_L));
183+
}
184+
185+
private void addMapping(String mappingName, Trigger... triggers) {
186+
inputManager.addMapping(mappingName, triggers);
187+
inputManager.addListener(this, mappingName);
188+
}
189+
190+
private BitmapText createLabelText(int x, int y, String text) {
191+
BitmapText bmp = new BitmapText(guiFont);
192+
bmp.setText(text);
193+
bmp.setLocalTranslation(x, settings.getHeight() - y, 0);
194+
bmp.setColor(ColorRGBA.Red);
195+
guiNode.attachChild(bmp);
196+
return bmp;
197+
}
198+
199+
private Geometry makeShape(String name, Mesh mesh, ColorRGBA color) {
200+
Geometry geo = new Geometry(name, mesh);
201+
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
202+
mat.setColor("Color", color);
203+
geo.setMaterial(mat);
204+
return geo;
205+
}
70206
}

0 commit comments

Comments
 (0)