/[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 1204 - (hide annotations) (download)
Thu May 24 21:43:45 2007 UTC (16 years, 11 months ago) by iliev
File size: 32937 byte(s)
upgrading to version 0.5a

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     * Adds the specified MIDI instrument map.
422     * @param map The MIDI instrument map to be added.
423     * @throws IllegalArgumentException If <code>map</code> is <code>null</code>.
424     */
425     public void
426     addMidiInstrumentMap(MidiInstrumentMap map) {
427     if(map == null) throw new IllegalArgumentException("map should be non-null!");
428    
429     midiInstrMaps.add(map);
430     fireMidiInstrumentMapAdded(map);
431     }
432    
433     /**
434     * Schedules a new task for creating a new MIDI instrument map on the backend side.
435     * @param name The name of the MIDI instrument map.
436     * @throws IllegalArgumentException If <code>name</code> is <code>null</code>.
437     */
438     public void
439     addBackendMidiInstrumentMap(String name) {
440     if(name == null) throw new IllegalArgumentException("name should be non-null!");
441    
442     CC.getTaskQueue().add(new Midi.AddInstrumentMap(name));
443     }
444    
445     /**
446     * Removes the specified MIDI instrument map.
447     * @param mapId The ID of the MIDI instrument map to be removed.
448     * @return <code>true</code> if the MIDI instrument map is removed successfully,
449     * <code>false</code> if the MIDI instrument map's list does not contain
450     * MIDI instrument map with ID <code>mapId</code>.
451     */
452     public boolean
453 iliev 1204 removeMidiInstrumentMapById(int mapId) {
454 iliev 1143 for(int i = 0; i < midiInstrMaps.size(); i++) {
455     MidiInstrumentMap m = getMidiInstrumentMap(i);
456     if(m.getMapId() == mapId) {
457     midiInstrMaps.remove(i);
458     fireMidiInstrumentMapRemoved(m);
459     return true;
460     }
461     }
462    
463     return false;
464     }
465    
466     /**
467     * Removes the specified MIDI instrument map.
468     * @param map The MIDI instrument map to remove.
469     * @return <code>true</code> if the specified MIDI instrument map was in the list,
470     * <code>false</code> otherwise.
471     */
472     public boolean
473     removeMidiInstrumentMap(MidiInstrumentMap map) {
474     boolean b = midiInstrMaps.removeElement(map);
475     if(b) fireMidiInstrumentMapRemoved(map);
476     return b;
477     }
478    
479     /** Removes all MIDI instrument maps. */
480     public void
481     removeAllMidiInstrumentMaps() {
482     for(int i = midiInstrMaps.size() - 1; i >= 0; i--) {
483     MidiInstrumentMap map = midiInstrMaps.get(i);
484     midiInstrMaps.removeElementAt(i);
485     fireMidiInstrumentMapRemoved(map);
486     }
487     }
488    
489     /**
490     * Schedules a new task for removing the
491     * specified MIDI instrument map on the backend side.
492     * @param mapId The numerical ID of the MIDI instrument map to remove.
493     */
494     public void
495     removeBackendMidiInstrumentMap(int mapId) {
496     CC.getTaskQueue().add(new Midi.RemoveInstrumentMap(mapId));
497     }
498    
499     /**
500     * Schedules a new task for changing the name of
501     * the specified MIDI instrument map on the backend side.
502     * @param mapId The numerical ID of the MIDI instrument map.
503     * @param name The new name for the specified MIDI instrument map.
504     */
505     public void
506     setBackendMidiInstrumentMapName(final int mapId, String name) {
507     final Task t = new Midi.SetInstrumentMapInfo(mapId, name);
508    
509     t.addTaskListener(new TaskListener() {
510     public void
511     taskPerformed(TaskEvent e) {
512     /*
513     * Because with the invokation of the method the task is considered
514     * to be done, if the task fails, we must update the settings.
515     */
516     if(t.doneWithErrors()) {
517     Task t2 = new Midi.UpdateInstrumentMapInfo(mapId);
518     CC.getTaskQueue().add(t2);
519     }
520     }
521     });
522     CC.getTaskQueue().add(t);
523     }
524    
525     /**
526     * Gets the default MIDI instrument map.
527     * @return The default MIDI instrument map or <code>null</code>
528     * if there are no maps created.
529     */
530     public MidiInstrumentMap
531     getDefaultMidiInstrumentMap() {
532     return defaultMidiInstrumentMap;
533     }
534    
535     /**
536     * Gets the default MIDI instrument map.
537     * @return The default MIDI instrument map or <code>null</code>
538     * if there are no maps created.
539     */
540     private MidiInstrumentMap
541     findDefaultMidiInstrumentMap() {
542     for(int i = 0; i < getMidiInstrumentMapCount(); i++) {
543     MidiInstrumentMap m = getMidiInstrumentMap(i);
544     if(m.getInfo().isDefault()) return m;
545     }
546    
547     return null;
548     }
549    
550     /**
551     * Schedules a new task for mapping a MIDI instrument on the backend side.
552     * @param mapId The id of the MIDI instrument map.
553     * @param bank The index of the MIDI bank, which shall contain the instrument.
554     * @param program The MIDI program number of the new instrument.
555     * @param instrInfo Provides the MIDI instrument settings.
556     */
557     public void
558     mapBackendMidiInstrument(int mapId, int bank, int program, MidiInstrumentInfo instrInfo) {
559     CC.getTaskQueue().add(new Midi.MapInstrument(mapId, bank, program, instrInfo));
560     }
561    
562     /**
563     * Schedules a new task for removing a MIDI instrument on the backend side.
564     * @param mapId The id of the MIDI instrument map containing the instrument to be removed.
565     * @param bank The index of the MIDI bank containing the instrument to be removed.
566     * @param program The MIDI program number of the instrument to be removed.
567     */
568     public void
569     unmapBackendMidiInstrument(int mapId, int bank, int program) {
570     CC.getTaskQueue().add(new Midi.UnmapInstrument(mapId, bank, program));
571     }
572    
573     /**
574 iliev 787 * Gets a list of all available engines.
575     * @return A list of all available engines.
576     */
577     public SamplerEngine[]
578     getEngines() { return engines; }
579    
580     /**
581     * Sets the list of all available engines.
582     * @param engines The new list of all available engines.
583     */
584     public void
585     setEngines(SamplerEngine[] engines) { this.engines = engines; }
586    
587     /**
588 iliev 1143 * Gets the model of the sampler channel with ID <code>channelId</code>.
589     * @param channelId The ID of the sampler channel whose model should be obtained.
590 iliev 787 * @return The model of the specified sampler channel or <code>null</code>
591 iliev 1143 * if there is no channel with ID <code>channelId</code>.
592 iliev 787 */
593     public SamplerChannelModel
594 iliev 1204 getChannelById(int channelId) {
595 iliev 787 for(SamplerChannelModel m : channelModels)
596 iliev 1143 if(m.getChannelId() == channelId) return m;
597 iliev 787
598     return null;
599     }
600    
601     /**
602     * Gets the current number of sampler channels.
603     * @return The current number of sampler channels.
604     */
605     public int
606     getChannelCount() { return channelModels.size(); }
607    
608     /**
609     * Gets the current list of sampler channel models.
610     * @return The current list of sampler channel models.
611     */
612     public SamplerChannelModel[]
613 iliev 1204 getChannels() {
614 iliev 787 return channelModels.toArray(new SamplerChannelModel[channelModels.size()]);
615     }
616    
617     /**
618 iliev 1143 * Schedules a new task for adding a new sampler channel on the
619     * backend side. The channel will be actually added to this model
620     * when the backend notifies for its creation.
621 iliev 787 * @see #addChannel
622     */
623     public void
624 iliev 1143 addBackendChannel() {
625     CC.getTaskQueue().add(new Channel.Add());
626 iliev 787 // We leave this event to be notified by the LinuxSampler notification system.
627     }
628    
629     /**
630     * Adds the specified sampler channel.
631     * @param channel The channel to be added.
632     */
633     public void
634     addChannel(SamplerChannel channel) {
635     DefaultSamplerChannelModel model = new DefaultSamplerChannelModel(channel);
636     channelModels.add(model);
637     fireSamplerChannelAdded(model);
638     }
639    
640     /**
641     * Updates the settings of the specified channel.
642     * @param channel A <code>SamplerChannel</code> instance containing the new settings
643     * for the channel.
644     */
645     public void
646 iliev 1143 updateChannel(SamplerChannel channel) {
647 iliev 787 for(SamplerChannelModel m : channelModels) {
648 iliev 1143 if(m.getChannelId() == channel.getChannelId()) {
649 iliev 787 m.setChannelInfo(channel);
650     return;
651     }
652     }
653    
654     CC.getLogger().log (
655 iliev 1143 Level.WARNING, "DefaultSamplerModel.unknownChannel!", channel.getChannelId()
656 iliev 787 );
657     }
658    
659     /**
660     * Removes the specified sampler channel.
661 iliev 1143 * @param channelId The ID of the channel to be removed.
662 iliev 787 * @return <code>true</code> if the channel is removed successfully, <code>false</code>
663 iliev 1143 * if the channel's list does not contain channel with ID <code>channelId</code>.
664 iliev 787 */
665     public boolean
666 iliev 1204 removeChannelById(int channelId) {
667 iliev 787 for(int i = 0; i < channelModels.size(); i++) {
668     SamplerChannelModel m = channelModels.get(i);
669 iliev 1143 if(m.getChannelId() == channelId) {
670 iliev 787 channelModels.remove(i);
671     fireSamplerChannelRemoved(m);
672     return true;
673     }
674     }
675    
676     return false;
677     }
678    
679     /**
680 iliev 1143 * Schedules a new task for removing the specified sampler channel on the backend side.
681     * @param channelId The ID of the channel to be removed.
682     */
683     public void
684     removeBackendChannel(int channelId) {
685     CC.getTaskQueue().add(new org.jsampler.task.Channel.Remove(channelId));
686     }
687    
688     /**
689 iliev 787 * Determines whether there is at least one solo channel in the current list
690     * of sampler channels.
691     * @return <code>true</code> if there is at least one solo channel in the current list of
692     * sampler channels, <code>false</code> otherwise.
693     */
694     public boolean
695     hasSoloChannel() {
696     for(SamplerChannelModel m : channelModels)
697     if(m.getChannelInfo().isSoloChannel()) return true;
698    
699     return false;
700     }
701    
702     /**
703     * Gets the number of solo channels in the current list of sampler channels.
704     * @return The number of solo channels in the current list of sampler channels.
705     */
706     public int
707     getSoloChannelCount() {
708     int count = 0;
709     for(SamplerChannelModel m : channelModels)
710     if(m.getChannelInfo().isSoloChannel()) count++;
711    
712     return count;
713     }
714    
715     /**
716     * Gets the number of muted channels in the current list of sampler channels.
717     * This number includes the channels muted because of the presence of a solo channel.
718     * @return The number of muted channels in the current list of sampler channels.
719     */
720     public int
721     getMutedChannelCount() {
722     int count = 0;
723     for(SamplerChannelModel m : channelModels)
724     if(m.getChannelInfo().isMuted()) count++;
725    
726     return count;
727     }
728    
729     /**
730     * Gets the number of channels muted because of the presence of a solo channel.
731     * @return The number of channels muted because of the presence of a solo channel.
732     */
733     public int
734     getMutedBySoloChannelCount() {
735     int count = 0;
736     for(SamplerChannelModel m : channelModels)
737     if(m.getChannelInfo().isMutedBySolo()) count++;
738    
739     return count;
740     }
741    
742     /**
743     * Gets the total number of active voices.
744     * @return The total number of active voices.
745     */
746     public int
747     getTotalVoiceCount() { return totalVoiceCount; }
748    
749     /**
750     * Gets the maximum number of active voices.
751     * @return The maximum number of active voices.
752     */
753     public int
754     getTotalVoiceCountMax() { return totalVoiceCountMax; }
755    
756     /**
757 iliev 1143 * Gets the golobal volume of the sampler.
758     * @return The golobal volume of the sampler.
759     */
760     public float
761     getVolume() { return volume; }
762    
763     /**
764     * Sets the global volume.
765     * @param volume The new volume value.
766     */
767     public void
768     setVolume(float volume) {
769     if(this.volume == volume) return;
770    
771     this.volume = volume;
772     fireVolumeChanged();
773     }
774    
775     /**
776     * Schedules a new task for setting the global volume on the backend side.
777     * @param volume The new volume value.
778     */
779     public void
780     setBackendVolume(float volume) {
781     CC.getTaskQueue().add(new Global.SetVolume(volume));
782     }
783    
784     /**
785     * Schedules a new task for resetting the sampler.
786     */
787     public void
788 iliev 1204 resetBackend() { CC.getTaskQueue().add(new org.jsampler.task.Global.ResetSampler()); }
789 iliev 1143
790     /**
791 iliev 787 * Updates the current and the maximum number of active voices in the sampler.
792     * @param count The new number of active voices.
793     * @param countMax The maximum number of active voices.
794     */
795     public void
796     updateActiveVoiceInfo(int count, int countMax) {
797     if(totalVoiceCount == count && totalVoiceCountMax == countMax) return;
798    
799     totalVoiceCount = count;
800     totalVoiceCountMax = countMax;
801     fireTotalVoiceCountChanged();
802     }
803    
804     /**
805     * Notifies listeners that a sampler channel has been added.
806 iliev 1143 * This method can be invoked outside the event-dispatching thread.
807 iliev 787 * @param channelModel A <code>SamplerChannelModel</code> instance.
808     */
809     private void
810     fireSamplerChannelAdded(SamplerChannelModel channelModel) {
811     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
812    
813     SwingUtilities.invokeLater(new Runnable() {
814     public void
815     run() { fireSamplerChannelAdded(e); }
816     });
817     }
818     /**
819     * Notifies listeners that a sampler channel has been added.
820     */
821     private void
822     fireSamplerChannelAdded(SamplerChannelListEvent e) {
823     Object[] listeners = listenerList.getListenerList();
824    
825     for(int i = listeners.length - 2; i >= 0; i -= 2) {
826     if(listeners[i] == SamplerChannelListListener.class) {
827     ((SamplerChannelListListener)listeners[i + 1]).channelAdded(e);
828     }
829     }
830     }
831    
832     /**
833     * Notifies listeners that a sampler channel has been removed.
834 iliev 1143 * This method can be invoked outside the event-dispatching thread.
835 iliev 787 * @param channelModel A <code>SamplerChannelModel</code> instance.
836     */
837     private void
838     fireSamplerChannelRemoved(SamplerChannelModel channelModel) {
839     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
840    
841     SwingUtilities.invokeLater(new Runnable() {
842     public void
843     run() { fireSamplerChannelRemoved(e); }
844     });
845     }
846    
847     /**
848     * Notifies listeners that a sampler channel has been removed.
849     */
850     private void
851     fireSamplerChannelRemoved(SamplerChannelListEvent e) {
852     Object[] listeners = listenerList.getListenerList();
853    
854     for(int i = listeners.length - 2; i >= 0; i -= 2) {
855     if(listeners[i] == SamplerChannelListListener.class) {
856     ((SamplerChannelListListener)listeners[i + 1]).channelRemoved(e);
857     }
858     }
859     }
860    
861     /**
862     * Notifies listeners that a MIDI device has been added.
863 iliev 1143 * This method can be invoked outside the event-dispatching thread.
864 iliev 787 * @param model A <code>MidiDeviceModel</code> instance.
865     */
866     private void
867     fireMidiDeviceAdded(MidiDeviceModel model) {
868     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
869    
870     SwingUtilities.invokeLater(new Runnable() {
871     public void
872     run() { fireMidiDeviceAdded(e); }
873     });
874     }
875     /**
876     * Notifies listeners that a MIDI device has been added.
877     */
878     private void
879     fireMidiDeviceAdded(MidiDeviceListEvent e) {
880     Object[] listeners = listenerList.getListenerList();
881    
882     for(int i = listeners.length - 2; i >= 0; i -= 2) {
883     if(listeners[i] == MidiDeviceListListener.class) {
884     ((MidiDeviceListListener)listeners[i + 1]).deviceAdded(e);
885     }
886     }
887     }
888    
889     /**
890     * Notifies listeners that a MIDI device has been removed.
891 iliev 1143 * This method can be invoked outside the event-dispatching thread.
892 iliev 787 * @param model A <code>MidiDeviceModel</code> instance.
893     */
894     private void
895     fireMidiDeviceRemoved(MidiDeviceModel model) {
896     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
897    
898     SwingUtilities.invokeLater(new Runnable() {
899     public void
900     run() { fireMidiDeviceRemoved(e); }
901     });
902     }
903    
904     /**
905     * Notifies listeners that a MIDI device has been removed.
906     */
907     private void
908     fireMidiDeviceRemoved(MidiDeviceListEvent e) {
909     Object[] listeners = listenerList.getListenerList();
910    
911     for(int i = listeners.length - 2; i >= 0; i -= 2) {
912     if(listeners[i] == MidiDeviceListListener.class) {
913     ((MidiDeviceListListener)listeners[i + 1]).deviceRemoved(e);
914     }
915     }
916     }
917    
918     /**
919     * Notifies listeners that an audio device has been added.
920 iliev 1143 * This method can be invoked outside the event-dispatching thread.
921 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
922     */
923     private void
924     fireAudioDeviceAdded(AudioDeviceModel model) {
925 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
926 iliev 787
927     SwingUtilities.invokeLater(new Runnable() {
928     public void
929     run() { fireAudioDeviceAdded(e); }
930     });
931     }
932    
933     /**
934     * Notifies listeners that an audio device has been added.
935     */
936     private void
937 iliev 1143 fireAudioDeviceAdded(ListEvent<AudioDeviceModel> e) {
938 iliev 787 Object[] listeners = listenerList.getListenerList();
939    
940     for(int i = listeners.length - 2; i >= 0; i -= 2) {
941 iliev 1143 if(listeners[i] == ListListener.class) {
942     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryAdded(e);
943 iliev 787 }
944     }
945     }
946    
947     /**
948     * Notifies listeners that an audio device has been removed.
949 iliev 1143 * This method can be invoked outside the event-dispatching thread.
950 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
951     */
952     private void
953     fireAudioDeviceRemoved(AudioDeviceModel model) {
954 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
955 iliev 787
956     SwingUtilities.invokeLater(new Runnable() {
957     public void
958     run() { fireAudioDeviceRemoved(e); }
959     });
960     }
961    
962     /**
963 iliev 1143 * Notifies listeners that a MIDI instrument map has been added to the list.
964     * This method can be invoked outside the event-dispatching thread.
965     */
966     private void
967     fireMidiInstrumentMapAdded(MidiInstrumentMap map) {
968     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
969    
970     SwingUtilities.invokeLater(new Runnable() {
971     public void
972     run() { fireMidiInstrumentMapAdded(e); }
973     });
974     }
975    
976     /** Notifies listeners that a MIDI instrument map has been added to the list. */
977     private void
978     fireMidiInstrumentMapAdded(ListEvent<MidiInstrumentMap> e) {
979     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryAdded(e);
980     }
981    
982     /**
983     * Notifies listeners that a MIDI instrument map has been removed from the list.
984     * This method can be invoked outside the event-dispatching thread.
985     */
986     private void
987     fireMidiInstrumentMapRemoved(MidiInstrumentMap map) {
988     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
989    
990     SwingUtilities.invokeLater(new Runnable() {
991     public void
992     run() { fireMidiInstrumentMapRemoved(e); }
993     });
994     }
995     /** Notifies listeners that a MIDI instrument map has been removed from the list. */
996     private void
997     fireMidiInstrumentMapRemoved(ListEvent<MidiInstrumentMap> e) {
998     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryRemoved(e);
999     }
1000    
1001    
1002     /**
1003 iliev 787 * Notifies listeners that an audio device has been removed.
1004     * This method should be invoked from the event-dispatching thread.
1005     */
1006     private void
1007 iliev 1143 fireAudioDeviceRemoved(ListEvent<AudioDeviceModel> e) {
1008 iliev 787 Object[] listeners = listenerList.getListenerList();
1009    
1010     for(int i = listeners.length - 2; i >= 0; i -= 2) {
1011 iliev 1143 if(listeners[i] == ListListener.class) {
1012     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryRemoved(e);
1013 iliev 787 }
1014     }
1015     }
1016    
1017 iliev 1143 /**
1018     * Notifies listeners that the global volume has changed.
1019     * This method can be invoked outside the event-dispatching thread.
1020     */
1021 iliev 787 private void
1022 iliev 1143 fireVolumeChanged() {
1023     final SamplerEvent e = new SamplerEvent(this);
1024    
1025     SwingUtilities.invokeLater(new Runnable() {
1026     public void
1027     run() { fireVolumeChanged(e); }
1028     });
1029     }
1030    
1031     /**
1032     * Notifies listeners that the global volume has changed.
1033     */
1034     private void
1035     fireVolumeChanged(SamplerEvent e) {
1036     for(SamplerListener l : listeners) l.volumeChanged(e);
1037     }
1038    
1039     /*
1040     * Notifies listeners that the total number of active voices has changed.
1041     * This method can be invoked outside the event-dispatching thread.
1042     */
1043     private void
1044 iliev 787 fireTotalVoiceCountChanged() {
1045     final SamplerEvent e = new SamplerEvent(this);
1046    
1047     SwingUtilities.invokeLater(new Runnable() {
1048     public void
1049     run() { fireTotalVoiceCountChanged(e); }
1050     });
1051     }
1052    
1053     /**
1054     * Notifies listeners that the total number of active voices has changed.
1055     */
1056     private void
1057     fireTotalVoiceCountChanged(SamplerEvent e) {
1058     for(SamplerListener l : listeners) l.totalVoiceCountChanged(e);
1059     }
1060 iliev 1143
1061     /**
1062     * Notifies listeners that the global volume has changed.
1063     */
1064     private void
1065     fireDefaultMapChanged() {
1066     SamplerEvent e = new SamplerEvent(this);
1067     for(SamplerListener l : listeners) l.defaultMapChanged(e);
1068     }
1069    
1070     private final Handler handler = new Handler();
1071    
1072     private Handler
1073     getHandler() { return handler; }
1074    
1075     private class Handler implements ListListener<MidiInstrumentMap> {
1076     /** Invoked when a new map is added to a list. */
1077     public void
1078     entryAdded(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1079    
1080     /** Invoked when a map is removed from a list. */
1081     public void
1082     entryRemoved(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1083    
1084     private void
1085     updateDefaultMap() {
1086     if(getDefaultMidiInstrumentMap() != findDefaultMidiInstrumentMap()) {
1087     defaultMidiInstrumentMap = findDefaultMidiInstrumentMap();
1088     fireDefaultMapChanged();
1089     }
1090     }
1091     }
1092 iliev 787 }

  ViewVC Help
Powered by ViewVC