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