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; |
24 |
|
25 |
import java.util.Vector; |
26 |
|
27 |
import net.sf.juife.PDUtils; |
28 |
|
29 |
import org.jsampler.event.MidiDeviceEvent; |
30 |
import org.jsampler.event.MidiDeviceListener; |
31 |
|
32 |
import org.jsampler.task.Midi; |
33 |
|
34 |
import org.linuxsampler.lscp.MidiInputDevice; |
35 |
import org.linuxsampler.lscp.Parameter; |
36 |
|
37 |
|
38 |
/** |
39 |
* The Default implementation of the <code>MidiDeviceModel</code> interface. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class DefaultMidiDeviceModel implements MidiDeviceModel { |
43 |
private MidiInputDevice midiDevice; |
44 |
|
45 |
private final Vector<MidiDeviceListener> listeners = new Vector<MidiDeviceListener>(); |
46 |
|
47 |
/** |
48 |
* Creates a new instance of <code>DefaultMidiDeviceModel</code> using the |
49 |
* specified non-null MIDI device. |
50 |
* @param midiDevice A <code>MidiInputDevice</code> instance providing the current |
51 |
* settings of the MIDI device which will be represented by this model. |
52 |
* @throws IllegalArgumentException If <code>midiDevice</code> is <code>null</code>. |
53 |
*/ |
54 |
public |
55 |
DefaultMidiDeviceModel(MidiInputDevice midiDevice) { |
56 |
if(midiDevice == null) |
57 |
throw new IllegalArgumentException("midiDevice must be non null"); |
58 |
|
59 |
this.midiDevice = midiDevice; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Registers the specified listener to be notified when |
64 |
* the settings of the MIDI device are changed. |
65 |
* @param l The <code>MidiDeviceListener</code> to register. |
66 |
*/ |
67 |
@Override |
68 |
public void |
69 |
addMidiDeviceListener(MidiDeviceListener l) { listeners.add(l); } |
70 |
|
71 |
/** |
72 |
* Removes the specified listener. |
73 |
* @param l The <code>MidiDeviceListener</code> to remove. |
74 |
*/ |
75 |
@Override |
76 |
public void |
77 |
removeMidiDeviceListener(MidiDeviceListener l) { listeners.remove(l); } |
78 |
|
79 |
/** |
80 |
* Gets the numerical ID of this MIDI device. |
81 |
* @return The numerical ID of this MIDI device or |
82 |
* -1 if the device number is not set. |
83 |
*/ |
84 |
@Override |
85 |
public int |
86 |
getDeviceId() { return midiDevice.getDeviceId(); } |
87 |
|
88 |
/** |
89 |
* Gets the current settings of the MIDI device represented by this model. |
90 |
* @return <code>MidiInputDevice</code> instance providing |
91 |
* the current settings of the MIDI device represented by this model. |
92 |
*/ |
93 |
@Override |
94 |
public MidiInputDevice |
95 |
getDeviceInfo() { return midiDevice; } |
96 |
|
97 |
/** |
98 |
* Updates the settings of the MIDI device represented by this model. |
99 |
* @param device The new MIDI device settings. |
100 |
*/ |
101 |
@Override |
102 |
public void |
103 |
setDeviceInfo(MidiInputDevice device) { |
104 |
midiDevice = device; |
105 |
fireSettingsChanged(); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Sets whether the MIDI device is enabled or disabled. |
110 |
* @param active If <code>true</code> the MIDI device is enabled, |
111 |
* else the device is disabled. |
112 |
*/ |
113 |
@Override |
114 |
public void |
115 |
setActive(boolean active) { |
116 |
if(active == getDeviceInfo().isActive()) return; |
117 |
|
118 |
midiDevice.setActive(active); |
119 |
fireSettingsChanged(); |
120 |
} |
121 |
|
122 |
/** |
123 |
* Determines whether the MIDI device is active. |
124 |
* @return <code>true</code> if the device is enabled and <code>false</code> otherwise. |
125 |
*/ |
126 |
@Override |
127 |
public boolean |
128 |
isActive() { return midiDevice.isActive(); } |
129 |
|
130 |
/** |
131 |
* Schedules a new task for enabling/disabling the MIDI device. |
132 |
* @param active If <code>true</code> the MIDI device is enabled, |
133 |
* else the device is disabled. |
134 |
*/ |
135 |
@Override |
136 |
public void |
137 |
setBackendActive(boolean active) { |
138 |
CC.getTaskQueue().add(new Midi.EnableDevice(getDeviceId(), active)); |
139 |
} |
140 |
|
141 |
/** |
142 |
* Schedules a new task for altering |
143 |
* a specific setting of the MIDI input device. |
144 |
* @param prm The parameter to be set. |
145 |
*/ |
146 |
@Override |
147 |
public void |
148 |
setBackendDeviceParameter(Parameter prm) { |
149 |
CC.getTaskQueue().add(new Midi.SetDeviceParameter(getDeviceId(), prm)); |
150 |
} |
151 |
|
152 |
/** |
153 |
* Schedules a new task for changing the port number of the MIDI device. |
154 |
* @param ports The new number of ports. |
155 |
*/ |
156 |
@Override |
157 |
public void |
158 |
setBackendPortCount(int ports) { |
159 |
CC.getTaskQueue().add(new Midi.SetPortCount(getDeviceId(), ports)); |
160 |
} |
161 |
|
162 |
/** |
163 |
* Schedules a new task for altering a specific |
164 |
* setting of the specified MIDI input port. |
165 |
* @param port The port number. |
166 |
* @param prm The parameter to be set. |
167 |
*/ |
168 |
@Override |
169 |
public void |
170 |
setBackendPortParameter(int port, Parameter prm) { |
171 |
CC.getTaskQueue().add(new Midi.SetPortParameter(getDeviceId(), port, prm)); |
172 |
} |
173 |
|
174 |
/** |
175 |
* Notifies listeners that the settings of the MIDI device are changed. |
176 |
*/ |
177 |
private void |
178 |
fireSettingsChanged() { |
179 |
PDUtils.runOnUiThread(new Runnable() { |
180 |
public void |
181 |
run() { |
182 |
MidiDeviceModel model = DefaultMidiDeviceModel.this; |
183 |
fireSettingsChanged(new MidiDeviceEvent(model, model)); |
184 |
} |
185 |
}); |
186 |
} |
187 |
|
188 |
/** |
189 |
* Notifies listeners that the settings of the MIDI device are changed. |
190 |
* This method should be invoked from the event-dispatching thread. |
191 |
*/ |
192 |
private void |
193 |
fireSettingsChanged(final MidiDeviceEvent e) { |
194 |
CC.getSamplerModel().setModified(true); |
195 |
for(MidiDeviceListener l : listeners) l.settingsChanged(e); |
196 |
} |
197 |
} |