/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/AudioDevicePane.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/fantasia/AudioDevicePane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (hide annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 10456 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 2288 * Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1286 *
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 iliev 1357 import java.beans.PropertyChangeEvent;
34     import java.beans.PropertyChangeListener;
35    
36 iliev 1286 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 iliev 1310 import net.sf.juife.Task;
51     import net.sf.juife.event.TaskEvent;
52     import net.sf.juife.event.TaskListener;
53    
54 iliev 1286 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 iliev 1310 import org.jsampler.task.Audio;
63 iliev 1785 import org.jsampler.view.fantasia.basic.PixmapPane;
64 iliev 2288 import org.jsampler.view.swing.ParameterTable;
65 iliev 1286
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 iliev 1357 import static org.jsampler.view.fantasia.FantasiaPrefs.preferences;
72 iliev 1286
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 iliev 1743 channelParamTable.setFillsViewportHeight(true);
90    
91 iliev 1286 optionsPane = new OptionsPane();
92     setOptionsPane(optionsPane);
93    
94     int id = model.getDeviceId();
95 iliev 1445 String s = model.getDeviceInfo().getDriverName();
96     setDeviceName(i18n.getLabel("AudioDevicePane.lDevName", id, s));
97 iliev 1286 }
98    
99 iliev 1785 @Override
100 iliev 1286 protected void
101     destroyDevice() {
102 iliev 1310 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 iliev 1286 }
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 iliev 1496 private final JSpinner spinnerChannels;
126 iliev 1286
127     private final JLabel lChannel =
128     new JLabel(i18n.getLabel("AudioDevicePane.lChannel"));
129    
130     private final JComboBox cbChannel = new JComboBox();
131    
132 iliev 1357 private final ParameterTable additionalParamsTable = new ParameterTable();
133     private final JPanel additionalParamsPane = new JPanel();
134    
135 iliev 1286 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 iliev 1496
161     Parameter<Integer> prm =
162     audioDeviceModel.getDeviceInfo().getChannelsParameter();
163     int min = 1;
164     if(prm.getRangeMin() != null) min = prm.getRangeMin().intValue();
165 iliev 1606 int max = 10000;
166 iliev 1496 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 iliev 1743 int h = spinnerChannels.getPreferredSize().height;
171     spinnerChannels.setPreferredSize(new Dimension(30, h));
172 iliev 1286 p.add(spinnerChannels);
173     p.setOpaque(false);
174    
175     mainPane.add(p);
176     mainPane.add(Box.createRigidArea(new Dimension(0, 5)));
177    
178 iliev 1357 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 iliev 1286 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 iliev 1357 p2 = new JPanel();
201 iliev 1286 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 iliev 1357 sp = new JScrollPane(channelParamTable);
211 iliev 1286 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 iliev 1357
231 iliev 1445 updateParams(audioDeviceModel.getDeviceInfo());
232 iliev 1357 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 iliev 1286 }
248    
249 iliev 1357 private void
250     updateAdditionalParamsViewState() {
251     String s = "AudioDevice.showAdditionalParameters";
252     additionalParamsPane.setVisible(preferences().getBoolProperty(s));
253     validate();
254     }
255    
256 iliev 1785 @Override
257 iliev 1286 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 iliev 1785 @Override
271 iliev 1286 public void
272     itemStateChanged(ItemEvent e) {
273     boolean a = checkActive.isSelected();
274     if(a != audioDeviceModel.isActive()) audioDeviceModel.setBackendActive(a);
275     }
276    
277 iliev 1785 @Override
278 iliev 1286 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 iliev 1785 @Override
287 iliev 1286 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 iliev 1445 updateParams(d);
299 iliev 1286
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 iliev 2192
309     /** Invoked when when a new send effect chain is added to the audio device. */
310     @Override public void sendEffectChainAdded(AudioDeviceEvent e) { }
311    
312     /** Invoked when when a send effect chain is removed from the audio device. */
313     @Override public void sendEffectChainRemoved(AudioDeviceEvent e) { }
314 iliev 1286
315 iliev 1785 @Override
316 iliev 1286 public void
317     parameterChanged(ParameterEvent e) {
318     int c = cbChannel.getSelectedIndex();
319     if(c == -1) {
320     CC.getLogger().warning("There is no audio channel selected!");
321     return;
322     }
323    
324     audioDeviceModel.setBackendChannelParameter(c, e.getParameter());
325     }
326 iliev 1445
327     private void
328     updateParams(AudioOutputDevice d) {
329 iliev 1606 Parameter p = d.getSampleRateParameter();
330     boolean b = p == null || p.getName() == null || p.getValue() == null;
331 iliev 1445 Parameter[] params = d.getAdditionalParameters();
332 iliev 1606 Parameter[] p2s;
333     if(b) p2s = new Parameter[params.length];
334     else p2s = new Parameter[params.length + 1];
335 iliev 1445
336     for(int i = 0; i < params.length; i++) p2s[i] = params[i];
337    
338 iliev 1606 if(!b) p2s[params.length] = p;
339 iliev 1445
340     additionalParamsTable.getModel().setParameters(p2s);
341     }
342 iliev 1286 }
343    
344     }

  ViewVC Help
Powered by ViewVC