1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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 |
|
65 |
import org.linuxsampler.lscp.AudioOutputChannel; |
66 |
import org.linuxsampler.lscp.AudioOutputDevice; |
67 |
import org.linuxsampler.lscp.Parameter; |
68 |
import org.linuxsampler.lscp.ParameterFactory; |
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 |
optionsPane = new OptionsPane(); |
90 |
setOptionsPane(optionsPane); |
91 |
|
92 |
int id = model.getDeviceId(); |
93 |
String s = model.getDeviceInfo().getDriverName(); |
94 |
setDeviceName(i18n.getLabel("AudioDevicePane.lDevName", id, s)); |
95 |
} |
96 |
|
97 |
protected void |
98 |
destroyDevice() { |
99 |
final Task t = new Audio.DestroyDevice(getDeviceId()); |
100 |
t.addTaskListener(new TaskListener() { |
101 |
public void |
102 |
taskPerformed(TaskEvent e) { |
103 |
if(t.doneWithErrors()) restoreDevice(); |
104 |
} |
105 |
}); |
106 |
|
107 |
CC.getTaskQueue().add(t); |
108 |
} |
109 |
|
110 |
public int |
111 |
getDeviceId() { return audioDeviceModel.getDeviceId(); } |
112 |
|
113 |
class OptionsPane extends PixmapPane implements ActionListener, ItemListener, |
114 |
ChangeListener, AudioDeviceListener, ParameterListener { |
115 |
|
116 |
private final JCheckBox checkActive = |
117 |
new JCheckBox(i18n.getLabel("AudioDevicePane.checkActive")); |
118 |
|
119 |
private final JLabel lChannels |
120 |
= new JLabel(i18n.getLabel("AudioDevicePane.lChannels")); |
121 |
|
122 |
private final JSpinner spinnerChannels; |
123 |
|
124 |
private final JLabel lChannel = |
125 |
new JLabel(i18n.getLabel("AudioDevicePane.lChannel")); |
126 |
|
127 |
private final JComboBox cbChannel = new JComboBox(); |
128 |
|
129 |
private final ParameterTable additionalParamsTable = new ParameterTable(); |
130 |
private final JPanel additionalParamsPane = new JPanel(); |
131 |
|
132 |
OptionsPane() { |
133 |
super(Res.gfxChannelOptions); |
134 |
|
135 |
setAlignmentX(LEFT_ALIGNMENT); |
136 |
|
137 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
138 |
setLayout(new java.awt.BorderLayout()); |
139 |
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
140 |
setOpaque(false); |
141 |
|
142 |
PixmapPane mainPane = new PixmapPane(Res.gfxRoundBg7); |
143 |
mainPane.setPixmapInsets(new Insets(3, 3, 3, 3)); |
144 |
mainPane.setOpaque(false); |
145 |
|
146 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
147 |
mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
148 |
mainPane.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
149 |
|
150 |
JPanel p = new JPanel(); |
151 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
152 |
|
153 |
p.add(checkActive); |
154 |
p.add(Box.createRigidArea(new Dimension(12, 0))); |
155 |
p.add(lChannels); |
156 |
p.add(Box.createRigidArea(new Dimension(5, 0))); |
157 |
|
158 |
Parameter<Integer> prm = |
159 |
audioDeviceModel.getDeviceInfo().getChannelsParameter(); |
160 |
int min = 1; |
161 |
if(prm.getRangeMin() != null) min = prm.getRangeMin().intValue(); |
162 |
int max = 10000; |
163 |
if(prm.getRangeMax() != null) max = prm.getRangeMax().intValue(); |
164 |
|
165 |
spinnerChannels = new JSpinner(new SpinnerNumberModel(1, min, max, 1)); |
166 |
if(prm.isFixed()) spinnerChannels.setEnabled(false); |
167 |
p.add(spinnerChannels); |
168 |
p.setOpaque(false); |
169 |
|
170 |
mainPane.add(p); |
171 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
172 |
|
173 |
JPanel p2 = additionalParamsPane; |
174 |
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS)); |
175 |
p2.setOpaque(false); |
176 |
|
177 |
p2.add(createHSeparator()); |
178 |
p2.add(Box.createRigidArea(new Dimension(0, 5))); |
179 |
|
180 |
JScrollPane sp = new JScrollPane(additionalParamsTable); |
181 |
|
182 |
sp.setPreferredSize(new Dimension(77, 90)); |
183 |
p2.add(sp); |
184 |
mainPane.add(p2); |
185 |
|
186 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
187 |
|
188 |
mainPane.add(createHSeparator()); |
189 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
190 |
|
191 |
p = new JPanel(); |
192 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
193 |
p.setOpaque(false); |
194 |
|
195 |
p2 = new JPanel(); |
196 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
197 |
p2.add(lChannel); |
198 |
p2.add(Box.createRigidArea(new Dimension(5, 0))); |
199 |
p2.add(cbChannel); |
200 |
p2.setOpaque(false); |
201 |
p.add(p2); |
202 |
|
203 |
p.add(Box.createRigidArea(new Dimension(0, 5))); |
204 |
|
205 |
sp = new JScrollPane(channelParamTable); |
206 |
sp.setPreferredSize(new Dimension(77, 90)); |
207 |
p.add(sp); |
208 |
|
209 |
mainPane.add(p); |
210 |
add(mainPane); |
211 |
|
212 |
checkActive.setSelected(audioDeviceModel.isActive()); |
213 |
spinnerChannels.setValue(audioDeviceModel.getDeviceInfo().getChannelCount()); |
214 |
|
215 |
cbChannel.addActionListener(this); |
216 |
checkActive.addItemListener(this); |
217 |
spinnerChannels.addChangeListener(this); |
218 |
audioDeviceModel.addAudioDeviceListener(this); |
219 |
channelParamTable.getModel().addParameterListener(this); |
220 |
|
221 |
AudioDeviceModel m = audioDeviceModel; |
222 |
for(AudioOutputChannel chn : m.getDeviceInfo().getAudioChannels()) { |
223 |
cbChannel.addItem(chn); |
224 |
} |
225 |
|
226 |
updateParams(audioDeviceModel.getDeviceInfo()); |
227 |
additionalParamsTable.getModel().addParameterListener(new ParameterListener() { |
228 |
public void |
229 |
parameterChanged(ParameterEvent e) { |
230 |
audioDeviceModel.setBackendDeviceParameter(e.getParameter()); |
231 |
} |
232 |
}); |
233 |
|
234 |
updateAdditionalParamsViewState(); |
235 |
String s = "AudioDevice.showAdditionalParameters"; |
236 |
preferences().addPropertyChangeListener(s, new PropertyChangeListener() { |
237 |
public void |
238 |
propertyChange(PropertyChangeEvent e) { |
239 |
updateAdditionalParamsViewState(); |
240 |
} |
241 |
}); |
242 |
} |
243 |
|
244 |
private void |
245 |
updateAdditionalParamsViewState() { |
246 |
String s = "AudioDevice.showAdditionalParameters"; |
247 |
additionalParamsPane.setVisible(preferences().getBoolProperty(s)); |
248 |
validate(); |
249 |
} |
250 |
|
251 |
public void |
252 |
actionPerformed(ActionEvent e) { |
253 |
Object obj = cbChannel.getSelectedItem(); |
254 |
if(obj == null) { |
255 |
channelParamTable.getModel().setParameters(new Parameter[0]); |
256 |
return; |
257 |
} |
258 |
|
259 |
AudioOutputChannel chn = (AudioOutputChannel)obj; |
260 |
|
261 |
channelParamTable.getModel().setParameters(chn.getAllParameters()); |
262 |
} |
263 |
|
264 |
public void |
265 |
itemStateChanged(ItemEvent e) { |
266 |
boolean a = checkActive.isSelected(); |
267 |
if(a != audioDeviceModel.isActive()) audioDeviceModel.setBackendActive(a); |
268 |
} |
269 |
|
270 |
public void |
271 |
stateChanged(ChangeEvent e) { |
272 |
int c = (Integer)spinnerChannels.getValue(); |
273 |
if(c != audioDeviceModel.getDeviceInfo().getAudioChannelCount()) { |
274 |
audioDeviceModel.setBackendChannelCount(c); |
275 |
} |
276 |
} |
277 |
|
278 |
public void |
279 |
settingsChanged(AudioDeviceEvent e) { |
280 |
int c = (Integer)spinnerChannels.getValue(); |
281 |
int nc = audioDeviceModel.getDeviceInfo().getAudioChannelCount(); |
282 |
if(c != nc) spinnerChannels.setValue(nc); |
283 |
|
284 |
boolean a = checkActive.isSelected(); |
285 |
boolean na = audioDeviceModel.isActive(); |
286 |
if(a != na) checkActive.setSelected(na); |
287 |
|
288 |
AudioOutputDevice d = e.getAudioDeviceModel().getDeviceInfo(); |
289 |
updateParams(d); |
290 |
|
291 |
int idx = cbChannel.getSelectedIndex(); |
292 |
cbChannel.removeAllItems(); |
293 |
for(AudioOutputChannel chn : d.getAudioChannels()) cbChannel.addItem(chn); |
294 |
|
295 |
if(idx >= cbChannel.getModel().getSize()) idx = 0; |
296 |
|
297 |
if(cbChannel.getModel().getSize() > 0) cbChannel.setSelectedIndex(idx); |
298 |
} |
299 |
|
300 |
public void |
301 |
parameterChanged(ParameterEvent e) { |
302 |
int c = cbChannel.getSelectedIndex(); |
303 |
if(c == -1) { |
304 |
CC.getLogger().warning("There is no audio channel selected!"); |
305 |
return; |
306 |
} |
307 |
|
308 |
audioDeviceModel.setBackendChannelParameter(c, e.getParameter()); |
309 |
} |
310 |
|
311 |
private void |
312 |
updateParams(AudioOutputDevice d) { |
313 |
Parameter p = d.getSampleRateParameter(); |
314 |
boolean b = p == null || p.getName() == null || p.getValue() == null; |
315 |
Parameter[] params = d.getAdditionalParameters(); |
316 |
Parameter[] p2s; |
317 |
if(b) p2s = new Parameter[params.length]; |
318 |
else p2s = new Parameter[params.length + 1]; |
319 |
|
320 |
for(int i = 0; i < params.length; i++) p2s[i] = params[i]; |
321 |
|
322 |
if(!b) p2s[params.length] = p; |
323 |
|
324 |
additionalParamsTable.getModel().setParameters(p2s); |
325 |
} |
326 |
} |
327 |
|
328 |
} |