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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 787 - (hide annotations) (download)
Mon Oct 10 16:03:12 2005 UTC (18 years, 6 months ago) by iliev
File size: 13739 byte(s)
* The first alpha-release of JSampler

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

  ViewVC Help
Powered by ViewVC