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