1 7 8 package javax.sound.midi; 9 10 import java.io.FileInputStream ; 11 import java.io.File ; 12 import java.io.InputStream ; 13 import java.io.OutputStream ; 14 import java.io.IOException ; 15 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import java.net.URL ; 23 24 import javax.sound.midi.spi.MidiFileWriter ; 25 import javax.sound.midi.spi.MidiFileReader ; 26 import javax.sound.midi.spi.SoundbankReader ; 27 import javax.sound.midi.spi.MidiDeviceProvider ; 28 29 import com.sun.media.sound.JDK13Services; 30 import com.sun.media.sound.ReferenceCountingDevice; 31 import com.sun.media.sound.AutoConnectSequencer; 32 33 34 147 public class MidiSystem { 148 149 152 private MidiSystem() { 153 } 154 155 156 167 public static MidiDevice.Info [] getMidiDeviceInfo() { 168 List allInfos = new ArrayList (); 169 List providers = getMidiDeviceProviders(); 170 171 for(int i = 0; i < providers.size(); i++) { 172 MidiDeviceProvider provider = (MidiDeviceProvider ) providers.get(i); 173 MidiDevice.Info [] tmpinfo = provider.getDeviceInfo(); 174 for (int j = 0; j < tmpinfo.length; j++) { 175 allInfos.add( tmpinfo[j] ); 176 } 177 } 178 MidiDevice.Info [] infosArray = (MidiDevice.Info []) allInfos.toArray(new MidiDevice.Info [0]); 179 return infosArray; 180 } 181 182 183 194 public static MidiDevice getMidiDevice(MidiDevice.Info info) throws MidiUnavailableException { 195 List providers = getMidiDeviceProviders(); 196 197 for(int i = 0; i < providers.size(); i++) { 198 MidiDeviceProvider provider = (MidiDeviceProvider ) providers.get(i); 199 if (provider.isDeviceSupported(info)) { 200 MidiDevice device = provider.getDevice(info); 201 return device; 202 } 203 } 204 throw new IllegalArgumentException ("Requested device not installed: " + info); 205 } 206 207 208 238 public static Receiver getReceiver() throws MidiUnavailableException { 239 MidiDevice device = getDefaultDeviceWrapper(Receiver .class); 241 Receiver receiver; 242 if (device instanceof ReferenceCountingDevice) { 243 receiver = ((ReferenceCountingDevice) device).getReceiverReferenceCounting(); 244 } else { 245 receiver = device.getReceiver(); 246 } 247 return receiver; 248 } 249 250 251 278 public static Transmitter getTransmitter() throws MidiUnavailableException { 279 MidiDevice device = getDefaultDeviceWrapper(Transmitter .class); 281 Transmitter transmitter; 282 if (device instanceof ReferenceCountingDevice) { 283 transmitter = ((ReferenceCountingDevice) device).getTransmitterReferenceCounting(); 284 } else { 285 transmitter = device.getTransmitter(); 286 } 287 return transmitter; 288 } 289 290 291 305 public static Synthesizer getSynthesizer() throws MidiUnavailableException { 306 return (Synthesizer ) getDefaultDeviceWrapper(Synthesizer .class); 308 } 309 310 311 347 public static Sequencer getSequencer() throws MidiUnavailableException { 348 return getSequencer(true); 349 } 350 351 352 353 396 public static Sequencer getSequencer(boolean connected) 397 throws MidiUnavailableException { 398 Sequencer seq = (Sequencer ) getDefaultDeviceWrapper(Sequencer .class); 399 400 if (connected) { 401 407 Receiver rec = null; 408 MidiUnavailableException mue = null; 409 410 try { 412 Synthesizer synth = getSynthesizer(); 413 if (synth instanceof ReferenceCountingDevice) { 414 rec = ((ReferenceCountingDevice) synth).getReceiverReferenceCounting(); 415 if (synth.getClass().toString().contains("com.sun.media.sound.MixerSynth") 417 && (synth.getDefaultSoundbank() == null)) { 418 rec = null; 420 synth.close(); 421 } 422 } else { 423 synth.open(); 424 try { 425 rec = synth.getReceiver(); 426 } finally { 427 if (rec == null) { 429 synth.close(); 430 } 431 } 432 } 433 } catch (MidiUnavailableException e) { 434 if (e instanceof MidiUnavailableException ) { 436 mue = (MidiUnavailableException ) e; 437 } 438 } 439 if (rec == null) { 440 try { 442 rec = MidiSystem.getReceiver(); 443 } catch (Exception e) { 444 if (e instanceof MidiUnavailableException ) { 446 mue = (MidiUnavailableException ) e; 447 } 448 } 449 } 450 if (rec != null) { 451 seq.getTransmitter().setReceiver(rec); 452 if (seq instanceof AutoConnectSequencer) { 453 ((AutoConnectSequencer) seq).setAutoConnect(rec); 454 } 455 } else { 456 if (mue != null) { 457 throw mue; 458 } 459 throw new MidiUnavailableException ("no receiver available"); 460 } 461 } 462 return seq; 463 } 464 465 466 467 468 486 public static Soundbank getSoundbank(InputStream stream) 487 throws InvalidMidiDataException , IOException { 488 489 SoundbankReader sp = null; 490 Soundbank s = null; 491 492 List providers = getSoundbankReaders(); 493 494 for(int i = 0; i < providers.size(); i++) { 495 sp = (SoundbankReader )providers.get(i); 496 s = sp.getSoundbank(stream); 497 498 if( s!= null) { 499 return s; 500 } 501 } 502 throw new InvalidMidiDataException ("cannot get soundbank from stream"); 503 504 } 505 506 507 517 public static Soundbank getSoundbank(URL url) 518 throws InvalidMidiDataException , IOException { 519 520 SoundbankReader sp = null; 521 Soundbank s = null; 522 523 List providers = getSoundbankReaders(); 524 525 for(int i = 0; i < providers.size(); i++) { 526 sp = (SoundbankReader )providers.get(i); 527 s = sp.getSoundbank(url); 528 529 if( s!= null) { 530 return s; 531 } 532 } 533 throw new InvalidMidiDataException ("cannot get soundbank from stream"); 534 535 } 536 537 538 549 public static Soundbank getSoundbank(File file) 550 throws InvalidMidiDataException , IOException { 551 552 SoundbankReader sp = null; 553 Soundbank s = null; 554 555 List providers = getSoundbankReaders(); 556 557 for(int i = 0; i < providers.size(); i++) { 558 sp = (SoundbankReader )providers.get(i); 559 s = sp.getSoundbank(file); 560 561 if( s!= null) { 562 return s; 563 } 564 } 565 throw new InvalidMidiDataException ("cannot get soundbank from stream"); 566 } 567 568 569 570 602 public static MidiFileFormat getMidiFileFormat(InputStream stream) 603 throws InvalidMidiDataException , IOException { 604 605 List providers = getMidiFileReaders(); 606 MidiFileFormat format = null; 607 608 for(int i = 0; i < providers.size(); i++) { 609 MidiFileReader reader = (MidiFileReader ) providers.get(i); 610 try { 611 format = reader.getMidiFileFormat( stream ); break; 613 } catch (InvalidMidiDataException e) { 614 continue; 615 } 616 } 617 618 if( format==null ) { 619 throw new InvalidMidiDataException ("input stream is not a supported file type"); 620 } else { 621 return format; 622 } 623 } 624 625 626 648 public static MidiFileFormat getMidiFileFormat(URL url) 649 throws InvalidMidiDataException , IOException { 650 651 List providers = getMidiFileReaders(); 652 MidiFileFormat format = null; 653 654 for(int i = 0; i < providers.size(); i++) { 655 MidiFileReader reader = (MidiFileReader ) providers.get(i); 656 try { 657 format = reader.getMidiFileFormat( url ); break; 659 } catch (InvalidMidiDataException e) { 660 continue; 661 } 662 } 663 664 if( format==null ) { 665 throw new InvalidMidiDataException ("url is not a supported file type"); 666 } else { 667 return format; 668 } 669 } 670 671 672 694 public static MidiFileFormat getMidiFileFormat(File file) 695 throws InvalidMidiDataException , IOException { 696 697 List providers = getMidiFileReaders(); 698 MidiFileFormat format = null; 699 700 for(int i = 0; i < providers.size(); i++) { 701 MidiFileReader reader = (MidiFileReader ) providers.get(i); 702 try { 703 format = reader.getMidiFileFormat( file ); break; 705 } catch (InvalidMidiDataException e) { 706 continue; 707 } 708 } 709 710 if( format==null ) { 711 throw new InvalidMidiDataException ("file is not a supported file type"); 712 } else { 713 return format; 714 } 715 } 716 717 718 749 public static Sequence getSequence(InputStream stream) 750 throws InvalidMidiDataException , IOException { 751 752 List providers = getMidiFileReaders(); 753 Sequence sequence = null; 754 755 for(int i = 0; i < providers.size(); i++) { 756 MidiFileReader reader = (MidiFileReader ) providers.get(i); 757 try { 758 sequence = reader.getSequence( stream ); break; 760 } catch (InvalidMidiDataException e) { 761 continue; 762 } 763 } 764 765 if( sequence==null ) { 766 throw new InvalidMidiDataException ("could not get sequence from input stream"); 767 } else { 768 return sequence; 769 } 770 } 771 772 773 793 public static Sequence getSequence(URL url) 794 throws InvalidMidiDataException , IOException { 795 796 List providers = getMidiFileReaders(); 797 Sequence sequence = null; 798 799 for(int i = 0; i < providers.size(); i++) { 800 MidiFileReader reader = (MidiFileReader ) providers.get(i); 801 try { 802 sequence = reader.getSequence( url ); break; 804 } catch (InvalidMidiDataException e) { 805 continue; 806 } 807 } 808 809 if( sequence==null ) { 810 throw new InvalidMidiDataException ("could not get sequence from URL"); 811 } else { 812 return sequence; 813 } 814 } 815 816 817 837 public static Sequence getSequence(File file) 838 throws InvalidMidiDataException , IOException { 839 840 List providers = getMidiFileReaders(); 841 Sequence sequence = null; 842 843 for(int i = 0; i < providers.size(); i++) { 844 MidiFileReader reader = (MidiFileReader ) providers.get(i); 845 try { 846 sequence = reader.getSequence( file ); break; 848 } catch (InvalidMidiDataException e) { 849 continue; 850 } 851 } 852 853 if( sequence==null ) { 854 throw new InvalidMidiDataException ("could not get sequence from file"); 855 } else { 856 return sequence; 857 } 858 } 859 860 861 867 public static int[] getMidiFileTypes() { 868 869 List providers = getMidiFileWriters(); 870 Set allTypes = new HashSet (); 871 872 874 for (int i = 0; i < providers.size(); i++ ) { 875 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 876 int[] types = writer.getMidiFileTypes(); 877 for (int j = 0; j < types.length; j++ ) { 878 allTypes.add(new Integer (types[j])); 879 } 880 } 881 int resultTypes[] = new int[allTypes.size()]; 882 int index = 0; 883 Iterator iterator = allTypes.iterator(); 884 while (iterator.hasNext()) { 885 Integer integer = (Integer ) iterator.next(); 886 resultTypes[index++] = integer.intValue(); 887 } 888 return resultTypes; 889 } 890 891 892 899 public static boolean isFileTypeSupported(int fileType) { 900 901 List providers = getMidiFileWriters(); 902 903 for (int i = 0; i < providers.size(); i++ ) { 904 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 905 if( writer.isFileTypeSupported(fileType)) { 906 return true; 907 } 908 } 909 return false; 910 } 911 912 913 921 public static int[] getMidiFileTypes(Sequence sequence) { 922 923 List providers = getMidiFileWriters(); 924 Set allTypes = new HashSet (); 925 926 928 for (int i = 0; i < providers.size(); i++ ) { 929 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 930 int[] types = writer.getMidiFileTypes(sequence); 931 for (int j = 0; j < types.length; j++ ) { 932 allTypes.add(new Integer (types[j])); 933 } 934 } 935 int resultTypes[] = new int[allTypes.size()]; 936 int index = 0; 937 Iterator iterator = allTypes.iterator(); 938 while (iterator.hasNext()) { 939 Integer integer = (Integer ) iterator.next(); 940 resultTypes[index++] = integer.intValue(); 941 } 942 return resultTypes; 943 } 944 945 946 955 public static boolean isFileTypeSupported(int fileType, Sequence sequence) { 956 957 List providers = getMidiFileWriters(); 958 959 for (int i = 0; i < providers.size(); i++ ) { 960 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 961 if( writer.isFileTypeSupported(fileType,sequence)) { 962 return true; 963 } 964 } 965 return false; 966 } 967 968 969 982 public static int write(Sequence in, int fileType, OutputStream out) throws IOException { 983 984 List providers = getMidiFileWriters(); 985 int bytesWritten = -2; 987 988 for (int i = 0; i < providers.size(); i++ ) { 989 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 990 if( writer.isFileTypeSupported( fileType, in ) ) { 991 992 bytesWritten = writer.write(in, fileType, out); 993 break; 994 } 995 } 996 if (bytesWritten == -2) { 997 throw new IllegalArgumentException ("MIDI file type is not supported"); 998 } 999 return bytesWritten; 1000 } 1001 1002 1003 1016 public static int write(Sequence in, int type, File out) throws IOException { 1017 1018 List providers = getMidiFileWriters(); 1019 int bytesWritten = -2; 1021 1022 for (int i = 0; i < providers.size(); i++ ) { 1023 MidiFileWriter writer = (MidiFileWriter ) providers.get(i); 1024 if( writer.isFileTypeSupported( type, in ) ) { 1025 1026 bytesWritten = writer.write(in, type, out); 1027 break; 1028 } 1029 } 1030 if (bytesWritten == -2) { 1031 throw new IllegalArgumentException ("MIDI file type is not supported"); 1032 } 1033 return bytesWritten; 1034 } 1035 1036 1037 1038 1040 private static List getMidiDeviceProviders() { 1041 return getProviders(MidiDeviceProvider .class); 1042 } 1043 1044 1045 private static List getSoundbankReaders() { 1046 return getProviders(SoundbankReader .class); 1047 } 1048 1049 1050 private static List getMidiFileWriters() { 1051 return getProviders(MidiFileWriter .class); 1052 } 1053 1054 1055 private static List getMidiFileReaders() { 1056 return getProviders(MidiFileReader .class); 1057 } 1058 1059 1060 1073 private static MidiDevice getDefaultDeviceWrapper(Class deviceClass) 1074 throws MidiUnavailableException { 1075 try { 1076 return getDefaultDevice(deviceClass); 1077 } catch (IllegalArgumentException iae) { 1078 MidiUnavailableException mae = new MidiUnavailableException (); 1079 mae.initCause(iae); 1080 throw mae; 1081 } 1082 } 1083 1084 1085 1092 private static MidiDevice getDefaultDevice(Class deviceClass) { 1093 List providers = getMidiDeviceProviders(); 1094 String providerClassName = JDK13Services.getDefaultProviderClassName(deviceClass); 1095 String instanceName = JDK13Services.getDefaultInstanceName(deviceClass); 1096 MidiDevice device; 1097 1098 if (providerClassName != null) { 1099 MidiDeviceProvider defaultProvider = getNamedProvider(providerClassName, providers); 1100 if (defaultProvider != null) { 1101 if (instanceName != null) { 1102 device = getNamedDevice(instanceName, defaultProvider, deviceClass); 1103 if (device != null) { 1104 return device; 1105 } 1106 } 1107 device = getFirstDevice(defaultProvider, deviceClass); 1108 if (device != null) { 1109 return device; 1110 } 1111 } 1112 } 1113 1114 1117 if (instanceName != null) { 1118 device = getNamedDevice(instanceName, providers, deviceClass); 1119 if (device != null) { 1120 return device; 1121 } 1122 } 1123 1124 1126 device = getFirstDevice(providers, deviceClass); 1127 if (device != null) { 1128 return device; 1129 } 1130 throw new IllegalArgumentException ("Requested device not installed"); 1131 } 1132 1133 1134 1135 1143 private static MidiDeviceProvider getNamedProvider(String providerClassName, List providers) { 1144 for(int i = 0; i < providers.size(); i++) { 1145 MidiDeviceProvider provider = (MidiDeviceProvider ) providers.get(i); 1146 if (provider.getClass().getName().equals(providerClassName)) { 1147 return provider; 1148 } 1149 } 1150 return null; 1151 } 1152 1153 1154 1162 private static MidiDevice getNamedDevice(String deviceName, 1163 MidiDeviceProvider provider, 1164 Class deviceClass) { 1165 MidiDevice device; 1166 device = getNamedDevice(deviceName, provider, deviceClass, 1168 false, false); 1169 if (device != null) { 1170 return device; 1171 } 1172 1173 if (deviceClass == Receiver .class) { 1174 device = getNamedDevice(deviceName, provider, deviceClass, 1176 true, false); 1177 if (device != null) { 1178 return device; 1179 } 1180 } 1181 1182 return getNamedDevice(deviceName, provider, deviceClass, 1184 true, true); 1185 } 1186 1187 1188 1196 private static MidiDevice getNamedDevice(String deviceName, 1197 MidiDeviceProvider provider, 1198 Class deviceClass, 1199 boolean allowSynthesizer, 1200 boolean allowSequencer) { 1201 MidiDevice.Info [] infos = provider.getDeviceInfo(); 1202 for (int i = 0; i < infos.length; i++) { 1203 if (infos[i].getName().equals(deviceName)) { 1204 MidiDevice device = provider.getDevice(infos[i]); 1205 if (isAppropriateDevice(device, deviceClass, 1206 allowSynthesizer, allowSequencer)) { 1207 return device; 1208 } 1209 } 1210 } 1211 return null; 1212 } 1213 1214 1215 1224 private static MidiDevice getNamedDevice(String deviceName, 1225 List providers, 1226 Class deviceClass) { 1227 MidiDevice device; 1228 device = getNamedDevice(deviceName, providers, deviceClass, 1230 false, false); 1231 if (device != null) { 1232 return device; 1233 } 1234 1235 if (deviceClass == Receiver .class) { 1236 device = getNamedDevice(deviceName, providers, deviceClass, 1238 true, false); 1239 if (device != null) { 1240 return device; 1241 } 1242 } 1243 1244 return getNamedDevice(deviceName, providers, deviceClass, 1246 true, true); 1247 } 1248 1249 1250 1259 private static MidiDevice getNamedDevice(String deviceName, 1260 List providers, 1261 Class deviceClass, 1262 boolean allowSynthesizer, 1263 boolean allowSequencer) { 1264 for(int i = 0; i < providers.size(); i++) { 1265 MidiDeviceProvider provider = (MidiDeviceProvider ) providers.get(i); 1266 MidiDevice device = getNamedDevice(deviceName, provider, 1267 deviceClass, 1268 allowSynthesizer, 1269 allowSequencer); 1270 if (device != null) { 1271 return device; 1272 } 1273 } 1274 return null; 1275 } 1276 1277 1278 1285 private static MidiDevice getFirstDevice(MidiDeviceProvider provider, 1286 Class deviceClass) { 1287 MidiDevice device; 1288 device = getFirstDevice(provider, deviceClass, 1290 false, false); 1291 if (device != null) { 1292 return device; 1293 } 1294 1295 if (deviceClass == Receiver .class) { 1296 device = getFirstDevice(provider, deviceClass, 1298 true, false); 1299 if (device != null) { 1300 return device; 1301 } 1302 } 1303 1304 return getFirstDevice(provider, deviceClass, 1306 true, true); 1307 } 1308 1309 1310 1317 private static MidiDevice getFirstDevice(MidiDeviceProvider provider, 1318 Class deviceClass, 1319 boolean allowSynthesizer, 1320 boolean allowSequencer) { 1321 MidiDevice.Info [] infos = provider.getDeviceInfo(); 1322 for (int j = 0; j < infos.length; j++) { 1323 MidiDevice device = provider.getDevice(infos[j]); 1324 if (isAppropriateDevice(device, deviceClass, 1325 allowSynthesizer, allowSequencer)) { 1326 return device; 1327 } 1328 } 1329 return null; 1330 } 1331 1332 1333 1341 private static MidiDevice getFirstDevice(List providers, 1342 Class deviceClass) { 1343 MidiDevice device; 1344 device = getFirstDevice(providers, deviceClass, 1346 false, false); 1347 if (device != null) { 1348 return device; 1349 } 1350 1351 if (deviceClass == Receiver .class) { 1352 device = getFirstDevice(providers, deviceClass, 1354 true, false); 1355 if (device != null) { 1356 return device; 1357 } 1358 } 1359 1360 return getFirstDevice(providers, deviceClass, 1362 true, true); 1363 } 1364 1365 1366 1374 private static MidiDevice getFirstDevice(List providers, 1375 Class deviceClass, 1376 boolean allowSynthesizer, 1377 boolean allowSequencer) { 1378 for(int i = 0; i < providers.size(); i++) { 1379 MidiDeviceProvider provider = (MidiDeviceProvider ) providers.get(i); 1380 MidiDevice device = getFirstDevice(provider, deviceClass, 1381 allowSynthesizer, 1382 allowSequencer); 1383 if (device != null) { 1384 return device; 1385 } 1386 } 1387 return null; 1388 } 1389 1390 1391 1412 private static boolean isAppropriateDevice(MidiDevice device, 1413 Class deviceClass, 1414 boolean allowSynthesizer, 1415 boolean allowSequencer) { 1416 if (deviceClass.isInstance(device)) { 1417 return true; 1420 } else { 1421 if ( (! (device instanceof Sequencer ) && 1428 ! (device instanceof Synthesizer ) ) || 1429 ((device instanceof Sequencer ) && allowSequencer) || 1430 ((device instanceof Synthesizer ) && allowSynthesizer)) { 1431 if ((deviceClass == Receiver .class && 1434 device.getMaxReceivers() != 0) || 1435 (deviceClass == Transmitter .class && 1436 device.getMaxTransmitters() != 0)) { 1437 return true; 1438 } 1439 } 1440 } 1441 return false; 1442 } 1443 1444 1445 1451 private static List getProviders(Class providerClass) { 1452 return JDK13Services.getProviders(providerClass); 1453 } 1454} 1455 | Popular Tags |