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 |
Integer deviceId = |
133 |
CC.getClient().createAudioOutputDevice(driver, parameters); |
134 |
setResult(deviceId); |
135 |
} catch(Exception x) { |
136 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
137 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
/** |
143 |
* This task destroys the specified audio output device. |
144 |
*/ |
145 |
public static class DestroyDevice extends EnhancedTask { |
146 |
private int deviceId; |
147 |
|
148 |
|
149 |
/** |
150 |
* Creates a new instance of <code>DestroyDevice</code>. |
151 |
* @param deviceId The ID of the audio output device to be destroyed. |
152 |
*/ |
153 |
public |
154 |
DestroyDevice(int deviceId) { |
155 |
setTitle("Audio.DestroyDevice_task"); |
156 |
setDescription(i18n.getMessage("Audio.DestroyDevice.desc", deviceId)); |
157 |
|
158 |
this.deviceId = deviceId; |
159 |
} |
160 |
|
161 |
/** The entry point of the task. */ |
162 |
public void |
163 |
run() { |
164 |
try { CC.getClient().destroyAudioOutputDevice(deviceId); } |
165 |
catch(Exception x) { |
166 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
167 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
/** |
173 |
* This task enables/disables a specific audio output device. |
174 |
*/ |
175 |
public static class EnableDevice extends EnhancedTask { |
176 |
private int dev; |
177 |
private boolean enable; |
178 |
|
179 |
/** |
180 |
* Creates new instance of <code>EnableDevice</code>. |
181 |
* @param dev The id of the device to be enabled/disabled. |
182 |
* @param enable Specify <code>true</code> to enable the audio device; |
183 |
* code>false</code> to disable it. |
184 |
*/ |
185 |
public |
186 |
EnableDevice(int dev, boolean enable) { |
187 |
setTitle("Audio.EnableDevice_task"); |
188 |
setDescription(i18n.getMessage("Audio.EnableDevice.desc", dev)); |
189 |
|
190 |
this.dev = dev; |
191 |
this.enable = enable; |
192 |
} |
193 |
|
194 |
/** The entry point of the task. */ |
195 |
public void |
196 |
run() { |
197 |
try { |
198 |
CC.getClient().enableAudioOutputDevice(dev, enable); |
199 |
|
200 |
// Not needed, but eventually speeds up the change. |
201 |
CC.getSamplerModel().getAudioDeviceById(dev).setActive(enable); |
202 |
} catch(Exception x) { |
203 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
204 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
205 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
/** |
210 |
* This task alters a specific setting of an audio output device. |
211 |
*/ |
212 |
public static class SetDeviceParameter extends EnhancedTask { |
213 |
private int dev; |
214 |
private Parameter prm; |
215 |
|
216 |
/** |
217 |
* Creates new instance of <code>SetDeviceParameter</code>. |
218 |
* @param dev The id of the device whose parameter should be set. |
219 |
* @param prm The parameter to be set. |
220 |
*/ |
221 |
public |
222 |
SetDeviceParameter(int dev, Parameter prm) { |
223 |
setTitle("Audio.SetDeviceParameter_task"); |
224 |
setDescription(i18n.getMessage("Audio.SetDeviceParameter.desc", dev)); |
225 |
|
226 |
this.dev = dev; |
227 |
this.prm = prm; |
228 |
} |
229 |
|
230 |
/** The entry point of the task. */ |
231 |
public void |
232 |
run() { |
233 |
try { CC.getClient().setAudioOutputDeviceParameter(dev, prm); } |
234 |
catch(Exception x) { |
235 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
236 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
237 |
} |
238 |
} |
239 |
} |
240 |
|
241 |
/** |
242 |
* This task alters a specific setting of an audio output channel. |
243 |
*/ |
244 |
public static class SetChannelParameter extends EnhancedTask { |
245 |
private int dev; |
246 |
private int channel; |
247 |
private Parameter prm; |
248 |
|
249 |
/** |
250 |
* Creates new instance of <code>SetChannelParameter</code>. |
251 |
* @param dev The id of the device whose channel parameter should be set. |
252 |
* @param channel The channel number. |
253 |
* @param prm The parameter to be set. |
254 |
*/ |
255 |
public |
256 |
SetChannelParameter(int dev, int channel, Parameter prm) { |
257 |
setTitle("Audio.SetChannelParameter_task"); |
258 |
setDescription(i18n.getMessage("Audio.SetChannelParameter.desc")); |
259 |
|
260 |
this.dev = dev; |
261 |
this.channel = channel; |
262 |
this.prm = prm; |
263 |
} |
264 |
|
265 |
/** The entry point of the task. */ |
266 |
public void |
267 |
run() { |
268 |
try { CC.getClient().setAudioOutputChannelParameter(dev, channel, prm); } |
269 |
catch(Exception x) { |
270 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
271 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
/** |
277 |
* This task changes the channel number of the speicifed audio output device. |
278 |
*/ |
279 |
public static class SetChannelCount extends EnhancedTask { |
280 |
private int deviceId; |
281 |
private int channels; |
282 |
|
283 |
/** |
284 |
* Creates new instance of <code>SetChannelCount</code>. |
285 |
* @param deviceId The id of the device whose channels number will be changed. |
286 |
* @param channels The new number of audio channels. |
287 |
*/ |
288 |
public |
289 |
SetChannelCount(int deviceId, int channels) { |
290 |
setTitle("SetAudioOutputChannelCount_task"); |
291 |
setDescription(i18n.getMessage("Audio.SetChannelCount.desc", deviceId)); |
292 |
|
293 |
this.deviceId = deviceId; |
294 |
this.channels = channels; |
295 |
} |
296 |
|
297 |
/** The entry point of the task. */ |
298 |
public void |
299 |
run() { |
300 |
try { CC.getClient().setAudioOutputChannelCount(deviceId, channels); } |
301 |
catch(Exception x) { |
302 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
303 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
304 |
} |
305 |
} |
306 |
} |
307 |
|
308 |
|
309 |
/** |
310 |
* This task updates the setting of an audio output device. |
311 |
*/ |
312 |
public static class UpdateDeviceInfo extends EnhancedTask { |
313 |
private int dev; |
314 |
|
315 |
/** |
316 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
317 |
* @param dev The id of the device, which settings should be updated. |
318 |
*/ |
319 |
public |
320 |
UpdateDeviceInfo(int dev) { |
321 |
setTitle("Audio.UpdateDeviceInfo_task"); |
322 |
setDescription(i18n.getMessage("Audio.UpdateDeviceInfo.desc", dev)); |
323 |
|
324 |
this.dev = dev; |
325 |
} |
326 |
|
327 |
/** The entry point of the task. */ |
328 |
public void |
329 |
run() { |
330 |
try { |
331 |
AudioOutputDevice d = CC.getClient().getAudioOutputDeviceInfo(dev); |
332 |
CC.getSamplerModel().getAudioDeviceById(dev).setDeviceInfo(d); |
333 |
} catch(Exception x) { |
334 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
335 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
336 |
} |
337 |
} |
338 |
} |
339 |
|
340 |
/** |
341 |
* This task updates the audio output device list. |
342 |
*/ |
343 |
public static class UpdateDevices extends EnhancedTask { |
344 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
345 |
public |
346 |
UpdateDevices() { |
347 |
setTitle("Audio.UpdateDevices_task"); |
348 |
setDescription(i18n.getMessage("Audio.UpdateDevices.desc")); |
349 |
} |
350 |
|
351 |
/** The entry point of the task. */ |
352 |
public void |
353 |
run() { |
354 |
try { |
355 |
SamplerModel sm = CC.getSamplerModel(); |
356 |
Integer[] devIDs = CC.getClient().getAudioOutputDeviceIDs(); |
357 |
|
358 |
boolean found = false; |
359 |
|
360 |
for(AudioDeviceModel m : sm.getAudioDevices()) { |
361 |
for(int i = 0; i < devIDs.length; i++) { |
362 |
if(m.getDeviceId() == devIDs[i]) { |
363 |
devIDs[i] = -1; |
364 |
found = true; |
365 |
} |
366 |
} |
367 |
|
368 |
if(!found) sm.removeAudioDeviceById(m.getDeviceId()); |
369 |
found = false; |
370 |
} |
371 |
|
372 |
AudioOutputDevice d; |
373 |
|
374 |
for(int id : devIDs) { |
375 |
if(id >= 0) { |
376 |
d = CC.getClient().getAudioOutputDeviceInfo(id); |
377 |
sm.addAudioDevice(d); |
378 |
} |
379 |
} |
380 |
} catch(Exception x) { |
381 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
382 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
383 |
} |
384 |
} |
385 |
} |
386 |
|
387 |
} |