/[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 1776 - (show annotations) (download)
Thu Sep 11 18:48:36 2008 UTC (15 years, 7 months ago) by iliev
File size: 6630 byte(s)
* Implemented virtual MIDI keyboard

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.BorderLayout;
26 import java.awt.Insets;
27
28 import java.beans.PropertyChangeEvent;
29 import java.beans.PropertyChangeListener;
30
31 import javax.swing.BorderFactory;
32
33 import javax.swing.event.ListSelectionEvent;
34 import javax.swing.event.ListSelectionListener;
35
36 import net.sf.juife.event.TaskEvent;
37 import net.sf.juife.event.TaskListener;
38
39 import org.jsampler.CC;
40 import org.jsampler.SamplerChannelModel;
41
42 import org.jsampler.event.SamplerChannelAdapter;
43 import org.jsampler.event.SamplerChannelEvent;
44 import org.jsampler.event.SamplerChannelListListener;
45 import org.jsampler.event.SamplerChannelListEvent;
46
47 import org.jsampler.view.JSChannel;
48 import org.jsampler.view.std.JSPianoRoll;
49
50 import org.linuxsampler.lscp.Instrument;
51
52 import org.linuxsampler.lscp.event.MidiDataEvent;
53 import org.linuxsampler.lscp.event.MidiDataListener;
54
55 import static org.jsampler.task.Global.GetFileInstrument;
56
57 /**
58 *
59 * @author Grigor Iliev
60 */
61 public class PianoKeyboardPane extends PixmapPane
62 implements ListSelectionListener, SamplerChannelListListener {
63
64 private final JSPianoRoll pianoRoll = new JSPianoRoll();
65 private SamplerChannelModel channel = null;
66
67 private String file = null;
68 private int index = -1;
69
70 public
71 PianoKeyboardPane() {
72 super(Res.gfxDeviceBg);
73 setPixmapInsets(new java.awt.Insets(1, 1, 1, 1));
74 setOpaque(false);
75
76 setLayout(new BorderLayout());
77 setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
78
79 PixmapPane p2 = new PixmapPane(Res.gfxBorder);
80 p2.setPixmapInsets(new Insets(1, 1, 1, 1));
81 p2.setLayout(new BorderLayout());
82 p2.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1));
83 p2.add(pianoRoll);
84 add(p2);
85
86 pianoRoll.setOpaque(false);
87 disablePianoRoll();
88 add(p2);
89
90 CC.getSamplerModel().addSamplerChannelListListener(this);
91 pianoRoll.addMidiDataListener(getHandler());
92
93 updateKeyRange();
94
95 PropertyChangeListener l = new PropertyChangeListener() {
96 public void
97 propertyChange(PropertyChangeEvent e) {
98 updateKeyRange();
99 }
100 };
101
102 CC.preferences().addPropertyChangeListener("midiKeyboard.firstKey", l);
103 CC.preferences().addPropertyChangeListener("midiKeyboard.lastKey", l);
104 }
105
106 public JSPianoRoll
107 getPianoRoll() { return pianoRoll; }
108
109 private void
110 updateKeyRange() {
111 int firstKey = CC.preferences().getIntProperty("midiKeyboard.firstKey");
112 int lastKey = CC.preferences().getIntProperty("midiKeyboard.lastKey");
113 pianoRoll.setKeyRange(firstKey, lastKey);
114 pianoRoll.setAllKeysDisabled(true);
115 updateInstrumentInfo();
116 }
117
118 @Override public void
119 valueChanged(ListSelectionEvent e) {
120 if(e.getValueIsAdjusting()) return;
121
122 JSChannel[] chnS = CC.getMainFrame().getChannelsPane(0).getSelectedChannels();
123 if(chnS == null || chnS.length == 0) {
124 disconnectChannel();
125 return;
126 }
127
128 if(chnS[0].getModel() == channel) return;
129 disconnectChannel();
130 connectChannel(chnS[0].getModel());
131 }
132
133 private void
134 updateInstrumentInfo() {
135 final GetFileInstrument i = new GetFileInstrument(file, index);
136
137 i.addTaskListener(new TaskListener() {
138 public void
139 taskPerformed(TaskEvent e) {
140 if(i.doneWithErrors()) return;
141 updatePianoKeyboard(i.getResult());
142 }
143 });
144
145 CC.getTaskQueue().add(i);
146 }
147
148 private void
149 disablePianoRoll() {
150 pianoRoll.reset(true);
151 pianoRoll.setPlayingEnabled(false);
152 }
153
154 private void
155 updatePianoKeyboard(Instrument instr) {
156 // The selected channel may have changed before this update
157 // due to concurency, so also checking whether file is null
158 if(instr == null || file == null) {
159 disablePianoRoll();
160 return;
161 }
162
163 pianoRoll.setPlayingEnabled(true);
164
165 pianoRoll.setShouldRepaint(false);
166 pianoRoll.reset(true);
167 pianoRoll.setDisabled(instr.getKeyMapping(), false);
168 pianoRoll.setKeyswitches(instr.getKeyswitchMapping(), true);
169 pianoRoll.setShouldRepaint(true);
170 pianoRoll.repaint();
171 }
172
173 private void
174 connectChannel(SamplerChannelModel chn) {
175 channel = chn;
176 channel.addMidiDataListener(pianoRoll);
177 channel.addSamplerChannelListener(getHandler());
178
179 file = channel.getChannelInfo().getInstrumentFile();
180 if(file == null) {
181 disablePianoRoll();
182 return;
183 }
184 index = channel.getChannelInfo().getInstrumentIndex();
185 updateInstrumentInfo();
186 }
187
188 private void
189 disconnectChannel() {
190 if(channel != null) {
191 channel.removeMidiDataListener(pianoRoll);
192 channel.removeSamplerChannelListener(getHandler());
193 channel = null;
194 file = null;
195 index = -1;
196 disablePianoRoll();
197 }
198 }
199
200 @Override public void
201 channelAdded(SamplerChannelListEvent e) { }
202
203 @Override public void
204 channelRemoved(SamplerChannelListEvent e) {
205 if(e.getChannelModel() == channel) {
206 disconnectChannel();
207 }
208 }
209
210 private final Handler handler = new Handler();
211
212 private Handler
213 getHandler() { return handler; }
214
215 private class Handler extends SamplerChannelAdapter implements MidiDataListener {
216 public void
217 midiDataArrived(MidiDataEvent e) {
218 if(channel == null) return;
219 channel.sendBackendMidiData(e);
220 }
221
222 public void
223 channelChanged(SamplerChannelEvent e) {
224 String newFile = channel.getChannelInfo().getInstrumentFile();
225 int newIndex = channel.getChannelInfo().getInstrumentIndex();
226
227 if(channel.getChannelInfo().getInstrumentStatus() != 100) {
228 //don't use disablePianoRoll because of unnecessary repainting
229 pianoRoll.setAllKeysPressed(false);
230 pianoRoll.removeAllKeyswitches();
231 pianoRoll.setAllKeysDisabled(true);
232 pianoRoll.setPlayingEnabled(false);
233 return;
234 }
235
236 if(newFile == null) {
237 if(file != null) disablePianoRoll();
238 file = null;
239 index = -1;
240 return;
241 }
242
243 if(newFile.equals(file) && newIndex == index) return;
244
245 file = newFile;
246 index = newIndex;
247 updateInstrumentInfo();
248 }
249 }
250 }

  ViewVC Help
Powered by ViewVC