|
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 |
|
35 | 35 | import com.jme3.audio.AudioData; |
36 | 36 | import com.jme3.audio.AudioNode; |
37 | 37 | import 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; |
| 43 | +import com.jme3.material.Material; |
| 44 | +import com.jme3.math.ColorRGBA; |
38 | 45 | import com.jme3.math.FastMath; |
39 | 46 | import com.jme3.math.Vector3f; |
| 47 | +import com.jme3.scene.Geometry; |
| 48 | +import com.jme3.scene.Mesh; |
| 49 | +import com.jme3.scene.debug.Grid; |
| 50 | +import com.jme3.scene.shape.Sphere; |
40 | 51 |
|
41 | | -public class TestReverb extends SimpleApplication { |
42 | | - |
43 | | - private AudioNode audioSource; |
44 | | - private float time = 0; |
45 | | - private float nextTime = 1; |
46 | | - |
47 | | - public static void main(String[] args) { |
48 | | - TestReverb test = new TestReverb(); |
49 | | - test.start(); |
50 | | - } |
51 | | - |
52 | | - @Override |
53 | | - public void simpleInitApp() { |
54 | | - audioSource = new AudioNode(assetManager, "Sound/Effects/Bang.wav", |
55 | | - AudioData.DataType.Buffer); |
56 | | - |
57 | | - float[] eax = new float[]{15, 38.0f, 0.300f, -1000, -3300, 0, |
58 | | - 1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f, 0.00f, |
59 | | - -229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f, 0.250f, |
60 | | - 0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f}; |
61 | | - audioRenderer.setEnvironment(new Environment(eax)); |
62 | | - Environment env = Environment.Cavern; |
63 | | - audioRenderer.setEnvironment(env); |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public void simpleUpdate(float tpf) { |
68 | | - time += tpf; |
69 | | - |
70 | | - if (time > nextTime) { |
71 | | - Vector3f v = new Vector3f(); |
72 | | - v.setX(FastMath.nextRandomFloat()); |
73 | | - v.setY(FastMath.nextRandomFloat()); |
74 | | - v.setZ(FastMath.nextRandomFloat()); |
75 | | - v.multLocal(40, 2, 40); |
76 | | - v.subtractLocal(20, 1, 20); |
77 | | - |
78 | | - audioSource.setLocalTranslation(v); |
79 | | - audioSource.playInstance(); |
80 | | - time = 0; |
81 | | - nextTime = FastMath.nextRandomFloat() * 2 + 0.5f; |
| 52 | +/** |
| 53 | + * @author capdevon |
| 54 | + */ |
| 55 | +public class TestReverb extends SimpleApplication implements ActionListener { |
| 56 | + |
| 57 | + public static void main(String[] args) { |
| 58 | + TestReverb app = new TestReverb(); |
| 59 | + app.start(); |
| 60 | + } |
| 61 | + |
| 62 | + private AudioNode audioSource; |
| 63 | + private float time = 0; |
| 64 | + private float nextTime = 1; |
| 65 | + |
| 66 | + /** |
| 67 | + * ### Effects ### |
| 68 | + * Changing a parameter value in the Effect Object after it has been attached to the Auxiliary Effect |
| 69 | + * Slot will not affect the effect in the effect slot. To update the parameters of the effect in the effect |
| 70 | + * slot, an application must update the parameters of an Effect object and then re-attach it to the |
| 71 | + * Auxiliary Effect Slot. |
| 72 | + */ |
| 73 | + private int index = 0; |
| 74 | + private final Environment[] environments = { |
| 75 | + Environment.Cavern, |
| 76 | + Environment.AcousticLab, |
| 77 | + Environment.Closet, |
| 78 | + Environment.Dungeon, |
| 79 | + Environment.Garage |
| 80 | + }; |
| 81 | + |
| 82 | + @Override |
| 83 | + public void simpleInitApp() { |
| 84 | + |
| 85 | + configureCamera(); |
| 86 | + |
| 87 | + // Activate the Environment preset |
| 88 | + audioRenderer.setEnvironment(environments[index]); |
| 89 | + |
| 90 | + // Activate 3D audio |
| 91 | + audioSource = new AudioNode(assetManager, "Sound/Effects/Bang.wav", AudioData.DataType.Buffer); |
| 92 | + audioSource.setLooping(false); |
| 93 | + audioSource.setVolume(1.2f); |
| 94 | + audioSource.setPositional(true); |
| 95 | + audioSource.setMaxDistance(100); |
| 96 | + audioSource.setRefDistance(5); |
| 97 | + audioSource.setReverbEnabled(true); |
| 98 | + audioSource.setReverbFilter(new LowPassFilter(1f, 1f)); |
| 99 | + rootNode.attachChild(audioSource); |
| 100 | + |
| 101 | + Geometry marker = makeShape("Marker", new Sphere(16, 16, 1f), ColorRGBA.Red); |
| 102 | + audioSource.attachChild(marker); |
| 103 | + |
| 104 | + Geometry grid = makeShape("DebugGrid", new Grid(21, 21, 4), ColorRGBA.Blue); |
| 105 | + grid.center().move(0, 0, 0); |
| 106 | + rootNode.attachChild(grid); |
| 107 | + |
| 108 | + registerInputMappings(); |
| 109 | + } |
| 110 | + |
| 111 | + private void configureCamera() { |
| 112 | + flyCam.setMoveSpeed(50f); |
| 113 | + flyCam.setDragToRotate(true); |
| 114 | + |
| 115 | + cam.setLocation(Vector3f.UNIT_XYZ.mult(50f)); |
| 116 | + cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y); |
| 117 | + } |
| 118 | + |
| 119 | + private Geometry makeShape(String name, Mesh mesh, ColorRGBA color) { |
| 120 | + Geometry geo = new Geometry(name, mesh); |
| 121 | + Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); |
| 122 | + mat.setColor("Color", color); |
| 123 | + geo.setMaterial(mat); |
| 124 | + return geo; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void simpleUpdate(float tpf) { |
| 129 | + time += tpf; |
| 130 | + |
| 131 | + if (time > nextTime) { |
| 132 | + time = 0; |
| 133 | + nextTime = FastMath.nextRandomFloat() * 2 + 0.5f; |
| 134 | + |
| 135 | + Vector3f position = getRandomPosition(); |
| 136 | + audioSource.setLocalTranslation(position); |
| 137 | + audioSource.playInstance(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private Vector3f getRandomPosition() { |
| 142 | + float x = FastMath.nextRandomFloat(); |
| 143 | + float y = FastMath.nextRandomFloat(); |
| 144 | + float z = FastMath.nextRandomFloat(); |
| 145 | + Vector3f vec = new Vector3f(x, y, z); |
| 146 | + vec.multLocal(40, 2, 40); |
| 147 | + vec.subtractLocal(20, 1, 20); |
| 148 | + return vec; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onAction(String name, boolean isPressed, float tpf) { |
| 153 | + if (!isPressed) return; |
| 154 | + |
| 155 | + if (name.equals("toggleReverbEnabled")) { |
| 156 | + boolean reverbEnabled = !audioSource.isReverbEnabled(); |
| 157 | + audioSource.setReverbEnabled(reverbEnabled); |
| 158 | + System.out.println("reverbEnabled: " + reverbEnabled); |
| 159 | + |
| 160 | + } else if (name.equals("nextEnvironment")) { |
| 161 | + index = (index + 1) % environments.length; |
| 162 | + audioRenderer.setEnvironment(environments[index]); |
| 163 | + System.out.println("Next Environment Index: " + index); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void registerInputMappings() { |
| 168 | + addMapping("toggleReverbEnabled", new KeyTrigger(KeyInput.KEY_SPACE)); |
| 169 | + addMapping("nextEnvironment", new KeyTrigger(KeyInput.KEY_N)); |
| 170 | + } |
| 171 | + |
| 172 | + private void addMapping(String mappingName, Trigger... triggers) { |
| 173 | + inputManager.addMapping(mappingName, triggers); |
| 174 | + inputManager.addListener(this, mappingName); |
82 | 175 | } |
83 | | - } |
| 176 | + |
84 | 177 | } |
0 commit comments