1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/*
* 11/19/04 1.0 moved to LGPL.
* 12/12/99 Initial version. mdm@techie.com
*-----------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*----------------------------------------------------------------------
*/
package javazoom.jl.decoder;
/**
* The <code>Equalizer</code> class can be used to specify
* equalization settings for the MPEG audio decoder.
* <p>
* The equalizer consists of 32 band-pass filters.
* Each band of the equalizer can take on a fractional value between
* -1.0 and +1.0.
* At -1.0, the input signal is attenuated by 6dB, at +1.0 the signal is
* amplified by 6dB.
*
* @see Decoder
*
* @author MDM
*/
public final class Equalizer
{
/**
* Equalizer setting to denote that a given band will not be
* present in the output signal.
*/
static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;
static public final Equalizer PASS_THRU_EQ = new Equalizer();
private static final int BANDS = 32;
private final float[] settings = new float[BANDS];
/**
* Creates a new <code>Equalizer</code> instance.
*/
public Equalizer()
{
}
// private Equalizer(float b1, float b2, float b3, float b4, float b5,
// float b6, float b7, float b8, float b9, float b10, float b11,
// float b12, float b13, float b14, float b15, float b16,
// float b17, float b18, float b19, float b20);
public Equalizer(float[] settings)
{
setFrom(settings);
}
public Equalizer(EQFunction eq)
{
setFrom(eq);
}
public void setFrom(float[] eq)
{
reset();
int max = (eq.length > BANDS) ? BANDS : eq.length;
for (int i=0; i<max; i++)
{
settings[i] = limit(eq[i]);
}
}
public void setFrom(EQFunction eq)
{
reset();
int max = BANDS;
for (int i=0; i<max; i++)
{
settings[i] = limit(eq.getBand(i));
}
}
/**
* Sets the bands of this equalizer to the value the bands of
* another equalizer. Bands that are not present in both equalizers are ignored.
*/
public void setFrom(Equalizer eq)
{
if (eq!=this)
{
setFrom(eq.settings);
}
}
/**
* Sets all bands to 0.0
*/
public void reset()
{
for (int i=0; i<BANDS; i++)
{
settings[i] = 0.0f;
}
}
/**
* Retrieves the number of bands present in this equalizer.
*/
public int getBandCount()
{
return settings.length;
}
public float setBand(int band, float neweq)
{
float eq = 0.0f;
if ((band>=0) && (band<BANDS))
{
eq = settings[band];
settings[band] = limit(neweq);
}
return eq;
}
/**
* Retrieves the eq setting for a given band.
*/
public float getBand(int band)
{
float eq = 0.0f;
if ((band>=0) && (band<BANDS))
{
eq = settings[band];
}
return eq;
}
private float limit(float eq)
{
if (eq==BAND_NOT_PRESENT)
return eq;
if (eq > 1.0f)
return 1.0f;
if (eq < -1.0f)
return -1.0f;
return eq;
}
/**
* Retrieves an array of floats whose values represent a
* scaling factor that can be applied to linear samples
* in each band to provide the equalization represented by
* this instance.
*
* @return an array of factors that can be applied to the
* subbands.
*/
float[] getBandFactors()
{
float[] factors = new float[BANDS];
for (int i=0, maxCount=BANDS; i<maxCount; i++)
{
factors[i] = getBandFactor(settings[i]);
}
return factors;
}
/**
* Converts an equalizer band setting to a sample factor.
* The factor is determined by the function f = 2^n where
* n is the equalizer band setting in the range [-1.0,1.0].
*
*/
float getBandFactor(float eq)
{
if (eq==BAND_NOT_PRESENT)
return 0.0f;
float f = (float)Math.pow(2.0, eq);
return f;
}
static abstract public class EQFunction
{
/**
* Returns the setting of a band in the equalizer.
*
* @param band The index of the band to retrieve the setting
* for.
*
* @return the setting of the specified band. This is a value between
* -1 and +1.
*/
public float getBand(int band)
{
return 0.0f;
}
}
}
|