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