/[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 1341 - (hide annotations) (download)
Mon Sep 10 22:29:09 2007 UTC (16 years, 7 months ago) by iliev
File size: 33760 byte(s)
* Fantasia: Added button to the channel screen for starting an instrument
  editor (point the mouse cursor over the channel screen and click 'Edit')

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 iliev 1341 CC.getTaskQueue().add(new Channel.Remove(channelId));
704 iliev 1143 }
705    
706     /**
707 iliev 1341 * Schedules a new task for starting an instrument editor for editing
708     * the loaded instrument on the specified sampler channel.
709     * @param channelId The sampler channel number.
710     */
711     public void
712     editBackendInstrument(int channelId) {
713     CC.getTaskQueue().add(new Channel.EditInstrument(channelId));
714     }
715    
716     /**
717 iliev 787 * Determines whether there is at least one solo channel in the current list
718     * of sampler channels.
719     * @return <code>true</code> if there is at least one solo channel in the current list of
720     * sampler channels, <code>false</code> otherwise.
721     */
722     public boolean
723     hasSoloChannel() {
724     for(SamplerChannelModel m : channelModels)
725     if(m.getChannelInfo().isSoloChannel()) return true;
726    
727     return false;
728     }
729    
730     /**
731     * Gets the number of solo channels in the current list of sampler channels.
732     * @return The number of solo channels in the current list of sampler channels.
733     */
734     public int
735     getSoloChannelCount() {
736     int count = 0;
737     for(SamplerChannelModel m : channelModels)
738     if(m.getChannelInfo().isSoloChannel()) count++;
739    
740     return count;
741     }
742    
743     /**
744     * Gets the number of muted channels in the current list of sampler channels.
745     * This number includes the channels muted because of the presence of a solo channel.
746     * @return The number of muted channels in the current list of sampler channels.
747     */
748     public int
749     getMutedChannelCount() {
750     int count = 0;
751     for(SamplerChannelModel m : channelModels)
752     if(m.getChannelInfo().isMuted()) count++;
753    
754     return count;
755     }
756    
757     /**
758     * Gets the number of channels muted because of the presence of a solo channel.
759     * @return The number of channels muted because of the presence of a solo channel.
760     */
761     public int
762     getMutedBySoloChannelCount() {
763     int count = 0;
764     for(SamplerChannelModel m : channelModels)
765     if(m.getChannelInfo().isMutedBySolo()) count++;
766    
767     return count;
768     }
769    
770     /**
771     * Gets the total number of active voices.
772     * @return The total number of active voices.
773     */
774     public int
775     getTotalVoiceCount() { return totalVoiceCount; }
776    
777     /**
778     * Gets the maximum number of active voices.
779     * @return The maximum number of active voices.
780     */
781     public int
782     getTotalVoiceCountMax() { return totalVoiceCountMax; }
783    
784     /**
785 iliev 1143 * Gets the golobal volume of the sampler.
786     * @return The golobal volume of the sampler.
787     */
788     public float
789     getVolume() { return volume; }
790    
791     /**
792     * Sets the global volume.
793     * @param volume The new volume value.
794     */
795     public void
796     setVolume(float volume) {
797     if(this.volume == volume) return;
798    
799     this.volume = volume;
800     fireVolumeChanged();
801     }
802    
803     /**
804     * Schedules a new task for setting the global volume on the backend side.
805     * @param volume The new volume value.
806     */
807     public void
808     setBackendVolume(float volume) {
809     CC.getTaskQueue().add(new Global.SetVolume(volume));
810     }
811    
812     /**
813     * Schedules a new task for resetting the sampler.
814     */
815     public void
816 iliev 1204 resetBackend() { CC.getTaskQueue().add(new org.jsampler.task.Global.ResetSampler()); }
817 iliev 1143
818     /**
819 iliev 787 * Updates the current and the maximum number of active voices in the sampler.
820     * @param count The new number of active voices.
821     * @param countMax The maximum number of active voices.
822     */
823     public void
824     updateActiveVoiceInfo(int count, int countMax) {
825     if(totalVoiceCount == count && totalVoiceCountMax == countMax) return;
826    
827     totalVoiceCount = count;
828     totalVoiceCountMax = countMax;
829     fireTotalVoiceCountChanged();
830     }
831    
832     /**
833     * Notifies listeners that a sampler channel has been added.
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     fireSamplerChannelAdded(SamplerChannelModel channelModel) {
839     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
840    
841     SwingUtilities.invokeLater(new Runnable() {
842     public void
843     run() { fireSamplerChannelAdded(e); }
844     });
845     }
846     /**
847     * Notifies listeners that a sampler channel has been added.
848     */
849     private void
850     fireSamplerChannelAdded(SamplerChannelListEvent e) {
851     Object[] listeners = listenerList.getListenerList();
852    
853     for(int i = listeners.length - 2; i >= 0; i -= 2) {
854     if(listeners[i] == SamplerChannelListListener.class) {
855     ((SamplerChannelListListener)listeners[i + 1]).channelAdded(e);
856     }
857     }
858     }
859    
860     /**
861     * Notifies listeners that a sampler channel has been removed.
862 iliev 1143 * This method can be invoked outside the event-dispatching thread.
863 iliev 787 * @param channelModel A <code>SamplerChannelModel</code> instance.
864     */
865     private void
866     fireSamplerChannelRemoved(SamplerChannelModel channelModel) {
867     final SamplerChannelListEvent e = new SamplerChannelListEvent(this, channelModel);
868    
869     SwingUtilities.invokeLater(new Runnable() {
870     public void
871     run() { fireSamplerChannelRemoved(e); }
872     });
873     }
874    
875     /**
876     * Notifies listeners that a sampler channel has been removed.
877     */
878     private void
879     fireSamplerChannelRemoved(SamplerChannelListEvent e) {
880     Object[] listeners = listenerList.getListenerList();
881    
882     for(int i = listeners.length - 2; i >= 0; i -= 2) {
883     if(listeners[i] == SamplerChannelListListener.class) {
884     ((SamplerChannelListListener)listeners[i + 1]).channelRemoved(e);
885     }
886     }
887     }
888    
889     /**
890     * Notifies listeners that a MIDI device has been added.
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     fireMidiDeviceAdded(MidiDeviceModel model) {
896     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
897    
898     SwingUtilities.invokeLater(new Runnable() {
899     public void
900     run() { fireMidiDeviceAdded(e); }
901     });
902     }
903     /**
904     * Notifies listeners that a MIDI device has been added.
905     */
906     private void
907     fireMidiDeviceAdded(MidiDeviceListEvent e) {
908     Object[] listeners = listenerList.getListenerList();
909    
910     for(int i = listeners.length - 2; i >= 0; i -= 2) {
911     if(listeners[i] == MidiDeviceListListener.class) {
912     ((MidiDeviceListListener)listeners[i + 1]).deviceAdded(e);
913     }
914     }
915     }
916    
917     /**
918     * Notifies listeners that a MIDI device has been removed.
919 iliev 1143 * This method can be invoked outside the event-dispatching thread.
920 iliev 787 * @param model A <code>MidiDeviceModel</code> instance.
921     */
922     private void
923     fireMidiDeviceRemoved(MidiDeviceModel model) {
924     final MidiDeviceListEvent e = new MidiDeviceListEvent(this, model);
925    
926     SwingUtilities.invokeLater(new Runnable() {
927     public void
928     run() { fireMidiDeviceRemoved(e); }
929     });
930     }
931    
932     /**
933     * Notifies listeners that a MIDI device has been removed.
934     */
935     private void
936     fireMidiDeviceRemoved(MidiDeviceListEvent e) {
937     Object[] listeners = listenerList.getListenerList();
938    
939     for(int i = listeners.length - 2; i >= 0; i -= 2) {
940     if(listeners[i] == MidiDeviceListListener.class) {
941     ((MidiDeviceListListener)listeners[i + 1]).deviceRemoved(e);
942     }
943     }
944     }
945    
946     /**
947     * Notifies listeners that an audio device has been added.
948 iliev 1143 * This method can be invoked outside the event-dispatching thread.
949 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
950     */
951     private void
952     fireAudioDeviceAdded(AudioDeviceModel model) {
953 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
954 iliev 787
955     SwingUtilities.invokeLater(new Runnable() {
956     public void
957     run() { fireAudioDeviceAdded(e); }
958     });
959     }
960    
961     /**
962     * Notifies listeners that an audio device has been added.
963     */
964     private void
965 iliev 1143 fireAudioDeviceAdded(ListEvent<AudioDeviceModel> e) {
966 iliev 787 Object[] listeners = listenerList.getListenerList();
967    
968     for(int i = listeners.length - 2; i >= 0; i -= 2) {
969 iliev 1143 if(listeners[i] == ListListener.class) {
970     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryAdded(e);
971 iliev 787 }
972     }
973     }
974    
975     /**
976     * Notifies listeners that an audio device has been removed.
977 iliev 1143 * This method can be invoked outside the event-dispatching thread.
978 iliev 787 * @param model A <code>AudioDeviceModel</code> instance.
979     */
980     private void
981     fireAudioDeviceRemoved(AudioDeviceModel model) {
982 iliev 1143 final ListEvent<AudioDeviceModel> e = new ListEvent<AudioDeviceModel>(this, model);
983 iliev 787
984     SwingUtilities.invokeLater(new Runnable() {
985     public void
986     run() { fireAudioDeviceRemoved(e); }
987     });
988     }
989    
990     /**
991 iliev 1143 * Notifies listeners that a MIDI instrument map has been added to the list.
992     * This method can be invoked outside the event-dispatching thread.
993     */
994     private void
995     fireMidiInstrumentMapAdded(MidiInstrumentMap map) {
996     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
997    
998     SwingUtilities.invokeLater(new Runnable() {
999     public void
1000     run() { fireMidiInstrumentMapAdded(e); }
1001     });
1002     }
1003    
1004     /** Notifies listeners that a MIDI instrument map has been added to the list. */
1005     private void
1006     fireMidiInstrumentMapAdded(ListEvent<MidiInstrumentMap> e) {
1007     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryAdded(e);
1008     }
1009    
1010     /**
1011     * Notifies listeners that a MIDI instrument map has been removed from the list.
1012     * This method can be invoked outside the event-dispatching thread.
1013     */
1014     private void
1015     fireMidiInstrumentMapRemoved(MidiInstrumentMap map) {
1016     final ListEvent<MidiInstrumentMap> e = new ListEvent<MidiInstrumentMap>(this, map);
1017    
1018     SwingUtilities.invokeLater(new Runnable() {
1019     public void
1020     run() { fireMidiInstrumentMapRemoved(e); }
1021     });
1022     }
1023     /** Notifies listeners that a MIDI instrument map has been removed from the list. */
1024     private void
1025     fireMidiInstrumentMapRemoved(ListEvent<MidiInstrumentMap> e) {
1026     for(ListListener<MidiInstrumentMap> l : mapsListeners) l.entryRemoved(e);
1027     }
1028    
1029    
1030     /**
1031 iliev 787 * Notifies listeners that an audio device has been removed.
1032     * This method should be invoked from the event-dispatching thread.
1033     */
1034     private void
1035 iliev 1143 fireAudioDeviceRemoved(ListEvent<AudioDeviceModel> e) {
1036 iliev 787 Object[] listeners = listenerList.getListenerList();
1037    
1038     for(int i = listeners.length - 2; i >= 0; i -= 2) {
1039 iliev 1143 if(listeners[i] == ListListener.class) {
1040     ((ListListener<AudioDeviceModel>)listeners[i + 1]).entryRemoved(e);
1041 iliev 787 }
1042     }
1043     }
1044    
1045 iliev 1143 /**
1046     * Notifies listeners that the global volume has changed.
1047     * This method can be invoked outside the event-dispatching thread.
1048     */
1049 iliev 787 private void
1050 iliev 1143 fireVolumeChanged() {
1051     final SamplerEvent e = new SamplerEvent(this);
1052    
1053     SwingUtilities.invokeLater(new Runnable() {
1054     public void
1055     run() { fireVolumeChanged(e); }
1056     });
1057     }
1058    
1059     /**
1060     * Notifies listeners that the global volume has changed.
1061     */
1062     private void
1063     fireVolumeChanged(SamplerEvent e) {
1064     for(SamplerListener l : listeners) l.volumeChanged(e);
1065     }
1066    
1067     /*
1068     * Notifies listeners that the total number of active voices has changed.
1069     * This method can be invoked outside the event-dispatching thread.
1070     */
1071     private void
1072 iliev 787 fireTotalVoiceCountChanged() {
1073     final SamplerEvent e = new SamplerEvent(this);
1074    
1075     SwingUtilities.invokeLater(new Runnable() {
1076     public void
1077     run() { fireTotalVoiceCountChanged(e); }
1078     });
1079     }
1080    
1081     /**
1082     * Notifies listeners that the total number of active voices has changed.
1083     */
1084     private void
1085     fireTotalVoiceCountChanged(SamplerEvent e) {
1086     for(SamplerListener l : listeners) l.totalVoiceCountChanged(e);
1087     }
1088 iliev 1143
1089     /**
1090     * Notifies listeners that the global volume has changed.
1091     */
1092     private void
1093     fireDefaultMapChanged() {
1094     SamplerEvent e = new SamplerEvent(this);
1095     for(SamplerListener l : listeners) l.defaultMapChanged(e);
1096     }
1097    
1098     private final Handler handler = new Handler();
1099    
1100     private Handler
1101     getHandler() { return handler; }
1102    
1103     private class Handler implements ListListener<MidiInstrumentMap> {
1104     /** Invoked when a new map is added to a list. */
1105     public void
1106     entryAdded(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1107    
1108     /** Invoked when a map is removed from a list. */
1109     public void
1110     entryRemoved(ListEvent<MidiInstrumentMap> e) { updateDefaultMap(); }
1111    
1112     private void
1113     updateDefaultMap() {
1114     if(getDefaultMidiInstrumentMap() != findDefaultMidiInstrumentMap()) {
1115     defaultMidiInstrumentMap = findDefaultMidiInstrumentMap();
1116     fireDefaultMapChanged();
1117     }
1118     }
1119     }
1120 iliev 787 }

  ViewVC Help
Powered by ViewVC