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.task; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
|
27 |
import org.jsampler.AudioDeviceModel; |
28 |
import org.jsampler.CC; |
29 |
import org.jsampler.EffectChain; |
30 |
import org.jsampler.SamplerModel; |
31 |
|
32 |
import org.linuxsampler.lscp.AudioOutputDevice; |
33 |
import org.linuxsampler.lscp.AudioOutputDriver; |
34 |
import org.linuxsampler.lscp.EffectInstance; |
35 |
import org.linuxsampler.lscp.Effect; |
36 |
import org.linuxsampler.lscp.Parameter; |
37 |
|
38 |
import static org.jsampler.JSI18n.i18n; |
39 |
|
40 |
|
41 |
/** |
42 |
* Provides the audio specific tasks. |
43 |
* @author Grigor Iliev |
44 |
*/ |
45 |
public class Audio { |
46 |
/** Forbids the instantiation of this class. */ |
47 |
private Audio() { } |
48 |
|
49 |
/** |
50 |
* This task retrieves all audio output drivers currently |
51 |
* available for the LinuxSampler instance. |
52 |
*/ |
53 |
public static class GetDrivers extends EnhancedTask<AudioOutputDriver[]> { |
54 |
/** Creates a new instance of <code>GetDrivers</code>. */ |
55 |
public |
56 |
GetDrivers() { |
57 |
setTitle("Audio.GetDrivers_task"); |
58 |
setDescription(i18n.getMessage("Audio.GetDrivers.desc")); |
59 |
} |
60 |
|
61 |
/** The entry point of the task. */ |
62 |
@Override |
63 |
public void |
64 |
exec() throws Exception { setResult(CC.getClient().getAudioOutputDrivers()); } |
65 |
} |
66 |
|
67 |
/** |
68 |
* This task retrieves detailed information about all parameters |
69 |
* of the specified audio output driver. |
70 |
*/ |
71 |
public static class GetDriverParametersInfo extends EnhancedTask<Parameter[]> { |
72 |
private String driver; |
73 |
Parameter[] depList; |
74 |
|
75 |
/** |
76 |
* Creates a new instance of <code>GetDriverParametersInfo</code>. |
77 |
* @param depList - A dependences list. |
78 |
*/ |
79 |
public |
80 |
GetDriverParametersInfo(String driver, Parameter... depList) { |
81 |
setTitle("Audio.GetDriverParametersInfo_task"); |
82 |
setDescription(i18n.getMessage("Audio.GetDriverParametersInfo.desc")); |
83 |
|
84 |
this.driver = driver; |
85 |
this.depList = depList; |
86 |
} |
87 |
|
88 |
/** The entry point of the task. */ |
89 |
@Override |
90 |
public void |
91 |
exec() throws Exception { |
92 |
AudioOutputDriver d; |
93 |
d = CC.getClient().getAudioOutputDriverInfo(driver, depList); |
94 |
setResult(d.getParameters()); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* This task creates a new audio output device. |
100 |
*/ |
101 |
public static class CreateDevice extends EnhancedTask<Integer> { |
102 |
private String driver; |
103 |
private Parameter[] parameters; |
104 |
|
105 |
|
106 |
/** |
107 |
* Creates a new instance of <code>CreateDevice</code>. |
108 |
* @param driver The desired audio output system. |
109 |
* @param parameters An optional list of driver specific parameters. |
110 |
*/ |
111 |
public |
112 |
CreateDevice(String driver, Parameter... parameters) { |
113 |
setTitle("Audio.CreateDevice_task"); |
114 |
setDescription(i18n.getMessage("Audio.CreateDevice.desc")); |
115 |
|
116 |
this.driver = driver; |
117 |
this.parameters = parameters; |
118 |
} |
119 |
|
120 |
/** The entry point of the task. */ |
121 |
@Override |
122 |
public void |
123 |
exec() throws Exception { |
124 |
Integer deviceId = CC.getClient().createAudioOutputDevice(driver, parameters); |
125 |
setResult(deviceId); |
126 |
} |
127 |
} |
128 |
|
129 |
/** |
130 |
* This task destroys the specified audio output device. |
131 |
*/ |
132 |
public static class DestroyDevice extends EnhancedTask { |
133 |
private int deviceId; |
134 |
|
135 |
|
136 |
/** |
137 |
* Creates a new instance of <code>DestroyDevice</code>. |
138 |
* @param deviceId The ID of the audio output device to be destroyed. |
139 |
*/ |
140 |
public |
141 |
DestroyDevice(int deviceId) { |
142 |
setTitle("Audio.DestroyDevice_task"); |
143 |
setDescription(i18n.getMessage("Audio.DestroyDevice.desc", deviceId)); |
144 |
|
145 |
this.deviceId = deviceId; |
146 |
} |
147 |
|
148 |
/** The entry point of the task. */ |
149 |
@Override |
150 |
public void |
151 |
exec() throws Exception { CC.getClient().destroyAudioOutputDevice(deviceId); } |
152 |
} |
153 |
|
154 |
/** |
155 |
* This task enables/disables a specific audio output device. |
156 |
*/ |
157 |
public static class EnableDevice extends EnhancedTask { |
158 |
private int dev; |
159 |
private boolean enable; |
160 |
|
161 |
/** |
162 |
* Creates new instance of <code>EnableDevice</code>. |
163 |
* @param dev The id of the device to be enabled/disabled. |
164 |
* @param enable Specify <code>true</code> to enable the audio device; |
165 |
* code>false</code> to disable it. |
166 |
*/ |
167 |
public |
168 |
EnableDevice(int dev, boolean enable) { |
169 |
setTitle("Audio.EnableDevice_task"); |
170 |
setDescription(i18n.getMessage("Audio.EnableDevice.desc", dev)); |
171 |
|
172 |
this.dev = dev; |
173 |
this.enable = enable; |
174 |
} |
175 |
|
176 |
/** The entry point of the task. */ |
177 |
@Override |
178 |
public void |
179 |
exec() throws Exception { |
180 |
CC.getClient().enableAudioOutputDevice(dev, enable); |
181 |
|
182 |
// Not needed, but eventually speeds up the change. |
183 |
CC.getSamplerModel().getAudioDeviceById(dev).setActive(enable); |
184 |
} |
185 |
} |
186 |
|
187 |
/** |
188 |
* This task alters a specific setting of an audio output device. |
189 |
*/ |
190 |
public static class SetDeviceParameter extends EnhancedTask { |
191 |
private int dev; |
192 |
private Parameter prm; |
193 |
|
194 |
/** |
195 |
* Creates new instance of <code>SetDeviceParameter</code>. |
196 |
* @param dev The id of the device whose parameter should be set. |
197 |
* @param prm The parameter to be set. |
198 |
*/ |
199 |
public |
200 |
SetDeviceParameter(int dev, Parameter prm) { |
201 |
setTitle("Audio.SetDeviceParameter_task"); |
202 |
setDescription(i18n.getMessage("Audio.SetDeviceParameter.desc", dev)); |
203 |
|
204 |
this.dev = dev; |
205 |
this.prm = prm; |
206 |
} |
207 |
|
208 |
/** The entry point of the task. */ |
209 |
@Override |
210 |
public void |
211 |
exec() throws Exception { |
212 |
CC.getClient().setAudioOutputDeviceParameter(dev, prm); |
213 |
} |
214 |
} |
215 |
|
216 |
/** |
217 |
* This task alters a specific setting of an audio output channel. |
218 |
*/ |
219 |
public static class SetChannelParameter extends EnhancedTask { |
220 |
private int dev; |
221 |
private int channel; |
222 |
private Parameter prm; |
223 |
|
224 |
/** |
225 |
* Creates new instance of <code>SetChannelParameter</code>. |
226 |
* @param dev The id of the device whose channel parameter should be set. |
227 |
* @param channel The channel number. |
228 |
* @param prm The parameter to be set. |
229 |
*/ |
230 |
public |
231 |
SetChannelParameter(int dev, int channel, Parameter prm) { |
232 |
setTitle("Audio.SetChannelParameter_task"); |
233 |
setDescription(i18n.getMessage("Audio.SetChannelParameter.desc")); |
234 |
|
235 |
this.dev = dev; |
236 |
this.channel = channel; |
237 |
this.prm = prm; |
238 |
} |
239 |
|
240 |
/** The entry point of the task. */ |
241 |
@Override |
242 |
public void |
243 |
exec() throws Exception { |
244 |
CC.getClient().setAudioOutputChannelParameter(dev, channel, prm); |
245 |
} |
246 |
} |
247 |
|
248 |
/** |
249 |
* This task changes the channel number of the speicifed audio output device. |
250 |
*/ |
251 |
public static class SetChannelCount extends EnhancedTask { |
252 |
private int deviceId; |
253 |
private int channels; |
254 |
|
255 |
/** |
256 |
* Creates new instance of <code>SetChannelCount</code>. |
257 |
* @param deviceId The id of the device whose channels number will be changed. |
258 |
* @param channels The new number of audio channels. |
259 |
*/ |
260 |
public |
261 |
SetChannelCount(int deviceId, int channels) { |
262 |
setTitle("SetAudioOutputChannelCount_task"); |
263 |
setDescription(i18n.getMessage("Audio.SetChannelCount.desc", deviceId)); |
264 |
|
265 |
this.deviceId = deviceId; |
266 |
this.channels = channels; |
267 |
} |
268 |
|
269 |
/** The entry point of the task. */ |
270 |
@Override |
271 |
public void |
272 |
exec() throws Exception { |
273 |
CC.getClient().setAudioOutputChannelCount(deviceId, channels); |
274 |
} |
275 |
} |
276 |
|
277 |
/** |
278 |
* This task adds a send effect chain to the specified audio output device. |
279 |
*/ |
280 |
public static class AddSendEffectChain extends EnhancedTask<Integer> { |
281 |
private int audioDeviceId; |
282 |
|
283 |
/** |
284 |
* Creates a new instance of <code>AddSendEffectChain</code>. |
285 |
* @param audioDeviceId The numerical ID of the audio output device. |
286 |
*/ |
287 |
public |
288 |
AddSendEffectChain(int audioDeviceId) { |
289 |
setTitle("Audio.AddSendEffectChain_task"); |
290 |
setDescription(i18n.getMessage("Audio.AddSendEffectChain.desc")); |
291 |
|
292 |
this.audioDeviceId = audioDeviceId; |
293 |
} |
294 |
|
295 |
/** The entry point of the task. */ |
296 |
@Override |
297 |
public void |
298 |
exec() throws Exception { |
299 |
Integer chainId = CC.getClient().addSendEffectChain(audioDeviceId); |
300 |
setResult(chainId); |
301 |
} |
302 |
} |
303 |
|
304 |
/** |
305 |
* This task removes the specified send effect chain of the specified audio output device. |
306 |
*/ |
307 |
public static class RemoveSendEffectChain extends EnhancedTask { |
308 |
private int audioDeviceId; |
309 |
private int chainId; |
310 |
|
311 |
/** |
312 |
* Creates a new instance of <code>RemoveSendEffectChain</code>. |
313 |
* @param audioDeviceId The numerical ID of the audio output device. |
314 |
* @param chainId The numerical ID of the send effect chain to remove. |
315 |
*/ |
316 |
public |
317 |
RemoveSendEffectChain(int audioDeviceId, int chainId) { |
318 |
setTitle("Audio.RemoveSendEffectChain_task"); |
319 |
setDescription(i18n.getMessage("Audio.RemoveSendEffectChain.desc")); |
320 |
|
321 |
this.audioDeviceId = audioDeviceId; |
322 |
this.chainId = chainId; |
323 |
} |
324 |
|
325 |
/** The entry point of the task. */ |
326 |
@Override |
327 |
public void |
328 |
exec() throws Exception { |
329 |
AudioDeviceModel adm = CC.getSamplerModel().getAudioDevice(audioDeviceId); |
330 |
EffectChain chain = adm.getSendEffectChainById(chainId); |
331 |
|
332 |
for(int i = chain.getEffectInstanceCount() - 1; i >= 0; i--) { |
333 |
CC.getClient().removeEffectInstanceFromChain ( |
334 |
audioDeviceId, chainId, i |
335 |
); |
336 |
|
337 |
int iid = chain.getEffectInstance(i).getInstanceId(); |
338 |
CC.getClient().destroyEffectInstance(iid); |
339 |
} |
340 |
CC.getClient().removeSendEffectChain(audioDeviceId, chainId); |
341 |
} |
342 |
} |
343 |
|
344 |
/** |
345 |
* This task creates new effect instances and inserts them |
346 |
* in the specified send effect chain at the specified position. |
347 |
*/ |
348 |
public static class AddNewEffectInstances extends EnhancedTask { |
349 |
private Effect[] effects; |
350 |
private int audioDeviceId; |
351 |
private int chainId; |
352 |
private int index; |
353 |
|
354 |
/** |
355 |
* Creates a new instance of <code>AddNewEffectInstances</code>. |
356 |
* @param audioDeviceId The numerical ID of the audio output device. |
357 |
* @param chainId The numerical ID of the send effect chain. |
358 |
* @param index The position in the chain where the newly created |
359 |
* effect instances should be inserted to. Use -1 to append. |
360 |
*/ |
361 |
public |
362 |
AddNewEffectInstances(Effect[] effects, int audioDeviceId, int chainId, int index) { |
363 |
setTitle("Audio.AddNewEffectInstances_task"); |
364 |
setDescription(i18n.getMessage("Audio.AddNewEffectInstances.desc")); |
365 |
|
366 |
this.effects = effects; |
367 |
this.audioDeviceId = audioDeviceId; |
368 |
this.chainId = chainId; |
369 |
this.index = index; |
370 |
} |
371 |
|
372 |
/** The entry point of the task. */ |
373 |
@Override |
374 |
public void |
375 |
exec() throws Exception { |
376 |
for(Effect e : effects) { |
377 |
int ei = CC.getClient().createEffectInstance(e); |
378 |
if(index != -1) { |
379 |
CC.getClient().insertEffectInstance(audioDeviceId, chainId, index, ei); |
380 |
} else { |
381 |
CC.getClient().appendEffectInstance(audioDeviceId, chainId, ei); |
382 |
} |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
/** |
388 |
* This task removes the specified effect instance from the specified send effect chain. |
389 |
*/ |
390 |
public static class RemoveEffectInstance extends EnhancedTask { |
391 |
private int audioDeviceId; |
392 |
private int chainId; |
393 |
private int instanceId; |
394 |
|
395 |
/** |
396 |
* Creates a new instance of <code>RemoveEffectInstance</code>. |
397 |
* @param audioDeviceId The numerical ID of the audio output device. |
398 |
* @param chainId The numerical ID of the send effect chain. |
399 |
* @param instanceId The numerical ID of the effect instance to remove. |
400 |
*/ |
401 |
public |
402 |
RemoveEffectInstance(int audioDeviceId, int chainId, int instanceId) { |
403 |
setTitle("Audio.RemoveEffectInstance_task"); |
404 |
setDescription(i18n.getMessage("Audio.RemoveEffectInstance.desc")); |
405 |
|
406 |
this.audioDeviceId = audioDeviceId; |
407 |
this.chainId = chainId; |
408 |
this.instanceId = instanceId; |
409 |
} |
410 |
|
411 |
/** The entry point of the task. */ |
412 |
@Override |
413 |
public void |
414 |
exec() throws Exception { |
415 |
AudioDeviceModel adm = CC.getSamplerModel().getAudioDevice(audioDeviceId); |
416 |
EffectChain chain = adm.getSendEffectChainById(chainId); |
417 |
|
418 |
CC.getClient().removeEffectInstanceFromChain ( |
419 |
audioDeviceId, chainId, chain.getIndex(instanceId) |
420 |
); |
421 |
|
422 |
CC.getClient().destroyEffectInstance(instanceId); |
423 |
|
424 |
} |
425 |
} |
426 |
|
427 |
|
428 |
/** |
429 |
* This task updates the send effect chain list of an audio output device. |
430 |
*/ |
431 |
public static class UpdateSendEffectChains extends EnhancedTask { |
432 |
private int devId; |
433 |
|
434 |
/** |
435 |
* Creates new instance of <code>UpdateSendEffectChains</code>. |
436 |
* @param devId The id of the device. |
437 |
*/ |
438 |
public |
439 |
UpdateSendEffectChains(int devId) { |
440 |
setTitle("Audio.UpdateSendEffectChains_task"); |
441 |
setDescription(i18n.getMessage("Audio.UpdateSendEffectChains.desc", devId)); |
442 |
|
443 |
this.devId = devId; |
444 |
} |
445 |
|
446 |
/** The entry point of the task. */ |
447 |
@Override |
448 |
public void |
449 |
exec() throws Exception { |
450 |
AudioDeviceModel m = CC.getSamplerModel().getAudioDeviceById(devId); |
451 |
|
452 |
Integer[] idS = CC.getClient().getSendEffectChainIDs(devId); |
453 |
|
454 |
ArrayList<Integer> removedChains = new ArrayList<Integer>(); |
455 |
|
456 |
for(int i = 0; i < m.getSendEffectChainCount(); i++) { |
457 |
boolean found = false; |
458 |
for(int j = 0; j < idS.length; j++) { |
459 |
if(idS[j] != null && m.getSendEffectChain(i).getChainId() == idS[j]) { |
460 |
found = true; |
461 |
idS[j] = null; |
462 |
} |
463 |
} |
464 |
if(!found) removedChains.add(m.getSendEffectChain(i).getChainId()); |
465 |
} |
466 |
|
467 |
for(int i : removedChains) m.removeSendEffectChain(i); |
468 |
|
469 |
for(int i = 0; i < idS.length; i++) { |
470 |
if(idS[i] != null) { |
471 |
m.addSendEffectChain ( |
472 |
new EffectChain(CC.getClient().getSendEffectChainInfo(devId, idS[i])) |
473 |
); |
474 |
} |
475 |
} |
476 |
} |
477 |
} |
478 |
|
479 |
/** |
480 |
* This task updates the list of effect instances. |
481 |
*/ |
482 |
public static class UpdateEffectInstances extends EnhancedTask { |
483 |
private int audioDeviceId; |
484 |
private int chainId; |
485 |
|
486 |
/** |
487 |
* Creates a new instance of <code>UpdateEffectInstances</code>. |
488 |
* @param audioDeviceId The numerical ID of the audio output device. |
489 |
* @param chainId The numerical ID of the send effect chain. |
490 |
*/ |
491 |
public |
492 |
UpdateEffectInstances(int audioDeviceId, int chainId) { |
493 |
setTitle("Audio.UpdateEffectInstances_task"); |
494 |
setDescription(i18n.getMessage("Audio.UpdateEffectInstances.desc")); |
495 |
|
496 |
this.audioDeviceId = audioDeviceId; |
497 |
this.chainId = chainId; |
498 |
} |
499 |
|
500 |
/** The entry point of the task. */ |
501 |
@Override |
502 |
public void |
503 |
exec() throws Exception { |
504 |
setSilent(true); |
505 |
|
506 |
EffectChain c = new EffectChain ( |
507 |
CC.getClient().getSendEffectChainInfo(audioDeviceId, chainId) |
508 |
); |
509 |
AudioDeviceModel m = CC.getSamplerModel().getAudioDeviceById(audioDeviceId); |
510 |
m.getSendEffectChainById(chainId).setEffectInstances(c.getEffectInstances()); |
511 |
} |
512 |
} |
513 |
|
514 |
|
515 |
/** |
516 |
* This task updates the setting of an audio output device. |
517 |
*/ |
518 |
public static class UpdateDeviceInfo extends EnhancedTask { |
519 |
private int dev; |
520 |
|
521 |
/** |
522 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
523 |
* @param dev The id of the device, which settings should be updated. |
524 |
*/ |
525 |
public |
526 |
UpdateDeviceInfo(int dev) { |
527 |
setTitle("Audio.UpdateDeviceInfo_task"); |
528 |
setDescription(i18n.getMessage("Audio.UpdateDeviceInfo.desc", dev)); |
529 |
|
530 |
this.dev = dev; |
531 |
} |
532 |
|
533 |
/** The entry point of the task. */ |
534 |
@Override |
535 |
public void |
536 |
exec() throws Exception { |
537 |
AudioOutputDevice d = CC.getClient().getAudioOutputDeviceInfo(dev); |
538 |
CC.getSamplerModel().getAudioDeviceById(dev).setDeviceInfo(d); |
539 |
} |
540 |
} |
541 |
|
542 |
/** |
543 |
* This task updates the audio output device list. |
544 |
*/ |
545 |
public static class UpdateDevices extends EnhancedTask { |
546 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
547 |
public |
548 |
UpdateDevices() { |
549 |
setTitle("Audio.UpdateDevices_task"); |
550 |
setDescription(i18n.getMessage("Audio.UpdateDevices.desc")); |
551 |
} |
552 |
|
553 |
/** The entry point of the task. */ |
554 |
@Override |
555 |
public void |
556 |
exec() throws Exception { |
557 |
SamplerModel sm = CC.getSamplerModel(); |
558 |
Integer[] devIDs = CC.getClient().getAudioOutputDeviceIDs(); |
559 |
|
560 |
boolean found = false; |
561 |
|
562 |
for(AudioDeviceModel m : sm.getAudioDevices()) { |
563 |
for(int i = 0; i < devIDs.length; i++) { |
564 |
if(m.getDeviceId() == devIDs[i]) { |
565 |
devIDs[i] = -1; |
566 |
found = true; |
567 |
} |
568 |
} |
569 |
|
570 |
if(!found) sm.removeAudioDeviceById(m.getDeviceId()); |
571 |
found = false; |
572 |
} |
573 |
|
574 |
AudioOutputDevice d; |
575 |
|
576 |
for(int id : devIDs) { |
577 |
if(id >= 0) { |
578 |
d = CC.getClient().getAudioOutputDeviceInfo(id); |
579 |
sm.addAudioDevice(d); |
580 |
} |
581 |
} |
582 |
} |
583 |
} |
584 |
|
585 |
} |