1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
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 javax.swing.SwingUtilities; |
28 |
|
29 |
import org.jsampler.event.AudioDeviceEvent; |
30 |
import org.jsampler.event.AudioDeviceListener; |
31 |
|
32 |
import org.jsampler.task.Audio; |
33 |
|
34 |
import org.linuxsampler.lscp.AudioOutputDevice; |
35 |
import org.linuxsampler.lscp.Parameter; |
36 |
|
37 |
|
38 |
/** |
39 |
* This class provides default implementation of the <code>AudioDeviceModel</code> interface. |
40 |
* @author Grigor Iliev |
41 |
*/ |
42 |
public class DefaultAudioDeviceModel implements AudioDeviceModel { |
43 |
private AudioOutputDevice audioDevice; |
44 |
|
45 |
private final Vector<AudioDeviceListener> listeners = new Vector<AudioDeviceListener>(); |
46 |
|
47 |
/** |
48 |
* Creates a new instance of <code>DefaultAudioDeviceModel</code> using the |
49 |
* specified non-null audio device. |
50 |
* @param audioDevice An <code>AudioOutputDevice</code> instance providing the current |
51 |
* settings of the audio device which will be represented by this model. |
52 |
* @throws IllegalArgumentException If <code>audioDevice</code> is <code>null</code>. |
53 |
*/ |
54 |
public |
55 |
DefaultAudioDeviceModel(AudioOutputDevice audioDevice) { |
56 |
if(audioDevice == null) |
57 |
throw new IllegalArgumentException("audioDevice must be non null"); |
58 |
|
59 |
this.audioDevice = audioDevice; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Registers the specified listener to be notified when |
64 |
* the settings of the audio device are changed. |
65 |
* @param l The <code>AudioDeviceListener</code> to register. |
66 |
*/ |
67 |
public void |
68 |
addAudioDeviceListener(AudioDeviceListener l) { listeners.add(l); } |
69 |
|
70 |
/** |
71 |
* Removes the specified listener. |
72 |
* @param l The <code>AudioDeviceListener</code> to remove. |
73 |
*/ |
74 |
public void |
75 |
removeAudioDeviceListener(AudioDeviceListener l) { listeners.remove(l); } |
76 |
|
77 |
/** |
78 |
* Gets the numerical ID of this audio device. |
79 |
* @return The numerical ID of this audio device or |
80 |
* -1 if the device number is not set. |
81 |
*/ |
82 |
public int |
83 |
getDeviceId() { return audioDevice.getDeviceId(); } |
84 |
|
85 |
/** |
86 |
* Gets the current settings of the audio device represented by this model. |
87 |
* @return <code>AudioOutputDevice</code> instance providing |
88 |
* the current settings of the audio device represented by this model. |
89 |
*/ |
90 |
public AudioOutputDevice |
91 |
getDeviceInfo() { return audioDevice; } |
92 |
|
93 |
/** |
94 |
* Updates the settings of the audio device represented by this model. |
95 |
* @param device The new audio device settings. |
96 |
*/ |
97 |
public void |
98 |
setDeviceInfo(AudioOutputDevice device) { |
99 |
audioDevice = device; |
100 |
fireSettingsChanged(); |
101 |
} |
102 |
|
103 |
/** |
104 |
* Sets whether the audio device is enabled or disabled. |
105 |
* @param active If <code>true</code> the audio device is enabled, |
106 |
* else the device is disabled. |
107 |
*/ |
108 |
public void |
109 |
setActive(boolean active) { |
110 |
if(active == getDeviceInfo().isActive()) return; |
111 |
|
112 |
audioDevice.setActive(active); |
113 |
fireSettingsChanged(); |
114 |
} |
115 |
|
116 |
/** |
117 |
* Determines whether the audio device is active. |
118 |
* @return <code>true</code> if the device is enabled and <code>false</code> otherwise. |
119 |
*/ |
120 |
public boolean |
121 |
isActive() { return audioDevice.isActive(); } |
122 |
|
123 |
/** |
124 |
* Schedules a new task for enabling/disabling the audio device. |
125 |
* @param active If <code>true</code> the audio device is enabled, |
126 |
* else the device is disabled. |
127 |
*/ |
128 |
public void |
129 |
setBackendActive(boolean active) { |
130 |
CC.getTaskQueue().add(new Audio.EnableDevice(getDeviceId(), active)); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Schedules a new task for changing the channel number of the audio device. |
135 |
* @param channels The new number of audio channels. |
136 |
*/ |
137 |
public void |
138 |
setBackendChannelCount(int channels) { |
139 |
CC.getTaskQueue().add(new Audio.SetChannelCount(getDeviceId(), channels)); |
140 |
} |
141 |
|
142 |
/** |
143 |
* Schedules a new task for altering a specific |
144 |
* setting of the specified audio output channel. |
145 |
* @param channel The channel number. |
146 |
* @param prm The parameter to be set. |
147 |
*/ |
148 |
public void |
149 |
setBackendChannelParameter(int channel, Parameter prm) { |
150 |
CC.getTaskQueue().add(new Audio.SetChannelParameter(getDeviceId(), channel, prm)); |
151 |
} |
152 |
|
153 |
/** |
154 |
* Notifies listeners that the settings of the audio device are changed. |
155 |
*/ |
156 |
private void |
157 |
fireSettingsChanged() { |
158 |
fireSettingsChanged(new AudioDeviceEvent(this, this)); |
159 |
} |
160 |
|
161 |
/** |
162 |
* Notifies listeners that the settings of the audio device are changed. |
163 |
* This method should be invoked from the event-dispatching thread. |
164 |
*/ |
165 |
private void |
166 |
fireSettingsChanged(final AudioDeviceEvent e) { |
167 |
SwingUtilities.invokeLater(new Runnable() { |
168 |
public void |
169 |
run() { for(AudioDeviceListener l : listeners) l.settingsChanged(e); } |
170 |
}); |
171 |
} |
172 |
} |