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