1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2011 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 org.jsampler.AudioDeviceModel; |
26 |
import org.jsampler.CC; |
27 |
import org.jsampler.EffectChain; |
28 |
import org.jsampler.SamplerModel; |
29 |
|
30 |
import org.linuxsampler.lscp.Effect; |
31 |
import org.linuxsampler.lscp.Instrument; |
32 |
import org.linuxsampler.lscp.SamplerEngine; |
33 |
import org.linuxsampler.lscp.ServerInfo; |
34 |
|
35 |
|
36 |
import static org.jsampler.JSI18n.i18n; |
37 |
|
38 |
|
39 |
/** |
40 |
* Provides tasks for managing the global settings of the sampler. |
41 |
* @author Grigor Iliev |
42 |
*/ |
43 |
public class Global { |
44 |
|
45 |
/** Forbids the instantiation of this class. */ |
46 |
private Global() { } |
47 |
|
48 |
/** |
49 |
* Establishes connection to LinuxSampler. |
50 |
*/ |
51 |
public static class Connect extends EnhancedTask { |
52 |
/** Creates a new instance of <code>Connect</code>. */ |
53 |
public |
54 |
Connect() { |
55 |
setTitle("Global.Connect_task"); |
56 |
setDescription(i18n.getMessage("Global.Connect.desc")); |
57 |
} |
58 |
|
59 |
/** The entry point of the task. */ |
60 |
@Override |
61 |
public void |
62 |
exec() throws Exception { CC.getClient().connect(); } |
63 |
} |
64 |
|
65 |
public static class Disconnect extends EnhancedTask { |
66 |
/** Creates a new instance of <code>Disconnect</code>. */ |
67 |
public |
68 |
Disconnect() { |
69 |
setSilent(true); |
70 |
setTitle("Global.Disconnect_task"); |
71 |
setDescription("Disconnecting..."); |
72 |
} |
73 |
|
74 |
/** The entry point of the task. */ |
75 |
@Override |
76 |
public void |
77 |
exec() throws Exception { |
78 |
CC.getClient().disconnect(); |
79 |
if(CC.getMainFrame().getLSConsoleModel() != null) { |
80 |
CC.getMainFrame().getLSConsoleModel().quit(); |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
/** |
86 |
* This task retrieves information about the LinuxSampler instance. |
87 |
*/ |
88 |
public static class GetServerInfo extends EnhancedTask<ServerInfo> { |
89 |
/** Creates a new instance of <code>GetServerInfo</code>. */ |
90 |
public |
91 |
GetServerInfo() { |
92 |
setTitle("Global.GetServerInfo_task"); |
93 |
setDescription(i18n.getMessage("Global.GetServerInfo.desc")); |
94 |
} |
95 |
|
96 |
/** The entry point of the task. */ |
97 |
@Override |
98 |
public void |
99 |
exec() throws Exception { setResult(CC.getClient().getServerInfo()); } |
100 |
} |
101 |
|
102 |
/** |
103 |
* This task resets the whole sampler. |
104 |
*/ |
105 |
public static class ResetSampler extends EnhancedTask { |
106 |
/** Creates a new instance of <code>ResetSampler</code>. */ |
107 |
public |
108 |
ResetSampler() { |
109 |
setTitle("Global.ResetSampler_task"); |
110 |
setDescription(i18n.getMessage("Global.ResetSampler.desc")); |
111 |
} |
112 |
|
113 |
/** The entry point of the task. */ |
114 |
@Override |
115 |
public void |
116 |
exec() throws Exception { CC.getClient().resetSampler(); } |
117 |
} |
118 |
|
119 |
/** |
120 |
* This task retrieves the list of all available engines. |
121 |
*/ |
122 |
public static class GetEngines extends EnhancedTask<SamplerEngine[]> { |
123 |
/** Creates a new instance of <code>GetEngines</code>. */ |
124 |
public |
125 |
GetEngines() { |
126 |
setTitle("Global.GetEngines_task"); |
127 |
setDescription(i18n.getMessage("Global.GetEngines.desc")); |
128 |
} |
129 |
|
130 |
/** The entry point of the task. */ |
131 |
@Override |
132 |
public void |
133 |
exec() throws Exception { setResult(CC.getClient().getEngines()); } |
134 |
} |
135 |
|
136 |
/** |
137 |
* This task gets the global volume of the sampler. |
138 |
*/ |
139 |
public static class GetVolume extends EnhancedTask<Float> { |
140 |
/** Creates a new instance of <code>GetVolume</code>. */ |
141 |
public |
142 |
GetVolume() { |
143 |
setTitle("Global.GetVolume_task"); |
144 |
setDescription(i18n.getMessage("Global.GetVolume.desc")); |
145 |
} |
146 |
|
147 |
/** The entry point of the task. */ |
148 |
@Override |
149 |
public void |
150 |
exec() throws Exception { setResult(CC.getClient().getVolume()); } |
151 |
} |
152 |
|
153 |
|
154 |
/** |
155 |
* This task sets the global volume of the sampler. |
156 |
*/ |
157 |
public static class SetVolume extends EnhancedTask { |
158 |
private float volume; |
159 |
|
160 |
/** |
161 |
* Creates new instance of <code>SetVolume</code>. |
162 |
* @param volume The new volume value. |
163 |
*/ |
164 |
public |
165 |
SetVolume(float volume) { |
166 |
setTitle("Global.SetVolume_task"); |
167 |
setDescription(i18n.getMessage("Global.SetVolume.desc")); |
168 |
this.volume = volume; |
169 |
} |
170 |
|
171 |
/** The entry point of the task. */ |
172 |
@Override |
173 |
public void |
174 |
exec() throws Exception { CC.getClient().setVolume(volume); } |
175 |
} |
176 |
|
177 |
|
178 |
/** |
179 |
* This task sets the global sampler-wide limits of maximum voices and streams. |
180 |
*/ |
181 |
public static class SetPolyphony extends EnhancedTask { |
182 |
private int maxVoices; |
183 |
private int maxStreams; |
184 |
|
185 |
/** |
186 |
* Creates new instance of <code>SetPolyphony</code>. |
187 |
* @param maxVoices The new global limit of maximum voices or |
188 |
* <code>-1</code> to ignore it. |
189 |
* @param maxStreams The new global limit of maximum disk streams or |
190 |
* <code>-1</code> to ignore it. |
191 |
*/ |
192 |
public |
193 |
SetPolyphony(int maxVoices, int maxStreams) { |
194 |
setTitle("Global.SetPolyphony_task"); |
195 |
setDescription(i18n.getMessage("Global.SetPolyphony.desc")); |
196 |
this.maxVoices = maxVoices; |
197 |
this.maxStreams = maxStreams; |
198 |
} |
199 |
|
200 |
/** The entry point of the task. */ |
201 |
@Override |
202 |
public void |
203 |
exec() throws Exception { |
204 |
if(maxVoices != -1) CC.getClient().setGlobalVoiceLimit(maxVoices); |
205 |
if(maxStreams != -1) CC.getClient().setGlobalStreamLimit(maxStreams); |
206 |
} |
207 |
} |
208 |
|
209 |
/** |
210 |
* This task updates the current number of all active voices |
211 |
* and the maximum number of active voices allowed. |
212 |
*/ |
213 |
public static class UpdateTotalVoiceCount extends EnhancedTask { |
214 |
/** Creates a new instance of <code>UpdateTotalVoiceCount</code>. */ |
215 |
public |
216 |
UpdateTotalVoiceCount() { |
217 |
setSilent(true); |
218 |
setTitle("Global.UpdateTotalVoiceCount_task"); |
219 |
setDescription(i18n.getMessage("Global.UpdateTotalVoiceCount.desc")); |
220 |
} |
221 |
|
222 |
/** The entry point of the task. */ |
223 |
@Override |
224 |
public void |
225 |
exec() throws Exception { |
226 |
SamplerModel sm = CC.getSamplerModel(); |
227 |
int voices = CC.getClient().getTotalVoiceCount(); |
228 |
int voicesMax = CC.getClient().getTotalVoiceCountMax(); |
229 |
sm.updateActiveVoiceInfo(voices, voicesMax); |
230 |
} |
231 |
|
232 |
/** |
233 |
* Used to decrease the traffic. All task in the queue |
234 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
235 |
* @see org.jsampler.CC#addTask |
236 |
*/ |
237 |
@Override |
238 |
public boolean |
239 |
equals(Object obj) { |
240 |
if(obj == null) return false; |
241 |
if(!(obj instanceof UpdateTotalVoiceCount)) return false; |
242 |
|
243 |
return true; |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
/** |
249 |
* This task sets the LSCP client's read timeout. |
250 |
*/ |
251 |
public static class SetClientReadTimeout extends EnhancedTask { |
252 |
private int timeout; |
253 |
|
254 |
/** |
255 |
* Creates new instance of <code>SetClientReadTimeout</code>. |
256 |
* @param timeout The new timeout value (in seconds). |
257 |
*/ |
258 |
public |
259 |
SetClientReadTimeout(int timeout) { |
260 |
setTitle("Global.SetClientReadTimeout_task"); |
261 |
setDescription(i18n.getMessage("Global.SetClientReadTimeout.desc")); |
262 |
this.timeout = timeout; |
263 |
} |
264 |
|
265 |
/** The entry point of the task. */ |
266 |
@Override |
267 |
public void |
268 |
exec() throws Exception { CC.getClient().setSoTimeout(timeout * 1000); } |
269 |
} |
270 |
|
271 |
/** |
272 |
* This task gets the list of instruments in the specified instrument file. |
273 |
*/ |
274 |
public static class GetFileInstruments extends EnhancedTask<Instrument[]> { |
275 |
private final String filename; |
276 |
|
277 |
/** Creates a new instance of <code>GetFileInstruments</code>. */ |
278 |
public |
279 |
GetFileInstruments(String filename) { |
280 |
setSilent(true); |
281 |
this.filename = filename; |
282 |
setTitle("Global.GetFileInstruments_task"); |
283 |
setDescription(i18n.getMessage("Global.GetFileInstruments.desc")); |
284 |
} |
285 |
|
286 |
/** The entry point of the task. */ |
287 |
@Override |
288 |
public void |
289 |
exec() throws Exception { |
290 |
setResult(CC.getClient().getFileInstruments(filename)); |
291 |
} |
292 |
} |
293 |
|
294 |
/** |
295 |
* This task gets information about the specified instrument. |
296 |
*/ |
297 |
public static class GetFileInstrument extends EnhancedTask<Instrument> { |
298 |
private final String filename; |
299 |
private final int instrIdx; |
300 |
|
301 |
/** Creates a new instance of <code>GetFileInstrument</code>. */ |
302 |
public |
303 |
GetFileInstrument(String filename, int instrIdx) { |
304 |
setSilent(true); |
305 |
this.filename = filename; |
306 |
this.instrIdx = instrIdx; |
307 |
setTitle("Global.GetFileInstrument_task"); |
308 |
setDescription(i18n.getMessage("Global.GetFileInstrument.desc")); |
309 |
} |
310 |
|
311 |
/** The entry point of the task. */ |
312 |
@Override |
313 |
public void |
314 |
exec() throws Exception { |
315 |
setResult(CC.getClient().getFileInstrumentInfo(filename, instrIdx)); |
316 |
} |
317 |
} |
318 |
|
319 |
/** |
320 |
* This task retrieves the list of internal effects, available to the sampler. |
321 |
*/ |
322 |
public static class GetEffects extends EnhancedTask<Effect[]> { |
323 |
/** Creates a new instance of <code>GetEffects</code>. */ |
324 |
public |
325 |
GetEffects() { |
326 |
setTitle("Global.GetEffects_task"); |
327 |
setDescription(i18n.getMessage("Global.GetEffects.desc")); |
328 |
} |
329 |
|
330 |
/** The entry point of the task. */ |
331 |
@Override |
332 |
public void |
333 |
exec() throws Exception { setResult(CC.getClient().getEffects()); } |
334 |
} |
335 |
|
336 |
/** |
337 |
* This task updates the send effect chains and the effect instances in those chains. |
338 |
*/ |
339 |
public static class UpdateSendEffectChains extends EnhancedTask { |
340 |
private final int audioDeviceId; |
341 |
|
342 |
/** Creates a new instance of <code>UpdateSendEffectChains</code>. */ |
343 |
public |
344 |
UpdateSendEffectChains() { this(-1); } |
345 |
|
346 |
/** Creates a new instance of <code>UpdateSendEffectChains</code>. */ |
347 |
public |
348 |
UpdateSendEffectChains(int audioDeviceId) { |
349 |
this.audioDeviceId = audioDeviceId; |
350 |
setTitle("Global.UpdateSendEffectChains_task"); |
351 |
setDescription(i18n.getMessage("Global.UpdateSendEffectChains.desc")); |
352 |
} |
353 |
|
354 |
/** The entry point of the task. */ |
355 |
@Override |
356 |
public void |
357 |
exec() throws Exception { |
358 |
// TODO: synchornization |
359 |
|
360 |
if(audioDeviceId < 0) { |
361 |
Integer[] aodIDs = CC.getClient().getAudioOutputDeviceIDs(); |
362 |
for(int id : aodIDs) { updateSendEffectChains(id); } |
363 |
} else { |
364 |
updateSendEffectChains(audioDeviceId); |
365 |
} |
366 |
} |
367 |
|
368 |
private void |
369 |
updateSendEffectChains(int devId) throws Exception { |
370 |
org.linuxsampler.lscp.EffectChain[] chains = |
371 |
CC.getClient().getSendEffectChains(devId); |
372 |
|
373 |
AudioDeviceModel adm = CC.getSamplerModel().getAudioDeviceById(devId); |
374 |
adm.removeAllSendEffectChains(); |
375 |
|
376 |
for(org.linuxsampler.lscp.EffectChain c : chains) { |
377 |
adm.addSendEffectChain(new EffectChain(c)); |
378 |
} |
379 |
} |
380 |
} |
381 |
|
382 |
public static class DummyTask extends EnhancedTask { |
383 |
@Override |
384 |
public void |
385 |
run() { } |
386 |
} |
387 |
} |