/[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 1285 - (hide annotations) (download)
Fri Aug 10 19:55:03 2007 UTC (16 years, 8 months ago) by iliev
File size: 33466 byte(s)
* Updated to version 0.6a. The Fantasia distribution is now
  capable of controlling all features available in LinuxSampler

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

  ViewVC Help
Powered by ViewVC