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