1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2011 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.ArrayList; |
26 |
import java.util.Vector; |
27 |
|
28 |
import javax.swing.SwingUtilities; |
29 |
|
30 |
import org.jsampler.event.AudioDeviceEvent; |
31 |
import org.jsampler.event.AudioDeviceListener; |
32 |
|
33 |
import org.jsampler.task.Audio; |
34 |
|
35 |
import org.linuxsampler.lscp.AudioOutputDevice; |
36 |
import org.linuxsampler.lscp.Effect; |
37 |
import org.linuxsampler.lscp.Parameter; |
38 |
|
39 |
|
40 |
/** |
41 |
* This class provides default implementation of the <code>AudioDeviceModel</code> interface. |
42 |
* @author Grigor Iliev |
43 |
*/ |
44 |
public class DefaultAudioDeviceModel implements AudioDeviceModel { |
45 |
private AudioOutputDevice audioDevice; |
46 |
|
47 |
private final ArrayList<AudioDeviceListener> listeners = new ArrayList<AudioDeviceListener>(); |
48 |
private final ArrayList<EffectChain> effectChains = new ArrayList<EffectChain>(); |
49 |
|
50 |
/** |
51 |
* Creates a new instance of <code>DefaultAudioDeviceModel</code> using the |
52 |
* specified non-null audio device. |
53 |
* @param audioDevice An <code>AudioOutputDevice</code> instance providing the current |
54 |
* settings of the audio device which will be represented by this model. |
55 |
* @throws IllegalArgumentException If <code>audioDevice</code> is <code>null</code>. |
56 |
*/ |
57 |
public |
58 |
DefaultAudioDeviceModel(AudioOutputDevice audioDevice) { |
59 |
if(audioDevice == null) |
60 |
throw new IllegalArgumentException("audioDevice must be non null"); |
61 |
|
62 |
this.audioDevice = audioDevice; |
63 |
} |
64 |
|
65 |
/** |
66 |
* Registers the specified listener to be notified when |
67 |
* the settings of the audio device are changed. |
68 |
* @param l The <code>AudioDeviceListener</code> to register. |
69 |
*/ |
70 |
@Override |
71 |
public void |
72 |
addAudioDeviceListener(AudioDeviceListener l) { listeners.add(l); } |
73 |
|
74 |
/** |
75 |
* Removes the specified listener. |
76 |
* @param l The <code>AudioDeviceListener</code> to remove. |
77 |
*/ |
78 |
@Override |
79 |
public void |
80 |
removeAudioDeviceListener(AudioDeviceListener l) { listeners.remove(l); } |
81 |
|
82 |
/** |
83 |
* Gets the numerical ID of this audio device. |
84 |
* @return The numerical ID of this audio device or |
85 |
* -1 if the device number is not set. |
86 |
*/ |
87 |
@Override |
88 |
public int |
89 |
getDeviceId() { return audioDevice.getDeviceId(); } |
90 |
|
91 |
/** |
92 |
* Gets the current settings of the audio device represented by this model. |
93 |
* @return <code>AudioOutputDevice</code> instance providing |
94 |
* the current settings of the audio device represented by this model. |
95 |
*/ |
96 |
@Override |
97 |
public AudioOutputDevice |
98 |
getDeviceInfo() { return audioDevice; } |
99 |
|
100 |
/** |
101 |
* Updates the settings of the audio device represented by this model. |
102 |
* @param device The new audio device settings. |
103 |
*/ |
104 |
@Override |
105 |
public void |
106 |
setDeviceInfo(AudioOutputDevice device) { |
107 |
audioDevice = device; |
108 |
fireSettingsChanged(); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Sets whether the audio device is enabled or disabled. |
113 |
* @param active If <code>true</code> the audio device is enabled, |
114 |
* else the device is disabled. |
115 |
*/ |
116 |
@Override |
117 |
public void |
118 |
setActive(boolean active) { |
119 |
if(active == getDeviceInfo().isActive()) return; |
120 |
|
121 |
audioDevice.setActive(active); |
122 |
fireSettingsChanged(); |
123 |
} |
124 |
|
125 |
/** |
126 |
* Determines whether the audio device is active. |
127 |
* @return <code>true</code> if the device is enabled and <code>false</code> otherwise. |
128 |
*/ |
129 |
@Override |
130 |
public boolean |
131 |
isActive() { return audioDevice.isActive(); } |
132 |
|
133 |
/** |
134 |
* Schedules a new task for enabling/disabling the audio device. |
135 |
* @param active If <code>true</code> the audio device is enabled, |
136 |
* else the device is disabled. |
137 |
*/ |
138 |
@Override |
139 |
public void |
140 |
setBackendActive(boolean active) { |
141 |
CC.getTaskQueue().add(new Audio.EnableDevice(getDeviceId(), active)); |
142 |
} |
143 |
|
144 |
/** |
145 |
* Schedules a new task for altering |
146 |
* a specific setting of the audio output device. |
147 |
* @param prm The parameter to be set. |
148 |
*/ |
149 |
@Override |
150 |
public void |
151 |
setBackendDeviceParameter(Parameter prm) { |
152 |
CC.getTaskQueue().add(new Audio.SetDeviceParameter(getDeviceId(), prm)); |
153 |
} |
154 |
|
155 |
/** |
156 |
* Schedules a new task for changing the channel number of the audio device. |
157 |
* @param channels The new number of audio channels. |
158 |
*/ |
159 |
@Override |
160 |
public void |
161 |
setBackendChannelCount(int channels) { |
162 |
CC.getTaskQueue().add(new Audio.SetChannelCount(getDeviceId(), channels)); |
163 |
} |
164 |
|
165 |
/** |
166 |
* Schedules a new task for altering a specific |
167 |
* setting of the specified audio output channel. |
168 |
* @param channel The channel number. |
169 |
* @param prm The parameter to be set. |
170 |
*/ |
171 |
@Override |
172 |
public void |
173 |
setBackendChannelParameter(int channel, Parameter prm) { |
174 |
CC.getTaskQueue().add(new Audio.SetChannelParameter(getDeviceId(), channel, prm)); |
175 |
} |
176 |
|
177 |
/** Gets the current number of send effect chains. */ |
178 |
@Override |
179 |
public int |
180 |
getSendEffectChainCount() { return effectChains.size(); } |
181 |
|
182 |
/** Gets the effect chain at the specified position. */ |
183 |
@Override |
184 |
public EffectChain |
185 |
getSendEffectChain(int chainIdx) { return effectChains.get(chainIdx); } |
186 |
|
187 |
@Override |
188 |
public EffectChain |
189 |
getSendEffectChainById(int chainId) { |
190 |
for(int i = 0; i < getSendEffectChainCount(); i++) { |
191 |
EffectChain chain = getSendEffectChain(i); |
192 |
if(chain.getChainId() == chainId) return chain; |
193 |
} |
194 |
|
195 |
return null; |
196 |
} |
197 |
|
198 |
/** |
199 |
* Gets the index of the send effect chain with ID <code>chainId</code>. |
200 |
* @param chainId The ID of the send effect chain. |
201 |
* @return The zero-based position of the specified send effect chain |
202 |
* in the send effect chain list or <code>-1</code> |
203 |
* if there is no send effect chain with ID <code>chainId</code>. |
204 |
*/ |
205 |
public int getSendEffectChainIndex(int chainId) { |
206 |
for(int i = 0; i < getSendEffectChainCount(); i++) { |
207 |
if(getSendEffectChain(i).getChainId() == chainId) return i; |
208 |
} |
209 |
|
210 |
return -1; |
211 |
} |
212 |
|
213 |
/** |
214 |
* Adds the specified send effect chain to the specified audio output device. |
215 |
*/ |
216 |
@Override |
217 |
public void |
218 |
addSendEffectChain(EffectChain chain) { |
219 |
effectChains.add(chain); |
220 |
fireSendEffectChainAdded(chain); |
221 |
} |
222 |
|
223 |
/** |
224 |
* Removes the specified send effect chain from the audio output device. |
225 |
*/ |
226 |
@Override |
227 |
public void |
228 |
removeSendEffectChain(int chainId) { |
229 |
for(int i = 0; i < effectChains.size(); i++) { |
230 |
if(effectChains.get(i).getChainId() == chainId) { |
231 |
fireSendEffectChainRemoved(effectChains.remove(i)); |
232 |
return; |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
public void |
238 |
removeAllSendEffectChains() { |
239 |
for(int i = effectChains.size() - 1; i >= 0; i--) { |
240 |
fireSendEffectChainRemoved(effectChains.remove(i)); |
241 |
} |
242 |
} |
243 |
|
244 |
/** |
245 |
* Schedules a new task for adding a new send effect chain and |
246 |
* assigning it to the specified audio output device. |
247 |
*/ |
248 |
@Override |
249 |
public void |
250 |
addBackendSendEffectChain() { |
251 |
CC.getTaskQueue().add(new Audio.AddSendEffectChain(getDeviceId())); |
252 |
} |
253 |
|
254 |
/** Schedules a new task for removing the specified send effect chain. */ |
255 |
@Override |
256 |
public void |
257 |
removeBackendSendEffectChain(int chainId) { |
258 |
CC.getTaskQueue().add(new Audio.RemoveSendEffectChain(getDeviceId(), chainId)); |
259 |
} |
260 |
|
261 |
/** |
262 |
* Schedules a new task for creating new effect instances and inserting them |
263 |
* in the specified send effect chain at the specified position. |
264 |
*/ |
265 |
@Override |
266 |
public void |
267 |
addBackendEffectInstances(Effect[] effects, int chainId, int index) { |
268 |
CC.getTaskQueue().add ( |
269 |
new Audio.AddNewEffectInstances(effects, getDeviceId(), chainId, index) |
270 |
); |
271 |
} |
272 |
|
273 |
/** |
274 |
* Schedules a new task for removing the specified |
275 |
* effect instance from the specified send effect chain. |
276 |
*/ |
277 |
@Override |
278 |
public void |
279 |
removeBackendEffectInstance(int chainId, int instanceId) { |
280 |
CC.getTaskQueue().add(new Audio.RemoveEffectInstance(getDeviceId(), chainId, instanceId)); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Notifies listeners that the settings of the audio device are changed. |
285 |
*/ |
286 |
private void |
287 |
fireSettingsChanged() { |
288 |
SwingUtilities.invokeLater(new Runnable() { |
289 |
public void |
290 |
run() { |
291 |
AudioDeviceModel model = DefaultAudioDeviceModel.this; |
292 |
fireSettingsChanged(new AudioDeviceEvent(model, model)); |
293 |
} |
294 |
}); |
295 |
} |
296 |
|
297 |
/** |
298 |
* Notifies listeners that the settings of the audio device are changed. |
299 |
* This method should be invoked from the event-dispatching thread. |
300 |
*/ |
301 |
private void |
302 |
fireSettingsChanged(final AudioDeviceEvent e) { |
303 |
CC.getSamplerModel().setModified(true); |
304 |
for(AudioDeviceListener l : listeners) l.settingsChanged(e); |
305 |
} |
306 |
|
307 |
private void |
308 |
fireSendEffectChainAdded(final EffectChain chain) { |
309 |
SwingUtilities.invokeLater(new Runnable() { |
310 |
public void |
311 |
run() { |
312 |
AudioDeviceModel m = DefaultAudioDeviceModel.this; |
313 |
fireSendEffectChainAdded(new AudioDeviceEvent(m, m, chain)); |
314 |
} |
315 |
}); |
316 |
} |
317 |
|
318 |
/** This method should be invoked from the event-dispatching thread. */ |
319 |
private void |
320 |
fireSendEffectChainAdded(final AudioDeviceEvent e) { |
321 |
CC.getSamplerModel().setModified(true); |
322 |
for(AudioDeviceListener l : listeners) l.sendEffectChainAdded(e); |
323 |
} |
324 |
|
325 |
private void |
326 |
fireSendEffectChainRemoved(final EffectChain chain) { |
327 |
SwingUtilities.invokeLater(new Runnable() { |
328 |
public void |
329 |
run() { |
330 |
AudioDeviceModel m = DefaultAudioDeviceModel.this; |
331 |
fireSendEffectChainRemoved(new AudioDeviceEvent(m, m, chain)); |
332 |
} |
333 |
}); |
334 |
} |
335 |
|
336 |
/** This method should be invoked from the event-dispatching thread. */ |
337 |
private void |
338 |
fireSendEffectChainRemoved(final AudioDeviceEvent e) { |
339 |
CC.getSamplerModel().setModified(true); |
340 |
for(AudioDeviceListener l : listeners) l.sendEffectChainRemoved(e); |
341 |
} |
342 |
} |