/[svn]/jsampler/trunk/src/org/jsampler/task/Midi.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/task/Midi.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1867 - (show annotations) (download)
Mon Mar 16 22:12:32 2009 UTC (15 years, 1 month ago) by iliev
File size: 20294 byte(s)
* proper handling of connection failures
* renamed Channels Panels to Channel Lanes
* code cleanup

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

  ViewVC Help
Powered by ViewVC