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 |
package org.jsampler.task; |
23 |
|
24 |
import java.util.Vector; |
25 |
import java.util.logging.Level; |
26 |
|
27 |
import net.sf.juife.Task; |
28 |
|
29 |
import org.linuxsampler.lscp.BoolParameter; |
30 |
import org.linuxsampler.lscp.MidiInputDevice; |
31 |
import org.linuxsampler.lscp.MidiInputDriver; |
32 |
import org.linuxsampler.lscp.MidiInstrumentEntry; |
33 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
34 |
import org.linuxsampler.lscp.MidiInstrumentMapInfo; |
35 |
import org.linuxsampler.lscp.Parameter; |
36 |
|
37 |
import org.jsampler.CC; |
38 |
import org.jsampler.HF; |
39 |
import org.jsampler.MidiDeviceModel; |
40 |
import org.jsampler.MidiInstrument; |
41 |
import org.jsampler.MidiInstrumentMap; |
42 |
import org.jsampler.SamplerModel; |
43 |
|
44 |
import static org.jsampler.JSI18n.i18n; |
45 |
|
46 |
|
47 |
/** |
48 |
* Provides the MIDI specific tasks. |
49 |
* @author Grigor Iliev |
50 |
*/ |
51 |
public class Midi { |
52 |
/** Forbits the instantiation of this class. */ |
53 |
private Midi() { } |
54 |
|
55 |
|
56 |
/** |
57 |
* This task retrieves all MIDI input drivers currently |
58 |
* available for the LinuxSampler instance. |
59 |
*/ |
60 |
public static class GetDrivers extends EnhancedTask<MidiInputDriver[]> { |
61 |
/** Creates a new instance of <code>GetDrivers</code>. */ |
62 |
public |
63 |
GetDrivers() { |
64 |
setTitle("Midi.GetDrivers_task"); |
65 |
setDescription(i18n.getMessage("Midi.GetDrivers.desc")); |
66 |
} |
67 |
|
68 |
/** The entry point of the task. */ |
69 |
public void |
70 |
run() { |
71 |
try { setResult(CC.getClient().getMidiInputDrivers()); } |
72 |
catch(Exception x) { |
73 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
74 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
/** |
80 |
* This task retrieves detailed information about all parameters |
81 |
* of the specified MIDI input driver. |
82 |
*/ |
83 |
public static class GetDriverParametersInfo extends EnhancedTask<Parameter[]> { |
84 |
private String driver; |
85 |
Parameter[] depList; |
86 |
|
87 |
/** |
88 |
* Creates a new instance of <code>GetDriverParametersInfo</code>. |
89 |
* @param depList - A dependences list. |
90 |
*/ |
91 |
public |
92 |
GetDriverParametersInfo(String driver, Parameter... depList) { |
93 |
setTitle("Midi.GetDriverParametersInfo_task"); |
94 |
setDescription(i18n.getMessage("Midi.GetDriverParametersInfo.desc")); |
95 |
|
96 |
this.driver = driver; |
97 |
this.depList = depList; |
98 |
} |
99 |
|
100 |
/** The entry point of the task. */ |
101 |
public void |
102 |
run() { |
103 |
try { |
104 |
MidiInputDriver d; |
105 |
d = CC.getClient().getMidiInputDriverInfo(driver, depList); |
106 |
setResult(d.getParameters()); |
107 |
} |
108 |
catch(Exception x) { |
109 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
110 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
/** |
116 |
* This task creates a new MIDI input device. |
117 |
*/ |
118 |
public static class CreateDevice extends EnhancedTask<Integer> { |
119 |
private String driver; |
120 |
private Parameter[] parameters; |
121 |
|
122 |
/** |
123 |
* Creates a new instance of <code>CreateDevice</code>. |
124 |
* @param driver The desired MIDI input system. |
125 |
* @param parameters An optional list of driver specific parameters. |
126 |
*/ |
127 |
public |
128 |
CreateDevice(String driver, Parameter... parameters) { |
129 |
setTitle("Midi.CreateDevice_task"); |
130 |
setDescription(i18n.getMessage("Midi.CreateDevice.desc")); |
131 |
|
132 |
this.driver = driver; |
133 |
this.parameters = parameters; |
134 |
} |
135 |
|
136 |
/** The entry point of the task. */ |
137 |
public void |
138 |
run() { |
139 |
try { |
140 |
run0(); |
141 |
} catch(Exception x) { |
142 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
143 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
144 |
} |
145 |
} |
146 |
|
147 |
private void |
148 |
run0() throws Exception { |
149 |
Integer deviceId = CC.getClient().createMidiInputDevice(driver, parameters); |
150 |
setResult(deviceId); |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* This task destroys the specified MIDI input device. |
156 |
*/ |
157 |
public static class DestroyDevice extends EnhancedTask { |
158 |
private int deviceId; |
159 |
|
160 |
/** |
161 |
* Creates a new instance of <code>DestroyDevice</code>. |
162 |
* @param deviceId The ID of the MIDI input device to be destroyed. |
163 |
*/ |
164 |
public |
165 |
DestroyDevice(int deviceId) { |
166 |
setTitle("Midi.DestroyDevice_task"); |
167 |
setDescription(i18n.getMessage("Midi.DestroyDevice.desc", deviceId)); |
168 |
|
169 |
this.deviceId = deviceId; |
170 |
} |
171 |
|
172 |
/** The entry point of the task. */ |
173 |
public void |
174 |
run() { |
175 |
try { CC.getClient().destroyMidiInputDevice(deviceId); } |
176 |
catch(Exception x) { |
177 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
178 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
/** |
184 |
* This task enables/disables a specific MIDI input device. |
185 |
*/ |
186 |
public static class EnableDevice extends EnhancedTask { |
187 |
private int dev; |
188 |
private boolean enable; |
189 |
|
190 |
/** |
191 |
* Creates new instance of <code>EnableDevice</code>. |
192 |
* @param dev The id of the device to be enabled/disabled. |
193 |
* @param enable Specify <code>true</code> to enable the MIDI device; |
194 |
* code>false</code> to disable it. |
195 |
*/ |
196 |
public |
197 |
EnableDevice(int dev, boolean enable) { |
198 |
setTitle("Midi.EnableDevice_task"); |
199 |
setDescription(i18n.getMessage("Midi.EnableDevice.desc", dev)); |
200 |
|
201 |
this.dev = dev; |
202 |
this.enable = enable; |
203 |
} |
204 |
|
205 |
/** The entry point of the task. */ |
206 |
public void |
207 |
run() { |
208 |
try { |
209 |
CC.getClient().enableMidiInputDevice(dev, enable); |
210 |
|
211 |
// Not needed, but eventually speeds up the change. |
212 |
CC.getSamplerModel().getMidiDeviceById(dev).setActive(enable); |
213 |
} catch(Exception x) { |
214 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
215 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
216 |
} |
217 |
} |
218 |
} |
219 |
|
220 |
/** |
221 |
* This task alters a specific setting of a created MIDI input device. |
222 |
*/ |
223 |
public static class SetDeviceParameter extends EnhancedTask { |
224 |
private int dev; |
225 |
private Parameter prm; |
226 |
|
227 |
/** |
228 |
* Creates new instance of <code>SetDeviceParameter</code>. |
229 |
* @param dev The id of the device whose parameter should be set. |
230 |
* @param prmName The parameter name. |
231 |
* @param value The new value for the specified parameter. |
232 |
*/ |
233 |
public |
234 |
SetDeviceParameter(int dev, String prmName, boolean value) { |
235 |
this(dev, new BoolParameter(prmName, value)); |
236 |
} |
237 |
|
238 |
/** |
239 |
* Creates new instance of <code>SetDeviceParameter</code>. |
240 |
* @param dev The id of the device whose parameter should be set. |
241 |
* @param prm The parameter to be set. |
242 |
*/ |
243 |
public |
244 |
SetDeviceParameter(int dev, Parameter prm) { |
245 |
setTitle("Midi.SetDeviceParameter_task"); |
246 |
setDescription(i18n.getMessage("Midi.SetDeviceParameter.desc")); |
247 |
|
248 |
this.dev = dev; |
249 |
this.prm = prm; |
250 |
} |
251 |
|
252 |
/** The entry point of the task. */ |
253 |
public void |
254 |
run() { |
255 |
try { |
256 |
CC.getClient().setMidiInputDeviceParameter(dev, prm); |
257 |
|
258 |
CC.getSamplerModel().getMidiDeviceById(dev); |
259 |
} catch(Exception x) { |
260 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
261 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
262 |
} |
263 |
} |
264 |
} |
265 |
|
266 |
/** |
267 |
* This task changes the port number of a speicific MIDI input device. |
268 |
*/ |
269 |
public static class SetPortCount extends EnhancedTask { |
270 |
private int deviceId; |
271 |
private int ports; |
272 |
|
273 |
/** |
274 |
* Creates new instance of <code>SetPortCount</code>. |
275 |
* @param deviceId The id of the device whose ports number will be changed. |
276 |
* @param ports The new number of ports. |
277 |
*/ |
278 |
public |
279 |
SetPortCount(int deviceId, int ports) { |
280 |
setTitle("SetMidiInputPortCount_task"); |
281 |
setDescription(i18n.getMessage("Midi.SetPortCount.desc", deviceId)); |
282 |
|
283 |
this.deviceId = deviceId; |
284 |
this.ports = ports; |
285 |
} |
286 |
|
287 |
/** The entry point of the task. */ |
288 |
public void |
289 |
run() { |
290 |
try { CC.getClient().setMidiInputPortCount(deviceId, ports); } |
291 |
catch(Exception x) { |
292 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
293 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
294 |
} |
295 |
} |
296 |
} |
297 |
|
298 |
/** |
299 |
* This task alters a specific setting of a MIDI input port. |
300 |
*/ |
301 |
public static class SetPortParameter extends EnhancedTask { |
302 |
private int dev; |
303 |
private int port; |
304 |
private Parameter prm; |
305 |
|
306 |
/** |
307 |
* Creates new instance of <code>SetPortParameter</code>. |
308 |
* @param dev The id of the device whose port parameter should be set. |
309 |
* @param port The number of the port. |
310 |
* @param prm The parameter to be set. |
311 |
*/ |
312 |
public |
313 |
SetPortParameter(int dev, int port, Parameter prm) { |
314 |
setTitle("Midi.SetPortParameter_task"); |
315 |
setDescription(i18n.getMessage("Midi.SetPortParameter.desc")); |
316 |
|
317 |
this.dev = dev; |
318 |
this.port = port; |
319 |
this.prm = prm; |
320 |
} |
321 |
|
322 |
/** The entry point of the task. */ |
323 |
public void |
324 |
run() { |
325 |
try { CC.getClient().setMidiInputPortParameter(dev, port, prm); } |
326 |
catch(Exception x) { |
327 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
328 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
329 |
} |
330 |
} |
331 |
} |
332 |
|
333 |
/** |
334 |
* This task updates the settings of a MIDI input device. |
335 |
*/ |
336 |
public static class UpdateDeviceInfo extends EnhancedTask { |
337 |
private int dev; |
338 |
|
339 |
/** |
340 |
* Creates new instance of <code>UpdateDeviceInfo</code>. |
341 |
* @param dev The id of the device, whose settings should be updated. |
342 |
*/ |
343 |
public |
344 |
UpdateDeviceInfo(int dev) { |
345 |
setTitle("Midi.UpdateDeviceInfo_task"); |
346 |
setDescription(i18n.getMessage("Midi.UpdateDeviceInfo.desc", dev)); |
347 |
|
348 |
this.dev = dev; |
349 |
} |
350 |
|
351 |
/** The entry point of the task. */ |
352 |
public void |
353 |
run() { |
354 |
try { |
355 |
MidiInputDevice mid = CC.getClient().getMidiInputDeviceInfo(dev); |
356 |
CC.getSamplerModel().getMidiDeviceById(dev).setDeviceInfo(mid); |
357 |
} catch(Exception x) { |
358 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
359 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
360 |
} |
361 |
} |
362 |
} |
363 |
|
364 |
/** |
365 |
* This task updates the MIDI device list and all MIDI devices' settings. |
366 |
*/ |
367 |
public static class UpdateDevices extends EnhancedTask { |
368 |
/** Creates a new instance of <code>UpdateDevices</code>. */ |
369 |
public |
370 |
UpdateDevices() { |
371 |
setTitle("Midi.UpdateDevices_task"); |
372 |
setDescription(i18n.getMessage("Midi.UpdateDevices.desc")); |
373 |
} |
374 |
|
375 |
/** The entry point of the task. */ |
376 |
public void |
377 |
run() { |
378 |
try { |
379 |
SamplerModel sm = CC.getSamplerModel(); |
380 |
Integer[] deviceIDs = CC.getClient().getMidiInputDeviceIDs(); |
381 |
|
382 |
boolean found = false; |
383 |
|
384 |
for(MidiDeviceModel m : sm.getMidiDevices()) { |
385 |
for(int i = 0; i < deviceIDs.length; i++) { |
386 |
if(m.getDeviceId() == deviceIDs[i]) { |
387 |
deviceIDs[i] = -1; |
388 |
found = true; |
389 |
} |
390 |
} |
391 |
|
392 |
if(!found) sm.removeMidiDeviceById(m.getDeviceId()); |
393 |
found = false; |
394 |
} |
395 |
|
396 |
MidiInputDevice dev; |
397 |
|
398 |
for(int id : deviceIDs) { |
399 |
if(id >= 0) { |
400 |
dev = CC.getClient().getMidiInputDeviceInfo(id); |
401 |
sm.addMidiDevice(dev); |
402 |
} |
403 |
} |
404 |
} catch(Exception x) { |
405 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
406 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
407 |
} |
408 |
} |
409 |
} |
410 |
|
411 |
/** |
412 |
* This task creates a new MIDI instrument map. |
413 |
*/ |
414 |
public static class AddInstrumentMap extends EnhancedTask<Integer> { |
415 |
private String name; |
416 |
|
417 |
/** |
418 |
* Creates a new instance of <code>AddInstrumentMap</code>. |
419 |
* |
420 |
* @param name The chosen name for the new MIDI instrument map. |
421 |
*/ |
422 |
public |
423 |
AddInstrumentMap(String name) { |
424 |
setTitle("Midi.AddMidiInstrumentMap_task"); |
425 |
setDescription(i18n.getMessage("Midi.AddMidiInstrumentMap.desc")); |
426 |
|
427 |
this.name = name; |
428 |
} |
429 |
|
430 |
/** The entry point of the task. */ |
431 |
public void |
432 |
run() { |
433 |
try { |
434 |
Integer mapId = CC.getClient().addMidiInstrumentMap(name); |
435 |
setResult(mapId); |
436 |
} catch(Exception x) { |
437 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
438 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
439 |
} |
440 |
} |
441 |
} |
442 |
|
443 |
/** |
444 |
* This task removes the specified MIDI instrument map. |
445 |
*/ |
446 |
public static class RemoveInstrumentMap extends EnhancedTask { |
447 |
private int mapId; |
448 |
|
449 |
/** |
450 |
* Creates a new instance of <code>RemoveInstrumentMap</code>. |
451 |
* |
452 |
* @param mapId The numerical ID of the MIDI instrument map to remove. |
453 |
*/ |
454 |
public |
455 |
RemoveInstrumentMap(int mapId) { |
456 |
setTitle("Midi.RemoveMidiInstrumentMap_task"); |
457 |
setDescription(i18n.getMessage("Midi.RemoveMidiInstrumentMap.desc", mapId)); |
458 |
|
459 |
this.mapId = mapId; |
460 |
} |
461 |
|
462 |
/** The entry point of the task. */ |
463 |
public void |
464 |
run() { |
465 |
try { |
466 |
CC.getClient().removeMidiInstrumentMap(mapId); |
467 |
} catch(Exception x) { |
468 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
469 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
470 |
} |
471 |
} |
472 |
} |
473 |
|
474 |
/** |
475 |
* This task changes the MIDI instrument map settings. |
476 |
*/ |
477 |
public static class SetInstrumentMapInfo extends EnhancedTask { |
478 |
private int mapId; |
479 |
private String name; |
480 |
|
481 |
/** |
482 |
* Creates a new instance of <code>SetInstrumentMapInfo</code>. |
483 |
* @param mapId The numerical ID of the MIDI instrument map. |
484 |
* @param name The new name for the specified MIDI instrument map. |
485 |
*/ |
486 |
public |
487 |
SetInstrumentMapInfo(int mapId, String name) { |
488 |
setTitle("Midi.SetInstrumentMapInfo_task"); |
489 |
setDescription(i18n.getMessage("Midi.SetInstrumentMapInfo.desc")); |
490 |
|
491 |
this.mapId = mapId; |
492 |
this.name = name; |
493 |
} |
494 |
|
495 |
/** The entry point of the task. */ |
496 |
public void |
497 |
run() { |
498 |
try { |
499 |
CC.getClient().setMidiInstrumentMapName(mapId, name); |
500 |
} catch(Exception x) { |
501 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
502 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
503 |
} |
504 |
} |
505 |
} |
506 |
|
507 |
/** |
508 |
* This task updates the settings of a MIDI instrument map. |
509 |
*/ |
510 |
public static class UpdateInstrumentMapInfo extends EnhancedTask { |
511 |
private int mapId; |
512 |
|
513 |
/** |
514 |
* Creates new instance of <code>UpdateInstrumentMapInfo</code>. |
515 |
* @param mapId The id of the MIDI instrument map, whose settings should be updated. |
516 |
*/ |
517 |
public |
518 |
UpdateInstrumentMapInfo(int mapId) { |
519 |
setTitle("Midi.UpdateInstrumentMapInfo_task"); |
520 |
setDescription(i18n.getMessage("Midi.UpdateInstrumentMapInfo.desc", mapId)); |
521 |
|
522 |
this.mapId = mapId; |
523 |
} |
524 |
|
525 |
/** The entry point of the task. */ |
526 |
public void |
527 |
run() { |
528 |
try { |
529 |
MidiInstrumentMapInfo info = |
530 |
CC.getClient().getMidiInstrumentMapInfo(mapId); |
531 |
|
532 |
CC.getSamplerModel().getMidiInstrumentMapById(mapId).setInfo(info); |
533 |
} catch(Exception x) { |
534 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
535 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
536 |
} |
537 |
} |
538 |
} |
539 |
|
540 |
/** |
541 |
* This task gets the MIDI instrument map list and all MIDI instruments' settings. |
542 |
*/ |
543 |
public static class GetInstrumentMaps extends EnhancedTask<MidiInstrumentMap[]> { |
544 |
/** Creates a new instance of <code>GetInstrumentMaps</code>. */ |
545 |
public |
546 |
GetInstrumentMaps() { |
547 |
setTitle("Midi.GetInstrumentMaps_task"); |
548 |
setDescription(i18n.getMessage("Midi.GetInstrumentMaps.desc")); |
549 |
} |
550 |
|
551 |
/** The entry point of the task. */ |
552 |
public void |
553 |
run() { |
554 |
try { |
555 |
MidiInstrumentMapInfo[] mims; |
556 |
mims = CC.getClient().getMidiInstrumentMaps(); |
557 |
MidiInstrumentMap[] maps = new MidiInstrumentMap[mims.length]; |
558 |
|
559 |
for(int i = 0; i < mims.length; i++) { |
560 |
maps[i] = createMap(mims[i]); |
561 |
} |
562 |
|
563 |
setResult(maps); |
564 |
} catch(Exception x) { |
565 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
566 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
567 |
} |
568 |
} |
569 |
|
570 |
private MidiInstrumentMap |
571 |
createMap(MidiInstrumentMapInfo m) throws Exception { |
572 |
MidiInstrumentMap map = new MidiInstrumentMap(m); |
573 |
MidiInstrumentInfo[] miis = CC.getClient().getMidiInstruments(m.getMapId()); |
574 |
|
575 |
for(MidiInstrumentInfo instrInfo : miis) { |
576 |
MidiInstrument instr = new MidiInstrument(instrInfo); |
577 |
map.mapMidiInstrument(instrInfo.getEntry(), instr); |
578 |
} |
579 |
|
580 |
return map; |
581 |
} |
582 |
} |
583 |
|
584 |
/** |
585 |
* This task updates the MIDI instrument map list. |
586 |
*/ |
587 |
public static class UpdateInstrumentMaps extends EnhancedTask { |
588 |
/** Creates a new instance of <code>UpdateInstrumentMaps</code>. */ |
589 |
public |
590 |
UpdateInstrumentMaps() { |
591 |
setTitle("Midi.UpdateInstrumentMaps_task"); |
592 |
setDescription(i18n.getMessage("Midi.UpdateInstrumentMaps.desc")); |
593 |
} |
594 |
|
595 |
/** The entry point of the task. */ |
596 |
public void |
597 |
run() { |
598 |
try { |
599 |
SamplerModel sm = CC.getSamplerModel(); |
600 |
Integer[] mapIDs = CC.getClient().getMidiInstrumentMapIDs(); |
601 |
|
602 |
boolean found = false; |
603 |
|
604 |
for(MidiInstrumentMap m : sm.getMidiInstrumentMaps()) { |
605 |
for(int i = 0; i < mapIDs.length; i++) { |
606 |
if(mapIDs[i] == m.getMapId()) { |
607 |
mapIDs[i] = -1; |
608 |
found = true; |
609 |
} |
610 |
} |
611 |
|
612 |
if(!found) sm.removeMidiInstrumentMapById(m.getMapId()); |
613 |
found = false; |
614 |
} |
615 |
|
616 |
MidiInstrumentMapInfo map; |
617 |
|
618 |
for(int id : mapIDs) { |
619 |
if(id >= 0) { |
620 |
map = CC.getClient().getMidiInstrumentMapInfo(id); |
621 |
sm.addMidiInstrumentMap(new MidiInstrumentMap(map)); |
622 |
} |
623 |
} |
624 |
} catch(Exception x) { |
625 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
626 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
627 |
x.printStackTrace(); |
628 |
} |
629 |
} |
630 |
} |
631 |
|
632 |
/** |
633 |
* This task maps a new MIDI instrument or replaces an existing one. |
634 |
*/ |
635 |
public static class MapInstrument extends EnhancedTask { |
636 |
private int mapId; |
637 |
private int bank; |
638 |
private int program; |
639 |
private MidiInstrumentInfo instrInfo; |
640 |
|
641 |
/** |
642 |
* Creates new instance of <code>MapInstrument</code>. |
643 |
* @param mapId The id of the MIDI instrument map. |
644 |
* @param bank The index of the MIDI bank, which will contain the instrument. |
645 |
* @param program The MIDI program number of the new instrument. |
646 |
* @param instrInfo Provides the MIDI instrument settings. |
647 |
*/ |
648 |
public |
649 |
MapInstrument(int mapId, int bank, int program, MidiInstrumentInfo instrInfo) { |
650 |
setTitle("Midi.MapInstrument_task"); |
651 |
setDescription(i18n.getMessage("Midi.MapInstrument.desc")); |
652 |
|
653 |
this.mapId = mapId; |
654 |
this.bank = bank; |
655 |
this.program = program; |
656 |
this.instrInfo = instrInfo; |
657 |
} |
658 |
|
659 |
/** The entry point of the task. */ |
660 |
public void |
661 |
run() { |
662 |
try { |
663 |
MidiInstrumentEntry entry = new MidiInstrumentEntry(bank, program); |
664 |
CC.getClient().mapMidiInstrument(mapId, entry, instrInfo, true); |
665 |
} catch(Exception x) { |
666 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
667 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
668 |
} |
669 |
} |
670 |
} |
671 |
|
672 |
/** |
673 |
* This task removes a MIDI instrument. |
674 |
*/ |
675 |
public static class UnmapInstrument extends EnhancedTask { |
676 |
private int mapId; |
677 |
private int bank; |
678 |
private int program; |
679 |
|
680 |
/** |
681 |
* Creates new instance of <code>UnmapInstrument</code>. |
682 |
* @param mapId The id of the MIDI instrument |
683 |
* map containing the instrument to be removed. |
684 |
* @param bank The index of the MIDI bank containing the instrument to be removed. |
685 |
* @param program The MIDI program number of the instrument to be removed. |
686 |
*/ |
687 |
public |
688 |
UnmapInstrument(int mapId, int bank, int program) { |
689 |
setTitle("Midi.UnmapInstrument_task"); |
690 |
setDescription(i18n.getMessage("Midi.UnmapInstrument.desc")); |
691 |
|
692 |
this.mapId = mapId; |
693 |
this.bank = bank; |
694 |
this.program = program; |
695 |
} |
696 |
|
697 |
/** The entry point of the task. */ |
698 |
public void |
699 |
run() { |
700 |
try { |
701 |
MidiInstrumentEntry entry = new MidiInstrumentEntry(bank, program); |
702 |
CC.getClient().unmapMidiInstrument(mapId, entry); |
703 |
} catch(Exception x) { |
704 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
705 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
706 |
} |
707 |
} |
708 |
} |
709 |
|
710 |
/** |
711 |
* This task updates the settings of a MIDI instrument. |
712 |
*/ |
713 |
public static class UpdateInstrumentInfo extends EnhancedTask { |
714 |
private int mapId; |
715 |
private int bank; |
716 |
private int program; |
717 |
|
718 |
/** |
719 |
* Creates new instance of <code>UpdateInstrumentInfo</code>. |
720 |
* @param mapId The id of the MIDI instrument map containg the instrument. |
721 |
* @param bank The index of the MIDI bank, containing the instrument. |
722 |
* @param program The MIDI program number of the instrument. |
723 |
*/ |
724 |
public |
725 |
UpdateInstrumentInfo(int mapId, int bank, int program) { |
726 |
setTitle("Midi.UpdateInstrumentInfo_task"); |
727 |
setDescription(i18n.getMessage("Midi.UpdateInstrumentInfo.desc")); |
728 |
|
729 |
this.mapId = mapId; |
730 |
this.bank = bank; |
731 |
this.program = program; |
732 |
} |
733 |
|
734 |
/** The entry point of the task. */ |
735 |
public void |
736 |
run() { |
737 |
try { |
738 |
MidiInstrumentInfo info = |
739 |
CC.getClient().getMidiInstrumentInfo(mapId, bank, program); |
740 |
|
741 |
MidiInstrumentMap map; |
742 |
map = CC.getSamplerModel().getMidiInstrumentMapById(mapId); |
743 |
map.getMidiInstrument(bank, program).setInfo(info); |
744 |
} catch(Exception x) { |
745 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
746 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
747 |
} |
748 |
} |
749 |
} |
750 |
|
751 |
/** |
752 |
* This task updates the MIDI instrument list on a specific MIDI instrument map. |
753 |
*/ |
754 |
public static class UpdateInstruments extends EnhancedTask { |
755 |
private int mapId; |
756 |
|
757 |
/** Creates a new instance of <code>UpdateInstruments</code>. */ |
758 |
public |
759 |
UpdateInstruments(int mapId) { |
760 |
setTitle("Midi.UpdateInstruments_task"); |
761 |
setDescription(i18n.getMessage("Midi.UpdateInstruments.desc")); |
762 |
|
763 |
this.mapId = mapId; |
764 |
} |
765 |
|
766 |
/** The entry point of the task. */ |
767 |
public void |
768 |
run() { |
769 |
try { |
770 |
SamplerModel sm = CC.getSamplerModel(); |
771 |
int[][] entries = CC.getClient().getMidiInstrumentEntries(mapId); |
772 |
MidiInstrumentMap map = sm.getMidiInstrumentMapById(mapId); |
773 |
boolean found = false; |
774 |
|
775 |
for(MidiInstrument instr : map.getAllMidiInstruments()) { |
776 |
for(int i = 0; i < entries.length; i++) { |
777 |
if(entries[i] == null) continue; |
778 |
|
779 |
if(equal(instr, entries[i])) { |
780 |
entries[i] = null; |
781 |
found = true; |
782 |
} |
783 |
} |
784 |
|
785 |
if(!found) { |
786 |
map.unmapMidiInstrument(instr.getInfo().getEntry()); |
787 |
} |
788 |
found = false; |
789 |
} |
790 |
|
791 |
for(int[] entry : entries) { |
792 |
if(entry != null) { |
793 |
MidiInstrumentInfo i; |
794 |
i = CC.getClient().getMidiInstrumentInfo ( |
795 |
entry[0], entry[1], entry[2] |
796 |
); |
797 |
MidiInstrument instr = new MidiInstrument(i); |
798 |
map.mapMidiInstrument(i.getEntry(), instr); |
799 |
} |
800 |
} |
801 |
} catch(Exception x) { |
802 |
setErrorMessage(getDescription() + ": " + HF.getErrorMessage(x)); |
803 |
CC.getLogger().log(Level.FINE, getErrorMessage(), x); |
804 |
} |
805 |
} |
806 |
|
807 |
private boolean |
808 |
equal(MidiInstrument instr, int[] entry) { |
809 |
if ( |
810 |
instr.getInfo().getMapId() == entry[0] && |
811 |
instr.getInfo().getMidiBank() == entry[1] && |
812 |
instr.getInfo().getMidiProgram() == entry[2] |
813 |
) return true; |
814 |
|
815 |
return false; |
816 |
} |
817 |
|
818 |
public int |
819 |
getMapId() { return mapId; } |
820 |
|
821 |
/** |
822 |
* Used to decrease the traffic. All task in the queue |
823 |
* equal to this are removed if added using {@link org.jsampler.CC#scheduleTask}. |
824 |
* @see org.jsampler.CC#addTask |
825 |
*/ |
826 |
public boolean |
827 |
equals(Object obj) { |
828 |
if(obj == null) return false; |
829 |
if(!(obj instanceof UpdateInstruments)) return false; |
830 |
if(((UpdateInstruments)obj).getMapId() != getMapId()) return false; |
831 |
|
832 |
return true; |
833 |
} |
834 |
} |
835 |
|
836 |
} |