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

Annotation of /jsampler/trunk/src/org/jsampler/DefaultSamplerModel.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: 20750 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 java.util.logging.Level;
28    
29     import javax.swing.SwingUtilities;
30    
31     import javax.swing.event.EventListenerList;
32    
33     import org.jsampler.event.AudioDeviceListEvent;
34     import org.jsampler.event.AudioDeviceListListener;
35     import org.jsampler.event.MidiDeviceListEvent;
36     import org.jsampler.event.MidiDeviceListListener;
37     import org.jsampler.event.SamplerChannelListEvent;
38     import org.jsampler.event.SamplerChannelListListener;
39     import org.jsampler.event.SamplerEvent;
40     import org.jsampler.event.SamplerListener;
41    
42     import org.jsampler.task.AddChannel;
43    
44     import org.linuxsampler.lscp.*;
45    
46    
47     /**
48     *
49     * @author Grigor Iliev
50     */
51     public class DefaultSamplerModel implements SamplerModel {
52     private ServerInfo serverInfo = null;
53     private AudioOutputDriver[] aoDrvS = null;
54     private MidiInputDriver[] miDrvS = null;
55     private SamplerEngine[] engines = null;
56    
57     private int totalVoiceCount = 0;
58     private int totalVoiceCountMax = 0;
59    
60     private final Vector<SamplerChannelModel> channelModels = new Vector<SamplerChannelModel>();
61     private final Vector<AudioDeviceModel> audioDeviceModels = new Vector<AudioDeviceModel>();
62     private final Vector<MidiDeviceModel> midiDeviceModels = new Vector<MidiDeviceModel>();
63    
64     private final Vector<SamplerListener> listeners = new Vector<SamplerListener>();
65     private final EventListenerList listenerList = new EventListenerList();
66    
67    
68     /** Creates a new instance of DefaultSamplerModel */
69     public
70     DefaultSamplerModel() {
71     }
72    
73     /**
74     * Registers the specified listener for receiving event messages.
75     * @param l The <code>SamplerListener</code> to register.
76     */
77     public void
78     addSamplerListener(SamplerListener l) { listeners.add(l); }
79    
80     /**
81     * Removes the specified listener.
82     * @param l The <code>SamplerListener</code> to remove.
83     */
84     public void
85     removeSamplerListener(SamplerListener l) { listeners.remove(l); }
86    
87     /**
88     * Registers the specified listener for receiving event messages.
89     * @param listener The <code>AudioDeviceListListener</code> to register.
90     */
91     public void
92     addAudioDeviceListListener(AudioDeviceListListener listener) {
93     listenerList.add(AudioDeviceListListener.class, listener);
94     }
95    
96     /**
97     * Removes the specified listener.
98     * @param listener The <code>AudioDeviceListListener</code> to remove.
99     */
100     public void
101     removeAudioDeviceListListener(AudioDeviceListListener listener) {
102     listenerList.remove(AudioDeviceListListener.class, listener);
103     }
104    
105     /**
106     * Registers the specified listener for receiving event messages.
107     * @param listener The <code>MidiDeviceListListener</code> to register.
108     */
109     public void
110     addMidiDeviceListListener(MidiDeviceListListener listener) {
111     listenerList.add(MidiDeviceListListener.class, listener);
112     }
113    
114     /**
115     * Removes the specified listener.
116     * @param listener The <code>MidiDeviceListListener</code> to remove.
117     */
118     public void
119     removeMidiDeviceListListener(MidiDeviceListListener listener) {
120     listenerList.remove(MidiDeviceListListener.class, listener);
121     }
122    
123     /**
124     * Registers the specified listener for receiving event messages.
125     * @param listener The <code>SamplerChannelListListener</code> to register.
126     */
127     public void
128     addSamplerChannelListListener(SamplerChannelListListener listener) {
129     listenerList.add(SamplerChannelListListener.class, listener);
130     }
131    
132     /**
133     * Removes the specified listener.
134     * @param listener The <code>SamplerChannelListListener</code> to remove.
135     */
136     public void
137     removeSamplerChannelListListener(SamplerChannelListListener listener) {
138     listenerList.remove(SamplerChannelListListener.class, listener);
139     }
140    
141     /**
142     * Gets information about the LinuxSampler instance the front-end is connected to.
143     *
144     * @return <code>ServerInfo</code> instance containing
145     * information about the LinuxSampler instance the front-end is connected to.
146     */
147     public ServerInfo
148     getServerInfo() { return serverInfo; }
149    
150     /**
151     * Sets information about the LinuxSampler instance the front-end is connected to.
152     *
153     * @param serverInfo <code>ServerInfo</code> instance containing
154     * information about the LinuxSampler instance the front-end is connected to.
155     */
156     public void
157     setServerInfo(ServerInfo serverInfo) { this.serverInfo = serverInfo; }
158    
159     /**
160     * Gets all audio output drivers currently available for the LinuxSampler instance.
161     *
162     * @return <code>AudioOutputDriver</code> array containing all audio output drivers
163     * currently available for the LinuxSampler instance.
164     */
165     public AudioOutputDriver[]
166     getAudioOutputDrivers() { return aoDrvS; }
167    
168     /**
169     * Sets the currently available audio output drivers for the LinuxSampler instance.
170     *
171     * @param drivers <code>AudioOutputDriver</code> array containing all audio output drivers
172     * currently available for the LinuxSampler instance.
173     */
174     public void
175     setAudioOutputDrivers(AudioOutputDriver[] drivers) { aoDrvS = drivers; }
176    
177     /**
178     * Gets the model of the audio device with ID <code>deviceID</code>.
179     * @param deviceID The ID of the audio device whose model should be obtained.
180     * @return The model of the specified audio device or <code>null</code>
181     * if there is no audio device with ID <code>deviceID</code>.
182     */
183     public AudioDeviceModel
184     getAudioDeviceModel(int deviceID) {
185     for(AudioDeviceModel m : audioDeviceModels)
186     if(m.getDeviceID() == deviceID) return m;
187    
188     return null;
189     }
190    
191     /**
192     * Gets the current number of audio devices.
193     * @return The current number of audio devices.
194     */
195     public int
196     getAudioDeviceCount() { return audioDeviceModels.size(); }
197    
198     /**
199     * Gets the current list of audio device models.
200     * @return The current list of audio device models.
201     */
202     public AudioDeviceModel[]
203     getAudioDeviceModels() {
204     return audioDeviceModels.toArray(new AudioDeviceModel[audioDeviceModels.size()]);
205     }
206    
207     /**
208     * Adds the specified audio device.
209     * @param device The audio device to be added.
210     */
211     public void
212     addAudioDevice(AudioOutputDevice device) {
213     DefaultAudioDeviceModel model = new DefaultAudioDeviceModel(device);
214     audioDeviceModels.add(model);
215     fireAudioDeviceAdded(model);
216     }
217    
218     /**
219     * Removes the specified audio device.
220     * @param deviceID The ID of the audio device to be removed.
221     * @return <code>true</code> if the audio device is removed successfully, <code>false</code>
222     * if the device list does not contain audio device with ID <code>deviceID</code>.
223     */
224     public boolean
225     removeAudioDevice(int deviceID) {
226     for(int i = 0; i < audioDeviceModels.size(); i++) {
227     AudioDeviceModel m = audioDeviceModels.get(i);
228     if(m.getDeviceID() == deviceID) {
229     audioDeviceModels.remove(i);
230     fireAudioDeviceRemoved(m);
231     return true;
232     }
233     }
234    
235     return false;
236     }
237    
238     /**
239     * Gets all MIDI input drivers currently available for the LinuxSampler instance.
240     *
241     * @return <code>MidiInputDriver</code> array containing all MIDI input drivers currently
242     * available for the LinuxSampler instance.
243     */
244     public MidiInputDriver[]
245     getMidiInputDrivers() { return miDrvS; }
246    
247     /**
248     * Sets the currently available MIDI input drivers for the LinuxSampler instance.
249     *
250     * @param drivers <code>MidiInputDriver</code> array containing all MIDI input drivers
251     * currently available for the LinuxSampler instance.
252     */
253     public void
254     setMidiInputDrivers(MidiInputDriver[] drivers) { miDrvS = drivers; }
255    
256     /**
257     * Gets the model of the MIDI device with ID <code>deviceID</code>.
258     * @param deviceID The ID of the MIDI device whose model should be obtained.
259     * @return The model of the specified MIDI device or <code>null</code>
260     * if there is no MIDI device with ID <code>deviceID</code>.
261     */
262     public MidiDeviceModel
263     getMidiDeviceModel(int deviceID) {
264     for(MidiDeviceModel m : midiDeviceModels)
265     if(m.getDeviceID() == deviceID) return m;
266    
267     return null;
268     }
269    
270     /**
271     * Gets the current number of MIDI input devices.
272     * @return The current number of MIDI input devices.
273     */
274     public int
275     getMidiDeviceCount() { return midiDeviceModels.size(); }
276    
277     /**
278     * Gets the current list of MIDI device models.
279     * @return The current list of MIDI device models.
280     */
281     public MidiDeviceModel[]
282     getMidiDeviceModels() {
283     return midiDeviceModels.toArray(new MidiDeviceModel[midiDeviceModels.size()]);
284     }
285    
286     /**
287     * Adds the specified MIDI device.
288     * @param device The MIDI device to be added.
289     */
290     public void
291     addMidiDevice(MidiInputDevice device) {
292     DefaultMidiDeviceModel model = new DefaultMidiDeviceModel(device);
293     midiDeviceModels.add(model);
294     fireMidiDeviceAdded(model);
295     }
296    
297     /**
298     * Removes the specified MIDI device.
299     * @param deviceID The ID of the MIDI device to be removed.
300     * @return <code>true</code> if the MIDI device is removed successfully, <code>false</code>
301     * if the device list does not contain MIDI device with ID <code>deviceID</code>.
302     */
303     public boolean
304     removeMidiDevice(int deviceID) {
305     for(int i = 0; i < midiDeviceModels.size(); i++) {
306     MidiDeviceModel m = midiDeviceModels.get(i);
307     if(m.getDeviceID() == deviceID) {
308     midiDeviceModels.remove(i);
309     fireMidiDeviceRemoved(m);
310     return true;
311     }
312     }
313    
314     return false;
315     }
316    
317     /**
318     * Gets a list of all available engines.
319     * @return A list of all available engines.
320     */
321     public SamplerEngine[]
322     getEngines() { return engines; }
323    
324     /**
325     * Sets the list of all available engines.
326     * @param engines The new list of all available engines.
327     */
328     public void
329     setEngines(SamplerEngine[] engines) { this.engines = engines; }
330    
331     /**
332     * Gets the model of the sampler channel with ID <code>channelID</code>.
333     * @param channelID The ID of the sampler channel whose model should be obtained.
334     * @return The model of the specified sampler channel or <code>null</code>
335     * if there is no channel with ID <code>channelID</code>.
336     */
337     public SamplerChannelModel
338     getChannelModel(int channelID) {
339     for(SamplerChannelModel m : channelModels)
340     if(m.getChannelID() == channelID) return m;
341    
342     return null;
343     }
344    
345     /**
346     * Gets the current number of sampler channels.
347     * @return The current number of sampler channels.
348     */
349     public int
350     getChannelCount() { return channelModels.size(); }
351    
352     /**
353     * Gets the current list of sampler channel models.
354     * @return The current list of sampler channel models.
355     */
356     public SamplerChannelModel[]
357     getChannelModels() {
358     return channelModels.toArray(new SamplerChannelModel[channelModels.size()]);
359     }
360    
361     /**
362     * Creates a new sampler channel. The channel will be actually added to this model
363     * when the back-end notifies for its creation.
364     * @see #addChannel
365     */
366     public void
367     createChannel() {
368     CC.getTaskQueue().add(new AddChannel());
369     // We leave this event to be notified by the LinuxSampler notification system.
370     }
371    
372     /**
373     * Adds the specified sampler channel.
374     * @param channel The channel to be added.
375     */
376     public void
377     addChannel(SamplerChannel channel) {
378     DefaultSamplerChannelModel model = new DefaultSamplerChannelModel(channel);
379     channelModels.add(model);
380     fireSamplerChannelAdded(model);
381     }
382    
383     /**
384     * Updates the settings of the specified channel.
385     * @param channel A <code>SamplerChannel</code> instance containing the new settings
386     * for the channel.
387     */
388     public void
389     changeChannel(SamplerChannel channel) {
390     for(SamplerChannelModel m : channelModels) {
391     if(m.getChannelID() == channel.getChannelID()) {
392     m.setChannelInfo(channel);
393     return;
394     }
395     }
396    
397     CC.getLogger().log (
398     Level.WARNING, "DefaultSamplerModel.unknownChannel!", channel.getChannelID()
399     );
400     }
401    
402     /**
403     * Removes the specified sampler channel.
404     * @param channelID The ID of the channel to be removed.
405     * @return <code>true</code> if the channel is removed successfully, <code>false</code>
406     * if the channel's list does not contain channel with ID <code>channelID</code>.
407     */
408     public boolean
409     removeChannel(int channelID) {
410     for(int i = 0; i < channelModels.size(); i++) {
411     SamplerChannelModel m = channelModels.get(i);
412     if(m.getChannelID() == channelID) {
413     channelModels.remove(i);
414     fireSamplerChannelRemoved(m);
415     return true;
416     }
417     }
418    
419     return false;
420     }
421    
422     /**
423     * Determines whether there is at least one solo channel in the current list
424     * of sampler channels.
425     * @return <code>true</code> if there is at least one solo channel in the current list of
426     * sampler channels, <code>false</code> otherwise.
427     */
428     public boolean
429     hasSoloChannel() {
430     for(SamplerChannelModel m : channelModels)
431     if(m.getChannelInfo().isSoloChannel()) return true;
432    
433     return false;
434     }
435    
436     /**
437     * Gets the number of solo channels in the current list of sampler channels.
438     * @return The number of solo channels in the current list of sampler channels.
439     */
440     public int
441     getSoloChannelCount() {
442     int count = 0;
443     for(SamplerChannelModel m : channelModels)
444     if(m.getChannelInfo().isSoloChannel()) count++;
445    
446     return count;
447     }
448    
449     /**
450     * Gets the number of muted channels in the current list of sampler channels.
451     * This number includes the channels muted because of the presence of a solo channel.
452     * @return The number of muted channels in the current list of sampler channels.
453     */
454     public int
455     getMutedChannelCount() {
456     int count = 0;
457     for(SamplerChannelModel m : channelModels)
458     if(m.getChannelInfo().isMuted()) count++;
459    
460     return count;
461     }
462    
463     /**
464     * Gets the number of channels muted because of the presence of a solo channel.
465     * @return The number of channels muted because of the presence of a solo channel.
466     */
467     public int
468     getMutedBySoloChannelCount() {
469     int count = 0;
470     for(SamplerChannelModel m : channelModels)
471     if(m.getChannelInfo().isMutedBySolo()) count++;
472    
473     return count;
474     }
475    
476     /**
477     * Gets the total number of active voices.
478     * @return The total number of active voices.
479     */
480     public int
481     getTotalVoiceCount() { return totalVoiceCount; }
482    
483     /**
484     * Gets the maximum number of active voices.
485     * @return The maximum number of active voices.
486     */
487     public int
488     getTotalVoiceCountMax() { return totalVoiceCountMax; }
489    
490     /**
491     * Updates the current and the maximum number of active voices in the sampler.
492     * @param count The new number of active voices.
493     * @param countMax The maximum number of active voices.
494     */
495     public void
496     updateActiveVoiceInfo(int count, int countMax) {
497     if(totalVoiceCount == count && totalVoiceCountMax == countMax) return;
498    
499     totalVoiceCount = count;
500     totalVoiceCountMax = countMax;
501     fireTotalVoiceCountChanged();
502     }
503    
504     /**
505     * Notifies listeners that a sampler channel has been added.
506     * @param channelModel A <code>SamplerChannelModel</code> instance.
507     */
508     private void
509     fireSamplerChannelAdded(SamplerChannelModel channelModel) {
510     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
511    
512     SwingUtilities.invokeLater(new Runnable() {
513     public void
514     run() { fireSamplerChannelAdded(e); }
515     });
516     }
517     /**
518     * Notifies listeners that a sampler channel has been added.
519     * This method should be invoked from the event-dispatching thread.
520     */
521     private void
522     fireSamplerChannelAdded(SamplerChannelListEvent e) {
523     Object[] listeners = listenerList.getListenerList();
524    
525     for(int i = listeners.length - 2; i >= 0; i -= 2) {
526     if(listeners[i] == SamplerChannelListListener.class) {
527     ((SamplerChannelListListener)listeners[i + 1]).channelAdded(e);
528     }
529     }
530     }
531    
532     /**
533     * Notifies listeners that a sampler channel has been removed.
534     * @param channelModel A <code>SamplerChannelModel</code> instance.
535     */
536     private void
537     fireSamplerChannelRemoved(SamplerChannelModel channelModel) {
538     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
539    
540     SwingUtilities.invokeLater(new Runnable() {
541     public void
542     run() { fireSamplerChannelRemoved(e); }
543     });
544     }
545    
546     /**
547     * Notifies listeners that a sampler channel has been removed.
548     * This method should be invoked from the event-dispatching thread.
549     */
550     private void
551     fireSamplerChannelRemoved(SamplerChannelListEvent e) {
552     Object[] listeners = listenerList.getListenerList();
553    
554     for(int i = listeners.length - 2; i >= 0; i -= 2) {
555     if(listeners[i] == SamplerChannelListListener.class) {
556     ((SamplerChannelListListener)listeners[i + 1]).channelRemoved(e);
557     }
558     }
559     }
560    
561     /**
562     * Notifies listeners that a MIDI device has been added.
563     * @param model A <code>MidiDeviceModel</code> instance.
564     */
565     private void
566     fireMidiDeviceAdded(MidiDeviceModel model) {
567     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
568    
569     SwingUtilities.invokeLater(new Runnable() {
570     public void
571     run() { fireMidiDeviceAdded(e); }
572     });
573     }
574     /**
575     * Notifies listeners that a MIDI device has been added.
576     * This method should be invoked from the event-dispatching thread.
577     */
578     private void
579     fireMidiDeviceAdded(MidiDeviceListEvent e) {
580     Object[] listeners = listenerList.getListenerList();
581    
582     for(int i = listeners.length - 2; i >= 0; i -= 2) {
583     if(listeners[i] == MidiDeviceListListener.class) {
584     ((MidiDeviceListListener)listeners[i + 1]).deviceAdded(e);
585     }
586     }
587     }
588    
589     /**
590     * Notifies listeners that a MIDI device has been removed.
591     * @param model A <code>MidiDeviceModel</code> instance.
592     */
593     private void
594     fireMidiDeviceRemoved(MidiDeviceModel model) {
595     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
596    
597     SwingUtilities.invokeLater(new Runnable() {
598     public void
599     run() { fireMidiDeviceRemoved(e); }
600     });
601     }
602    
603     /**
604     * Notifies listeners that a MIDI device has been removed.
605     * This method should be invoked from the event-dispatching thread.
606     */
607     private void
608     fireMidiDeviceRemoved(MidiDeviceListEvent e) {
609     Object[] listeners = listenerList.getListenerList();
610    
611     for(int i = listeners.length - 2; i >= 0; i -= 2) {
612     if(listeners[i] == MidiDeviceListListener.class) {
613     ((MidiDeviceListListener)listeners[i + 1]).deviceRemoved(e);
614     }
615     }
616     }
617    
618     /**
619     * Notifies listeners that an audio device has been added.
620     * @param model A <code>AudioDeviceModel</code> instance.
621     */
622     private void
623     fireAudioDeviceAdded(AudioDeviceModel model) {
624     final AudioDeviceListEvent e = new AudioDeviceListEvent(this, model);
625    
626     SwingUtilities.invokeLater(new Runnable() {
627     public void
628     run() { fireAudioDeviceAdded(e); }
629     });
630     }
631    
632     /**
633     * Notifies listeners that an audio device has been added.
634     * This method should be invoked from the event-dispatching thread.
635     */
636     private void
637     fireAudioDeviceAdded(AudioDeviceListEvent e) {
638     Object[] listeners = listenerList.getListenerList();
639    
640     for(int i = listeners.length - 2; i >= 0; i -= 2) {
641     if(listeners[i] == AudioDeviceListListener.class) {
642     ((AudioDeviceListListener)listeners[i + 1]).deviceAdded(e);
643     }
644     }
645     }
646    
647     /**
648     * Notifies listeners that an audio device has been removed.
649     * @param model A <code>AudioDeviceModel</code> instance.
650     */
651     private void
652     fireAudioDeviceRemoved(AudioDeviceModel model) {
653     final AudioDeviceListEvent e = new AudioDeviceListEvent(this, model);
654    
655     SwingUtilities.invokeLater(new Runnable() {
656     public void
657     run() { fireAudioDeviceRemoved(e); }
658     });
659     }
660    
661     /**
662     * Notifies listeners that an audio device has been removed.
663     * This method should be invoked from the event-dispatching thread.
664     */
665     private void
666     fireAudioDeviceRemoved(AudioDeviceListEvent e) {
667     Object[] listeners = listenerList.getListenerList();
668    
669     for(int i = listeners.length - 2; i >= 0; i -= 2) {
670     if(listeners[i] == AudioDeviceListListener.class) {
671     ((AudioDeviceListListener)listeners[i + 1]).deviceRemoved(e);
672     }
673     }
674     }
675    
676     /** Notifies listeners that the total number of active voices has changed. */
677     private void
678     fireTotalVoiceCountChanged() {
679     final SamplerEvent e = new SamplerEvent(this);
680    
681     SwingUtilities.invokeLater(new Runnable() {
682     public void
683     run() { fireTotalVoiceCountChanged(e); }
684     });
685     }
686    
687     /**
688     * Notifies listeners that the total number of active voices has changed.
689     * This method should be invoked from the event-dispatching thread.
690     */
691     private void
692     fireTotalVoiceCountChanged(SamplerEvent e) {
693     for(SamplerListener l : listeners) l.totalVoiceCountChanged(e);
694     }
695     }

  ViewVC Help
Powered by ViewVC