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 javax.swing.BorderFactory; |
34 |
import javax.swing.Box; |
35 |
import javax.swing.BoxLayout; |
36 |
import javax.swing.JCheckBox; |
37 |
import javax.swing.JComboBox; |
38 |
import javax.swing.JLabel; |
39 |
import javax.swing.JPanel; |
40 |
import javax.swing.JScrollPane; |
41 |
import javax.swing.JSpinner; |
42 |
import javax.swing.SpinnerNumberModel; |
43 |
|
44 |
import javax.swing.event.ChangeEvent; |
45 |
import javax.swing.event.ChangeListener; |
46 |
|
47 |
import net.sf.juife.Task; |
48 |
import net.sf.juife.event.TaskEvent; |
49 |
import net.sf.juife.event.TaskListener; |
50 |
|
51 |
import org.jsampler.AudioDeviceModel; |
52 |
import org.jsampler.CC; |
53 |
|
54 |
import org.jsampler.event.AudioDeviceEvent; |
55 |
import org.jsampler.event.AudioDeviceListener; |
56 |
import org.jsampler.event.ParameterEvent; |
57 |
import org.jsampler.event.ParameterListener; |
58 |
|
59 |
import org.jsampler.task.Audio; |
60 |
import org.jsampler.view.ParameterTable; |
61 |
|
62 |
import org.linuxsampler.lscp.AudioOutputChannel; |
63 |
import org.linuxsampler.lscp.AudioOutputDevice; |
64 |
import org.linuxsampler.lscp.Parameter; |
65 |
|
66 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
67 |
|
68 |
/** |
69 |
* |
70 |
* @author Grigor Iliev |
71 |
*/ |
72 |
public class AudioDevicePane extends DevicePane { |
73 |
private final OptionsPane optionsPane; |
74 |
private final ParameterTable channelParamTable = new ParameterTable(); |
75 |
|
76 |
private final AudioDeviceModel audioDeviceModel; |
77 |
|
78 |
|
79 |
/** Creates a new instance of <code>AudioDevicePane</code> */ |
80 |
public |
81 |
AudioDevicePane(AudioDeviceModel model) { |
82 |
audioDeviceModel = model; |
83 |
|
84 |
optionsPane = new OptionsPane(); |
85 |
setOptionsPane(optionsPane); |
86 |
|
87 |
int id = model.getDeviceId(); |
88 |
setDeviceName(i18n.getLabel("AudioDevicePane.lDevName", id)); |
89 |
} |
90 |
|
91 |
protected void |
92 |
destroyDevice() { |
93 |
final Task t = new Audio.DestroyDevice(getDeviceId()); |
94 |
t.addTaskListener(new TaskListener() { |
95 |
public void |
96 |
taskPerformed(TaskEvent e) { |
97 |
if(t.doneWithErrors()) restoreDevice(); |
98 |
} |
99 |
}); |
100 |
|
101 |
CC.getTaskQueue().add(t); |
102 |
} |
103 |
|
104 |
public int |
105 |
getDeviceId() { return audioDeviceModel.getDeviceId(); } |
106 |
|
107 |
class OptionsPane extends PixmapPane implements ActionListener, ItemListener, |
108 |
ChangeListener, AudioDeviceListener, ParameterListener { |
109 |
|
110 |
private final JCheckBox checkActive = |
111 |
new JCheckBox(i18n.getLabel("AudioDevicePane.checkActive")); |
112 |
|
113 |
private final JLabel lChannels |
114 |
= new JLabel(i18n.getLabel("AudioDevicePane.lChannels")); |
115 |
|
116 |
private final JSpinner spinnerChannels |
117 |
= new JSpinner(new SpinnerNumberModel(1, 1, 50, 1)); |
118 |
|
119 |
private final JLabel lChannel = |
120 |
new JLabel(i18n.getLabel("AudioDevicePane.lChannel")); |
121 |
|
122 |
private final JComboBox cbChannel = new JComboBox(); |
123 |
|
124 |
OptionsPane() { |
125 |
super(Res.gfxChannelOptions); |
126 |
|
127 |
setAlignmentX(LEFT_ALIGNMENT); |
128 |
|
129 |
setPixmapInsets(new Insets(1, 1, 1, 1)); |
130 |
setLayout(new java.awt.BorderLayout()); |
131 |
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
132 |
setOpaque(false); |
133 |
|
134 |
PixmapPane mainPane = new PixmapPane(Res.gfxRoundBg7); |
135 |
mainPane.setPixmapInsets(new Insets(3, 3, 3, 3)); |
136 |
mainPane.setOpaque(false); |
137 |
|
138 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
139 |
mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
140 |
mainPane.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); |
141 |
|
142 |
JPanel p = new JPanel(); |
143 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
144 |
|
145 |
p.add(checkActive); |
146 |
p.add(Box.createRigidArea(new Dimension(12, 0))); |
147 |
p.add(lChannels); |
148 |
p.add(Box.createRigidArea(new Dimension(5, 0))); |
149 |
p.add(spinnerChannels); |
150 |
p.setOpaque(false); |
151 |
|
152 |
mainPane.add(p); |
153 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
154 |
|
155 |
mainPane.add(createHSeparator()); |
156 |
mainPane.add(Box.createRigidArea(new Dimension(0, 5))); |
157 |
|
158 |
p = new JPanel(); |
159 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
160 |
p.setOpaque(false); |
161 |
|
162 |
JPanel p2 = new JPanel(); |
163 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
164 |
p2.add(lChannel); |
165 |
p2.add(Box.createRigidArea(new Dimension(5, 0))); |
166 |
p2.add(cbChannel); |
167 |
p2.setOpaque(false); |
168 |
p.add(p2); |
169 |
|
170 |
p.add(Box.createRigidArea(new Dimension(0, 5))); |
171 |
|
172 |
JScrollPane sp = new JScrollPane(channelParamTable); |
173 |
sp.setPreferredSize(new Dimension(77, 90)); |
174 |
p.add(sp); |
175 |
|
176 |
mainPane.add(p); |
177 |
add(mainPane); |
178 |
|
179 |
checkActive.setSelected(audioDeviceModel.isActive()); |
180 |
spinnerChannels.setValue(audioDeviceModel.getDeviceInfo().getChannelCount()); |
181 |
|
182 |
cbChannel.addActionListener(this); |
183 |
checkActive.addItemListener(this); |
184 |
spinnerChannels.addChangeListener(this); |
185 |
audioDeviceModel.addAudioDeviceListener(this); |
186 |
channelParamTable.getModel().addParameterListener(this); |
187 |
|
188 |
AudioDeviceModel m = audioDeviceModel; |
189 |
for(AudioOutputChannel chn : m.getDeviceInfo().getAudioChannels()) { |
190 |
cbChannel.addItem(chn); |
191 |
} |
192 |
} |
193 |
|
194 |
public void |
195 |
actionPerformed(ActionEvent e) { |
196 |
Object obj = cbChannel.getSelectedItem(); |
197 |
if(obj == null) { |
198 |
channelParamTable.getModel().setParameters(new Parameter[0]); |
199 |
return; |
200 |
} |
201 |
|
202 |
AudioOutputChannel chn = (AudioOutputChannel)obj; |
203 |
|
204 |
channelParamTable.getModel().setParameters(chn.getAllParameters()); |
205 |
} |
206 |
|
207 |
public void |
208 |
itemStateChanged(ItemEvent e) { |
209 |
boolean a = checkActive.isSelected(); |
210 |
if(a != audioDeviceModel.isActive()) audioDeviceModel.setBackendActive(a); |
211 |
} |
212 |
|
213 |
public void |
214 |
stateChanged(ChangeEvent e) { |
215 |
int c = (Integer)spinnerChannels.getValue(); |
216 |
if(c != audioDeviceModel.getDeviceInfo().getAudioChannelCount()) { |
217 |
audioDeviceModel.setBackendChannelCount(c); |
218 |
} |
219 |
} |
220 |
|
221 |
public void |
222 |
settingsChanged(AudioDeviceEvent e) { |
223 |
int c = (Integer)spinnerChannels.getValue(); |
224 |
int nc = audioDeviceModel.getDeviceInfo().getAudioChannelCount(); |
225 |
if(c != nc) spinnerChannels.setValue(nc); |
226 |
|
227 |
boolean a = checkActive.isSelected(); |
228 |
boolean na = audioDeviceModel.isActive(); |
229 |
if(a != na) checkActive.setSelected(na); |
230 |
|
231 |
AudioOutputDevice d = e.getAudioDeviceModel().getDeviceInfo(); |
232 |
|
233 |
int idx = cbChannel.getSelectedIndex(); |
234 |
cbChannel.removeAllItems(); |
235 |
for(AudioOutputChannel chn : d.getAudioChannels()) cbChannel.addItem(chn); |
236 |
|
237 |
if(idx >= cbChannel.getModel().getSize()) idx = 0; |
238 |
|
239 |
if(cbChannel.getModel().getSize() > 0) cbChannel.setSelectedIndex(idx); |
240 |
} |
241 |
|
242 |
public void |
243 |
parameterChanged(ParameterEvent e) { |
244 |
int c = cbChannel.getSelectedIndex(); |
245 |
if(c == -1) { |
246 |
CC.getLogger().warning("There is no audio channel selected!"); |
247 |
return; |
248 |
} |
249 |
|
250 |
audioDeviceModel.setBackendChannelParameter(c, e.getParameter()); |
251 |
} |
252 |
} |
253 |
|
254 |
} |