1 | package org.apollo.widgets;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Font;
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.sound.sampled.AudioSystem;
|
---|
14 | import javax.sound.sampled.Mixer;
|
---|
15 | import javax.swing.Box;
|
---|
16 | import javax.swing.BoxLayout;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JComboBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JSlider;
|
---|
22 | import javax.swing.event.ChangeEvent;
|
---|
23 | import javax.swing.event.ChangeListener;
|
---|
24 |
|
---|
25 | import org.apollo.audio.ApolloPlaybackMixer;
|
---|
26 | import org.apollo.audio.ApolloSubjectChangedEvent;
|
---|
27 | import org.apollo.audio.SampledAudioManager;
|
---|
28 | import org.apollo.mvc.Observer;
|
---|
29 | import org.apollo.mvc.Subject;
|
---|
30 | import org.apollo.mvc.SubjectChangedEvent;
|
---|
31 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
32 | import org.expeditee.items.Text;
|
---|
33 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
34 |
|
---|
35 | public class AudioConfiguration extends InteractiveWidget implements Observer {
|
---|
36 |
|
---|
37 | private static MixerItem[] inputMixerItems = null;
|
---|
38 |
|
---|
39 | private JComboBox inputMixureCombo;
|
---|
40 | private JSlider masterVolumeSlider;
|
---|
41 | private JCheckBox masterMuteCheckBox;
|
---|
42 | private JLabel inputMixerDescription;
|
---|
43 |
|
---|
44 | public AudioConfiguration(Text source, String[] args) {
|
---|
45 | super(source, new JPanel(), 260, -1, 150, 150);
|
---|
46 |
|
---|
47 | Font titleFont = new Font("Arial", Font.BOLD, 12);
|
---|
48 | Font descFont = new Font("Arial", Font.PLAIN, 12);
|
---|
49 |
|
---|
50 | inputMixureCombo = new JComboBox(getInputMixerItems());
|
---|
51 |
|
---|
52 | inputMixureCombo.addActionListener( new ActionListener() {
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | if (inputMixerItems.length > 0) {
|
---|
55 | SampledAudioManager.getInstance().setCurrentInputMixure(
|
---|
56 | ((MixerItem)inputMixureCombo.getSelectedItem())._inf);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | masterVolumeSlider = new JSlider();
|
---|
62 | masterVolumeSlider.setMinimum(0);
|
---|
63 | masterVolumeSlider.setMaximum(100);
|
---|
64 | masterVolumeSlider.setMaximumSize(new Dimension(300, 20));
|
---|
65 |
|
---|
66 | masterVolumeSlider.addChangeListener(new ChangeListener() {
|
---|
67 | public void stateChanged(ChangeEvent e) {
|
---|
68 | float f = (float)masterVolumeSlider.getValue();
|
---|
69 | f /= 100.0f;
|
---|
70 | ApolloPlaybackMixer.getInstance().setMasterVolume(f);
|
---|
71 | }
|
---|
72 | });
|
---|
73 |
|
---|
74 | masterMuteCheckBox = new JCheckBox();
|
---|
75 | masterMuteCheckBox.setText("Master Mute");
|
---|
76 | masterMuteCheckBox.addChangeListener(new ChangeListener() {
|
---|
77 | public void stateChanged(ChangeEvent e) {
|
---|
78 | ApolloPlaybackMixer.getInstance().setMasterMute(masterMuteCheckBox.isSelected());
|
---|
79 | }
|
---|
80 | });
|
---|
81 |
|
---|
82 |
|
---|
83 | inputMixerDescription = new JLabel();
|
---|
84 | inputMixerDescription.setFont(descFont);
|
---|
85 |
|
---|
86 | JLabel masterVolumeLevelLabel = new JLabel("Master volume:");
|
---|
87 | masterVolumeLevelLabel.setFont(titleFont);
|
---|
88 |
|
---|
89 | JLabel deviceLabel = new JLabel("Input device:");
|
---|
90 | deviceLabel.setFont(titleFont);
|
---|
91 | // Assemble
|
---|
92 | JPanel mixerPane = new JPanel(new GridBagLayout());
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 | GridBagConstraints c = new GridBagConstraints();
|
---|
97 | c.gridx = 0;
|
---|
98 | c.gridy = 0;
|
---|
99 | c.fill = GridBagConstraints.BOTH;
|
---|
100 | c.insets = new Insets(0,10,0,0);
|
---|
101 | mixerPane.add(deviceLabel, c);
|
---|
102 |
|
---|
103 | c = new GridBagConstraints();
|
---|
104 | c.gridx = 1;
|
---|
105 | c.gridy = 0;
|
---|
106 | c.weightx = 1.0f;
|
---|
107 | c.fill = GridBagConstraints.BOTH;
|
---|
108 | c.insets = new Insets(0,10,0,10);
|
---|
109 | mixerPane.add(inputMixureCombo, c);
|
---|
110 |
|
---|
111 | mixerPane.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
112 | masterVolumeLevelLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
113 | inputMixerDescription.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
114 | masterVolumeSlider.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
115 | masterMuteCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT);
|
---|
116 |
|
---|
117 | _swingComponent.add(Box.createRigidArea(new Dimension(1, 12)));
|
---|
118 | _swingComponent.setLayout(new BoxLayout(_swingComponent, BoxLayout.Y_AXIS));
|
---|
119 | _swingComponent.add(mixerPane);
|
---|
120 | _swingComponent.add(Box.createRigidArea(new Dimension(1, 4)));
|
---|
121 | _swingComponent.add(inputMixerDescription);
|
---|
122 | _swingComponent.add(Box.createRigidArea(new Dimension(1, 12)));
|
---|
123 | _swingComponent.add(masterVolumeLevelLabel);
|
---|
124 | _swingComponent.add(masterVolumeSlider);
|
---|
125 | _swingComponent.add(Box.createRigidArea(new Dimension(1, 12)));
|
---|
126 | _swingComponent.add(masterMuteCheckBox);
|
---|
127 | _swingComponent.add(Box.createRigidArea(new Dimension(1, 12)));
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 | // Set state to match SampledAudioManager
|
---|
132 | updateSelectedMixers();
|
---|
133 | updateMasterVolumeControl();
|
---|
134 | updateMasterMuteControl();
|
---|
135 |
|
---|
136 | _swingComponent.doLayout();
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | protected void onParentStateChanged(int eventType) {
|
---|
142 | super.onParentStateChanged(eventType);
|
---|
143 |
|
---|
144 | switch (eventType) {
|
---|
145 |
|
---|
146 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
|
---|
147 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
|
---|
148 | case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
|
---|
149 | ApolloPlaybackMixer.getInstance().removeObserver(this);
|
---|
150 | SampledAudioManager.getInstance().removeObserver(this);
|
---|
151 | break;
|
---|
152 |
|
---|
153 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
|
---|
154 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
|
---|
155 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
|
---|
156 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
|
---|
157 | ApolloPlaybackMixer.getInstance().addObserver(this);
|
---|
158 | SampledAudioManager.getInstance().addObserver(this);
|
---|
159 | break;
|
---|
160 |
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Syncs the I/O mixer combos to the current I/O mixers set in the SampledAudioManager
|
---|
167 | *
|
---|
168 | */
|
---|
169 | private void updateSelectedMixers() {
|
---|
170 |
|
---|
171 | // Update selected input mixer
|
---|
172 | Mixer.Info inf = SampledAudioManager.getInstance().getCurrentInputMixureInfo();
|
---|
173 |
|
---|
174 | for (MixerItem mi : inputMixerItems) {
|
---|
175 | if (mi._inf.equals(inf)) {
|
---|
176 | if (inputMixureCombo.getSelectedItem() != mi)
|
---|
177 | inputMixureCombo.setSelectedItem(mi);
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*StringBuilder desc = new StringBuilder(inf.getDescription().length());
|
---|
182 | int i = 0;
|
---|
183 | final int CUTTOFF_LENGTH = 10;
|
---|
184 | while (i < inf.getDescription().length()) {
|
---|
185 | String part = null;
|
---|
186 |
|
---|
187 | if ((i + CUTTOFF_LENGTH) > inf.getDescription().length()) {
|
---|
188 | part = inf.getDescription().substring(i);
|
---|
189 | } else {
|
---|
190 | part = inf.getDescription().substring(i, i + CUTTOFF_LENGTH);
|
---|
191 | }
|
---|
192 | desc.append(part);
|
---|
193 | desc.append('\n');
|
---|
194 | i += CUTTOFF_LENGTH;
|
---|
195 | }*/
|
---|
196 |
|
---|
197 | inputMixerDescription.setText(inf.getDescription());
|
---|
198 |
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Synchs volumeSlider with master volume state.
|
---|
203 | */
|
---|
204 | private void updateMasterVolumeControl() {
|
---|
205 |
|
---|
206 | int mv = (int)(ApolloPlaybackMixer.getInstance().getMasterVolume() * 100.0f);
|
---|
207 |
|
---|
208 | if (masterVolumeSlider.getValue() != mv) {
|
---|
209 | masterVolumeSlider.setValue(mv);
|
---|
210 | }
|
---|
211 |
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Synchs mute checkbox with master mute state.
|
---|
216 | */
|
---|
217 | private void updateMasterMuteControl() {
|
---|
218 |
|
---|
219 | if (this.masterMuteCheckBox.isSelected() != ApolloPlaybackMixer.getInstance().isMasterMuteOn()) {
|
---|
220 | masterMuteCheckBox.setSelected(!masterMuteCheckBox.isSelected());
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (masterMuteCheckBox.isSelected()) {
|
---|
224 | masterMuteCheckBox.setText("Master Mute (on)");
|
---|
225 | } else {
|
---|
226 | masterMuteCheckBox.setText("Master Mute (off)");
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | protected String[] getArgs() {
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Obtain currently available mixers to choose from
|
---|
239 | */
|
---|
240 | private MixerItem[] getInputMixerItems() {
|
---|
241 |
|
---|
242 | List<Mixer.Info> mixers;
|
---|
243 |
|
---|
244 | if (inputMixerItems == null) {
|
---|
245 |
|
---|
246 | mixers = SampledAudioManager.getInstance().getSupportedInputMixures();
|
---|
247 | inputMixerItems = new MixerItem[mixers.size()];
|
---|
248 | for (int i = 0; i < inputMixerItems.length; i++) {
|
---|
249 | inputMixerItems[i] = new MixerItem(mixers.get(i));
|
---|
250 | }
|
---|
251 |
|
---|
252 | }
|
---|
253 | return inputMixerItems;
|
---|
254 | }
|
---|
255 |
|
---|
256 | public Subject getObservedSubject() {
|
---|
257 | return SampledAudioManager.getInstance();
|
---|
258 | }
|
---|
259 |
|
---|
260 | public void modelChanged(Subject source, SubjectChangedEvent event) {
|
---|
261 |
|
---|
262 | switch (event.getID()) {
|
---|
263 | case ApolloSubjectChangedEvent.VOLUME:
|
---|
264 | updateMasterVolumeControl();
|
---|
265 | break;
|
---|
266 | case ApolloSubjectChangedEvent.MUTE:
|
---|
267 | updateMasterMuteControl();
|
---|
268 | break;
|
---|
269 | case ApolloSubjectChangedEvent.INPUT_MIXER:
|
---|
270 | updateSelectedMixers();
|
---|
271 | break;
|
---|
272 | }
|
---|
273 |
|
---|
274 | }
|
---|
275 |
|
---|
276 | public void setObservedSubject(Subject parent) {
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Used by combobox for selecting a mixer
|
---|
282 | * @author Brook Novak
|
---|
283 | */
|
---|
284 | class MixerItem {
|
---|
285 |
|
---|
286 | Mixer.Info _inf;
|
---|
287 | int targetlineCount;
|
---|
288 |
|
---|
289 | MixerItem(Mixer.Info inf) {
|
---|
290 | if (inf == null) throw new NullPointerException("inf");
|
---|
291 | _inf = inf;
|
---|
292 | targetlineCount = AudioSystem.getMixer(_inf).getTargetLineInfo().length;
|
---|
293 | }
|
---|
294 |
|
---|
295 | @Override
|
---|
296 | public String toString() {
|
---|
297 | return _inf.getName();
|
---|
298 | // return _inf.getVendor() + " " +_inf.getName() + " " +_inf.getVersion();
|
---|
299 | //return _inf.getDescription() + " [" + targetlineCount + " target lines]";
|
---|
300 | }
|
---|
301 |
|
---|
302 | }
|
---|
303 | }
|
---|