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