|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2025 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.audio; |
| 33 | + |
| 34 | +import com.jme3.export.InputCapsule; |
| 35 | +import com.jme3.export.JmeExporter; |
| 36 | +import com.jme3.export.JmeImporter; |
| 37 | +import com.jme3.export.OutputCapsule; |
| 38 | +import com.jme3.util.NativeObject; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | + |
| 42 | +/** |
| 43 | + * Represents an OpenAL EFX Band-Pass Filter. |
| 44 | + * Changes to settings are applied immediately to the native OpenAL filter. |
| 45 | + */ |
| 46 | +public class BandPassFilter extends Filter { |
| 47 | + |
| 48 | + // Default values based on OpenAL EFX specification defaults |
| 49 | + protected float volume = 1.0f; |
| 50 | + protected float highFreqVolume = 1.0f; |
| 51 | + protected float lowFreqVolume = 1.0f; |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructs a low-pass filter with default settings. |
| 55 | + * Required for jME deserialization |
| 56 | + */ |
| 57 | + public BandPassFilter() {} |
| 58 | + |
| 59 | + protected BandPassFilter(int id) { |
| 60 | + super(id); |
| 61 | + } |
| 62 | + |
| 63 | + public BandPassFilter(float volume, float highFreqVolume, float lowFreqVolume) { |
| 64 | + super(); |
| 65 | + setVolume(volume); |
| 66 | + setHighFreqVolume(highFreqVolume); |
| 67 | + setLowFreqVolume(lowFreqVolume); |
| 68 | + } |
| 69 | + |
| 70 | + public float getVolume() { |
| 71 | + return volume; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets the overall gain of the Band-Pass filter. |
| 76 | + * The change is immediately applied to the native OpenAL filter. |
| 77 | + * |
| 78 | + * @param volume The gain value (0.0 to 1.0). |
| 79 | + */ |
| 80 | + public void setVolume(float volume) { |
| 81 | + if (volume < 0 || volume > 1) |
| 82 | + throw new IllegalArgumentException("Volume must be between 0 and 1"); |
| 83 | + |
| 84 | + this.volume = volume; |
| 85 | + this.updateNeeded = true; |
| 86 | + } |
| 87 | + |
| 88 | + public float getHighFreqVolume() { |
| 89 | + return highFreqVolume; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the gain at high frequencies for the Band-Pass filter. |
| 94 | + * The change is immediately applied to the native OpenAL filter. |
| 95 | + * |
| 96 | + * @param highFreqVolume The high-frequency gain value (0.0 to 1.0). |
| 97 | + */ |
| 98 | + public void setHighFreqVolume(float highFreqVolume) { |
| 99 | + if (highFreqVolume < 0 || highFreqVolume > 1) |
| 100 | + throw new IllegalArgumentException("High freq volume must be between 0 and 1"); |
| 101 | + |
| 102 | + this.highFreqVolume = highFreqVolume; |
| 103 | + this.updateNeeded = true; |
| 104 | + } |
| 105 | + |
| 106 | + public float getLowFreqVolume() { |
| 107 | + return lowFreqVolume; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Sets the gain at low frequencies for the Band-Pass filter. |
| 112 | + * The change is immediately applied to the native OpenAL filter. |
| 113 | + * |
| 114 | + * @param lowFreqVolume The low-frequency gain value (0.0 to 1.0). |
| 115 | + */ |
| 116 | + public void setLowFreqVolume(float lowFreqVolume) { |
| 117 | + if (lowFreqVolume < 0 || lowFreqVolume > 1) |
| 118 | + throw new IllegalArgumentException("Low freq volume must be between 0 and 1"); |
| 119 | + |
| 120 | + this.lowFreqVolume = lowFreqVolume; |
| 121 | + this.updateNeeded = true; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public NativeObject createDestructableClone() { |
| 126 | + return new BandPassFilter(this.id); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Retrieves a unique identifier for this filter. Used internally for native object management. |
| 131 | + * |
| 132 | + * @return a unique long identifier. |
| 133 | + */ |
| 134 | + @Override |
| 135 | + public long getUniqueId() { |
| 136 | + return ((long) OBJTYPE_FILTER << 32) | (0xffffffffL & (long) id); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void write(JmeExporter ex) throws IOException { |
| 141 | + super.write(ex); |
| 142 | + OutputCapsule oc = ex.getCapsule(this); |
| 143 | + oc.write(this.volume, "volume", 10.f); |
| 144 | + oc.write(this.lowFreqVolume, "lf_volume", 1.0f); |
| 145 | + oc.write(this.highFreqVolume, "hf_volume", 1.0f); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void read(JmeImporter im) throws IOException { |
| 150 | + super.read(im); |
| 151 | + InputCapsule ic = im.getCapsule(this); |
| 152 | + this.volume = ic.readFloat("volume", 1.0f); |
| 153 | + this.lowFreqVolume = ic.readFloat("lf_volume", 1.0f); |
| 154 | + this.highFreqVolume = ic.readFloat("hf_volume", 1.0f); |
| 155 | + } |
| 156 | +} |
0 commit comments