f0eb618501149290791fac587ea409ba4ffeef24
[IRC.git] / Robust / src / Tests / 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 \r
23 /**\r
24  * The <code>Equalizer</code> class can be used to specify\r
25  * equalization settings for the MPEG audio decoder. \r
26  * <p>\r
27  * The equalizer consists of 32 band-pass filters. \r
28  * Each band of the equalizer can take on a fractional value between \r
29  * -1.0 and +1.0.\r
30  * At -1.0, the input signal is attenuated by 6dB, at +1.0 the signal is\r
31  * amplified by 6dB. \r
32  * \r
33  * @see Decoder\r
34  * \r
35  * @author MDM\r
36  */\r
37 @LATTICE("B<T")\r
38 @METHODDEFAULT("OUT<V,V<SH,SH<C,C<IN,SH*,THISLOC=IN,GLOBALLOC=IN")\r
39 public final class Equalizer\r
40 {               \r
41         /**\r
42          * Equalizer setting to denote that a given band will not be\r
43          * present in the output signal.\r
44          */\r
45         @LOC("T") static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;\r
46                 \r
47         @LOC("T") static public final Equalizer PASS_THRU_EQ = new Equalizer();\r
48         \r
49         @LOC("T") private static final int BANDS = 32;\r
50         \r
51         @LOC("B") private final float[] settings = new float[BANDS];\r
52         \r
53         /**\r
54          * Creates a new <code>Equalizer</code> instance. \r
55          */\r
56         public Equalizer()\r
57         {               \r
58         }\r
59         \r
60 //      private Equalizer(float b1, float b2, float b3, float b4, float b5,\r
61 //                                       float b6, float b7, float b8, float b9, float b10, float b11,\r
62 //                                       float b12, float b13, float b14, float b15, float b16,\r
63 //                                       float b17, float b18, float b19, float b20);\r
64 \r
65         public Equalizer(float[] settings)\r
66         {\r
67                 setFrom(settings);\r
68         }\r
69         \r
70         public Equalizer(EQFunction eq)\r
71         {\r
72                 setFrom(eq);\r
73         }\r
74         \r
75         public void setFrom(float[] eq)\r
76         {\r
77                 reset();\r
78                 int max = (eq.length > BANDS) ? BANDS : eq.length;\r
79                 \r
80                 for (int i=0; i<max; i++)\r
81                 {\r
82                         settings[i] = limit(eq[i]);\r
83                 }\r
84         }\r
85 \r
86         public void setFrom(EQFunction eq)\r
87         {\r
88                 reset();\r
89                 int max = BANDS;\r
90                 \r
91                 for (int i=0; i<max; i++)\r
92                 {\r
93                         settings[i] = limit(eq.getBand(i));\r
94                 }               \r
95         }\r
96         \r
97         /**\r
98          * Sets the bands of this equalizer to the value the bands of\r
99          * another equalizer. Bands that are not present in both equalizers are ignored. \r
100          */\r
101         public void setFrom(Equalizer eq)\r
102         {\r
103                 if (eq!=this)\r
104                 {\r
105                         setFrom(eq.settings);\r
106                 }\r
107         }\r
108         \r
109         \r
110         \r
111         \r
112         /**\r
113          * Sets all bands to 0.0\r
114          */\r
115         public void reset()\r
116         {\r
117                 for (int i=0; i<BANDS; i++)\r
118                 {\r
119                         settings[i] = 0.0f;\r
120                 }\r
121         }\r
122 \r
123         \r
124         /**\r
125          * Retrieves the number of bands present in this equalizer.\r
126          */\r
127         public int getBandCount()\r
128         {\r
129                 return settings.length; \r
130         }\r
131         \r
132         public float setBand(int band, float neweq)\r
133         {\r
134                 float eq = 0.0f;\r
135                 \r
136                 if ((band>=0) && (band<BANDS))\r
137                 {\r
138                         eq = settings[band];\r
139                         settings[band] = limit(neweq);\r
140                 }\r
141                 \r
142                 return eq;              \r
143         }\r
144         \r
145         \r
146         \r
147         /**\r
148          * Retrieves the eq setting for a given band.\r
149          */\r
150         public float getBand(int band)\r
151         {\r
152                 float eq = 0.0f;\r
153                 \r
154                 if ((band>=0) && (band<BANDS))\r
155                 {\r
156                         eq = settings[band];\r
157                 }\r
158                 \r
159                 return eq;\r
160         }\r
161         \r
162         private float limit(float eq)\r
163         {\r
164                 if (eq==BAND_NOT_PRESENT)\r
165                         return eq;\r
166                 if (eq > 1.0f)\r
167                         return 1.0f;\r
168                 if (eq < -1.0f)\r
169                         return -1.0f;\r
170                 \r
171                 return eq;\r
172         }\r
173         \r
174         /**\r
175          * Retrieves an array of floats whose values represent a\r
176          * scaling factor that can be applied to linear samples\r
177          * in each band to provide the equalization represented by\r
178          * this instance. \r
179          * \r
180          * @return      an array of factors that can be applied to the\r
181          *                      subbands.\r
182          */\r
183         @RETURNLOC("OUT") \r
184         float[] getBandFactors()\r
185         {\r
186                 @LOC("OUT") float[] factors = new float[BANDS];\r
187                 @LOC("C") int maxCount = BANDS;\r
188                 for (@LOC("SH") int i=0; i<maxCount; i++)\r
189                 {\r
190                         factors[i] = getBandFactor(settings[i]);\r
191                 }\r
192                 \r
193                 return factors;\r
194         }\r
195         \r
196         /**\r
197          * Converts an equalizer band setting to a sample factor.\r
198          * The factor is determined by the function f = 2^n where\r
199          * n is the equalizer band setting in the range [-1.0,1.0].\r
200          *       \r
201          */\r
202         @RETURNLOC("C")\r
203         float getBandFactor(@LOC("IN") float eq)\r
204         {\r
205                 if (eq==BAND_NOT_PRESENT)\r
206                         return 0.0f;\r
207                 \r
208                 @LOC("C") float f = (float)Math.pow(2.0, eq);\r
209                 return f;\r
210         }\r
211         \r
212         \r
213         static abstract public class EQFunction\r
214         {\r
215                 /**\r
216                  * Returns the setting of a band in the equalizer. \r
217                  * \r
218                  * @param band  The index of the band to retrieve the setting\r
219                  *                              for. \r
220                  * \r
221                  * @return              the setting of the specified band. This is a value between\r
222                  *                              -1 and +1.\r
223                  */\r
224                 public float getBand(int band)\r
225                 {\r
226                         return 0.0f;    \r
227                 }\r
228                 \r
229         }\r
230                 \r
231 }\r