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
|
/*
This file is part of Subsonic.
Subsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2011 (C) Sindre Mehus
*/
package github.daneren2005.subphonic.audiofx;
import java.io.Serializable;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.audiofx.Equalizer;
import android.util.Log;
import github.daneren2005.subphonic.util.FileUtil;
/**
* Backward-compatible wrapper for {@link Equalizer}, which is API Level 9.
*
* @author Sindre Mehus
* @version $Id$
*/
public class EqualizerController {
private static final String TAG = EqualizerController.class.getSimpleName();
private final Context context;
private Equalizer equalizer;
// Class initialization fails when this throws an exception.
static {
try {
Class.forName("android.media.audiofx.Equalizer");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Throws an exception if the {@link Equalizer} class is not available.
*/
public static void checkAvailable() throws Throwable {
// Calling here forces class initialization.
}
public EqualizerController(Context context, MediaPlayer mediaPlayer) {
this.context = context;
try {
equalizer = new Equalizer(0, mediaPlayer.getAudioSessionId());
} catch (Throwable x) {
Log.w(TAG, "Failed to create equalizer.", x);
}
}
public void saveSettings() {
try {
if (isAvailable()) {
FileUtil.serialize(context, new EqualizerSettings(equalizer), "equalizer.dat");
}
} catch (Throwable x) {
Log.w(TAG, "Failed to save equalizer settings.", x);
}
}
public void loadSettings() {
try {
if (isAvailable()) {
EqualizerSettings settings = FileUtil.deserialize(context, "equalizer.dat");
if (settings != null) {
settings.apply(equalizer);
}
}
} catch (Throwable x) {
Log.w(TAG, "Failed to load equalizer settings.", x);
}
}
public boolean isAvailable() {
return equalizer != null;
}
public boolean isEnabled() {
return isAvailable() && equalizer.getEnabled();
}
public void release() {
if (isAvailable()) {
equalizer.release();
}
}
public Equalizer getEqualizer() {
return equalizer;
}
private static class EqualizerSettings implements Serializable {
private final short[] bandLevels;
private short preset;
private final boolean enabled;
public EqualizerSettings(Equalizer equalizer) {
enabled = equalizer.getEnabled();
bandLevels = new short[equalizer.getNumberOfBands()];
for (short i = 0; i < equalizer.getNumberOfBands(); i++) {
bandLevels[i] = equalizer.getBandLevel(i);
}
try {
preset = equalizer.getCurrentPreset();
} catch (Exception x) {
preset = -1;
}
}
public void apply(Equalizer equalizer) {
for (short i = 0; i < bandLevels.length; i++) {
equalizer.setBandLevel(i, bandLevels[i]);
}
if (preset >= 0 && preset < equalizer.getNumberOfPresets()) {
equalizer.usePreset(preset);
}
equalizer.setEnabled(enabled);
}
}
}
|