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