summaryrefslogtreecommitdiff
path: root/songdbj/javazoom/jl/decoder/Equalizer.java
diff options
context:
space:
mode:
authorMichiel Van Der Kolk <not.valid@email.address>2005-07-11 15:42:37 +0000
committerMichiel Van Der Kolk <not.valid@email.address>2005-07-11 15:42:37 +0000
commit9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e (patch)
tree4c304cd4151020bd5494d279ee68a105ae3a5a3a /songdbj/javazoom/jl/decoder/Equalizer.java
parentdfa8ecbe609ca8ea194d08560a44fb9a92e94b4b (diff)
downloadrockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.tar.gz
rockbox-9fee0ec4ca0c5b7a334cc29dbb58e76c7a4c736e.zip
Songdb java version, source. only 1.5 compatible
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7101 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'songdbj/javazoom/jl/decoder/Equalizer.java')
-rw-r--r--songdbj/javazoom/jl/decoder/Equalizer.java227
1 files changed, 227 insertions, 0 deletions
diff --git a/songdbj/javazoom/jl/decoder/Equalizer.java b/songdbj/javazoom/jl/decoder/Equalizer.java
new file mode 100644
index 0000000000..57545a939d
--- /dev/null
+++ b/songdbj/javazoom/jl/decoder/Equalizer.java
@@ -0,0 +1,227 @@
1/*
2 * 11/19/04 1.0 moved to LGPL.
3 * 12/12/99 Initial version. mdm@techie.com
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
22package 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 */
38public 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 static public final float BAND_NOT_PRESENT = Float.NEGATIVE_INFINITY;
45
46 static public 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 static abstract public 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}