/[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 1737 - (hide annotations) (download)
Thu May 8 17:26:19 2008 UTC (15 years, 11 months ago) by iliev
File size: 37714 byte(s)
* Major memory optimizations when too many sampler channels are present

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

  ViewVC Help
Powered by ViewVC