1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.fantasia; |
24 |
|
25 |
import java.awt.Dimension; |
26 |
import java.awt.Insets; |
27 |
|
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.awt.event.ItemEvent; |
31 |
import java.awt.event.ItemListener; |
32 |
|
33 |
import java.beans.PropertyChangeEvent; |
34 |
import java.beans.PropertyChangeListener; |
35 |
|
36 |
import javax.swing.BorderFactory; |
37 |
import javax.swing.Box; |
38 |
import javax.swing.BoxLayout; |
39 |
import javax.swing.JCheckBox; |
40 |
import javax.swing.JComboBox; |
41 |
import javax.swing.JLabel; |
42 |
import javax.swing.JPanel; |
43 |
import javax.swing.JScrollPane; |
44 |
import javax.swing.JSpinner; |
45 |
import javax.swing.SpinnerNumberModel; |
46 |
|
47 |
import javax.swing.event.ChangeEvent; |
48 |
import javax.swing.event.ChangeListener; |
49 |
|
50 |
import net.sf.juife.Task; |
51 |
import net.sf.juife.event.TaskEvent; |
52 |
import net.sf.juife.event.TaskListener; |
53 |
|
54 |
import org.jsampler.AudioDeviceModel; |
55 |
import org.jsampler.CC; |
56 |
|
57 |
import org.jsampler.event.AudioDeviceEvent; |
58 |
import org.jsampler.event.AudioDeviceListener; |
59 |
import org.jsampler.event.ParameterEvent; |
60 |
import org.jsampler.event.ParameterListener; |
61 |
|
62 |
import org.jsampler.task.Audio; |
63 |
import org.jsampler.view.ParameterTable; |
64 |
import org.jsampler.view.fantasia.basic.PixmapPane; |
65 |
|
66 |
import org.linuxsampler.lscp.AudioOutputChannel; |
67 |
import org.linuxsampler.lscp.AudioOutputDevice; |
68 |
import org.linuxsampler.lscp.Parameter; |
69 |
|
70 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
71 |
import static org.jsampler.view.fantasia.FantasiaPrefs.preferences; |
72 |
|
73 |
/** |
74 |
* |
75 |
* @author Grigor Iliev |
76 |
*/ |
77 |
public class AudioDevicePane extends DevicePane { |
78 |
private final OptionsPane optionsPane; |
79 |
private final ParameterTable channelParamTable = new ParameterTable(); |
80 |
|
81 |
private final AudioDeviceModel audioDeviceModel; |
82 |
|
83 |
|
84 |
/** Creates a new instance of <code>AudioDevicePane</code> */ |
85 |
public |
86 |
AudioDevicePane(AudioDeviceModel model) { |
87 |
audioDeviceModel = model; |
88 |
|
89 |
channelParamTable.setFillsViewportHeight(true); |
90 |
|
91 |
optionsPane = new OptionsPane(); |
92 |
setOptionsPane(optionsPane); |
93 |
|
94 |
int id = model.getDeviceId(); |
95 |
String s = model.getDeviceInfo().getDriverName(); |
96 |
setDeviceName(i18n.getLabel("AudioDevicePane.lDevName", id, s)); |
97 |
} |
98 |
|
99 |
@Override |
100 |
protected void |
101 |
destroyDevice() { |
102 |
final Task t = new Audio.DestroyDevice(getDeviceId()); |
103 |
t.addTaskListener(new TaskListener() { |
104 |
public void |
105 |
taskPerformed(TaskEvent e) { |
106 |
if(t.doneWithErrors()) restoreDevice(); |
107 |
} |
108 |
}); |
109 |
|
110 |
CC.getTaskQueue().add(t); |
111 |
} |
112 |
|
113 |
public int |
114 |
getDeviceId() { return audioDeviceModel.getDeviceId(); } |
115 |
|
116 |
class OptionsPane extends PixmapPane implements ActionListener, ItemListener, |
117 |
ChangeListener, AudioDeviceListener, ParameterListener { |
118 |
|
119 |
private final JCheckBox checkActive = |
120 |
new JCheckBox(i18n.getLabel("AudioDevicePane.checkActive")); |
121 |
|
122 |
private final JLabel lChannels |
123 |
= new JLabel(i18n.getLabel("AudioDevicePane.lChannels")); |
124 |
|
125 |
private final JSpinner spinnerChannels; |
126 |
|
127 |
private final JLabel lChannel = |
128 |
new JLabel(i18n.getLabel("AudioDevicePane.lChannel")); |
129 |
|
130 |
private final JComboBox cbChannel = new JComboBox(); |
131 |
|
132 |
private final ParameterTable additionalParamsTable = new ParameterTable(); |
133 |
private final JPanel additionalParamsPane = new JPanel(); |
134 |
|
135 |
OptionsPane() { |
136 |
super(Res.gfxChannelOptions); |
137 |
|
138 |
setAlignmentX(LEFT_ALIGNMENT); |
139 |
|
140 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
141 |
setLayout(new java.awt.BorderLayout()); |
142 |
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
143 |
setOpaque(false); |
144 |
|
145 |
PixmapPane mainPane = new PixmapPane(Res.gfxRoundBg7); |
146 |
mainPane.setPixmapInsets(new Insets(3, 3, 3, 3)); |
147 |
mainPane.setOpaque(false); |
148 |
|
149 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
150 |
mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
151 |
mainPane.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
152 |
|
153 |
JPanel p = new JPanel(); |
154 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
155 |
|
156 |
p.add(checkActive); |
157 |
p.add(Box.createRigidArea(new Dimension(12, 0))); |
158 |
p.add(lChannels); |
159 |
p.add(Box.createRigidArea(new Dimension(5, 0))); |
160 |
|
161 |
Parameter<Integer> prm = |
162 |
audioDeviceModel.getDeviceInfo().getChannelsParameter(); |
163 |
int min = 1; |
164 |
if(prm.getRangeMin() != null) min = prm.getRangeMin().intValue(); |
165 |
int max = 10000; |
166 |
if(prm.getRangeMax() != null) max = prm.getRangeMax().intValue(); |
167 |
|
168 |
spinnerChannels = new JSpinner(new SpinnerNumberModel(1, min, max, 1)); |
169 |
if(prm.isFixed()) spinnerChannels.setEnabled(false); |
170 |
int h = spinnerChannels.getPreferredSize().height; |
171 |
spinnerChannels.setPreferredSize(new Dimension(30, h)); |
172 |
p.add(spinnerChannels); |
173 |
p.setOpaque(false); |
174 |
|
175 |
mainPane.add(p); |
176 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
177 |
|
178 |
JPanel p2 = additionalParamsPane; |
179 |
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS)); |
180 |
p2.setOpaque(false); |
181 |
|
182 |
p2.add(createHSeparator()); |
183 |
p2.add(Box.createRigidArea(new Dimension(0, 5))); |
184 |
|
185 |
JScrollPane sp = new JScrollPane(additionalParamsTable); |
186 |
|
187 |
sp.setPreferredSize(new Dimension(77, 90)); |
188 |
p2.add(sp); |
189 |
mainPane.add(p2); |
190 |
|
191 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
192 |
|
193 |
mainPane.add(createHSeparator()); |
194 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
195 |
|
196 |
p = new JPanel(); |
197 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
198 |
p.setOpaque(false); |
199 |
|
200 |
p2 = new JPanel(); |
201 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
202 |
p2.add(lChannel); |
203 |
p2.add(Box.createRigidArea(new Dimension(5, 0))); |
204 |
p2.add(cbChannel); |
205 |
p2.setOpaque(false); |
206 |
p.add(p2); |
207 |
|
208 |
p.add(Box.createRigidArea(new Dimension(0, 5))); |
209 |
|
210 |
sp = new JScrollPane(channelParamTable); |
211 |
sp.setPreferredSize(new Dimension(77, 90)); |
212 |
p.add(sp); |
213 |
|
214 |
mainPane.add(p); |
215 |
add(mainPane); |
216 |
|
217 |
checkActive.setSelected(audioDeviceModel.isActive()); |
218 |
spinnerChannels.setValue(audioDeviceModel.getDeviceInfo().getChannelCount()); |
219 |
|
220 |
cbChannel.addActionListener(this); |
221 |
checkActive.addItemListener(this); |
222 |
spinnerChannels.addChangeListener(this); |
223 |
audioDeviceModel.addAudioDeviceListener(this); |
224 |
channelParamTable.getModel().addParameterListener(this); |
225 |
|
226 |
AudioDeviceModel m = audioDeviceModel; |
227 |
for(AudioOutputChannel chn : m.getDeviceInfo().getAudioChannels()) { |
228 |
cbChannel.addItem(chn); |
229 |
} |
230 |
|
231 |
updateParams(audioDeviceModel.getDeviceInfo()); |
232 |
additionalParamsTable.getModel().addParameterListener(new ParameterListener() { |
233 |
public void |
234 |
parameterChanged(ParameterEvent e) { |
235 |
audioDeviceModel.setBackendDeviceParameter(e.getParameter()); |
236 |
} |
237 |
}); |
238 |
|
239 |
updateAdditionalParamsViewState(); |
240 |
String s = "AudioDevice.showAdditionalParameters"; |
241 |
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
242 |
public void |
243 |
propertyChange(PropertyChangeEvent e) { |
244 |
updateAdditionalParamsViewState(); |
245 |
} |
246 |
}); |
247 |
} |
248 |
|
249 |
private void |
250 |
updateAdditionalParamsViewState() { |
251 |
String s = "AudioDevice.showAdditionalParameters"; |
252 |
additionalParamsPane.setVisible(preferences().getBoolProperty(s)); |
253 |
validate(); |
254 |
} |
255 |
|
256 |
@Override |
257 |
public void |
258 |
actionPerformed(ActionEvent e) { |
259 |
Object obj = cbChannel.getSelectedItem(); |
260 |
if(obj == null) { |
261 |
channelParamTable.getModel().setParameters(new Parameter[0]); |
262 |
return; |
263 |
} |
264 |
|
265 |
AudioOutputChannel chn = (AudioOutputChannel)obj; |
266 |
|
267 |
channelParamTable.getModel().setParameters(chn.getAllParameters()); |
268 |
} |
269 |
|
270 |
@Override |
271 |
public void |
272 |
itemStateChanged(ItemEvent e) { |
273 |
boolean a = checkActive.isSelected(); |
274 |
if(a != audioDeviceModel.isActive()) audioDeviceModel.setBackendActive(a); |
275 |
} |
276 |
|
277 |
@Override |
278 |
public void |
279 |
stateChanged(ChangeEvent e) { |
280 |
int c = (Integer)spinnerChannels.getValue(); |
281 |
if(c != audioDeviceModel.getDeviceInfo().getAudioChannelCount()) { |
282 |
audioDeviceModel.setBackendChannelCount(c); |
283 |
} |
284 |
} |
285 |
|
286 |
@Override |
287 |
public void |
288 |
settingsChanged(AudioDeviceEvent e) { |
289 |
int c = (Integer)spinnerChannels.getValue(); |
290 |
int nc = audioDeviceModel.getDeviceInfo().getAudioChannelCount(); |
291 |
if(c != nc) spinnerChannels.setValue(nc); |
292 |
|
293 |
boolean a = checkActive.isSelected(); |
294 |
boolean na = audioDeviceModel.isActive(); |
295 |
if(a != na) checkActive.setSelected(na); |
296 |
|
297 |
AudioOutputDevice d = e.getAudioDeviceModel().getDeviceInfo(); |
298 |
updateParams(d); |
299 |
|
300 |
int idx = cbChannel.getSelectedIndex(); |
301 |
cbChannel.removeAllItems(); |
302 |
for(AudioOutputChannel chn : d.getAudioChannels()) cbChannel.addItem(chn); |
303 |
|
304 |
if(idx >= cbChannel.getModel().getSize()) idx = 0; |
305 |
|
306 |
if(cbChannel.getModel().getSize() > 0) cbChannel.setSelectedIndex(idx); |
307 |
} |
308 |
|
309 |
@Override |
310 |
public void |
311 |
parameterChanged(ParameterEvent e) { |
312 |
int c = cbChannel.getSelectedIndex(); |
313 |
if(c == -1) { |
314 |
CC.getLogger().warning("There is no audio channel selected!"); |
315 |
return; |
316 |
} |
317 |
|
318 |
audioDeviceModel.setBackendChannelParameter(c, e.getParameter()); |
319 |
} |
320 |
|
321 |
private void |
322 |
updateParams(AudioOutputDevice d) { |
323 |
Parameter p = d.getSampleRateParameter(); |
324 |
boolean b = p == null || p.getName() == null || p.getValue() == null; |
325 |
Parameter[] params = d.getAdditionalParameters(); |
326 |
Parameter[] p2s; |
327 |
if(b) p2s = new Parameter[params.length]; |
328 |
else p2s = new Parameter[params.length + 1]; |
329 |
|
330 |
for(int i = 0; i < params.length; i++) p2s[i] = params[i]; |
331 |
|
332 |
if(!b) p2s[params.length] = p; |
333 |
|
334 |
additionalParamsTable.getModel().setParameters(p2s); |
335 |
} |
336 |
} |
337 |
|
338 |
} |