Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/Equalizer.java
8650 views
/*1* 11/19/04 1.0 moved to LGPL.2* 12/12/99 Initial version. [email protected]3*-----------------------------------------------------------------------4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU Library General Public License as published6* by the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU Library General Public License for more details.13*14* You should have received a copy of the GNU Library General Public15* License along with this program; if not, write to the Free Software16* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.17*----------------------------------------------------------------------18*/192021package javazoom.jl.decoder;2223/**24* The <code>Equalizer</code> class can be used to specify25* equalization settings for the MPEG audio decoder.26* <p>27* The equalizer consists of 32 band-pass filters.28* Each band of the equalizer can take on a fractional value between29* -1.0 and +1.0.30* At -1.0, the input signal is attenuated by 6dB, at +1.0 the signal is31* amplified by 6dB.32*33* @see Decoder34*35* @author MDM36*/37public final class Equalizer38{39/**40* Equalizer setting to denote that a given band will not be41* present in the output signal.42*/43public static final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;4445public static final Equalizer PASS_THRU_EQ = new Equalizer();4647private static final int BANDS = 32;4849private final float[] settings = new float[BANDS];5051/**52* Creates a new <code>Equalizer</code> instance.53*/54public Equalizer()55{56}5758// private Equalizer(float b1, float b2, float b3, float b4, float b5,59// float b6, float b7, float b8, float b9, float b10, float b11,60// float b12, float b13, float b14, float b15, float b16,61// float b17, float b18, float b19, float b20);6263public Equalizer(float[] settings)64{65setFrom(settings);66}6768public Equalizer(EQFunction eq)69{70setFrom(eq);71}7273public void setFrom(float[] eq)74{75reset();76int max = (eq.length > BANDS) ? BANDS : eq.length;7778for (int i=0; i<max; i++)79{80settings[i] = limit(eq[i]);81}82}8384public void setFrom(EQFunction eq)85{86reset();87int max = BANDS;8889for (int i=0; i<max; i++)90{91settings[i] = limit(eq.getBand(i));92}93}9495/**96* Sets the bands of this equalizer to the value the bands of97* another equalizer. Bands that are not present in both equalizers are ignored.98*/99public void setFrom(Equalizer eq)100{101if (eq!=this)102{103setFrom(eq.settings);104}105}106107108109110/**111* Sets all bands to 0.0112*/113public void reset()114{115for (int i=0; i<BANDS; i++)116{117settings[i] = 0.0f;118}119}120121122/**123* Retrieves the number of bands present in this equalizer.124*/125public int getBandCount()126{127return settings.length;128}129130public float setBand(int band, float neweq)131{132float eq = 0.0f;133134if ((band>=0) && (band<BANDS))135{136eq = settings[band];137settings[band] = limit(neweq);138}139140return eq;141}142143144145/**146* Retrieves the EQ setting for a given band.147*/148public float getBand(int band)149{150float eq = 0.0f;151152if ((band>=0) && (band<BANDS))153{154eq = settings[band];155}156157return eq;158}159160private float limit(float eq)161{162if (eq==BAND_NOT_PRESENT)163return eq;164if (eq > 1.0f)165return 1.0f;166if (eq < -1.0f)167return -1.0f;168169return eq;170}171172/**173* Retrieves an array of floats whose values represent a174* scaling factor that can be applied to linear samples175* in each band to provide the equalization represented by176* this instance.177*178* @return an array of factors that can be applied to the179* subbands.180*/181float[] getBandFactors()182{183float[] factors = new float[BANDS];184for (int i=0, maxCount=BANDS; i<maxCount; i++)185{186factors[i] = getBandFactor(settings[i]);187}188189return factors;190}191192/**193* Converts an equalizer band setting to a sample factor.194* The factor is determined by the function f = 2^n where195* n is the equalizer band setting in the range [-1.0,1.0].196*197*/198float getBandFactor(float eq)199{200if (eq==BAND_NOT_PRESENT)201return 0.0f;202203float f = (float)Math.pow(2.0, eq);204return f;205}206207208public abstract static class EQFunction209{210/**211* Returns the setting of a band in the equalizer.212*213* @param band The index of the band to retrieve the setting214* for.215*216* @return the setting of the specified band. This is a value between217* -1 and +1.218*/219public float getBand(int band)220{221return 0.0f;222}223224}225226}227228229