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

Contents of /jsampler/trunk/src/org/jsampler/view/fantasia/PianoKeyboardPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1880 - (show annotations) (download)
Sun Mar 29 19:10:49 2009 UTC (15 years ago) by iliev
File size: 16652 byte(s)
* fixed bug in the sampler channel number notifications

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2008 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.AlphaComposite;
26 import java.awt.Color;
27 import java.awt.Composite;
28 import java.awt.Dimension;
29 import java.awt.GradientPaint;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.GridBagConstraints;
33 import java.awt.GridBagLayout;
34 import java.awt.Insets;
35 import java.awt.Paint;
36 import java.awt.RenderingHints;
37
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.FocusEvent;
41 import java.awt.event.FocusListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseAdapter;
44
45 import java.awt.geom.Line2D;
46 import java.awt.geom.Rectangle2D;
47
48 import java.beans.PropertyChangeEvent;
49 import java.beans.PropertyChangeListener;
50
51 import javax.swing.BorderFactory;
52 import javax.swing.Box;
53 import javax.swing.BoxLayout;
54 import javax.swing.ImageIcon;
55 import javax.swing.JButton;
56 import javax.swing.JPanel;
57 import javax.swing.JToggleButton;
58
59 import javax.swing.event.ListSelectionEvent;
60 import javax.swing.event.ListSelectionListener;
61
62 import net.sf.juife.event.TaskEvent;
63 import net.sf.juife.event.TaskListener;
64
65 import org.jsampler.CC;
66 import org.jsampler.SamplerChannelModel;
67
68 import org.jsampler.event.SamplerChannelAdapter;
69 import org.jsampler.event.SamplerChannelEvent;
70 import org.jsampler.event.SamplerChannelListListener;
71 import org.jsampler.event.SamplerChannelListEvent;
72
73 import org.jsampler.view.JSChannel;
74 import org.jsampler.view.fantasia.basic.*;
75 import org.jsampler.view.std.JSPianoRoll;
76
77 import org.linuxsampler.lscp.Instrument;
78 import org.linuxsampler.lscp.SamplerChannel;
79
80 import org.linuxsampler.lscp.event.MidiDataEvent;
81 import org.linuxsampler.lscp.event.MidiDataListener;
82
83 import static javax.swing.Action.SMALL_ICON;
84 import static org.jsampler.task.Global.GetFileInstrument;
85 import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
86
87 /**
88 *
89 * @author Grigor Iliev
90 */
91 public class PianoKeyboardPane extends FantasiaPanel
92 implements ListSelectionListener, SamplerChannelListListener {
93
94 protected final JToggleButton btnPower = new PowerButton();
95 private final FantasiaLabel lDisplay = new FantasiaLabel(" ", true);
96 //private final PitchWheel pitchWheel = new PitchWheel();
97 //private final ModWheel modWheel = new ModWheel();
98 private final JSPianoRoll pianoRoll = new JSPianoRoll();
99 private SamplerChannelModel channel = null;
100
101 private String file = null;
102 private int index = -1;
103
104 public
105 PianoKeyboardPane() {
106 pianoRoll.actionDecreaseKeyNumber.putValue(SMALL_ICON, Res.gfxBtnDecrease);
107 pianoRoll.actionIncreaseKeyNumber.putValue(SMALL_ICON, Res.gfxBtnIncrease);
108 pianoRoll.actionScrollLeft.putValue(SMALL_ICON, Res.gfxBtnScrollLeft);
109 pianoRoll.actionScrollRight.putValue(SMALL_ICON, Res.gfxBtnScrollRight);
110
111 setOpaque(false);
112
113 GridBagLayout gridbag = new GridBagLayout();
114 GridBagConstraints c = new GridBagConstraints();
115
116 setLayout(gridbag);
117
118 setBorder(BorderFactory.createEmptyBorder(0, 3, 5, 3));
119
120 c.fill = GridBagConstraints.NONE;
121
122 c.gridx = 0;
123 c.gridy = 0;
124 c.anchor = GridBagConstraints.NORTHWEST;
125 c.insets = new Insets(3, 3, 3, 3);
126 gridbag.setConstraints(btnPower, c);
127 add(btnPower);
128
129 JPanel p = createVSeparator();
130 c.gridx = 1;
131 c.gridy = 0;
132 c.gridheight = 2;
133 c.insets = new Insets(3, 0, 5, 3);
134 c.fill = GridBagConstraints.VERTICAL;
135 c.weighty = 1.0;
136 gridbag.setConstraints(p, c);
137 add(p);
138
139 /*c.gridx = 2;
140 c.gridy = 1;
141 c.gridheight = 1;
142 c.insets = new Insets(0, 3, 5, 3);
143 c.anchor = GridBagConstraints.CENTER;
144 gridbag.setConstraints(pitchWheel, c);
145 add(pitchWheel);
146
147 c.gridx = 3;
148 c.gridy = 1;
149 c.insets = new Insets(0, 0, 5, 6);
150 gridbag.setConstraints(modWheel, c);
151 add(modWheel);*/
152
153 p = new KeyRangePropsPane();
154
155 c.gridx = 2;
156 c.gridy = 0;
157 c.gridwidth = 3;
158 c.gridheight = 1;
159 c.insets = new Insets(0, 0, 0, 0);
160 c.fill = GridBagConstraints.NONE;
161 c.weighty = 0.0;
162 c.anchor = GridBagConstraints.WEST;
163 gridbag.setConstraints(p, c);
164 add(p);
165
166 p = new KeyRangePropsPane();
167
168 c.gridx = 8;
169 c.gridy = 0;
170 c.gridwidth = 1;
171 c.anchor = GridBagConstraints.EAST;
172 c.insets = new Insets(0, 0, 0, 10);
173 gridbag.setConstraints(p, c);
174 add(p);
175
176 lDisplay.setPreferredSize(new Dimension(300, lDisplay.getPreferredSize().height));
177
178 c.gridx = 6;
179 c.gridy = 0;
180 c.insets = new Insets(3, 3, 3, 12);
181 c.fill = GridBagConstraints.NONE;
182 c.weightx = 0.0;
183 gridbag.setConstraints(lDisplay, c);
184 add(lDisplay);
185
186 p = new JPanel();
187 p.setOpaque(false);
188 c.gridx = 5;
189 c.gridy = 0;
190 c.insets = new Insets(0, 0, 0, 0);
191 c.fill = GridBagConstraints.HORIZONTAL;
192 c.weightx = 1.0;
193 gridbag.setConstraints(p, c);
194 add(p);
195
196 p = new JPanel();
197 p.setOpaque(false);
198 c.gridx = 7;
199 c.gridy = 0;
200 gridbag.setConstraints(p, c);
201 add(p);
202
203 pianoRoll.setFocusable(false);
204 pianoRoll.setBackground(new Color(0x2e2e2e));
205 //pianoRoll.setOpaque(false);
206
207 disablePianoRoll();
208
209 c.gridx = 4;
210 c.gridy = 1;
211 c.gridwidth = 5;
212 c.insets = new Insets(0, 3, 0, 12);
213 c.fill = GridBagConstraints.BOTH;
214 c.weightx = 1.0;
215 c.weighty = 1.0;
216 gridbag.setConstraints(pianoRoll, c);
217 add(pianoRoll);
218
219 CC.getSamplerModel().addSamplerChannelListListener(this);
220 pianoRoll.addMidiDataListener(getHandler());
221
222 updateKeyRange();
223
224 PropertyChangeListener l = new PropertyChangeListener() {
225 public void
226 propertyChange(PropertyChangeEvent e) {
227 updateKeyRange();
228 }
229 };
230
231 CC.preferences().addPropertyChangeListener("midiKeyboard.firstKey", l);
232 CC.preferences().addPropertyChangeListener("midiKeyboard.lastKey", l);
233
234 addFocusListener(getHandler());
235
236 MouseAdapter l2 = new MouseAdapter() {
237 public void
238 mouseClicked(MouseEvent e) { requestFocusInWindow(); }
239 };
240
241 addKeyListener(pianoRoll.getKeyListener());
242 pianoRoll.registerKeys(this);
243
244 addMouseListener(l2);
245 pianoRoll.addMouseListener(l2);
246 lDisplay.addMouseListener(l2);
247 }
248
249 public JSPianoRoll
250 getPianoRoll() { return pianoRoll; }
251
252 private void
253 updateKeyRange() {
254 int firstKey = CC.preferences().getIntProperty("midiKeyboard.firstKey");
255 int lastKey = CC.preferences().getIntProperty("midiKeyboard.lastKey");
256 pianoRoll.setKeyRange(firstKey, lastKey);
257 }
258
259 @Override public void
260 valueChanged(ListSelectionEvent e) {
261 if(e.getValueIsAdjusting()) return;
262
263 JSChannel[] chnS = CC.getMainFrame().getSelectedChannelsPane().getSelectedChannels();
264 if(chnS == null || chnS.length == 0) {
265 disconnectChannel();
266 return;
267 }
268
269 if(chnS[0].getModel() == channel) return;
270 disconnectChannel();
271 connectChannel(chnS[0].getModel());
272 }
273
274 private void
275 updateInstrumentInfo() {
276 final GetFileInstrument i = new GetFileInstrument(file, index);
277
278 i.addTaskListener(new TaskListener() {
279 public void
280 taskPerformed(TaskEvent e) {
281 if(i.doneWithErrors()) return;
282 updatePianoKeyboard(i.getResult());
283 }
284 });
285
286 CC.getTaskQueue().add(i);
287 }
288
289 private void
290 disablePianoRoll() {
291 pianoRoll.reset(true);
292 pianoRoll.setPlayingEnabled(false);
293 }
294
295 private void
296 updatePianoKeyboard(Instrument instr) {
297 // The selected channel may have changed before this update
298 // due to concurency, so also checking whether file is null
299 if(instr == null || file == null) {
300 disablePianoRoll();
301 return;
302 }
303
304 pianoRoll.setPlayingEnabled(true);
305
306 pianoRoll.setShouldRepaint(false);
307 pianoRoll.reset(true);
308 pianoRoll.setDisabled(instr.getKeyMapping(), false);
309 pianoRoll.setKeyswitches(instr.getKeyswitchMapping(), true);
310 pianoRoll.setShouldRepaint(true);
311 pianoRoll.repaint();
312 }
313
314 private void
315 connectChannel(SamplerChannelModel chn) {
316 channel = chn;
317 channel.addMidiDataListener(pianoRoll);
318 channel.addSamplerChannelListener(getHandler());
319
320 updateDisplay();
321
322 file = channel.getChannelInfo().getInstrumentFile();
323 if(file == null) {
324 disablePianoRoll();
325 return;
326 }
327 index = channel.getChannelInfo().getInstrumentIndex();
328 updateInstrumentInfo();
329 }
330
331 private void
332 disconnectChannel() {
333 if(channel != null) {
334 channel.removeMidiDataListener(pianoRoll);
335 channel.removeSamplerChannelListener(getHandler());
336 channel = null;
337 file = null;
338 index = -1;
339 disablePianoRoll();
340 }
341
342 updateDisplay();
343 }
344
345 @Override public void
346 channelAdded(SamplerChannelListEvent e) {
347 if(CC.getSamplerModel().getChannelListIsAdjusting()) return;
348 updateDisplay();
349 }
350
351 @Override public void
352 channelRemoved(SamplerChannelListEvent e) {
353 if(e.getChannelModel() == channel) {
354 disconnectChannel();
355 }
356
357 updateDisplay();
358 }
359
360 private JPanel
361 createVSeparator() {
362 PixmapPane p = new PixmapPane(Res.gfxVLine);
363 p.setAlignmentY(JPanel.TOP_ALIGNMENT);
364 p.setMinimumSize(new Dimension(2, 30));
365 p.setPreferredSize(new Dimension(2, 60));
366 p.setMaximumSize(new Dimension(2, Short.MAX_VALUE));
367 return p;
368 }
369
370 private Color color1 = new Color(0x7a7a7a);
371 private Color color2 = new Color(0x5e5e5e);
372 private Color color3 = new Color(0x2e2e2e);
373
374 @Override
375 public void
376 paintComponent(Graphics g) {
377 Graphics2D g2 = (Graphics2D)g;
378
379 Paint oldPaint = g2.getPaint();
380 Composite oldComposite = g2.getComposite();
381
382 Insets insets = this.getInsets();
383 double x1 = insets.left;
384 double y1 = insets.top;
385
386 double w = getSize().getWidth();
387 double x2 = w - insets.right - 1;
388 double h = getSize().getHeight();
389 double y2 = h - insets.bottom - 1;
390
391 FantasiaPainter.paintGradient(g2, x1, y1, x2, y2 - 10, color1, color2);
392
393 g2.setRenderingHint (
394 RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF
395 );
396
397 double y3 = y2 - 10;
398 if(y3 < 0) y3 = 0;
399
400 Rectangle2D.Double rect = new Rectangle2D.Double(x1, y3, x2 - x1 + 1, 11);
401
402 GradientPaint gr = new GradientPaint (
403 0.0f, (float)y3, color2,
404 0.0f, (float)h, color3
405 );
406
407 g2.setPaint(gr);
408 g2.fill(rect);
409
410 drawOutBorder(g2, x1, y1, x2, y2);
411
412 double prX = pianoRoll.getLocation().getX();
413 double prY = pianoRoll.getLocation().getY();
414 drawInBorder (
415 g2, prX - 2, prY - 2,
416 prX + pianoRoll.getSize().getWidth() + 1, prY + pianoRoll.getSize().getHeight() - 3
417 );
418
419 g2.setPaint(oldPaint);
420 g2.setComposite(oldComposite);
421 }
422
423 private void
424 drawOutBorder(Graphics2D g2, double x1, double y1, double x2, double y2) {
425 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f);
426 g2.setComposite(ac);
427
428 g2.setPaint(Color.WHITE);
429 Line2D.Double l = new Line2D.Double(x1, y1, x2, y1);
430 g2.draw(l);
431
432 g2.setComposite(ac.derive(0.20f));
433 l = new Line2D.Double(x1, y1 + 1, x2, y1 + 1);
434 g2.draw(l);
435
436 g2.setComposite(ac.derive(0.255f));
437
438 l = new Line2D.Double(x1, y1, x1, y2);
439 g2.draw(l);
440
441 g2.setComposite(ac.derive(0.40f));
442 g2.setPaint(Color.BLACK);
443
444 //l = new Line2D.Double(x1, y2, x2, y2);
445 //g2.draw(l);
446
447 g2.setComposite(ac.derive(0.20f));
448
449 l = new Line2D.Double(x2, y1, x2, y2);
450 g2.draw(l);
451 }
452
453 private void
454 drawInBorder(Graphics2D g2, double x1, double y1, double x2, double y2) {
455 AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.40f);
456 g2.setComposite(ac);
457
458 g2.setPaint(Color.WHITE);
459 Line2D.Double l = new Line2D.Double(x1, y2, x2, y2);
460 //g2.draw(l);
461
462 g2.setComposite(ac.derive(0.255f));
463
464 l = new Line2D.Double(x2 - 1, y1 + 1, x2 - 1, y2 + 1);
465 g2.draw(l);
466
467 g2.setComposite(ac.derive(0.13f));
468
469 l = new Line2D.Double(x2, y1, x2, y2);
470 g2.draw(l);
471
472 g2.setComposite(ac.derive(0.20f));
473 g2.setPaint(Color.BLACK);
474
475 l = new Line2D.Double(x1, y1, x2, y1);
476 g2.draw(l);
477
478 g2.setComposite(ac.derive(0.40f));
479 g2.setPaint(Color.BLACK);
480
481 l = new Line2D.Double(x1 + 1, y1 + 1, x2 - 1, y1 + 1);
482 g2.draw(l);
483
484 g2.setComposite(ac.derive(0.20f));
485
486 l = new Line2D.Double(x1, y1, x1, y2);
487 g2.draw(l);
488
489 g2.setComposite(ac.derive(0.40f));
490
491 l = new Line2D.Double(x1 + 1, y1 + 1, x1 + 1, y2 + 1);
492 g2.draw(l);
493 }
494
495 private void
496 updateDisplay() {
497 // TODO: called too often?
498 if(channel == null) {
499 lDisplay.setText(i18n.getLabel("PianoKeyboardPane.noChannel"));
500 return;
501 }
502
503 SamplerChannel sc = channel.getChannelInfo();
504 String s = CC.getMainFrame().getChannelPath(channel);
505
506 StringBuffer sb = new StringBuffer();
507 sb.append(i18n.getLabel("PianoKeyboardPane.channel", s)).append(" - ");
508
509 int status = sc.getInstrumentStatus();
510 if(status >= 0 && status < 100) {
511 sb.append(i18n.getLabel("ChannelScreen.loadingInstrument", status));
512 } else if(status == -1) {
513 sb.append(i18n.getLabel("PianoKeyboardPane.noInstrument"));
514 } else if(status < -1) {
515 sb.append(i18n.getLabel("ChannelScreen.errorLoadingInstrument"));
516 } else {
517 if(sc.getInstrumentName() != null) sb.append(sc.getInstrumentName());
518 }
519
520 lDisplay.setText(sb.toString());
521 }
522
523
524 class KeyRangePropsPane extends JPanel {
525 private final JButton btnIncrease =
526 new PixmapButton(pianoRoll.actionIncreaseKeyNumber, Res.gfxBtnIncrease);
527
528 private final JButton btnDecrease =
529 new PixmapButton(pianoRoll.actionDecreaseKeyNumber, Res.gfxBtnDecrease);
530
531 private final JButton btnScrollLeft =
532 new PixmapButton(pianoRoll.actionScrollLeft, Res.gfxBtnScrollLeft);
533
534 private final JButton btnScrollRight =
535 new PixmapButton(pianoRoll.actionScrollRight, Res.gfxBtnScrollRight);
536
537 KeyRangePropsPane() {
538 setOpaque(false);
539 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
540
541 btnIncrease.setFocusable(false);
542 btnDecrease.setFocusable(false);
543 btnScrollLeft.setFocusable(false);
544 btnScrollRight.setFocusable(false);
545
546
547 btnDecrease.setPressedIcon(Res.gfxBtnDecreaseRO);
548 add(btnDecrease);
549
550 btnIncrease.setPressedIcon(Res.gfxBtnIncreaseRO);
551 add(btnIncrease);
552
553 add(Box.createRigidArea(new Dimension(6, 0)));
554
555 btnScrollLeft.setPressedIcon(Res.gfxBtnScrollLeftRO);
556 add(btnScrollLeft);
557
558 btnScrollRight.setPressedIcon(Res.gfxBtnScrollRightRO);
559 add(btnScrollRight);
560 }
561 }
562
563
564 public static class PowerButton extends PixmapToggleButton implements ActionListener {
565 PowerButton() {
566 this(Res.gfxPowerOff, Res.gfxPowerOn);
567 }
568
569 PowerButton(ImageIcon defaultIcon, ImageIcon selectedIcon) {
570 super(defaultIcon, selectedIcon);
571
572 setSelected(true);
573 addActionListener(this);
574 }
575
576 @Override
577 public void
578 actionPerformed(ActionEvent e) {
579 boolean b = isSelected();
580 MainFrame frm = (MainFrame)CC.getMainFrame();
581 if(frm == null) return;
582 frm.setMidiKeyboardVisible(b);
583 }
584
585 @Override
586 public boolean
587 contains(int x, int y) { return (x - 11)*(x - 11) + (y - 11)*(y - 11) < 71; }
588 }
589
590 private final Handler handler = new Handler();
591
592 private Handler
593 getHandler() { return handler; }
594
595 private class Handler extends SamplerChannelAdapter implements FocusListener, MidiDataListener {
596 @Override
597 public void
598 midiDataArrived(MidiDataEvent e) {
599 if(channel == null) return;
600 channel.sendBackendMidiData(e);
601 }
602
603 @Override
604 public void
605 channelChanged(SamplerChannelEvent e) {
606 updateDisplay();
607
608 String newFile = channel.getChannelInfo().getInstrumentFile();
609 int newIndex = channel.getChannelInfo().getInstrumentIndex();
610
611 if(channel.getChannelInfo().getInstrumentStatus() != 100) {
612 //don't use disablePianoRoll because of unnecessary repainting
613 pianoRoll.setAllKeysPressed(false);
614 pianoRoll.removeAllKeyswitches();
615 pianoRoll.setAllKeysDisabled(true);
616 pianoRoll.setPlayingEnabled(false);
617 return;
618 }
619
620 if(newFile == null) {
621 if(file != null) disablePianoRoll();
622 file = null;
623 index = -1;
624 return;
625 }
626
627 if(newFile.equals(file) && newIndex == index) return;
628
629 file = newFile;
630 index = newIndex;
631 updateInstrumentInfo();
632 }
633
634 @Override
635 public void
636 focusGained(FocusEvent e) {
637
638 }
639
640 @Override
641 public void
642 focusLost(FocusEvent e) {
643
644 }
645 }
646 }

  ViewVC Help
Powered by ViewVC