|
1 | 1 | /* |
2 | | - * Copyright (c) 2009-2012 jMonkeyEngine |
| 2 | + * Copyright (c) 2009-2025 jMonkeyEngine |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
|
37 | 37 | import com.jme3.audio.AudioNode; |
38 | 38 | import com.jme3.audio.AudioSource; |
39 | 39 | 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; |
40 | 53 |
|
41 | | -public class TestOgg extends SimpleApplication { |
| 54 | +/** |
| 55 | + * |
| 56 | + * @author capdevon |
| 57 | + */ |
| 58 | +public class TestOgg extends SimpleApplication implements ActionListener { |
42 | 59 |
|
| 60 | + private final StringBuilder sb = new StringBuilder(); |
| 61 | + private int frameCount = 0; |
| 62 | + private BitmapText bmp; |
43 | 63 | private AudioNode audioSource; |
| 64 | + private float volume = 1.0f; |
| 65 | + private float pitch = 1.0f; |
44 | 66 |
|
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) { |
46 | 76 | TestOgg test = new TestOgg(); |
47 | 77 | test.start(); |
48 | 78 | } |
49 | 79 |
|
50 | 80 | @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 | + |
53 | 94 | 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); |
54 | 101 | 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 | + } |
55 | 137 | } |
56 | 138 |
|
57 | 139 | @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); |
67 | 172 | } |
68 | 173 | } |
69 | 174 |
|
| 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 | + } |
70 | 206 | } |
0 commit comments