/[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 1545 - (hide annotations) (download)
Tue Dec 4 18:28:29 2007 UTC (16 years, 5 months ago) by iliev
File size: 35787 byte(s)
* Added support for monitoring the total number of active disk streams

1 iliev 787 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 1143 * Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 787 *
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 iliev 1143 import net.sf.juife.Task;
34     import net.sf.juife.event.TaskEvent;
35     import net.sf.juife.event.TaskListener;
36    
37     import org.jsampler.event.ListEvent;
38     import org.jsampler.event.ListListener;
39 iliev 787 import org.jsampler.event.MidiDeviceListEvent;
40     import org.jsampler.event.MidiDeviceListListener;
41     import org.jsampler.event.SamplerChannelListEvent;
42     import org.jsampler.event.SamplerChannelListListener;
43     import org.jsampler.event.SamplerEvent;
44     import org.jsampler.event.SamplerListener;
45    
46 iliev 1143 import org.jsampler.task.Audio;
47     import org.jsampler.task.Channel;
48     import org.jsampler.task.Global;
49     import org.jsampler.task.Midi;
50 iliev 787
51     import org.linuxsampler.lscp.*;
52    
53    
54     /**
55 iliev 911 * This class provides default implementation of the <code>SamplerModel</code> interface.
56 iliev 1143 * Note that the setter methods of this class does <b>not</b> alter any settings
57     * on the backend side unless otherwise specified.
58 iliev 787 * @author Grigor Iliev
59     */
60     public class DefaultSamplerModel implements SamplerModel {
61     private ServerInfo serverInfo = null;
62     private AudioOutputDriver[] aoDrvS = null;
63     private MidiInputDriver[] miDrvS = null;
64     private SamplerEngine[] engines = null;
65    
66 iliev 1545 private int totalStreamCount = 0;
67 iliev 787 private int totalVoiceCount = 0;
68     private int totalVoiceCountMax = 0;
69    
70 iliev 1143 private float volume = 1;
71     private MidiInstrumentMap defaultMidiInstrumentMap;
72    
73 iliev 787 private final Vector<SamplerChannelModel> channelModels = new Vector<SamplerChannelModel>();
74     private final Vector<AudioDeviceModel> audioDeviceModels = new Vector<AudioDeviceModel>();
75     private final Vector<MidiDeviceModel> midiDeviceModels = new Vector<MidiDeviceModel>();
76 iliev 1143 private final Vector<MidiInstrumentMap> midiInstrMaps = new Vector<MidiInstrumentMap>();
77 iliev 787
78     private final Vector<SamplerListener> listeners = new Vector<SamplerListener>();
79 iliev 1143 private final Vector<ListListener<MidiInstrumentMap>> mapsListeners =
80     new Vector<ListListener<MidiInstrumentMap>>();
81    
82 iliev 787 private final EventListenerList listenerList = new EventListenerList();
83    
84    
85     /** Creates a new instance of DefaultSamplerModel */
86     public
87     DefaultSamplerModel() {
88 iliev 1143 addMidiInstrumentMapListListener(getHandler());
89 iliev 787 }
90    
91     /**
92     * Registers the specified listener for receiving event messages.
93     * @param l The <code>SamplerListener</code> to register.
94     */
95     public void
96     addSamplerListener(SamplerListener l) { listeners.add(l); }
97    
98     /**
99     * Removes the specified listener.
100     * @param l The <code>SamplerListener</code> to remove.
101     */
102     public void
103     removeSamplerListener(SamplerListener l) { listeners.remove(l); }
104    
105     /**
106     * Registers the specified listener for receiving event messages.
107     * @param listener The <code>AudioDeviceListListener</code> to register.
108     */
109     public void
110 iliev 1143 addAudioDeviceListListener(ListListener<AudioDeviceModel> listener) {
111     listenerList.add(ListListener.class, listener);
112 iliev 787 }
113    
114     /**
115     * Removes the specified listener.
116     * @param listener The <code>AudioDeviceListListener</code> to remove.
117     */
118     public void
119 iliev 1143 removeAudioDeviceListListener(ListListener<AudioDeviceModel> listener) {
120     listenerList.remove(ListListener.class, listener);
121 iliev 787 }
122    
123     /**
124     * Registers the specified listener for receiving event messages.
125     * @param listener The <code>MidiDeviceListListener</code> to register.
126     */
127     public void
128     addMidiDeviceListListener(MidiDeviceListListener listener) {
129     listenerList.add(MidiDeviceListListener.class, listener);
130     }
131    
132     /**
133     * Removes the specified listener.
134     * @param listener The <code>MidiDeviceListListener</code> to remove.
135     */
136     public void
137     removeMidiDeviceListListener(MidiDeviceListListener listener) {
138     listenerList.remove(MidiDeviceListListener.class, listener);
139     }
140    
141     /**
142     * Registers the specified listener for receiving event messages.
143 iliev 1143 * @param listener The <code>ListListener</code> to register.
144     */
145     public void
146     addMidiInstrumentMapListListener(ListListener<MidiInstrumentMap> listener) {
147     mapsListeners.add(listener);
148     }
149    
150     /**
151     * Removes the specified listener.
152     * @param listener The <code>ListListener</code> to remove.
153     */
154     public void
155     removeMidiInstrumentMapListListener(ListListener<MidiInstrumentMap> listener) {
156     mapsListeners.remove(listener);
157     }
158    
159     /**
160     * Registers the specified listener for receiving event messages.
161 iliev 787 * @param listener The <code>SamplerChannelListListener</code> to register.
162     */
163     public void
164     addSamplerChannelListListener(SamplerChannelListListener listener) {
165     listenerList.add(SamplerChannelListListener.class, listener);
166     }
167    
168     /**
169     * Removes the specified listener.
170     * @param listener The <code>SamplerChannelListListener</code> to remove.
171     */
172     public void
173     removeSamplerChannelListListener(SamplerChannelListListener listener) {
174     listenerList.remove(SamplerChannelListListener.class, listener);
175     }
176    
177     /**
178     * Gets information about the LinuxSampler instance the front-end is connected to.
179     *
180     * @return <code>ServerInfo</code> instance containing
181     * information about the LinuxSampler instance the front-end is connected to.
182     */
183     public ServerInfo
184     getServerInfo() { return serverInfo; }
185    
186     /**
187     * Sets information about the LinuxSampler instance the front-end is connected to.
188     *
189     * @param serverInfo <code>ServerInfo</code> instance containing
190     * information about the LinuxSampler instance the front-end is connected to.
191     */
192     public void
193     setServerInfo(ServerInfo serverInfo) { this.serverInfo = serverInfo; }
194    
195     /**
196     * Gets all audio output drivers currently available for the LinuxSampler instance.
197     *
198     * @return <code>AudioOutputDriver</code> array containing all audio output drivers
199     * currently available for the LinuxSampler instance.
200     */
201     public AudioOutputDriver[]
202     getAudioOutputDrivers() { return aoDrvS; }
203    
204     /**
205     * Sets the currently available audio output drivers for the LinuxSampler instance.
206     *
207     * @param drivers <code>AudioOutputDriver</code> array containing all audio output drivers
208     * currently available for the LinuxSampler instance.
209     */
210     public void
211     setAudioOutputDrivers(AudioOutputDriver[] drivers) { aoDrvS = drivers; }
212    
213     /**
214 iliev 1467 * Gets the model of the audio device at the specified position.
215     * @param index The position of the audio device to return.
216     * @return The model of the audio device at the specified position.
217     * @see #getAudioDeviceCount
218     */
219     public AudioDeviceModel
220     getAudioDevice(int index) {
221     return audioDeviceModels.get(index);
222     }
223    
224     /**
225 iliev 1143 * Gets the model of the audio device with ID <code>deviceId</code>.
226     * @param deviceId The ID of the audio device whose model should be obtained.
227 iliev 787 * @return The model of the specified audio device or <code>null</code>
228 iliev 1143 * if there is no audio device with ID <code>deviceId</code>.
229 iliev 787 */
230     public AudioDeviceModel
231 iliev 1204 getAudioDeviceById(int deviceId) {
232 iliev 787 for(AudioDeviceModel m : audioDeviceModels)
233 iliev 1143 if(m.getDeviceId() == deviceId) return m;
234 iliev 787
235     return null;
236     }
237    
238     /**
239     * Gets the current number of audio devices.
240     * @return The current number of audio devices.
241     */
242     public int
243     getAudioDeviceCount() { return audioDeviceModels.size(); }
244    
245     /**
246     * Gets the current list of audio device models.
247     * @return The current list of audio device models.
248     */
249     public AudioDeviceModel[]
250 iliev 1204 getAudioDevices() {
251 iliev 787 return audioDeviceModels.toArray(new AudioDeviceModel[audioDeviceModels.size()]);
252     }
253    
254     /**
255     * Adds the specified audio device.
256     * @param device The audio device to be added.
257     */
258     public void
259     addAudioDevice(AudioOutputDevice device) {
260     DefaultAudioDeviceModel model = new DefaultAudioDeviceModel(device);
261     audioDeviceModels.add(model);
262     fireAudioDeviceAdded(model);
263     }
264    
265     /**
266     * Removes the specified audio device.
267 iliev 1143 * @param deviceId The ID of the audio device to be removed.
268 iliev 787 * @return <code>true</code> if the audio device is removed successfully, <code>false</code>
269 iliev 1143 * if the device list does not contain audio device with ID <code>deviceId</code>.
270 iliev 787 */
271     public boolean
272 iliev 1204 removeAudioDeviceById(int deviceId) {
273 iliev 787 for(int i = 0; i < audioDeviceModels.size(); i++) {
274     AudioDeviceModel m = audioDeviceModels.get(i);
275 iliev 1143 if(m.getDeviceId() == deviceId) {
276 iliev 787 audioDeviceModels.remove(i);
277     fireAudioDeviceRemoved(m);
278     return true;
279     }
280     }
281    
282     return false;
283     }
284    
285     /**
286 iliev 1143 * Schedules a new task for removing the specified audio device on the backend side.
287     * @param deviceId The ID of the audio device to be removed.
288     */
289     public void
290     removeBackendAudioDevice(int deviceId) {
291     CC.getTaskQueue().add(new Audio.DestroyDevice(deviceId));
292     }
293    
294     /**
295 iliev 787 * Gets all MIDI input drivers currently available for the LinuxSampler instance.
296     *
297     * @return <code>MidiInputDriver</code> array containing all MIDI input drivers currently
298     * available for the LinuxSampler instance.
299     */
300     public MidiInputDriver[]
301     getMidiInputDrivers() { return miDrvS; }
302    
303     /**
304     * Sets the currently available MIDI input drivers for the LinuxSampler instance.
305     *
306     * @param drivers <code>MidiInputDriver</code> array containing all MIDI input drivers
307     * currently available for the LinuxSampler instance.
308     */
309     public void
310     setMidiInputDrivers(MidiInputDriver[] drivers) { miDrvS = drivers; }
311    
312     /**
313 iliev 1467 * Gets the model of the MIDI device at the specified position.
314     * @param index The position of the MIDI device to return.
315     * @return The model of the MIDI device at the specified position.
316     */
317     public MidiDeviceModel
318     getMidiDevice(int index) {
319     return midiDeviceModels.get(index);
320     }
321    
322     /**
323 iliev 1143 * Gets the model of the MIDI device with ID <code>deviceId</code>.
324     * @param deviceId The ID of the MIDI device whose model should be obtained.
325 iliev 787 * @return The model of the specified MIDI device or <code>null</code>
326 iliev 1143 * if there is no MIDI device with ID <code>deviceId</code>.
327 iliev 787 */
328     public MidiDeviceModel
329 iliev 1204 getMidiDeviceById(int deviceId) {
330 iliev 787 for(MidiDeviceModel m : midiDeviceModels)
331 iliev 1143 if(m.getDeviceId() == deviceId) return m;
332 iliev 787
333     return null;
334     }
335    
336     /**
337     * Gets the current number of MIDI input devices.
338     * @return The current number of MIDI input devices.
339     */
340     public int
341     getMidiDeviceCount() { return midiDeviceModels.size(); }
342    
343     /**
344     * Gets the current list of MIDI device models.
345     * @return The current list of MIDI device models.
346     */
347     public MidiDeviceModel[]
348 iliev 1204 getMidiDevices() {
349 iliev 787 return midiDeviceModels.toArray(new MidiDeviceModel[midiDeviceModels.size()]);
350     }
351    
352     /**
353     * Adds the specified MIDI device.
354     * @param device The MIDI device to be added.
355     */
356     public void
357     addMidiDevice(MidiInputDevice device) {
358     DefaultMidiDeviceModel model = new DefaultMidiDeviceModel(device);
359     midiDeviceModels.add(model);
360     fireMidiDeviceAdded(model);
361     }
362    
363     /**
364 iliev 1143 * Schedules a new task for adding new MIDI device.
365     * @param driver The desired MIDI input system.
366     * @param parameters An optional list of driver specific parameters.
367     */
368     public void
369     addBackendMidiDevice(String driver, Parameter... parameters) {
370     CC.getTaskQueue().add(new Midi.CreateDevice(driver, parameters));
371     }
372    
373     /**
374 iliev 787 * Removes the specified MIDI device.
375 iliev 1143 * @param deviceId The ID of the MIDI device to be removed.
376 iliev 787 * @return <code>true</code> if the MIDI device is removed successfully, <code>false</code>
377 iliev 1143 * if the device list does not contain MIDI device with ID <code>deviceId</code>.
378 iliev 787 */
379     public boolean
380 iliev 1204 removeMidiDeviceById(int deviceId) {
381 iliev 787 for(int i = 0; i < midiDeviceModels.size(); i++) {
382     MidiDeviceModel m = midiDeviceModels.get(i);
383 iliev 1143 if(m.getDeviceId() == deviceId) {
384 iliev 787 midiDeviceModels.remove(i);
385     fireMidiDeviceRemoved(m);
386     return true;
387     }
388     }
389    
390     return false;
391     }
392    
393     /**
394 iliev 1143 * Schedules a new task for removing the specified MIDI device.
395     * @param deviceId The ID of the MIDI input device to be destroyed.
396     */
397     public void
398     removeBackendMidiDevice(int deviceId) {
399     CC.getTaskQueue().add(new Midi.DestroyDevice(deviceId));
400     }
401    
402     /**
403     * Gets the MIDI instrument map with ID <code>mapId</code>.
404     * @param mapId The ID of the MIDI instrument map to obtain.
405     * @return The MIDI instrument map with the specified ID or <code>null</code>
406     * if there is no MIDI instrument map with ID <code>mapId</code>.
407     */
408     public MidiInstrumentMap
409     getMidiInstrumentMapById(int mapId) {
410     for(MidiInstrumentMap m : midiInstrMaps)
411     if(m.getMapId() == mapId) return m;
412    
413     return null;
414     }
415    
416     /**
417     * Gets the MIDI instrument map at the specified position.
418     * @param index The position of the MIDI instrument map to return.
419     * @return The MIDI instrument map at the specified position.
420     */
421     public MidiInstrumentMap
422     getMidiInstrumentMap(int index) {
423     return midiInstrMaps.get(index);
424     }
425    
426     /**
427     * Gets the current number of MIDI instrument maps.
428     * @return The current number of MIDI instrument maps.
429     */
430     public int
431     getMidiInstrumentMapCount() { return midiInstrMaps.size(); }
432    
433     /**
434     * Gets the current list of MIDI instrument maps.
435     * @return The current list of MIDI instrument maps.
436     */
437     public MidiInstrumentMap[]
438     getMidiInstrumentMaps() {
439     return midiInstrMaps.toArray(new MidiInstrumentMap[midiInstrMaps.size()]);
440     }
441    
442     /**
443 iliev 1285 * Gets the position of the specified MIDI instrument map in the list.
444     * @param map The map whose index should be returned.
445     * @return The position of the specified map in the list,
446     * or -1 if <code>map</code> is <code>null</code> or
447     * the map list does not contain the specified map.
448     */
449     public int
450     getMidiInstrumentMapIndex(MidiInstrumentMap map) {
451     if(map == null) return -1;
452    
453     for(int i = 0; i < getMidiInstrumentMapCount(); i++) {
454     if(getMidiInstrumentMap(i) == map) return i;
455     }
456    
457     return -1;
458     }
459    
460     /**
461 iliev 1143 * Adds the specified MIDI instrument map.
462     * @param map The MIDI instrument map to be added.
463     * @throws IllegalArgumentException If <code>map</code> is <code>null</code>.
464     */
465     public void
466     addMidiInstrumentMap(MidiInstrumentMap map) {
467     if(map == null) throw new IllegalArgumentException("map should be non-null!");
468    
469     midiInstrMaps.add(map);
470     fireMidiInstrumentMapAdded(map);
471     }
472    
473     /**
474     * Schedules a new task for creating a new MIDI instrument map on the backend side.
475     * @param name The name of the MIDI instrument map.
476     * @throws IllegalArgumentException If <code>name</code> is <code>null</code>.
477     */
478     public void
479     addBackendMidiInstrumentMap(String name) {
480     if(name == null) throw new IllegalArgumentException("name should be non-null!");
481    
482     CC.getTaskQueue().add(new Midi.AddInstrumentMap(name));
483     }
484    
485     /**
486     * Removes the specified MIDI instrument map.
487     * @param mapId The ID of the MIDI instrument map to be removed.
488     * @return <code>true</code> if the MIDI instrument map is removed successfully,
489     * <code>false</code> if the MIDI instrument map's list does not contain
490     * MIDI instrument map with ID <code>mapId</code>.
491     */
492     public boolean
493 iliev 1204 removeMidiInstrumentMapById(int mapId) {
494 iliev 1143 for(int i = 0; i < midiInstrMaps.size(); i++) {
495     MidiInstrumentMap m = getMidiInstrumentMap(i);
496     if(m.getMapId() == mapId) {
497     midiInstrMaps.remove(i);
498     fireMidiInstrumentMapRemoved(m);
499     return true;
500     }
501     }
502    
503     return false;
504     }
505    
506     /**
507     * Removes the specified MIDI instrument map.
508     * @param map The MIDI instrument map to remove.
509     * @return <code>true</code> if the specified MIDI instrument map was in the list,
510     * <code>false</code> otherwise.
511     */
512     public boolean
513     removeMidiInstrumentMap(MidiInstrumentMap map) {
514     boolean b = midiInstrMaps.removeElement(map);
515     if(b) fireMidiInstrumentMapRemoved(map);
516     return b;
517     }
518    
519     /** Removes all MIDI instrument maps. */
520     public void
521     removeAllMidiInstrumentMaps() {
522     for(int i = midiInstrMaps.size() - 1; i >= 0; i--) {
523     MidiInstrumentMap map = midiInstrMaps.get(i);
524     midiInstrMaps.removeElementAt(i);
525     fireMidiInstrumentMapRemoved(map);
526     }
527     }
528    
529     /**
530     * Schedules a new task for removing the
531     * specified MIDI instrument map on the backend side.
532     * @param mapId The numerical ID of the MIDI instrument map to remove.
533     */
534     public void
535     removeBackendMidiInstrumentMap(int mapId) {
536     CC.getTaskQueue().add(new Midi.RemoveInstrumentMap(mapId));
537     }
538    
539     /**
540     * Schedules a new task for changing the name of
541     * the specified MIDI instrument map on the backend side.
542     * @param mapId The numerical ID of the MIDI instrument map.
543     * @param name The new name for the specified MIDI instrument map.
544     */
545     public void
546     setBackendMidiInstrumentMapName(final int mapId, String name) {
547     final Task t = new Midi.SetInstrumentMapInfo(mapId, name);
548    
549     t.addTaskListener(new TaskListener() {
550     public void
551     taskPerformed(TaskEvent e) {
552     /*
553     * Because with the invokation of the method the task is considered
554     * to be done, if the task fails, we must update the settings.
555     */
556     if(t.doneWithErrors()) {
557     Task t2 = new Midi.UpdateInstrumentMapInfo(mapId);
558     CC.getTaskQueue().add(t2);
559     }
560     }
561     });
562     CC.getTaskQueue().add(t);
563     }
564    
565     /**
566     * Gets the default MIDI instrument map.
567     * @return The default MIDI instrument map or <code>null</code>
568     * if there are no maps created.
569     */
570     public MidiInstrumentMap
571     getDefaultMidiInstrumentMap() {
572     return defaultMidiInstrumentMap;
573     }
574    
575     /**
576     * Gets the default MIDI instrument map.
577     * @return The default MIDI instrument map or <code>null</code>
578     * if there are no maps created.
579     */
580     private MidiInstrumentMap
581     findDefaultMidiInstrumentMap() {
582     for(int i = 0; i < getMidiInstrumentMapCount(); i++) {
583     MidiInstrumentMap m = getMidiInstrumentMap(i);
584     if(m.getInfo().isDefault()) return m;
585     }
586    
587     return null;
588     }
589    
590     /**
591     * Schedules a new task for mapping a MIDI instrument on the backend side.
592     * @param mapId The id of the MIDI instrument map.
593     * @param bank The index of the MIDI bank, which shall contain the instrument.
594     * @param program The MIDI program number of the new instrument.
595     * @param instrInfo Provides the MIDI instrument settings.
596     */
597     public void
598     mapBackendMidiInstrument(int mapId, int bank, int program, MidiInstrumentInfo instrInfo) {
599     CC.getTaskQueue().add(new Midi.MapInstrument(mapId, bank, program, instrInfo));
600     }
601    
602     /**
603     * Schedules a new task for removing a MIDI instrument on the backend side.
604     * @param mapId The id of the MIDI instrument map containing the instrument to be removed.
605     * @param bank The index of the MIDI bank containing the instrument to be removed.
606     * @param program The MIDI program number of the instrument to be removed.
607     */
608     public void
609     unmapBackendMidiInstrument(int mapId, int bank, int program) {
610     CC.getTaskQueue().add(new Midi.UnmapInstrument(mapId, bank, program));
611     }
612    
613     /**
614 iliev 787 * Gets a list of all available engines.
615     * @return A list of all available engines.
616     */
617     public SamplerEngine[]
618     getEngines() { return engines; }
619    
620     /**
621     * Sets the list of all available engines.
622     * @param engines The new list of all available engines.
623     */
624     public void
625     setEngines(SamplerEngine[] engines) { this.engines = engines; }
626    
627     /**
628 iliev 1467 * Gets the model of the sampler channel in the specified position.
629     * @param index The position of the channel to return.
630     * @return The model of the specified sampler channel.
631     * @see #getchannelCount
632     */
633     public SamplerChannelModel
634     getChannel(int index) {
635     return channelModels.get(index);
636     }
637    
638     /**
639 iliev 1143 * Gets the model of the sampler channel with ID <code>channelId</code>.
640     * @param channelId The ID of the sampler channel whose model should be obtained.
641 iliev 787 * @return The model of the specified sampler channel or <code>null</code>
642 iliev 1143 * if there is no channel with ID <code>channelId</code>.
643 iliev 787 */
644     public SamplerChannelModel
645 iliev 1204 getChannelById(int channelId) {
646 iliev 787 for(SamplerChannelModel m : channelModels)
647 iliev 1143 if(m.getChannelId() == channelId) return m;
648 iliev 787
649     return null;
650     }
651    
652     /**
653     * Gets the current number of sampler channels.
654     * @return The current number of sampler channels.
655     */
656     public int
657     getChannelCount() { return channelModels.size(); }
658    
659     /**
660     * Gets the current list of sampler channel models.
661     * @return The current list of sampler channel models.
662     */
663     public SamplerChannelModel[]
664 iliev 1204 getChannels() {
665 iliev 787 return channelModels.toArray(new SamplerChannelModel[channelModels.size()]);
666     }
667    
668     /**
669 iliev 1143 * Schedules a new task for adding a new sampler channel on the
670     * backend side. The channel will be actually added to this model
671     * when the backend notifies for its creation.
672 iliev 787 * @see #addChannel
673     */
674     public void
675 iliev 1143 addBackendChannel() {
676     CC.getTaskQueue().add(new Channel.Add());
677 iliev 787 // We leave this event to be notified by the LinuxSampler notification system.
678     }
679    
680     /**
681     * Adds the specified sampler channel.
682     * @param channel The channel to be added.
683     */
684     public void
685     addChannel(SamplerChannel channel) {
686     DefaultSamplerChannelModel model = new DefaultSamplerChannelModel(channel);
687     channelModels.add(model);
688     fireSamplerChannelAdded(model);
689     }
690    
691     /**
692     * Updates the settings of the specified channel.
693     * @param channel A <code>SamplerChannel</code> instance containing the new settings
694     * for the channel.
695     */
696     public void
697 iliev 1143 updateChannel(SamplerChannel channel) {
698 iliev 787 for(SamplerChannelModel m : channelModels) {
699 iliev 1143 if(m.getChannelId() == channel.getChannelId()) {
700 iliev 787 m.setChannelInfo(channel);
701     return;
702     }
703     }
704    
705     CC.getLogger().log (
706 iliev 1143 Level.WARNING, "DefaultSamplerModel.unknownChannel!", channel.getChannelId()
707 iliev 787 );
708     }
709    
710     /**
711     * Removes the specified sampler channel.
712 iliev 1143 * @param channelId The ID of the channel to be removed.
713 iliev 787 * @return <code>true</code> if the channel is removed successfully, <code>false</code>
714 iliev 1143 * if the channel's list does not contain channel with ID <code>channelId</code>.
715 iliev 787 */
716     public boolean
717 iliev 1204 removeChannelById(int channelId) {
718 iliev 787 for(int i = 0; i < channelModels.size(); i++) {
719     SamplerChannelModel m = channelModels.get(i);
720 iliev 1143 if(m.getChannelId() == channelId) {
721 iliev 787 channelModels.remove(i);
722     fireSamplerChannelRemoved(m);
723     return true;
724     }
725     }
726    
727     return false;
728     }
729    
730     /**
731 iliev 1143 * Schedules a new task for removing the specified sampler channel on the backend side.
732     * @param channelId The ID of the channel to be removed.
733     */
734     public void
735     removeBackendChannel(int channelId) {
736 iliev 1341 CC.getTaskQueue().add(new Channel.Remove(channelId));
737 iliev 1143 }
738    
739     /**
740 iliev 1341 * Schedules a new task for starting an instrument editor for editing
741     * the loaded instrument on the specified sampler channel.
742     * @param channelId The sampler channel number.
743     */
744     public void
745     editBackendInstrument(int channelId) {
746     CC.getTaskQueue().add(new Channel.EditInstrument(channelId));
747     }
748    
749     /**
750 iliev 787 * Determines whether there is at least one solo channel in the current list
751     * of sampler channels.
752     * @return <code>true</code> if there is at least one solo channel in the current list of
753     * sampler channels, <code>false</code> otherwise.
754     */
755     public boolean
756     hasSoloChannel() {
757     for(SamplerChannelModel m : channelModels)
758     if(m.getChannelInfo().isSoloChannel()) return true;
759    
760     return false;
761     }
762    
763     /**
764     * Gets the number of solo channels in the current list of sampler channels.
765     * @return The number of solo channels in the current list of sampler channels.
766     */
767     public int
768     getSoloChannelCount() {
769     int count = 0;
770     for(SamplerChannelModel m : channelModels)
771     if(m.getChannelInfo().isSoloChannel()) count++;
772    
773     return count;
774     }
775    
776     /**
777     * Gets the number of muted channels in the current list of sampler channels.
778     * This number includes the channels muted because of the presence of a solo channel.
779     * @return The number of muted channels in the current list of sampler channels.
780     */
781     public int
782     getMutedChannelCount() {
783     int count = 0;
784     for(SamplerChannelModel m : channelModels)
785     if(m.getChannelInfo().isMuted()) count++;
786    
787     return count;
788     }
789    
790     /**
791     * Gets the number of channels muted because of the presence of a solo channel.
792     * @return The number of channels muted because of the presence of a solo channel.
793     */
794     public int
795     getMutedBySoloChannelCount() {
796     int count = 0;
797     for(SamplerChannelModel m : channelModels)
798     if(m.getChannelInfo().isMutedBySolo()) count++;
799    
800     return count;
801     }
802    
803     /**
804 iliev 1545 * Gets the total number of active streams.
805     * @return The total number of active streams.
806     */
807     public int
808     getTotalStreamCount() { return totalStreamCount; }
809    
810     /**
811 iliev 787 * Gets the total number of active voices.
812     * @return The total number of active voices.
813     */
814     public int
815     getTotalVoiceCount() { return totalVoiceCount; }
816    
817     /**
818     * Gets the maximum number of active voices.
819     * @return The maximum number of active voices.
820     */
821     public int
822     getTotalVoiceCountMax() { return totalVoiceCountMax; }
823    
824     /**
825 iliev 1143 * Gets the golobal volume of the sampler.
826     * @return The golobal volume of the sampler.
827     */
828     public float
829     getVolume() { return volume; }
830    
831     /**
832     * Sets the global volume.
833     * @param volume The new volume value.
834     */
835     public void
836     setVolume(float volume) {
837     if(this.volume == volume) return;
838    
839     this.volume = volume;
840     fireVolumeChanged();
841     }
842    
843     /**
844     * Schedules a new task for setting the global volume on the backend side.
845     * @param volume The new volume value.
846     */
847     public void
848     setBackendVolume(float volume) {
849     CC.getTaskQueue().add(new Global.SetVolume(volume));
850     }
851    
852     /**
853     * Schedules a new task for resetting the sampler.
854     */
855     public void
856 iliev 1204 resetBackend() { CC.getTaskQueue().add(new org.jsampler.task.Global.ResetSampler()); }
857 iliev 1143
858     /**
859 iliev 1545 * Updates the current number of active disk streams in the sampler.
860     * @param count The new number of active streams.
861     */
862     public void
863     updateActiveStreamsInfo(int count) {
864     if(totalStreamCount == count) return;
865    
866     totalStreamCount = count;
867     fireTotalStreamCountChanged();
868     }
869    
870     /**
871 iliev 787 * Updates the current and the maximum number of active voices in the sampler.
872     * @param count The new number of active voices.
873     * @param countMax The maximum number of active voices.
874     */
875     public void
876     updateActiveVoiceInfo(int count, int countMax) {
877     if(totalVoiceCount == count && totalVoiceCountMax == countMax) return;
878    
879     totalVoiceCount = count;
880     totalVoiceCountMax = countMax;
881     fireTotalVoiceCountChanged();
882     }
883    
884     /**
885     * Notifies listeners that a sampler channel has been added.
886 iliev 1143 * This method can be invoked outside the event-dispatching thread.
887 iliev 787 * @param channelModel A <code>SamplerChannelModel</code> instance.
888     */
889     private void
890     fireSamplerChannelAdded(SamplerChannelModel channelModel) {
891     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
892    
893     SwingUtilities.invokeLater(new Runnable() {
894     public void
895     run() { fireSamplerChannelAdded(e); }
896     });
897     }
898     /**
899     * Notifies listeners that a sampler channel has been added.
900     */
901     private void
902     fireSamplerChannelAdded(SamplerChannelListEvent e) {
903     Object[] listeners = listenerList.getListenerList();
904    
905     for(int i = listeners.length - 2; i >= 0; i -= 2) {
906     if(listeners[i] == SamplerChannelListListener.class) {
907     ((SamplerChannelListListener)listeners[i + 1]).channelAdded(e);
908     }
909     }
910     }
911    
912     /**
913     * Notifies listeners that a sampler channel has been removed.
914 iliev 1143 * This method can be invoked outside the event-dispatching thread.
915 iliev 787 * @param channelModel A <code>SamplerChannelModel</code> instance.
916     */
917     private void
918     fireSamplerChannelRemoved(SamplerChannelModel channelModel) {
919     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
920    
921     SwingUtilities.invokeLater(new Runnable() {
922     public void
923     run() { fireSamplerChannelRemoved(e); }
924     });
925     }
926    
927     /**
928     * Notifies listeners that a sampler channel has been removed.
929     */
930     private void
931     fireSamplerChannelRemoved(SamplerChannelListEvent e) {
932     Object[] listeners = listenerList.getListenerList();
933    
934     for(int i = listeners.length - 2; i >= 0; i -= 2) {
935     if(listeners[i] == SamplerChannelListListener.class) {
936     ((SamplerChannelListListener)listeners[i + 1]).channelRemoved(e);
937     }
938     }
939     }
940    
941     /**
942     * Notifies listeners that a MIDI device has been added.
943 iliev 1143 * This method can be invoked outside the event-dispatching thread.
944 iliev 787 * @param model A <code>MidiDeviceModel</code> instance.
945     */
946     private void
947     fireMidiDeviceAdded(MidiDeviceModel model) {
948     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
949    
950     SwingUtilities.invokeLater(new Runnable() {
951     public void
952     run() { fireMidiDeviceAdded(e); }
953     });
954     }
955     /**
956     * Notifies listeners that a MIDI device has been added.
957     */
958     private void
959     fireMidiDeviceAdded(MidiDeviceListEvent e) {
960     Object[] listeners = listenerList.getListenerList();
961    
962     for(int i = listeners.length - 2; i >= 0; i -= 2) {
963     if(listeners[i] == MidiDeviceListListener.class) {
964     ((MidiDeviceListListener)listeners[i + 1]).deviceAdded(e);
965     }
966     }
967     }
968    
969     /**
970     * Notifies listeners that a MIDI device has been removed.
971 iliev 1143 * This method can be invoked outside the event-dispatching thread.
972 iliev 787 * @param model A <code>MidiDeviceModel</code> instance.
973     */
974     private void
975     fireMidiDeviceRemoved(MidiDeviceModel model) {
976     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
977    
978     SwingUtilities.invokeLater(new Runnable() {
979     public void
980     run() { fireMidiDeviceRemoved(e); }
981     });
982     }
983    
984     /**
985     * Notifies listeners that a MIDI device has been removed.
986     */
987     private void
988     fireMidiDeviceRemoved(MidiDeviceListEvent e) {
989     Object[] listeners = listenerList.getListenerList();
990    
991     for(int i = listeners.length - 2; i >= 0; i -= 2) {
992     if(listeners[i] == MidiDeviceListListener.class) {
993     ((MidiDeviceListListener)listeners[i + 1]).deviceRemoved(e);
994     }
995     }
996     }
997    
998     /**
999     * Notifies listeners that an audio device has been added.
1000 iliev 1143 * This method can be invoked outside the event-dispatching thread.
1001 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
1002     */
1003     private void
1004     fireAudioDeviceAdded(AudioDeviceModel model) {
1005 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
1006 iliev 787
1007     SwingUtilities.invokeLater(new Runnable() {
1008     public void
1009     run() { fireAudioDeviceAdded(e); }
1010     });
1011     }
1012    
1013     /**
1014     * Notifies listeners that an audio device has been added.
1015     */
1016     private void
1017 iliev 1143 fireAudioDeviceAdded(ListEvent<AudioDeviceModel> e) {
1018 iliev 787 Object[] listeners = listenerList.getListenerList();
1019    
1020     for(int i = listeners.length - 2; i >= 0; i -= 2) {
1021 iliev 1143 if(listeners[i] == ListListener.class) {
1022     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryAdded(e);
1023 iliev 787 }
1024     }
1025     }
1026    
1027     /**
1028     * Notifies listeners that an audio device has been removed.
1029 iliev 1143 * This method can be invoked outside the event-dispatching thread.
1030 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
1031     */
1032     private void
1033     fireAudioDeviceRemoved(AudioDeviceModel model) {
1034 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
1035 iliev 787
1036     SwingUtilities.invokeLater(new Runnable() {
1037     public void
1038     run() { fireAudioDeviceRemoved(e); }
1039     });
1040     }
1041    
1042     /**
1043 iliev 1143 * Notifies listeners that a MIDI instrument map has been added to the list.
1044     * This method can be invoked outside the event-dispatching thread.
1045     */
1046     private void
1047     fireMidiInstrumentMapAdded(MidiInstrumentMap map) {
1048     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
1049    
1050     SwingUtilities.invokeLater(new Runnable() {
1051     public void
1052     run() { fireMidiInstrumentMapAdded(e); }
1053     });
1054     }
1055    
1056     /** Notifies listeners that a MIDI instrument map has been added to the list. */
1057     private void
1058     fireMidiInstrumentMapAdded(ListEvent<MidiInstrumentMap> e) {
1059     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryAdded(e);
1060     }
1061    
1062     /**
1063     * Notifies listeners that a MIDI instrument map has been removed from the list.
1064     * This method can be invoked outside the event-dispatching thread.
1065     */
1066     private void
1067     fireMidiInstrumentMapRemoved(MidiInstrumentMap map) {
1068     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
1069    
1070     SwingUtilities.invokeLater(new Runnable() {
1071     public void
1072     run() { fireMidiInstrumentMapRemoved(e); }
1073     });
1074     }
1075     /** Notifies listeners that a MIDI instrument map has been removed from the list. */
1076     private void
1077     fireMidiInstrumentMapRemoved(ListEvent<MidiInstrumentMap> e) {
1078     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryRemoved(e);
1079     }
1080    
1081    
1082     /**
1083 iliev 787 * Notifies listeners that an audio device has been removed.
1084     * This method should be invoked from the event-dispatching thread.
1085     */
1086     private void
1087 iliev 1143 fireAudioDeviceRemoved(ListEvent<AudioDeviceModel> e) {
1088 iliev 787 Object[] listeners = listenerList.getListenerList();
1089    
1090     for(int i = listeners.length - 2; i >= 0; i -= 2) {
1091 iliev 1143 if(listeners[i] == ListListener.class) {
1092     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryRemoved(e);
1093 iliev 787 }
1094     }
1095     }
1096    
1097 iliev 1143 /**
1098     * Notifies listeners that the global volume has changed.
1099     * This method can be invoked outside the event-dispatching thread.
1100     */
1101 iliev 787 private void
1102 iliev 1143 fireVolumeChanged() {
1103     final SamplerEvent e = new SamplerEvent(this);
1104    
1105     SwingUtilities.invokeLater(new Runnable() {
1106     public void
1107     run() { fireVolumeChanged(e); }
1108     });
1109     }
1110    
1111     /**
1112     * Notifies listeners that the global volume has changed.
1113     */
1114     private void
1115     fireVolumeChanged(SamplerEvent e) {
1116     for(SamplerListener l : listeners) l.volumeChanged(e);
1117     }
1118    
1119     /*
1120 iliev 1545 * Notifies listeners that the total number of active streams has changed.
1121     * This method can be invoked outside the event-dispatching thread.
1122     */
1123     private void
1124     fireTotalStreamCountChanged() {
1125     final SamplerEvent e = new SamplerEvent(this);
1126    
1127     SwingUtilities.invokeLater(new Runnable() {
1128     public void
1129     run() { fireTotalStreamCountChanged(e); }
1130     });
1131     }
1132    
1133     /**
1134     * Notifies listeners that the total number of active streams has changed.
1135     */
1136     private void
1137     fireTotalStreamCountChanged(SamplerEvent e) {
1138     for(SamplerListener l : listeners) l.totalStreamCountChanged(e);
1139     }
1140    
1141     /*
1142 iliev 1143 * Notifies listeners that the total number of active voices has changed.
1143     * This method can be invoked outside the event-dispatching thread.
1144     */
1145     private void
1146 iliev 787 fireTotalVoiceCountChanged() {
1147     final SamplerEvent e = new SamplerEvent(this);
1148    
1149     SwingUtilities.invokeLater(new Runnable() {
1150     public void
1151     run() { fireTotalVoiceCountChanged(e); }
1152     });
1153     }
1154    
1155     /**
1156     * Notifies listeners that the total number of active voices has changed.
1157     */
1158     private void
1159     fireTotalVoiceCountChanged(SamplerEvent e) {
1160     for(SamplerListener l : listeners) l.totalVoiceCountChanged(e);
1161     }
1162 iliev 1143
1163     /**
1164     * Notifies listeners that the global volume has changed.
1165     */
1166     private void
1167     fireDefaultMapChanged() {
1168     SamplerEvent e = new SamplerEvent(this);
1169     for(SamplerListener l : listeners) l.defaultMapChanged(e);
1170     }
1171    
1172     private final Handler handler = new Handler();
1173    
1174     private Handler
1175     getHandler() { return handler; }
1176    
1177     private class Handler implements ListListener<MidiInstrumentMap> {
1178     /** Invoked when a new map is added to a list. */
1179     public void
1180     entryAdded(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1181    
1182     /** Invoked when a map is removed from a list. */
1183     public void
1184     entryRemoved(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1185    
1186     private void
1187     updateDefaultMap() {
1188     if(getDefaultMidiInstrumentMap() != findDefaultMidiInstrumentMap()) {
1189     defaultMidiInstrumentMap = findDefaultMidiInstrumentMap();
1190     fireDefaultMapChanged();
1191     }
1192     }
1193     }
1194 iliev 787 }

  ViewVC Help
Powered by ViewVC