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