1dc88f42de9bdd820ef8c76ce8340ecbc709cefc
[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 public final class Equalizer\r
38 {               \r
39         /**\r
40          * Equalizer setting to denote that a given band will not be\r
41          * present in the output signal.\r
42          */\r
43         static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;\r
44                 \r
45         static public final Equalizer   PASS_THRU_EQ = new Equalizer();\r
46         \r
47         private static final int BANDS = 32;\r
48         \r
49         private final float[]   settings = new float[BANDS];\r
50         \r
51         /**\r
52          * Creates a new <code>Equalizer</code> instance. \r
53          */\r
54         public Equalizer()\r
55         {               \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         {\r
65                 setFrom(settings);\r
66         }\r
67         \r
68         public Equalizer(EQFunction eq)\r
69         {\r
70                 setFrom(eq);\r
71         }\r
72         \r
73         public void setFrom(float[] eq)\r
74         {\r
75                 reset();\r
76                 int max = (eq.length > BANDS) ? BANDS : eq.length;\r
77                 \r
78                 for (int i=0; i<max; i++)\r
79                 {\r
80                         settings[i] = limit(eq[i]);\r
81                 }\r
82         }\r
83 \r
84         public void setFrom(EQFunction eq)\r
85         {\r
86                 reset();\r
87                 int max = BANDS;\r
88                 \r
89                 for (int i=0; i<max; i++)\r
90                 {\r
91                         settings[i] = limit(eq.getBand(i));\r
92                 }               \r
93         }\r
94         \r
95         /**\r
96          * Sets the bands of this equalizer to the value the bands of\r
97          * another equalizer. Bands that are not present in both equalizers are ignored. \r
98          */\r
99         public void setFrom(Equalizer eq)\r
100         {\r
101                 if (eq!=this)\r
102                 {\r
103                         setFrom(eq.settings);\r
104                 }\r
105         }\r
106         \r
107         \r
108         \r
109         \r
110         /**\r
111          * Sets all bands to 0.0\r
112          */\r
113         public void reset()\r
114         {\r
115                 for (int i=0; i<BANDS; i++)\r
116                 {\r
117                         settings[i] = 0.0f;\r
118                 }\r
119         }\r
120 \r
121         \r
122         /**\r
123          * Retrieves the number of bands present in this equalizer.\r
124          */\r
125         public int getBandCount()\r
126         {\r
127                 return settings.length; \r
128         }\r
129         \r
130         public float setBand(int band, float neweq)\r
131         {\r
132                 float eq = 0.0f;\r
133                 \r
134                 if ((band>=0) && (band<BANDS))\r
135                 {\r
136                         eq = settings[band];\r
137                         settings[band] = limit(neweq);\r
138                 }\r
139                 \r
140                 return eq;              \r
141         }\r
142         \r
143         \r
144         \r
145         /**\r
146          * Retrieves the eq setting for a given band.\r
147          */\r
148         public float getBand(int band)\r
149         {\r
150                 float eq = 0.0f;\r
151                 \r
152                 if ((band>=0) && (band<BANDS))\r
153                 {\r
154                         eq = settings[band];\r
155                 }\r
156                 \r
157                 return eq;\r
158         }\r
159         \r
160         private float limit(float eq)\r
161         {\r
162                 if (eq==BAND_NOT_PRESENT)\r
163                         return eq;\r
164                 if (eq > 1.0f)\r
165                         return 1.0f;\r
166                 if (eq < -1.0f)\r
167                         return -1.0f;\r
168                 \r
169                 return eq;\r
170         }\r
171         \r
172         /**\r
173          * Retrieves an array of floats whose values represent a\r
174          * scaling factor that can be applied to linear samples\r
175          * in each band to provide the equalization represented by\r
176          * this instance. \r
177          * \r
178          * @return      an array of factors that can be applied to the\r
179          *                      subbands.\r
180          */\r
181         float[] getBandFactors()\r
182         {\r
183                 float[] factors = new float[BANDS];\r
184                 for (int i=0, maxCount=BANDS; i<maxCount; i++)\r
185                 {\r
186                         factors[i] = getBandFactor(settings[i]);\r
187                 }\r
188                 \r
189                 return factors;\r
190         }\r
191         \r
192         /**\r
193          * Converts an equalizer band setting to a sample factor.\r
194          * The factor is determined by the function f = 2^n where\r
195          * n is the equalizer band setting in the range [-1.0,1.0].\r
196          *       \r
197          */\r
198         float getBandFactor(float eq)\r
199         {\r
200                 if (eq==BAND_NOT_PRESENT)\r
201                         return 0.0f;\r
202                 \r
203                 float f = (float)Math.pow(2.0, eq);\r
204                 return f;\r
205         }\r
206         \r
207         \r
208         static abstract public class EQFunction\r
209         {\r
210                 /**\r
211                  * Returns the setting of a band in the equalizer. \r
212                  * \r
213                  * @param band  The index of the band to retrieve the setting\r
214                  *                              for. \r
215                  * \r
216                  * @return              the setting of the specified band. This is a value between\r
217                  *                              -1 and +1.\r
218                  */\r
219                 public float getBand(int band)\r
220                 {\r
221                         return 0.0f;    \r
222                 }\r
223                 \r
224         }\r
225                 \r
226 }\r