1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2011 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.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Insets; |
28 |
|
29 |
import java.awt.event.ActionEvent; |
30 |
import java.awt.event.ActionListener; |
31 |
|
32 |
import java.util.logging.Level; |
33 |
|
34 |
import javax.swing.BorderFactory; |
35 |
import javax.swing.Box; |
36 |
import javax.swing.BoxLayout; |
37 |
import javax.swing.JComboBox; |
38 |
import javax.swing.JComponent; |
39 |
import javax.swing.JLabel; |
40 |
import javax.swing.JPanel; |
41 |
import javax.swing.SwingConstants; |
42 |
|
43 |
import org.jsampler.AudioDeviceModel; |
44 |
import org.jsampler.CC; |
45 |
import org.jsampler.MidiDeviceModel; |
46 |
import org.jsampler.MidiInstrumentMap; |
47 |
import org.jsampler.SamplerModel; |
48 |
|
49 |
import org.jsampler.event.ListEvent; |
50 |
import org.jsampler.event.ListListener; |
51 |
import org.jsampler.event.MidiDeviceEvent; |
52 |
import org.jsampler.event.MidiDeviceListEvent; |
53 |
import org.jsampler.event.MidiDeviceListListener; |
54 |
import org.jsampler.event.MidiDeviceListener; |
55 |
import org.jsampler.event.SamplerAdapter; |
56 |
import org.jsampler.event.SamplerChannelAdapter; |
57 |
import org.jsampler.event.SamplerChannelEvent; |
58 |
import org.jsampler.event.SamplerEvent; |
59 |
import org.jsampler.event.SamplerListener; |
60 |
|
61 |
import org.jsampler.view.fantasia.basic.*; |
62 |
import org.jsampler.view.std.JSChannelOutputRoutingDlg; |
63 |
import org.jsampler.view.swing.SHF; |
64 |
|
65 |
import org.linuxsampler.lscp.AudioOutputDevice; |
66 |
import org.linuxsampler.lscp.MidiInputDevice; |
67 |
import org.linuxsampler.lscp.MidiPort; |
68 |
import org.linuxsampler.lscp.SamplerChannel; |
69 |
|
70 |
import org.pushingpixels.substance.api.SubstanceLookAndFeel; |
71 |
|
72 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
73 |
|
74 |
/** |
75 |
* |
76 |
* @author Grigor Iliev |
77 |
*/ |
78 |
public class NormalChannelOptionsView extends JPanel implements ChannelOptionsView { |
79 |
private final Channel channel; |
80 |
private MidiDeviceModel midiDevice = null; |
81 |
|
82 |
private final JComboBox cbMidiDevice = new FantasiaComboBox(); |
83 |
private final JComboBox cbMidiPort = new FantasiaComboBox(); |
84 |
private final JComboBox cbMidiChannel = new FantasiaComboBox(); |
85 |
private final JComboBox cbInstrumentMap = new FantasiaComboBox(); |
86 |
private final JComboBox cbAudioDevice = new FantasiaComboBox(); |
87 |
|
88 |
private final PixmapButton btnChannelRouting; |
89 |
|
90 |
private boolean update = false; |
91 |
|
92 |
private final SamplerListener samplerListener; |
93 |
private final MapListListener mapListListener = new MapListListener(); |
94 |
|
95 |
private class NoMap { |
96 |
public String |
97 |
toString() { return "[None]"; } |
98 |
} |
99 |
|
100 |
private NoMap noMap = new NoMap(); |
101 |
|
102 |
private class DefaultMap { |
103 |
public String |
104 |
toString() { return "[Default]"; } |
105 |
} |
106 |
|
107 |
private DefaultMap defaultMap = new DefaultMap(); |
108 |
|
109 |
/** Creates a new instance of <code>NormalChannelOptionsView</code> */ |
110 |
public |
111 |
NormalChannelOptionsView(final Channel channel) { |
112 |
setLayout(new BorderLayout()); |
113 |
PixmapPane bgp = new PixmapPane(Res.gfxChannelOptions); |
114 |
bgp.setPixmapInsets(new Insets(1, 1, 1, 1)); |
115 |
|
116 |
this.channel = channel; |
117 |
|
118 |
bgp.setBorder(BorderFactory.createEmptyBorder(5, 4, 5, 4)); |
119 |
bgp.setLayout(new BoxLayout(bgp, BoxLayout.X_AXIS)); |
120 |
|
121 |
bgp.setPreferredSize(new Dimension(420, 44)); |
122 |
bgp.setMinimumSize(getPreferredSize()); |
123 |
bgp.setMaximumSize(getPreferredSize()); |
124 |
|
125 |
JPanel p = new FantasiaPanel(); |
126 |
p.setBorder(BorderFactory.createEmptyBorder(3, 4, 3, 4)); |
127 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
128 |
JLabel l = new JLabel(Res.gfxMidiInputTitle); |
129 |
l.setAlignmentX(LEFT_ALIGNMENT); |
130 |
p.add(l); |
131 |
|
132 |
JPanel p2 = new FantasiaPanel(); |
133 |
p2.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0)); |
134 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
135 |
|
136 |
Object o = cbMidiDevice.getRenderer(); |
137 |
if(o instanceof JLabel) ((JLabel )o).setHorizontalAlignment(SwingConstants.CENTER); |
138 |
|
139 |
cbMidiDevice.setPreferredSize(new Dimension(40, 18)); |
140 |
cbMidiDevice.setMinimumSize(cbMidiDevice.getPreferredSize()); |
141 |
cbMidiDevice.setMaximumSize(cbMidiDevice.getPreferredSize()); |
142 |
p2.add(cbMidiDevice); |
143 |
|
144 |
p2.add(Box.createRigidArea(new Dimension(3, 0))); |
145 |
|
146 |
o = cbMidiPort.getRenderer(); |
147 |
if(o instanceof JLabel) ((JLabel )o).setHorizontalAlignment(SwingConstants.LEFT); |
148 |
|
149 |
cbMidiPort.addActionListener(new ActionListener() { |
150 |
public void |
151 |
actionPerformed(ActionEvent e) { |
152 |
updateCbMidiPortToolTipText(); |
153 |
} |
154 |
}); |
155 |
|
156 |
cbMidiPort.setPreferredSize(new Dimension(62, 18)); |
157 |
cbMidiPort.setMinimumSize(cbMidiPort.getPreferredSize()); |
158 |
cbMidiPort.setMaximumSize(cbMidiPort.getPreferredSize()); |
159 |
p2.add(cbMidiPort); |
160 |
|
161 |
p2.add(Box.createRigidArea(new Dimension(3, 0))); |
162 |
|
163 |
o = cbMidiChannel.getRenderer(); |
164 |
if(o instanceof JLabel) ((JLabel )o).setHorizontalAlignment(SwingConstants.CENTER); |
165 |
|
166 |
cbMidiChannel.addItem("All"); |
167 |
for(int i = 1; i <= 16; i++) cbMidiChannel.addItem("Channel " + String.valueOf(i)); |
168 |
cbMidiChannel.setPreferredSize(new Dimension(84, 18)); |
169 |
cbMidiChannel.setMinimumSize(cbMidiChannel.getPreferredSize()); |
170 |
cbMidiChannel.setMaximumSize(cbMidiChannel.getPreferredSize()); |
171 |
|
172 |
p2.add(cbMidiChannel); |
173 |
p2.setAlignmentX(LEFT_ALIGNMENT); |
174 |
p2.setOpaque(false); |
175 |
p.add(p2); |
176 |
p.setBackground(new java.awt.Color(0x818181)); |
177 |
|
178 |
bgp.add(p); |
179 |
|
180 |
bgp.add(Box.createRigidArea(new Dimension(4, 0))); |
181 |
|
182 |
p = new FantasiaPanel(); |
183 |
p.setOpaque(true); |
184 |
p.setBorder(BorderFactory.createEmptyBorder(3, 4, 3, 4)); |
185 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
186 |
l = new JLabel(Res.gfxInstrumentMapTitle); |
187 |
l.setAlignmentX(LEFT_ALIGNMENT); |
188 |
l.setAlignmentX(LEFT_ALIGNMENT); |
189 |
p.add(l); |
190 |
|
191 |
p.add(Box.createRigidArea(new Dimension(0, 3))); |
192 |
|
193 |
//o = cbInstrumentMap.getRenderer(); |
194 |
//if(o instanceof JLabel) ((JLabel )o).setHorizontalAlignment(SwingConstants.CENTER); |
195 |
|
196 |
cbInstrumentMap.setPreferredSize(new Dimension(122, 18)); |
197 |
cbInstrumentMap.setMinimumSize(cbInstrumentMap.getPreferredSize()); |
198 |
cbInstrumentMap.setMaximumSize(cbInstrumentMap.getPreferredSize()); |
199 |
cbInstrumentMap.setAlignmentX(LEFT_ALIGNMENT); |
200 |
p.add(cbInstrumentMap); |
201 |
p.setBackground(new java.awt.Color(0x818181)); |
202 |
bgp.add(p); |
203 |
|
204 |
bgp.add(Box.createRigidArea(new Dimension(4, 0))); |
205 |
|
206 |
p = new FantasiaPanel(); |
207 |
p.setOpaque(true); |
208 |
p.setBorder(BorderFactory.createEmptyBorder(3, 4, 3, 4)); |
209 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
210 |
l = new JLabel(Res.gfxAudioOutputTitle); |
211 |
l.setAlignmentX(LEFT_ALIGNMENT); |
212 |
p.add(l); |
213 |
|
214 |
//p.add(Box.createRigidArea(new Dimension(0, 3))); |
215 |
|
216 |
p2 = new FantasiaPanel(); |
217 |
p2.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0)); |
218 |
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); |
219 |
p2.setOpaque(false); |
220 |
p2.setAlignmentX(LEFT_ALIGNMENT); |
221 |
|
222 |
o = cbAudioDevice.getRenderer(); |
223 |
if(o instanceof JLabel) ((JLabel )o).setHorizontalAlignment(SwingConstants.RIGHT); |
224 |
|
225 |
cbAudioDevice.setPreferredSize(new Dimension(40, 18)); |
226 |
cbAudioDevice.setMinimumSize(cbAudioDevice.getPreferredSize()); |
227 |
cbAudioDevice.setMaximumSize(cbAudioDevice.getPreferredSize()); |
228 |
|
229 |
p2.add(cbAudioDevice); |
230 |
p2.add(Box.createRigidArea(new Dimension(3, 0))); |
231 |
btnChannelRouting = new PixmapButton(Res.gfxBtnCr, Res.gfxBtnCrRO); |
232 |
btnChannelRouting.setPressedIcon(Res.gfxBtnCrRO); |
233 |
btnChannelRouting.setEnabled(false); |
234 |
btnChannelRouting.setToolTipText(i18n.getLabel("ChannelOptions.routing")); |
235 |
|
236 |
btnChannelRouting.addActionListener(new ActionListener() { |
237 |
public void |
238 |
actionPerformed(ActionEvent e) { |
239 |
SamplerChannel c = channel.getChannelInfo(); |
240 |
new JSChannelOutputRoutingDlg(SHF.getMainFrame(), c).setVisible(true); |
241 |
|
242 |
} |
243 |
}); |
244 |
|
245 |
p2.add(btnChannelRouting); |
246 |
|
247 |
p.add(p2); |
248 |
p.setBackground(new java.awt.Color(0x818181)); |
249 |
p2 = new FantasiaPanel(); |
250 |
p2.setLayout(new java.awt.BorderLayout()); |
251 |
p.add(p2); |
252 |
bgp.add(p); |
253 |
|
254 |
add(bgp); |
255 |
|
256 |
cbMidiDevice.addActionListener(new ActionListener() { |
257 |
public void |
258 |
actionPerformed(ActionEvent e) { setMidiDevice(); } |
259 |
}); |
260 |
|
261 |
cbMidiPort.addActionListener(new ActionListener() { |
262 |
public void |
263 |
actionPerformed(ActionEvent e) { setMidiPort(); } |
264 |
}); |
265 |
|
266 |
cbMidiChannel.addActionListener(new ActionListener() { |
267 |
public void |
268 |
actionPerformed(ActionEvent e) { setMidiChannel(); } |
269 |
}); |
270 |
|
271 |
samplerListener = new SamplerAdapter() { |
272 |
/** Invoked when the default MIDI instrument map is changed. */ |
273 |
public void |
274 |
defaultMapChanged(SamplerEvent e) { |
275 |
updateCbInstrumentMapToolTipText(); |
276 |
|
277 |
} |
278 |
}; |
279 |
|
280 |
CC.getSamplerModel().addSamplerListener(samplerListener); |
281 |
|
282 |
cbInstrumentMap.addItem(noMap); |
283 |
cbInstrumentMap.addItem(defaultMap); |
284 |
for(MidiInstrumentMap map : CC.getSamplerModel().getMidiInstrumentMaps()) { |
285 |
cbInstrumentMap.addItem(map); |
286 |
} |
287 |
|
288 |
int map = channel.getModel().getChannelInfo().getMidiInstrumentMapId(); |
289 |
cbInstrumentMap.setSelectedItem(CC.getSamplerModel().getMidiInstrumentMapById(map)); |
290 |
if(cbInstrumentMap.getSelectedItem() == null) { |
291 |
if(map == -1) cbInstrumentMap.setSelectedItem(noMap); |
292 |
else if(map == -2) { |
293 |
cbInstrumentMap.setSelectedItem(defaultMap); |
294 |
} |
295 |
} |
296 |
|
297 |
updateCbInstrumentMapToolTipText(); |
298 |
|
299 |
if(channel.getModel().getChannelInfo().getEngine() == null) { |
300 |
cbInstrumentMap.setEnabled(false); |
301 |
} |
302 |
|
303 |
cbInstrumentMap.addActionListener(new ActionListener() { |
304 |
public void |
305 |
actionPerformed(ActionEvent e) { updateInstrumentMap(); } |
306 |
}); |
307 |
|
308 |
CC.getSamplerModel().addMidiInstrumentMapListListener(mapListListener); |
309 |
|
310 |
cbAudioDevice.addActionListener(new ActionListener() { |
311 |
public void |
312 |
actionPerformed(ActionEvent e) { setBackendAudioDevice(); } |
313 |
}); |
314 |
|
315 |
channel.getModel().addSamplerChannelListener(new SamplerChannelAdapter() { |
316 |
public void |
317 |
channelChanged(SamplerChannelEvent e) { updateChannelProperties(); } |
318 |
}); |
319 |
|
320 |
CC.getSamplerModel().addMidiDeviceListListener(getHandler()); |
321 |
CC.getSamplerModel().addAudioDeviceListListener(getHandler()); |
322 |
|
323 |
updateMidiDevices(); |
324 |
updateAudioDevices(); |
325 |
updateChannelProperties(); |
326 |
} |
327 |
|
328 |
////////////////////////////////////////////// |
329 |
// Implementation of the ChannelOptionsView interface |
330 |
////////////////////////////////////////////// |
331 |
|
332 |
public JComponent |
333 |
getComponent() { return this; } |
334 |
|
335 |
public void |
336 |
installView() { } |
337 |
|
338 |
public void |
339 |
uninstallView() { |
340 |
onDestroy(); |
341 |
} |
342 |
|
343 |
public void |
344 |
updateChannelInfo() { |
345 |
|
346 |
} |
347 |
|
348 |
////////////////////////////////////////////// |
349 |
|
350 |
/** |
351 |
* Updates the channel settings. This method is invoked when changes to the |
352 |
* channel were made. |
353 |
*/ |
354 |
private void |
355 |
updateChannelProperties() { |
356 |
SamplerModel sm = CC.getSamplerModel(); |
357 |
SamplerChannel sc = channel.getModel().getChannelInfo(); |
358 |
|
359 |
MidiDeviceModel mm = sm.getMidiDeviceById(sc.getMidiInputDevice()); |
360 |
AudioDeviceModel am = sm.getAudioDeviceById(sc.getAudioOutputDevice()); |
361 |
|
362 |
if(isUpdate()) CC.getLogger().warning("Unexpected update state!"); |
363 |
|
364 |
setUpdate(true); |
365 |
|
366 |
try { |
367 |
cbMidiDevice.setSelectedItem(mm == null ? null : mm.getDeviceInfo()); |
368 |
|
369 |
cbAudioDevice.setSelectedItem(am == null ? null : am.getDeviceInfo()); |
370 |
btnChannelRouting.setEnabled(am != null); |
371 |
} catch(Exception x) { |
372 |
CC.getLogger().log(Level.WARNING, "Unkown error", x); |
373 |
} |
374 |
|
375 |
if(sc.getEngine() != null) { |
376 |
cbInstrumentMap.setEnabled(true); |
377 |
int id = sc.getMidiInstrumentMapId(); |
378 |
Object o; |
379 |
if(id == -2) o = defaultMap; |
380 |
else if(id == -1) o = noMap; |
381 |
else o = CC.getSamplerModel().getMidiInstrumentMapById(id); |
382 |
|
383 |
if(cbInstrumentMap.getSelectedItem() != o) { |
384 |
cbInstrumentMap.setSelectedItem(o); |
385 |
} |
386 |
} else { |
387 |
cbInstrumentMap.setSelectedItem(noMap); |
388 |
cbInstrumentMap.setEnabled(false); |
389 |
} |
390 |
|
391 |
setUpdate(false); |
392 |
} |
393 |
|
394 |
/** |
395 |
* Updates the MIDI device list. |
396 |
*/ |
397 |
private void |
398 |
updateMidiDevices() { |
399 |
SamplerModel sm = CC.getSamplerModel(); |
400 |
SamplerChannel sc = channel.getModel().getChannelInfo(); |
401 |
|
402 |
setUpdate(true); |
403 |
|
404 |
try { |
405 |
cbMidiDevice.removeAllItems(); |
406 |
|
407 |
for(MidiDeviceModel m : sm.getMidiDevices()) |
408 |
cbMidiDevice.addItem(m.getDeviceInfo()); |
409 |
|
410 |
MidiDeviceModel mm = sm.getMidiDeviceById(sc.getMidiInputDevice()); |
411 |
cbMidiDevice.setSelectedItem(mm == null ? null : mm.getDeviceInfo()); |
412 |
} catch(Exception x) { |
413 |
CC.getLogger().log(Level.WARNING, "Unkown error", x); |
414 |
} |
415 |
|
416 |
setUpdate(false); |
417 |
} |
418 |
|
419 |
|
420 |
private void |
421 |
updateInstrumentMap() { |
422 |
updateCbInstrumentMapToolTipText(); |
423 |
|
424 |
int id = channel.getModel().getChannelInfo().getMidiInstrumentMapId(); |
425 |
Object o = cbInstrumentMap.getSelectedItem(); |
426 |
if(o == null && id == -1) return; |
427 |
|
428 |
int cbId; |
429 |
if(o == null || o == noMap) cbId = -1; |
430 |
else if(o == defaultMap) cbId = -2; |
431 |
else cbId = ((MidiInstrumentMap)o).getMapId(); |
432 |
|
433 |
if(cbId == id) return; |
434 |
|
435 |
channel.getModel().setBackendMidiInstrumentMap(cbId); |
436 |
} |
437 |
|
438 |
private void |
439 |
updateCbMidiPortToolTipText() { |
440 |
if(cbMidiPort.getSelectedItem() == null) { |
441 |
cbMidiPort.setToolTipText(null); |
442 |
return; |
443 |
} |
444 |
|
445 |
cbMidiPort.setToolTipText(cbMidiPort.getSelectedItem().toString()); |
446 |
} |
447 |
|
448 |
private void |
449 |
updateCbInstrumentMapToolTipText() { |
450 |
if(cbInstrumentMap.getSelectedItem() != defaultMap) { |
451 |
cbInstrumentMap.setToolTipText(null); |
452 |
return; |
453 |
} |
454 |
|
455 |
MidiInstrumentMap m = CC.getSamplerModel().getDefaultMidiInstrumentMap(); |
456 |
if(m != null) { |
457 |
String s = i18n.getLabel("Channel.ttDefault", m.getName()); |
458 |
cbInstrumentMap.setToolTipText(s); |
459 |
} else { |
460 |
cbInstrumentMap.setToolTipText(null); |
461 |
} |
462 |
} |
463 |
|
464 |
/** |
465 |
* Updates the audio device list. |
466 |
*/ |
467 |
private void |
468 |
updateAudioDevices() { |
469 |
SamplerModel sm = CC.getSamplerModel(); |
470 |
SamplerChannel sc = channel.getModel().getChannelInfo(); |
471 |
|
472 |
setUpdate(true); |
473 |
|
474 |
try { |
475 |
cbAudioDevice.removeAllItems(); |
476 |
|
477 |
for(AudioDeviceModel m : sm.getAudioDevices()) |
478 |
cbAudioDevice.addItem(m.getDeviceInfo()); |
479 |
|
480 |
AudioDeviceModel am = sm.getAudioDeviceById(sc.getAudioOutputDevice()); |
481 |
cbAudioDevice.setSelectedItem(am == null ? null : am.getDeviceInfo()); |
482 |
} catch(Exception x) { |
483 |
CC.getLogger().log(Level.WARNING, "Unkown error", x); |
484 |
} |
485 |
|
486 |
setUpdate(false); |
487 |
} |
488 |
|
489 |
private void |
490 |
setMidiDevice() { |
491 |
MidiInputDevice mid = (MidiInputDevice)cbMidiDevice.getSelectedItem(); |
492 |
|
493 |
if(!isUpdate()) { |
494 |
if(mid != null) { |
495 |
channel.getModel().setBackendMidiInputDevice(mid.getDeviceId()); |
496 |
} |
497 |
|
498 |
return; |
499 |
} |
500 |
|
501 |
if(midiDevice != null) midiDevice.removeMidiDeviceListener(getHandler()); |
502 |
|
503 |
cbMidiPort.removeAllItems(); |
504 |
|
505 |
if(mid == null) { |
506 |
midiDevice = null; |
507 |
cbMidiPort.setEnabled(false); |
508 |
|
509 |
cbMidiChannel.setSelectedItem(null); |
510 |
cbMidiChannel.setEnabled(false); |
511 |
} else { |
512 |
midiDevice = CC.getSamplerModel().getMidiDeviceById(mid.getDeviceId()); |
513 |
if(midiDevice != null) midiDevice.addMidiDeviceListener(getHandler()); |
514 |
|
515 |
cbMidiPort.setEnabled(true); |
516 |
|
517 |
MidiPort[] ports = mid.getMidiPorts(); |
518 |
for(MidiPort port : ports) cbMidiPort.addItem(port); |
519 |
|
520 |
int p = channel.getModel().getChannelInfo().getMidiInputPort(); |
521 |
cbMidiPort.setSelectedItem(p >= 0 && p < ports.length ? ports[p] : null); |
522 |
|
523 |
cbMidiPort.putClientProperty ( |
524 |
SubstanceLookAndFeel.COMBO_POPUP_PROTOTYPE, |
525 |
new FantasiaComboBox.WidestComboPopupPrototype() |
526 |
); |
527 |
|
528 |
cbMidiChannel.setEnabled(true); |
529 |
int c = channel.getModel().getChannelInfo().getMidiInputChannel(); |
530 |
cbMidiChannel.setSelectedItem(c == -1 ? "All" : "Channel " + (c + 1)); |
531 |
} |
532 |
} |
533 |
|
534 |
private void |
535 |
setMidiPort() { |
536 |
if(isUpdate()) return; |
537 |
|
538 |
channel.getModel().setBackendMidiInputPort(cbMidiPort.getSelectedIndex()); |
539 |
} |
540 |
|
541 |
private void |
542 |
setMidiChannel() { |
543 |
if(isUpdate()) return; |
544 |
|
545 |
Object o = cbMidiChannel.getSelectedItem(); |
546 |
if(o == null) return; |
547 |
String s = o.toString(); |
548 |
|
549 |
int c = s.equals("All") ? -1 : Integer.parseInt(s.substring(8)) - 1; |
550 |
|
551 |
channel.getModel().setBackendMidiInputChannel(c); |
552 |
} |
553 |
|
554 |
private void |
555 |
setBackendAudioDevice() { |
556 |
if(isUpdate()) return; |
557 |
AudioOutputDevice dev = (AudioOutputDevice)cbAudioDevice.getSelectedItem(); |
558 |
if(dev != null) channel.getModel().setBackendAudioOutputDevice(dev.getDeviceId()); |
559 |
} |
560 |
|
561 |
/** |
562 |
* Determines whether the currently processed changes are due to update. |
563 |
* @return <code>true</code> if the currently processed changes are due to update and |
564 |
* <code>false</code> if the currently processed changes are due to user input. |
565 |
*/ |
566 |
private boolean |
567 |
isUpdate() { return update; } |
568 |
|
569 |
/** |
570 |
* Sets whether the currently processed changes are due to update. |
571 |
* @param b Specify <code>true</code> to indicate that the currently |
572 |
* processed changes are due to update; <code>false</code> |
573 |
* indicates that the currently processed changes are due to user input. |
574 |
*/ |
575 |
private void |
576 |
setUpdate(boolean b) { update = b; } |
577 |
|
578 |
protected void |
579 |
onDestroy() { |
580 |
SamplerModel sm = CC.getSamplerModel(); |
581 |
|
582 |
sm.removeMidiDeviceListListener(getHandler()); |
583 |
sm.removeAudioDeviceListListener(getHandler()); |
584 |
sm.removeMidiInstrumentMapListListener(mapListListener); |
585 |
sm.removeSamplerListener(samplerListener); |
586 |
|
587 |
if(midiDevice != null) { |
588 |
midiDevice.removeMidiDeviceListener(getHandler()); |
589 |
} |
590 |
} |
591 |
|
592 |
private final Handler handler = new Handler(); |
593 |
|
594 |
private Handler |
595 |
getHandler() { return handler; } |
596 |
|
597 |
private class Handler implements MidiDeviceListListener, ListListener<AudioDeviceModel>, |
598 |
MidiDeviceListener { |
599 |
/** |
600 |
* Invoked when a new MIDI device is created. |
601 |
* @param e A <code>MidiDeviceListEvent</code> |
602 |
* instance providing the event information. |
603 |
*/ |
604 |
@Override |
605 |
public void |
606 |
deviceAdded(MidiDeviceListEvent e) { |
607 |
cbMidiDevice.addItem(e.getMidiDeviceModel().getDeviceInfo()); |
608 |
} |
609 |
|
610 |
/** |
611 |
* Invoked when a MIDI device is removed. |
612 |
* @param e A <code>MidiDeviceListEvent</code> |
613 |
* instance providing the event information. |
614 |
*/ |
615 |
@Override |
616 |
public void |
617 |
deviceRemoved(MidiDeviceListEvent e) { |
618 |
cbMidiDevice.removeItem(e.getMidiDeviceModel().getDeviceInfo()); |
619 |
} |
620 |
|
621 |
/** |
622 |
* Invoked when a new audio device is created. |
623 |
* @param e An <code>AudioDeviceListEvent</code> |
624 |
* instance providing the event information. |
625 |
*/ |
626 |
@Override |
627 |
public void |
628 |
entryAdded(ListEvent<AudioDeviceModel> e) { |
629 |
cbAudioDevice.addItem(e.getEntry().getDeviceInfo()); |
630 |
} |
631 |
|
632 |
/** |
633 |
* Invoked when an audio device is removed. |
634 |
* @param e An <code>AudioDeviceListEvent</code> |
635 |
* instance providing the event information. |
636 |
*/ |
637 |
@Override |
638 |
public void |
639 |
entryRemoved(ListEvent<AudioDeviceModel> e) { |
640 |
cbAudioDevice.removeItem(e.getEntry().getDeviceInfo()); |
641 |
} |
642 |
|
643 |
@Override |
644 |
public void |
645 |
settingsChanged(MidiDeviceEvent e) { |
646 |
if(isUpdate()) { |
647 |
CC.getLogger().warning("Invalid update state"); |
648 |
return; |
649 |
} |
650 |
|
651 |
setUpdate(true); |
652 |
int idx = cbMidiPort.getSelectedIndex(); |
653 |
MidiInputDevice d = e.getMidiDeviceModel().getDeviceInfo(); |
654 |
|
655 |
cbMidiPort.removeAllItems(); |
656 |
for(MidiPort port : d.getMidiPorts()) cbMidiPort.addItem(port); |
657 |
|
658 |
if(idx >= cbMidiPort.getModel().getSize()) idx = 0; |
659 |
|
660 |
setUpdate(false); |
661 |
|
662 |
if(cbMidiPort.getModel().getSize() > 0) cbMidiPort.setSelectedIndex(idx); |
663 |
} |
664 |
} |
665 |
|
666 |
private class MapListListener implements ListListener<MidiInstrumentMap> { |
667 |
/** Invoked when a new MIDI instrument map is added to a list. */ |
668 |
@Override |
669 |
public void |
670 |
entryAdded(ListEvent<MidiInstrumentMap> e) { |
671 |
cbInstrumentMap.insertItemAt(e.getEntry(), cbInstrumentMap.getItemCount()); |
672 |
boolean b = channel.getModel().getChannelInfo().getEngine() != null; |
673 |
if(b && !cbInstrumentMap.isEnabled()) cbInstrumentMap.setEnabled(true); |
674 |
} |
675 |
|
676 |
/** Invoked when a new MIDI instrument map is removed from a list. */ |
677 |
@Override |
678 |
public void |
679 |
entryRemoved(ListEvent<MidiInstrumentMap> e) { |
680 |
cbInstrumentMap.removeItem(e.getEntry()); |
681 |
if(cbInstrumentMap.getItemCount() == 0) { // TODO: ? |
682 |
cbInstrumentMap.setSelectedItem(noMap); |
683 |
cbInstrumentMap.setEnabled(false); |
684 |
} |
685 |
} |
686 |
} |
687 |
} |