2 * 11/19/04 1.0 moved to LGPL.
3 * 12/12/99 Initial version. mdm@techie.com
4 *-----------------------------------------------------------------------
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as published
7 * by the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *----------------------------------------------------------------------
22 * The <code>Equalizer</code> class can be used to specify equalization settings
23 * for the MPEG audio decoder.
25 * The equalizer consists of 32 band-pass filters. Each band of the equalizer
26 * can take on a fractional value between -1.0 and +1.0. At -1.0, the input
27 * signal is attenuated by 6dB, at +1.0 the signal is amplified by 6dB.
33 @LATTICE("B<IDX,IDX<T,IDX*")
34 @METHODDEFAULT("OUT<V,V<SH,SH<C,C<IN,IN<P,C*,SH*,THISLOC=IN,GLOBALLOC=IN")
35 public final class Equalizer {
37 * Equalizer setting to denote that a given band will not be present in the
41 static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;
44 static public final Equalizer PASS_THRU_EQ = new Equalizer();
47 private static final int BANDS = 32;
50 private final float[] settings = new float[BANDS];
53 * Creates a new <code>Equalizer</code> instance.
58 // 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);
63 public Equalizer(float[] settings) {
67 public Equalizer(EQFunction eq) {
71 public void setFrom(float[] eq) {
73 int max = (eq.length > BANDS) ? BANDS : eq.length;
75 for (int i = 0; i < max; i++) {
76 settings[i] = limit(eq[i]);
80 public void setFrom(EQFunction eq) {
84 for (int i = 0; i < max; i++) {
85 settings[i] = limit(eq.getBand(i));
90 * Sets the bands of this equalizer to the value the bands of another
91 * equalizer. Bands that are not present in both equalizers are ignored.
93 public void setFrom(Equalizer eq) {
100 * Sets all bands to 0.0
102 public void reset() {
103 for (int i = 0; i < BANDS; i++) {
109 * Retrieves the number of bands present in this equalizer.
111 public int getBandCount() {
112 return settings.length;
115 public float setBand(int band, float neweq) {
118 if ((band >= 0) && (band < BANDS)) {
120 settings[band] = limit(neweq);
127 * Retrieves the eq setting for a given band.
129 public float getBand(int band) {
132 if ((band >= 0) && (band < BANDS)) {
139 private float limit(float eq) {
140 if (eq == BAND_NOT_PRESENT)
151 * Retrieves an array of floats whose values represent a scaling factor that
152 * can be applied to linear samples in each band to provide the equalization
153 * represented by this instance.
155 * @return an array of factors that can be applied to the subbands.
157 @LATTICE("OUT<THIS,THISLOC=THIS")
159 float[] getBandFactors() {
160 @LOC("OUT") float[] factors = new float[BANDS];
161 @LOC("THIS,Equalizer.IDX") int maxCount = BANDS;
162 for (@LOC("THIS,Equalizer.IDX") int i = 0; i < maxCount; i++) {
163 factors[i] = getBandFactor(settings[i]);
170 * Converts an equalizer band setting to a sample factor. The factor is
171 * determined by the function f = 2^n where n is the equalizer band setting in
172 * the range [-1.0,1.0].
177 float getBandFactor(@LOC("IN") float eq) {
178 if (eq == BAND_NOT_PRESENT)
181 @LOC("C") float f = (float) Math.pow(2.0, eq);
185 static abstract public class EQFunction {
187 * Returns the setting of a band in the equalizer.
190 * The index of the band to retrieve the setting for.
192 * @return the setting of the specified band. This is a value between -1 and
195 public float getBand(int band) {