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 creates a new audio output device. |
72 |
*/ |
73 |
public static class CreateDevice extends EnhancedTask<Integer> { |
74 |
private String driver; |
75 |
private Parameter[] parameters; |
76 |
|
77 |
|
78 |
/** |
79 |
* Creates a new instance of <code>CreateDevice</code>. |
80 |
* @param driver The desired audio output system. |
81 |
* @param parameters An optional list of driver specific parameters. |
82 |
*/ |
83 |
public |
84 |
CreateDevice(String driver, Parameter... parameters) { |
85 |
setTitle("Audio.CreateDevice_task"); |
86 |
setDescription(i18n.getMessage("Audio.CreateDevice.desc")); |
87 |
|
88 |
this.driver = driver; |
89 |
this.parameters = parameters; |
90 |
} |
91 |
|
92 |
/** The entry point of the task. */ |
93 |
public void |
94 |
run() { |
95 |
try { |
96 |
// TODO: move this to jlscp |
97 |
java.util.Vector<Parameter> v = new java.util.Vector<Parameter>(); |
98 |
for(Parameter p : parameters) { |
99 |
if(p.getValue() != null) v.add(p); |
100 |
} |
101 |
parameters = v.toArray(new Parameter[v.size()]); |
102 |
/////// |
103 |
|
104 |
Integer deviceId = |
105 |
CC.getClient().createAudioOutputDevice(driver, parameters); |
106 |
setResult(deviceId); |
107 |
} catch(Exception x) { |
108 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
109 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
/** |
115 |
* This task destroys the specified audio output device. |
116 |
*/ |
117 |
public static class DestroyDevice extends EnhancedTask { |
118 |
private int deviceId; |
119 |
|
120 |
|
121 |
/** |
122 |
* Creates a new instance of <code>DestroyDevice</code>. |
123 |
* @param deviceId The ID of the audio output device to be destroyed. |
124 |
*/ |
125 |
public |
126 |
DestroyDevice(int deviceId) { |
127 |
setTitle("Audio.DestroyDevice_task"); |
128 |
setDescription(i18n.getMessage("Audio.DestroyDevice.desc", deviceId)); |
129 |
|
130 |
this.deviceId = deviceId; |
131 |
} |
132 |
|
133 |
/** The entry point of the task. */ |
134 |
public void |
135 |
run() { |
136 |
try { CC.getClient().destroyAudioOutputDevice(deviceId); } |
137 |
catch(Exception x) { |
138 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
139 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
/** |
145 |
* This task enables/disables a specific audio output device. |
146 |
*/ |
147 |
public static class EnableDevice extends EnhancedTask { |
148 |
private int dev; |
149 |
private boolean enable; |
150 |
|
151 |
/** |
152 |
* Creates new instance of <code>EnableDevice</code>. |
153 |
* @param dev The id of the device to be enabled/disabled. |
154 |
* @param enable Specify <code>true</code> to enable the audio device; |
155 |
* code>false</code> to disable it. |
156 |
*/ |
157 |
public |
158 |
EnableDevice(int dev, boolean enable) { |
159 |
setTitle("Audio.EnableDevice_task"); |
160 |
setDescription(i18n.getMessage("Audio.EnableDevice.desc", dev)); |
161 |
|
162 |
this.dev = dev; |
163 |
this.enable = enable; |
164 |
} |
165 |
|
166 |
/** The entry point of the task. */ |
167 |
public void |
168 |
run() { |
169 |
try { |
170 |
CC.getClient().enableAudioOutputDevice(dev, enable); |
171 |
|
172 |
// Not needed, but eventually speeds up the change. |
173 |
CC.getSamplerModel().getAudioDeviceById(dev).setActive(enable); |
174 |
} catch(Exception x) { |
175 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
176 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
/** |
182 |
* This task alters a specific setting of an audio output channel. |
183 |
*/ |
184 |
public static class SetChannelParameter extends EnhancedTask { |
185 |
private int dev; |
186 |
private int channel; |
187 |
private Parameter prm; |
188 |
|
189 |
/** |
190 |
* Creates new instance of <code>SetChannelParameter</code>. |
191 |
* @param dev The id of the device whose port parameter should be set. |
192 |
* @param channel The channel number. |
193 |
* @param prm The parameter to be set. |
194 |
*/ |
195 |
public |
196 |
SetChannelParameter(int dev, int channel, Parameter prm) { |
197 |
setTitle("Audio.SetChannelParameter_task"); |
198 |
setDescription(i18n.getMessage("Audio.SetChannelParameter.desc")); |
199 |
|
200 |
this.dev = dev; |
201 |
this.channel = channel; |
202 |
this.prm = prm; |
203 |
} |
204 |
|
205 |
/** The entry point of the task. */ |
206 |
public void |
207 |
run() { |
208 |
try { CC.getClient().setAudioOutputChannelParameter(dev, channel, prm); } |
209 |
catch(Exception x) { |
210 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
211 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
/** |
217 |
* This task changes the channel number of the speicifed audio output device. |
218 |
*/ |
219 |
public static class SetChannelCount extends EnhancedTask { |
220 |
private int deviceId; |
221 |
private int channels; |
222 |
|
223 |
/** |
224 |
* Creates new instance of <code>SetChannelCount</code>. |
225 |
* @param deviceId The id of the device whose channels number will be changed. |
226 |
* @param channels The new number of audio channels. |
227 |
*/ |
228 |
public |
229 |
SetChannelCount(int deviceId, int channels) { |
230 |
setTitle("SetAudioOutputChannelCount_task"); |
231 |
setDescription(i18n.getMessage("Audio.SetChannelCount.desc", deviceId)); |
232 |
|
233 |
this.deviceId = deviceId; |
234 |
this.channels = channels; |
235 |
} |
236 |
|
237 |
/** The entry point of the task. */ |
238 |
public void |
239 |
run() { |
240 |
try { CC.getClient().setAudioOutputChannelCount(deviceId, channels); } |
241 |
catch(Exception x) { |
242 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
243 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
|
249 |
/** |
250 |
* This task updates the setting of an audio output device. |
251 |
*/ |
252 |
public static class UpdateDeviceInfo extends EnhancedTask { |
253 |
private int dev; |
254 |
|
255 |
/** |
256 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
257 |
* @param dev The id of the device, which settings should be updated. |
258 |
*/ |
259 |
public |
260 |
UpdateDeviceInfo(int dev) { |
261 |
setTitle("Audio.UpdateDeviceInfo_task"); |
262 |
setDescription(i18n.getMessage("Audio.UpdateDeviceInfo.desc", dev)); |
263 |
|
264 |
this.dev = dev; |
265 |
} |
266 |
|
267 |
/** The entry point of the task. */ |
268 |
public void |
269 |
run() { |
270 |
try { |
271 |
AudioOutputDevice d = CC.getClient().getAudioOutputDeviceInfo(dev); |
272 |
CC.getSamplerModel().getAudioDeviceById(dev).setDeviceInfo(d); |
273 |
} catch(Exception x) { |
274 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
275 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
/** |
281 |
* This task updates the audio output device list. |
282 |
*/ |
283 |
public static class UpdateDevices extends EnhancedTask { |
284 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
285 |
public |
286 |
UpdateDevices() { |
287 |
setTitle("Audio.UpdateDevices_task"); |
288 |
setDescription(i18n.getMessage("Audio.UpdateDevices.desc")); |
289 |
} |
290 |
|
291 |
/** The entry point of the task. */ |
292 |
public void |
293 |
run() { |
294 |
try { |
295 |
SamplerModel sm = CC.getSamplerModel(); |
296 |
Integer[] devIDs = CC.getClient().getAudioOutputDeviceIDs(); |
297 |
|
298 |
boolean found = false; |
299 |
|
300 |
for(AudioDeviceModel m : sm.getAudioDevices()) { |
301 |
for(int i = 0; i < devIDs.length; i++) { |
302 |
if(m.getDeviceId() == devIDs[i]) { |
303 |
devIDs[i] = -1; |
304 |
found = true; |
305 |
} |
306 |
} |
307 |
|
308 |
if(!found) sm.removeAudioDeviceById(m.getDeviceId()); |
309 |
found = false; |
310 |
} |
311 |
|
312 |
AudioOutputDevice d; |
313 |
|
314 |
for(int id : devIDs) { |
315 |
if(id >= 0) { |
316 |
d = CC.getClient().getAudioOutputDeviceInfo(id); |
317 |
sm.addAudioDevice(d); |
318 |
} |
319 |
} |
320 |
} catch(Exception x) { |
321 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
322 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
323 |
} |
324 |
} |
325 |
} |
326 |
|
327 |
} |