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 channel. |
219 |
*/ |
220 |
public static class SetChannelParameter extends EnhancedTask { |
221 |
private int dev; |
222 |
private int channel; |
223 |
private Parameter prm; |
224 |
|
225 |
/** |
226 |
* Creates new instance of <code>SetChannelParameter</code>. |
227 |
* @param dev The id of the device whose port parameter should be set. |
228 |
* @param channel The channel number. |
229 |
* @param prm The parameter to be set. |
230 |
*/ |
231 |
public |
232 |
SetChannelParameter(int dev, int channel, Parameter prm) { |
233 |
setTitle("Audio.SetChannelParameter_task"); |
234 |
setDescription(i18n.getMessage("Audio.SetChannelParameter.desc")); |
235 |
|
236 |
this.dev = dev; |
237 |
this.channel = channel; |
238 |
this.prm = prm; |
239 |
} |
240 |
|
241 |
/** The entry point of the task. */ |
242 |
public void |
243 |
run() { |
244 |
try { CC.getClient().setAudioOutputChannelParameter(dev, channel, prm); } |
245 |
catch(Exception x) { |
246 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
247 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
248 |
} |
249 |
} |
250 |
} |
251 |
|
252 |
/** |
253 |
* This task changes the channel number of the speicifed audio output device. |
254 |
*/ |
255 |
public static class SetChannelCount extends EnhancedTask { |
256 |
private int deviceId; |
257 |
private int channels; |
258 |
|
259 |
/** |
260 |
* Creates new instance of <code>SetChannelCount</code>. |
261 |
* @param deviceId The id of the device whose channels number will be changed. |
262 |
* @param channels The new number of audio channels. |
263 |
*/ |
264 |
public |
265 |
SetChannelCount(int deviceId, int channels) { |
266 |
setTitle("SetAudioOutputChannelCount_task"); |
267 |
setDescription(i18n.getMessage("Audio.SetChannelCount.desc", deviceId)); |
268 |
|
269 |
this.deviceId = deviceId; |
270 |
this.channels = channels; |
271 |
} |
272 |
|
273 |
/** The entry point of the task. */ |
274 |
public void |
275 |
run() { |
276 |
try { CC.getClient().setAudioOutputChannelCount(deviceId, channels); } |
277 |
catch(Exception x) { |
278 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
279 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
|
285 |
/** |
286 |
* This task updates the setting of an audio output device. |
287 |
*/ |
288 |
public static class UpdateDeviceInfo extends EnhancedTask { |
289 |
private int dev; |
290 |
|
291 |
/** |
292 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
293 |
* @param dev The id of the device, which settings should be updated. |
294 |
*/ |
295 |
public |
296 |
UpdateDeviceInfo(int dev) { |
297 |
setTitle("Audio.UpdateDeviceInfo_task"); |
298 |
setDescription(i18n.getMessage("Audio.UpdateDeviceInfo.desc", dev)); |
299 |
|
300 |
this.dev = dev; |
301 |
} |
302 |
|
303 |
/** The entry point of the task. */ |
304 |
public void |
305 |
run() { |
306 |
try { |
307 |
AudioOutputDevice d = CC.getClient().getAudioOutputDeviceInfo(dev); |
308 |
CC.getSamplerModel().getAudioDeviceById(dev).setDeviceInfo(d); |
309 |
} catch(Exception x) { |
310 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
311 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
/** |
317 |
* This task updates the audio output device list. |
318 |
*/ |
319 |
public static class UpdateDevices extends EnhancedTask { |
320 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
321 |
public |
322 |
UpdateDevices() { |
323 |
setTitle("Audio.UpdateDevices_task"); |
324 |
setDescription(i18n.getMessage("Audio.UpdateDevices.desc")); |
325 |
} |
326 |
|
327 |
/** The entry point of the task. */ |
328 |
public void |
329 |
run() { |
330 |
try { |
331 |
SamplerModel sm = CC.getSamplerModel(); |
332 |
Integer[] devIDs = CC.getClient().getAudioOutputDeviceIDs(); |
333 |
|
334 |
boolean found = false; |
335 |
|
336 |
for(AudioDeviceModel m : sm.getAudioDevices()) { |
337 |
for(int i = 0; i < devIDs.length; i++) { |
338 |
if(m.getDeviceId() == devIDs[i]) { |
339 |
devIDs[i] = -1; |
340 |
found = true; |
341 |
} |
342 |
} |
343 |
|
344 |
if(!found) sm.removeAudioDeviceById(m.getDeviceId()); |
345 |
found = false; |
346 |
} |
347 |
|
348 |
AudioOutputDevice d; |
349 |
|
350 |
for(int id : devIDs) { |
351 |
if(id >= 0) { |
352 |
d = CC.getClient().getAudioOutputDeviceInfo(id); |
353 |
sm.addAudioDevice(d); |
354 |
} |
355 |
} |
356 |
} catch(Exception x) { |
357 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
358 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
359 |
} |
360 |
} |
361 |
} |
362 |
|
363 |
} |