~gryle-devel/gryle/trunk-deleted

« back to all changes in this revision

Viewing changes to src/javazoom/jl/decoder/Equalizer.java

  • Committer: Richard Leo Marsh Warburton
  • Date: 2007-01-13 22:08:02 UTC
  • mto: (1.1.6 gryle)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: rlmw@viglab-28-20070113220802-6cjjur0hdk1rce47
added src files

Show diffs side-by-side

added added

removed removed

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