1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2006 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.logging.Level; |
26 |
|
27 |
import org.jsampler.AudioDeviceModel; |
28 |
import org.jsampler.CC; |
29 |
import org.jsampler.HF; |
30 |
import org.jsampler.SamplerModel; |
31 |
|
32 |
import org.linuxsampler.lscp.AudioOutputDevice; |
33 |
import org.linuxsampler.lscp.AudioOutputDriver; |
34 |
import org.linuxsampler.lscp.Parameter; |
35 |
|
36 |
import static org.jsampler.JSI18n.i18n; |
37 |
|
38 |
|
39 |
/** |
40 |
* Provides the audio specific tasks. |
41 |
* @author Grigor Iliev |
42 |
*/ |
43 |
public class Audio { |
44 |
/** Forbits the instantiation of this class. */ |
45 |
private Audio() { } |
46 |
|
47 |
/** |
48 |
* This task retrieves all audio output drivers currently |
49 |
* available for the LinuxSampler instance. |
50 |
*/ |
51 |
public static class GetDrivers extends EnhancedTask<AudioOutputDriver[]> { |
52 |
/** Creates a new instance of <code>GetDrivers</code>. */ |
53 |
public |
54 |
GetDrivers() { |
55 |
setTitle("Audio.GetDrivers_task"); |
56 |
setDescription(i18n.getMessage("Audio.GetDrivers.desc")); |
57 |
} |
58 |
|
59 |
/** The entry point of the task. */ |
60 |
public void |
61 |
run() { |
62 |
try { setResult(CC.getClient().getAudioOutputDrivers()); } |
63 |
catch(Exception x) { |
64 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
65 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
66 |
} |
67 |
} |
68 |
} |
69 |
|
70 |
/** |
71 |
* This task retrieves detailed information about all parameters |
72 |
* of the specified audio output driver. |
73 |
*/ |
74 |
public static class GetDriverParametersInfo extends EnhancedTask<Parameter[]> { |
75 |
private String driver; |
76 |
Parameter[] depList; |
77 |
|
78 |
/** |
79 |
* Creates a new instance of <code>GetDriverParametersInfo</code>. |
80 |
* @param depList - A dependences list. |
81 |
*/ |
82 |
public |
83 |
GetDriverParametersInfo(String driver, Parameter... depList) { |
84 |
setTitle("Audio.GetDriverParametersInfo_task"); |
85 |
setDescription(i18n.getMessage("Audio.GetDriverParametersInfo.desc")); |
86 |
|
87 |
this.driver = driver; |
88 |
this.depList = depList; |
89 |
} |
90 |
|
91 |
/** The entry point of the task. */ |
92 |
public void |
93 |
run() { |
94 |
try { |
95 |
AudioOutputDriver d; |
96 |
d = CC.getClient().getAudioOutputDriverInfo(driver, depList); |
97 |
setResult(d.getParameters()); |
98 |
} |
99 |
catch(Exception x) { |
100 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
101 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
/** |
107 |
* This task creates a new audio output device. |
108 |
*/ |
109 |
public static class CreateDevice extends EnhancedTask<Integer> { |
110 |
private String driver; |
111 |
private Parameter[] parameters; |
112 |
|
113 |
|
114 |
/** |
115 |
* Creates a new instance of <code>CreateDevice</code>. |
116 |
* @param driver The desired audio output system. |
117 |
* @param parameters An optional list of driver specific parameters. |
118 |
*/ |
119 |
public |
120 |
CreateDevice(String driver, Parameter... parameters) { |
121 |
setTitle("Audio.CreateDevice_task"); |
122 |
setDescription(i18n.getMessage("Audio.CreateDevice.desc")); |
123 |
|
124 |
this.driver = driver; |
125 |
this.parameters = parameters; |
126 |
} |
127 |
|
128 |
/** The entry point of the task. */ |
129 |
public void |
130 |
run() { |
131 |
try { |
132 |
// TODO: move this to jlscp |
133 |
java.util.Vector<Parameter> v = new java.util.Vector<Parameter>(); |
134 |
for(Parameter p : parameters) { |
135 |
if(p.getValue() != null) v.add(p); |
136 |
} |
137 |
parameters = v.toArray(new Parameter[v.size()]); |
138 |
/////// |
139 |
|
140 |
Integer deviceId = |
141 |
CC.getClient().createAudioOutputDevice(driver, parameters); |
142 |
setResult(deviceId); |
143 |
} catch(Exception x) { |
144 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
145 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
146 |
} |
147 |
} |
148 |
} |
149 |
|
150 |
/** |
151 |
* This task destroys the specified audio output device. |
152 |
*/ |
153 |
public static class DestroyDevice extends EnhancedTask { |
154 |
private int deviceId; |
155 |
|
156 |
|
157 |
/** |
158 |
* Creates a new instance of <code>DestroyDevice</code>. |
159 |
* @param deviceId The ID of the audio output device to be destroyed. |
160 |
*/ |
161 |
public |
162 |
DestroyDevice(int deviceId) { |
163 |
setTitle("Audio.DestroyDevice_task"); |
164 |
setDescription(i18n.getMessage("Audio.DestroyDevice.desc", deviceId)); |
165 |
|
166 |
this.deviceId = deviceId; |
167 |
} |
168 |
|
169 |
/** The entry point of the task. */ |
170 |
public void |
171 |
run() { |
172 |
try { CC.getClient().destroyAudioOutputDevice(deviceId); } |
173 |
catch(Exception x) { |
174 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
175 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
/** |
181 |
* This task enables/disables a specific audio output device. |
182 |
*/ |
183 |
public static class EnableDevice extends EnhancedTask { |
184 |
private int dev; |
185 |
private boolean enable; |
186 |
|
187 |
/** |
188 |
* Creates new instance of <code>EnableDevice</code>. |
189 |
* @param dev The id of the device to be enabled/disabled. |
190 |
* @param enable Specify <code>true</code> to enable the audio device; |
191 |
* code>false</code> to disable it. |
192 |
*/ |
193 |
public |
194 |
EnableDevice(int dev, boolean enable) { |
195 |
setTitle("Audio.EnableDevice_task"); |
196 |
setDescription(i18n.getMessage("Audio.EnableDevice.desc", dev)); |
197 |
|
198 |
this.dev = dev; |
199 |
this.enable = enable; |
200 |
} |
201 |
|
202 |
/** The entry point of the task. */ |
203 |
public void |
204 |
run() { |
205 |
try { |
206 |
CC.getClient().enableAudioOutputDevice(dev, enable); |
207 |
|
208 |
// Not needed, but eventually speeds up the change. |
209 |
CC.getSamplerModel().getAudioDeviceById(dev).setActive(enable); |
210 |
} catch(Exception x) { |
211 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
212 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
213 |
} |
214 |
} |
215 |
} |
216 |
|
217 |
/** |
218 |
* This task alters a specific setting of an audio output device. |
219 |
*/ |
220 |
public static class SetDeviceParameter extends EnhancedTask { |
221 |
private int dev; |
222 |
private Parameter prm; |
223 |
|
224 |
/** |
225 |
* Creates new instance of <code>SetDeviceParameter</code>. |
226 |
* @param dev The id of the device whose parameter should be set. |
227 |
* @param prm The parameter to be set. |
228 |
*/ |
229 |
public |
230 |
SetDeviceParameter(int dev, Parameter prm) { |
231 |
setTitle("Audio.SetDeviceParameter_task"); |
232 |
setDescription(i18n.getMessage("Audio.SetDeviceParameter.desc", dev)); |
233 |
|
234 |
this.dev = dev; |
235 |
this.prm = prm; |
236 |
} |
237 |
|
238 |
/** The entry point of the task. */ |
239 |
public void |
240 |
run() { |
241 |
try { CC.getClient().setAudioOutputDeviceParameter(dev, prm); } |
242 |
catch(Exception x) { |
243 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
244 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
245 |
} |
246 |
} |
247 |
} |
248 |
|
249 |
/** |
250 |
* This task alters a specific setting of an audio output channel. |
251 |
*/ |
252 |
public static class SetChannelParameter extends EnhancedTask { |
253 |
private int dev; |
254 |
private int channel; |
255 |
private Parameter prm; |
256 |
|
257 |
/** |
258 |
* Creates new instance of <code>SetChannelParameter</code>. |
259 |
* @param dev The id of the device whose channel parameter should be set. |
260 |
* @param channel The channel number. |
261 |
* @param prm The parameter to be set. |
262 |
*/ |
263 |
public |
264 |
SetChannelParameter(int dev, int channel, Parameter prm) { |
265 |
setTitle("Audio.SetChannelParameter_task"); |
266 |
setDescription(i18n.getMessage("Audio.SetChannelParameter.desc")); |
267 |
|
268 |
this.dev = dev; |
269 |
this.channel = channel; |
270 |
this.prm = prm; |
271 |
} |
272 |
|
273 |
/** The entry point of the task. */ |
274 |
public void |
275 |
run() { |
276 |
try { CC.getClient().setAudioOutputChannelParameter(dev, channel, prm); } |
277 |
catch(Exception x) { |
278 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
279 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
/** |
285 |
* This task changes the channel number of the speicifed audio output device. |
286 |
*/ |
287 |
public static class SetChannelCount extends EnhancedTask { |
288 |
private int deviceId; |
289 |
private int channels; |
290 |
|
291 |
/** |
292 |
* Creates new instance of <code>SetChannelCount</code>. |
293 |
* @param deviceId The id of the device whose channels number will be changed. |
294 |
* @param channels The new number of audio channels. |
295 |
*/ |
296 |
public |
297 |
SetChannelCount(int deviceId, int channels) { |
298 |
setTitle("SetAudioOutputChannelCount_task"); |
299 |
setDescription(i18n.getMessage("Audio.SetChannelCount.desc", deviceId)); |
300 |
|
301 |
this.deviceId = deviceId; |
302 |
this.channels = channels; |
303 |
} |
304 |
|
305 |
/** The entry point of the task. */ |
306 |
public void |
307 |
run() { |
308 |
try { CC.getClient().setAudioOutputChannelCount(deviceId, channels); } |
309 |
catch(Exception x) { |
310 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
311 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
|
317 |
/** |
318 |
* This task updates the setting of an audio output device. |
319 |
*/ |
320 |
public static class UpdateDeviceInfo extends EnhancedTask { |
321 |
private int dev; |
322 |
|
323 |
/** |
324 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
325 |
* @param dev The id of the device, which settings should be updated. |
326 |
*/ |
327 |
public |
328 |
UpdateDeviceInfo(int dev) { |
329 |
setTitle("Audio.UpdateDeviceInfo_task"); |
330 |
setDescription(i18n.getMessage("Audio.UpdateDeviceInfo.desc", dev)); |
331 |
|
332 |
this.dev = dev; |
333 |
} |
334 |
|
335 |
/** The entry point of the task. */ |
336 |
public void |
337 |
run() { |
338 |
try { |
339 |
AudioOutputDevice d = CC.getClient().getAudioOutputDeviceInfo(dev); |
340 |
CC.getSamplerModel().getAudioDeviceById(dev).setDeviceInfo(d); |
341 |
} catch(Exception x) { |
342 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
343 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
344 |
} |
345 |
} |
346 |
} |
347 |
|
348 |
/** |
349 |
* This task updates the audio output device list. |
350 |
*/ |
351 |
public static class UpdateDevices extends EnhancedTask { |
352 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
353 |
public |
354 |
UpdateDevices() { |
355 |
setTitle("Audio.UpdateDevices_task"); |
356 |
setDescription(i18n.getMessage("Audio.UpdateDevices.desc")); |
357 |
} |
358 |
|
359 |
/** The entry point of the task. */ |
360 |
public void |
361 |
run() { |
362 |
try { |
363 |
SamplerModel sm = CC.getSamplerModel(); |
364 |
Integer[] devIDs = CC.getClient().getAudioOutputDeviceIDs(); |
365 |
|
366 |
boolean found = false; |
367 |
|
368 |
for(AudioDeviceModel m : sm.getAudioDevices()) { |
369 |
for(int i = 0; i < devIDs.length; i++) { |
370 |
if(m.getDeviceId() == devIDs[i]) { |
371 |
devIDs[i] = -1; |
372 |
found = true; |
373 |
} |
374 |
} |
375 |
|
376 |
if(!found) sm.removeAudioDeviceById(m.getDeviceId()); |
377 |
found = false; |
378 |
} |
379 |
|
380 |
AudioOutputDevice d; |
381 |
|
382 |
for(int id : devIDs) { |
383 |
if(id >= 0) { |
384 |
d = CC.getClient().getAudioOutputDeviceInfo(id); |
385 |
sm.addAudioDevice(d); |
386 |
} |
387 |
} |
388 |
} catch(Exception x) { |
389 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
390 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
391 |
} |
392 |
} |
393 |
} |
394 |
|
395 |
} |