/[svn]/jsampler/trunk/src/org/jsampler/DefaultSamplerChannelModel.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/DefaultSamplerChannelModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 911 - (show annotations) (download)
Mon Aug 7 18:25:58 2006 UTC (17 years, 8 months ago) by iliev
File size: 14445 byte(s)
updating to JSampler 0.3a

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005 Grigor Kirilov Iliev
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 javax.swing.SwingUtilities;
28
29 import net.sf.juife.Task;
30 import net.sf.juife.event.TaskEvent;
31 import net.sf.juife.event.TaskListener;
32
33 import org.jsampler.event.SamplerChannelEvent;
34 import org.jsampler.event.SamplerChannelListener;
35
36 import org.jsampler.task.DuplicateChannels;
37 import org.jsampler.task.LoadEngine;
38 import org.jsampler.task.LoadInstrument;
39 import org.jsampler.task.ResetChannel;
40 import org.jsampler.task.SetChannelAudioOutputDevice;
41 import org.jsampler.task.SetChannelMidiInputChannel;
42 import org.jsampler.task.SetChannelMidiInputDevice;
43 import org.jsampler.task.SetChannelMidiInputPort;
44 import org.jsampler.task.SetChannelVolume;
45 import org.jsampler.task.SetMuteChannel;
46 import org.jsampler.task.SetSoloChannel;
47
48 import org.linuxsampler.lscp.SamplerChannel;
49
50
51 /**
52 * This class provides default implementation of the <code>SamplerChannelModel</code> interface.
53 * @author Grigor Iliev
54 */
55 public class DefaultSamplerChannelModel implements SamplerChannelModel {
56 private SamplerChannel channel;
57 private int streamCount = 0;
58 private int voiceCount = 0;
59
60 private final Vector<SamplerChannelListener> listeners =
61 new Vector<SamplerChannelListener>();
62
63 /**
64 * Creates a new instance of <code>DefaultSamplerChannelModel</code> using the
65 * specified channel settings.
66 * @param channel A non-null <code>SamplerChannel</code> instance containing the current
67 * settings of the channel which will be represented by this sampler channel model.
68 * @throws IllegalArgumentException If <code>channel</code> is <code>null</code>.
69 */
70 public DefaultSamplerChannelModel(SamplerChannel channel) {
71 if(channel == null) throw new IllegalArgumentException("channel must be non null");
72 this.channel = channel;
73 }
74
75 /**
76 * Registers the specified listener for receiving event messages.
77 * @param l The <code>SamplerChannelListener</code> to register.
78 */
79 public void
80 addSamplerChannelListener(SamplerChannelListener l) { listeners.add(l); }
81
82 /**
83 * Removes the specified listener.
84 * @param l The <code>SamplerChannelListener</code> to remove.
85 */
86 public void
87 removeSamplerChannelListener(SamplerChannelListener l) { listeners.remove(l); }
88
89 /**
90 * Gets the sampler channel number.
91 * @return The sampler channel number or -1 if the sampler channel number is not set.
92 */
93 public int
94 getChannelID() { return channel == null ? -1 : channel.getChannelID(); }
95
96 /**
97 * Gets the current settings of the sampler channel.
98 * @return <code>SamplerChannel</code> instance containing
99 * the current settings of the sampler channel.
100 */
101 public SamplerChannel
102 getChannelInfo() { return channel; }
103
104 /**
105 * Sets the current settings of the sampler channel.
106 * Note that this method does not changes the channel settings on
107 * the back-end. It is invoked to update them when the back-end
108 * notifies that the channel settings are changed.
109 * @param channel A <code>SamplerChannel</code> instance containing
110 * the new settings for this sampler channel.
111 * @throws IllegalArgumentException If <code>channel</code> is <code>null</code>.
112 */
113 public void
114 setChannelInfo(SamplerChannel channel) {
115 if(channel == null) throw new IllegalArgumentException("channel must be non null");
116 if(this.channel == channel) return;
117
118 this.channel = channel;
119 fireSamplerChannelChanged();
120 }
121
122 /**
123 * Gets the number of active disk streams.
124 * @return The number of active disk streams.
125 */
126 public int
127 getStreamCount() { return streamCount; }
128
129 /**
130 * Sets the number of active disk streams.
131 * @param count The new number of active disk streams.
132 */
133 public void
134 setStreamCount(int count) {
135 if(streamCount == count) return;
136
137 streamCount = count;
138 fireStreamCountChanged();
139 }
140
141 /**
142 * Gets the number of active voices.
143 * @return The number of active voices.
144 */
145 public int
146 getVoiceCount() { return voiceCount; }
147
148 /**
149 * Sets the number of active voices.
150 * @param count The new number of active voices.
151 */
152 public void
153 setVoiceCount(int count) {
154 if(voiceCount == count) return;
155
156 voiceCount = count;
157 fireVoiceCountChanged();
158 }
159
160 /**
161 * Sets the sampler engine type to be used.
162 * @param engine The name of the engine type to be used.
163 */
164 public void
165 setEngineType(String engine) {
166 final LoadEngine loadEngine = new LoadEngine(engine, getChannelID());
167 final SamplerChannelEvent event = new SamplerChannelEvent(this);
168
169 loadEngine.addTaskListener(new TaskListener() {
170 public void
171 taskPerformed(TaskEvent e) {
172 /*
173 * Because with the invokation of the method the task is considered
174 * to be done, if the task fails, we must notify for a channel
175 * changes. This should be done to revert the old channel settings.
176 */
177 if(loadEngine.doneWithErrors()) fireSamplerChannelChanged(event);
178 }
179 });
180 CC.getTaskQueue().add(loadEngine);
181
182 // We leave this event to be notified by the LinuxSampler notification system.
183 }
184
185 /**
186 * Sets the mute mode of the channel.
187 * @param mute Specifies the mute mode. If <code>true</code> the channel is muted, else
188 * the channel is unmuted.
189 */
190 public void
191 setMute(boolean mute) {
192 final SetMuteChannel smc = new SetMuteChannel(getChannelID(), mute);
193 final SamplerChannelEvent event = new SamplerChannelEvent(this);
194
195 smc.addTaskListener(new TaskListener() {
196 public void
197 taskPerformed(TaskEvent e) {
198 /*
199 * Because with the invokation of the method the task is considered
200 * to be done, if the task fails, we must notify for a channel
201 * changes. This should be done to revert the old channel settings.
202 */
203 if(smc.doneWithErrors()) fireSamplerChannelChanged(event);
204 }
205 });
206 CC.getTaskQueue().add(smc);
207
208 // We leave this event to be notified by the LinuxSampler notification system.
209 }
210
211 /**
212 * Sets the solo mode of the channel.
213 * @param solo Specifies the solo mode. If <code>true</code> the channel is soloed, else
214 * the channel is unsoloed.
215 */
216 public void
217 setSolo(boolean solo) {
218 final SetSoloChannel ssc = new SetSoloChannel(getChannelID(), solo);
219 final SamplerChannelEvent event = new SamplerChannelEvent(this);
220
221 ssc.addTaskListener(new TaskListener() {
222 public void
223 taskPerformed(TaskEvent e) {
224 /*
225 * Because with the invokation of the method the task is considered
226 * to be done, if the task fails, we must notify for a channel
227 * changes. This should be done to revert the old channel settings.
228 */
229 if(ssc.doneWithErrors()) fireSamplerChannelChanged(event);
230 }
231 });
232 CC.getTaskQueue().add(ssc);
233
234 // We leave this event to be notified by the LinuxSampler notification system.
235 }
236
237 /**
238 * Sets the channel volume.
239 * @param volume Specifies the new volume value.
240 */
241 public void
242 setVolume(float volume) {
243 final SetChannelVolume scv = new SetChannelVolume(getChannelID(), volume);
244 final SamplerChannelEvent event = new SamplerChannelEvent(this);
245
246 scv.addTaskListener(new TaskListener() {
247 public void
248 taskPerformed(TaskEvent e) {
249 /*
250 * Because with the invokation of the method the task is considered
251 * to be done, if the task fails, we must notify for a channel
252 * changes. This should be done to revert the old channel settings.
253 */
254 if(scv.doneWithErrors()) fireSamplerChannelChanged(event);
255 }
256 });
257 CC.getTaskQueue().add(scv);
258
259 // We leave this event to be notified by the LinuxSampler notification system.
260 }
261
262 /**
263 * Sets the MIDI input device of the channel represented by this model.
264 * @param deviceID Specifies the numerical ID of the MIDI input device to be set.
265 */
266 public void
267 setMidiInputDevice(int deviceID) {
268 final Task scmid = new SetChannelMidiInputDevice(getChannelID(), deviceID);
269 final SamplerChannelEvent event = new SamplerChannelEvent(this);
270
271 scmid.addTaskListener(new TaskListener() {
272 public void
273 taskPerformed(TaskEvent e) {
274 /*
275 * Because with the invokation of the method the task is considered
276 * to be done, if the task fails, we must notify for a channel
277 * changes. This should be done to revert the old channel settings.
278 */
279 if(scmid.doneWithErrors()) fireSamplerChannelChanged(event);
280 }
281 });
282 CC.getTaskQueue().add(scmid);
283
284 // We leave this event to be notified by the LinuxSampler notification system.
285 }
286
287 /**
288 * Sets the MIDI input port of the channel represented by this model.
289 * @param port Specifies the number of the MIDI input port.
290 */
291 public void
292 setMidiInputPort(int port) {
293 final Task scmip = new SetChannelMidiInputPort(getChannelID(), port);
294 final SamplerChannelEvent event = new SamplerChannelEvent(this);
295
296 scmip.addTaskListener(new TaskListener() {
297 public void
298 taskPerformed(TaskEvent e) {
299 /*
300 * Because with the invokation of the method the task is considered
301 * to be done, if the task fails, we must notify for a channel
302 * changes. This should be done to revert the old channel settings.
303 */
304 if(scmip.doneWithErrors()) fireSamplerChannelChanged(event);
305 }
306 });
307 CC.getTaskQueue().add(scmip);
308
309 // We leave this event to be notified by the LinuxSampler notification system.
310 }
311
312 /**
313 * Sets the MIDI channel that the channel represented by this model should listen to.
314 * @param channel Specifies the MIDI channel that the channel
315 * represented by this model should listen to.
316 */
317 public void
318 setMidiInputChannel(int channel) {
319 final Task scmic = new SetChannelMidiInputChannel(getChannelID(), channel);
320 final SamplerChannelEvent event = new SamplerChannelEvent(this);
321
322 scmic.addTaskListener(new TaskListener() {
323 public void
324 taskPerformed(TaskEvent e) {
325 /*
326 * Because with the invokation of the method the task is considered
327 * to be done, if the task fails, we must notify for a channel
328 * changes. This should be done to revert the old channel settings.
329 */
330 if(scmic.doneWithErrors()) fireSamplerChannelChanged(event);
331 }
332 });
333 CC.getTaskQueue().add(scmic);
334
335 // We leave this event to be notified by the LinuxSampler notification system.
336 }
337
338 /**
339 * Sets the audio output device of the channel represented by this model.
340 * @param deviceID Specifies the numerical ID of the audio output device to be set.
341 */
342 public void
343 setAudioOutputDevice(int deviceID) {
344 final Task scaod = new SetChannelAudioOutputDevice(getChannelID(), deviceID);
345 final SamplerChannelEvent event = new SamplerChannelEvent(this);
346
347 scaod.addTaskListener(new TaskListener() {
348 public void
349 taskPerformed(TaskEvent e) {
350 /*
351 * Because with the invokation of the method the task is considered
352 * to be done, if the task fails, we must notify for a channel
353 * changes. This should be done to revert the old channel settings.
354 */
355 if(scaod.doneWithErrors()) fireSamplerChannelChanged(event);
356 }
357 });
358 CC.getTaskQueue().add(scaod);
359
360 // We leave this event to be notified by the LinuxSampler notification system.
361 }
362
363 /**
364 * Loads and assigns the specified instrument
365 * to the sampler channel represented by this model.
366 * @param filename The file name of the instrument to be loaded.
367 * @param InstrIndex The index of the instrument in the instrument file to be loaded.
368 */
369 public void
370 loadInstrument(String filename, int InstrIndex) {
371 final Task li = new LoadInstrument(filename, InstrIndex, getChannelID());
372 CC.getTaskQueue().add(li);
373
374 // We leave this event to be notified by the LinuxSampler notification system.
375 }
376
377 /** Resets the channel. */
378 public void
379 resetChannel() {
380 CC.getTaskQueue().add(new ResetChannel(getChannelID()));
381
382 // We leave this event to be notified by the LinuxSampler notification system.
383 }
384
385 /** Duplicates the channel. */
386 public void
387 duplicateChannel() {
388 CC.getTaskQueue().add(new DuplicateChannels(getChannelInfo()));
389 }
390
391 /** Notifies listeners that the sampler channel settings has changed. */
392 protected void
393 fireSamplerChannelChanged() {
394 final SamplerChannelEvent e = new SamplerChannelEvent(this);
395
396 SwingUtilities.invokeLater(new Runnable() {
397 public void
398 run() { fireSamplerChannelChanged(e); }
399 });
400 }
401
402 /**
403 * Notifies listeners that the sampler channel settings has changed.
404 * This method should be invoked from the event-dispatching thread.
405 */
406 protected void
407 fireSamplerChannelChanged(SamplerChannelEvent e) {
408 for(SamplerChannelListener l : listeners) l.channelChanged(e);
409 }
410
411 /** Notifies listeners that the number of active disk streams has changed. */
412 protected void
413 fireStreamCountChanged() {
414 final SamplerChannelEvent e = new SamplerChannelEvent(this);
415
416 SwingUtilities.invokeLater(new Runnable() {
417 public void
418 run() { fireStreamCountChanged(e); }
419 });
420 }
421
422 /**
423 * Notifies listeners that the number of active disk streams has changed.
424 * This method should be invoked from the event-dispatching thread.
425 */
426 protected void
427 fireStreamCountChanged(SamplerChannelEvent e) {
428 for(SamplerChannelListener l : listeners) l.streamCountChanged(e);
429 }
430
431 /** Notifies listeners that the number of active voices has changed. */
432 protected void
433 fireVoiceCountChanged() {
434 final SamplerChannelEvent e = new SamplerChannelEvent(this);
435
436 SwingUtilities.invokeLater(new Runnable() {
437 public void
438 run() { fireVoiceCountChanged(e); }
439 });
440 }
441
442 /**
443 * Notifies listeners that the number of active voices has changed.
444 * This method should be invoked from the event-dispatching thread.
445 */
446 protected void
447 fireVoiceCountChanged(SamplerChannelEvent e) {
448 for(SamplerChannelListener l : listeners) l.voiceCountChanged(e);
449 }
450 }

  ViewVC Help
Powered by ViewVC