adding a test case
[IRC.git] / Robust / src / Benchmarks / SSJava / MP3Decoder / Equalizer.java
1 /*
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.
9  *
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.
14  *
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  *----------------------------------------------------------------------
19  */
20
21 /**
22  * The <code>Equalizer</code> class can be used to specify equalization settings
23  * for the MPEG audio decoder.
24  * <p>
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.
28  * 
29  * @see Decoder
30  * 
31  * @author MDM
32  */
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 {
36   /**
37    * Equalizer setting to denote that a given band will not be present in the
38    * output signal.
39    */
40   @LOC("T")
41   static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;
42
43   @LOC("T")
44   static public final Equalizer PASS_THRU_EQ = new Equalizer();
45
46   @LOC("T")
47   private static final int BANDS = 32;
48
49   @LOC("B")
50   private final float[] settings = new float[BANDS];
51
52   /**
53    * Creates a new <code>Equalizer</code> instance.
54    */
55   public Equalizer() {
56   }
57
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);
62
63   public Equalizer(float[] settings) {
64     setFrom(settings);
65   }
66
67   public Equalizer(EQFunction eq) {
68     setFrom(eq);
69   }
70
71   public void setFrom(float[] eq) {
72     reset();
73     int max = (eq.length > BANDS) ? BANDS : eq.length;
74
75     for (int i = 0; i < max; i++) {
76       settings[i] = limit(eq[i]);
77     }
78   }
79
80   public void setFrom(EQFunction eq) {
81     reset();
82     int max = BANDS;
83
84     for (int i = 0; i < max; i++) {
85       settings[i] = limit(eq.getBand(i));
86     }
87   }
88
89   /**
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.
92    */
93   public void setFrom(Equalizer eq) {
94     if (eq != this) {
95       setFrom(eq.settings);
96     }
97   }
98
99   /**
100    * Sets all bands to 0.0
101    */
102   public void reset() {
103     for (int i = 0; i < BANDS; i++) {
104       settings[i] = 0.0f;
105     }
106   }
107
108   /**
109    * Retrieves the number of bands present in this equalizer.
110    */
111   public int getBandCount() {
112     return settings.length;
113   }
114
115   public float setBand(int band, float neweq) {
116     float eq = 0.0f;
117
118     if ((band >= 0) && (band < BANDS)) {
119       eq = settings[band];
120       settings[band] = limit(neweq);
121     }
122
123     return eq;
124   }
125
126   /**
127    * Retrieves the eq setting for a given band.
128    */
129   public float getBand(int band) {
130     float eq = 0.0f;
131
132     if ((band >= 0) && (band < BANDS)) {
133       eq = settings[band];
134     }
135
136     return eq;
137   }
138
139   private float limit(float eq) {
140     if (eq == BAND_NOT_PRESENT)
141       return eq;
142     if (eq > 1.0f)
143       return 1.0f;
144     if (eq < -1.0f)
145       return -1.0f;
146
147     return eq;
148   }
149
150   /**
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.
154    * 
155    * @return an array of factors that can be applied to the subbands.
156    */
157   @LATTICE("OUT<THIS,THISLOC=THIS")
158   @RETURNLOC("OUT")
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]);
164     }
165
166     return factors;
167   }
168
169   /**
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].
173    * 
174    */
175   @RETURNLOC("C")
176   @PCLOC("P")
177   float getBandFactor(@LOC("IN") float eq) {
178     if (eq == BAND_NOT_PRESENT)
179       return 0.0f;
180
181     @LOC("C") float f = (float) Math.pow(2.0, eq);
182     return f;
183   }
184
185   static abstract public class EQFunction {
186     /**
187      * Returns the setting of a band in the equalizer.
188      * 
189      * @param band
190      *          The index of the band to retrieve the setting for.
191      * 
192      * @return the setting of the specified band. This is a value between -1 and
193      *         +1.
194      */
195     public float getBand(int band) {
196       return 0.0f;
197     }
198
199   }
200
201 }