-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.java
228 lines (203 loc) · 8.53 KB
/
Audio.java
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
228
/*
* Copyright 2011 Witoslaw Koczewsi <wi@koczewski.de>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
* General Public License as published by the Free Software Foundation, either version 3 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 Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License along with this program. If not,
* see <http://www.gnu.org/licenses/>.
*/
import java.util.ArrayList;
import java.util.List;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.BooleanControl;
import javax.sound.sampled.CompoundControl;
import javax.sound.sampled.Control;
import javax.sound.sampled.Control.Type;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.Mixer.Info;
public class Audio {
public static void setMasterOutputVolume(float value) {
if (value < 0 || value > 1)
throw new IllegalArgumentException(
"Volume can only be set to a value from 0 to 1. Given value is illegal: " + value);
Line line = getMasterOutputLine();
if (line == null) throw new RuntimeException("Master output port not found");
boolean opened = open(line);
try {
FloatControl control = getVolumeControl(line);
if (control == null)
throw new RuntimeException("Volume control not found in master port: " + toString(line));
control.setValue(value);
} finally {
if (opened) line.close();
}
}
public static Float getMasterOutputVolume() {
Line line = getMasterOutputLine();
if (line == null) return null;
boolean opened = open(line);
try {
FloatControl control = getVolumeControl(line);
if (control == null) return null;
return control.getValue();
} finally {
if (opened) line.close();
}
}
public static void setMasterOutputMute(boolean value) {
Line line = getMasterOutputLine();
if (line == null) throw new RuntimeException("Master output port not found");
boolean opened = open(line);
try {
BooleanControl control = getMuteControl(line);
if (control == null)
throw new RuntimeException("Mute control not found in master port: " + toString(line));
control.setValue(value);
} finally {
if (opened) line.close();
}
}
public static Boolean getMasterOutputMute() {
Line line = getMasterOutputLine();
if (line == null) return null;
boolean opened = open(line);
try {
BooleanControl control = getMuteControl(line);
if (control == null) return null;
return control.getValue();
} finally {
if (opened) line.close();
}
}
public static Line getMasterOutputLine() {
for (Mixer mixer : getMixers()) {
for (Line line : getAvailableOutputLines(mixer)) {
if (line.getLineInfo().toString().contains("SPEAKER")) return line;
}
}
return null;
}
public static FloatControl getVolumeControl(Line line) {
if (!line.isOpen()) throw new RuntimeException("Line is closed: " + toString(line));
return (FloatControl) findControl(FloatControl.Type.VOLUME, line.getControls());
}
public static BooleanControl getMuteControl(Line line) {
if (!line.isOpen()) throw new RuntimeException("Line is closed: " + toString(line));
return (BooleanControl) findControl(BooleanControl.Type.MUTE, line.getControls());
}
private static Control findControl(Type type, Control... controls) {
if (controls == null || controls.length == 0) return null;
for (Control control : controls) {
if (control.getType().equals(type)) return control;
if (control instanceof CompoundControl) {
CompoundControl compoundControl = (CompoundControl) control;
Control member = findControl(type, compoundControl.getMemberControls());
if (member != null) return member;
}
}
return null;
}
public static List<Mixer> getMixers() {
Info[] infos = AudioSystem.getMixerInfo();
List<Mixer> mixers = new ArrayList<Mixer>(infos.length);
for (Info info : infos) {
Mixer mixer = AudioSystem.getMixer(info);
mixers.add(mixer);
}
return mixers;
}
public static List<Line> getAvailableOutputLines(Mixer mixer) {
return getAvailableLines(mixer, mixer.getTargetLineInfo());
}
public static List<Line> getAvailableInputLines(Mixer mixer) {
return getAvailableLines(mixer, mixer.getSourceLineInfo());
}
private static List<Line> getAvailableLines(Mixer mixer, Line.Info[] lineInfos) {
List<Line> lines = new ArrayList<Line>(lineInfos.length);
for (Line.Info lineInfo : lineInfos) {
Line line;
line = getLineIfAvailable(mixer, lineInfo);
if (line != null) lines.add(line);
}
return lines;
}
public static Line getLineIfAvailable(Mixer mixer, Line.Info lineInfo) {
try {
return mixer.getLine(lineInfo);
} catch (LineUnavailableException ex) {
return null;
}
}
public static String getHierarchyInfo() {
StringBuilder sb = new StringBuilder();
for (Mixer mixer : getMixers()) {
sb.append("Mixer: ").append(toString(mixer)).append("\n");
for (Line line : getAvailableOutputLines(mixer)) {
sb.append(" OUT: ").append(toString(line)).append("\n");
boolean opened = open(line);
for (Control control : line.getControls()) {
sb.append(" Control: ").append(toString(control)).append("\n");
if (control instanceof CompoundControl) {
CompoundControl compoundControl = (CompoundControl) control;
for (Control subControl : compoundControl.getMemberControls()) {
sb.append(" Sub-Control: ").append(toString(subControl)).append("\n");
}
}
}
if (opened) line.close();
}
for (Line line : getAvailableOutputLines(mixer)) {
sb.append(" IN: ").append(toString(line)).append("\n");
boolean opened = open(line);
for (Control control : line.getControls()) {
sb.append(" Control: ").append(toString(control)).append("\n");
if (control instanceof CompoundControl) {
CompoundControl compoundControl = (CompoundControl) control;
for (Control subControl : compoundControl.getMemberControls()) {
sb.append(" Sub-Control: ").append(toString(subControl)).append("\n");
}
}
}
if (opened) line.close();
}
sb.append("\n");
}
return sb.toString();
}
public static boolean open(Line line) {
if (line.isOpen()) return false;
try {
line.open();
} catch (LineUnavailableException ex) {
return false;
}
return true;
}
public static String toString(Control control) {
if (control == null) return null;
return control.toString() + " (" + control.getType().toString() + ")";
}
public static String toString(Line line) {
if (line == null) return null;
Line.Info info = line.getLineInfo();
return info.toString();// + " (" + line.getClass().getSimpleName() + ")";
}
public static String toString(Mixer mixer) {
if (mixer == null) return null;
StringBuilder sb = new StringBuilder();
Info info = mixer.getMixerInfo();
sb.append(info.getName());
sb.append(" (").append(info.getDescription()).append(")");
sb.append(mixer.isOpen() ? " [open]" : " [closed]");
return sb.toString();
}
}