1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
65: public class MidiSystem
66: {
67: private MidiSystem()
68: {
69:
70: }
71:
72:
77: public static MidiDevice.Info[] getMidiDeviceInfo()
78: {
79: Iterator<MidiDeviceProvider> deviceProviders =
80: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
81: List<MidiDevice.Info> infoList = new ArrayList<MidiDevice.Info>();
82:
83: while (deviceProviders.hasNext())
84: {
85: MidiDeviceProvider provider = (MidiDeviceProvider) deviceProviders.next();
86: MidiDevice.Info[] infos = provider.getDeviceInfo();
87: for (int i = infos.length; i > 0; )
88: infoList.add(infos[--i]);
89: }
90:
91: return infoList.toArray(new MidiDevice.Info[infoList.size()]);
92: }
93:
94:
102: public static MidiDevice getMidiDevice(MidiDevice.Info info)
103: throws MidiUnavailableException
104: {
105: Iterator<MidiDeviceProvider> deviceProviders =
106: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
107:
108: if (! deviceProviders.hasNext())
109: throw new MidiUnavailableException("No MIDI device providers available.");
110:
111: do
112: {
113: MidiDeviceProvider provider =
114: (MidiDeviceProvider) deviceProviders.next();
115: if (provider.isDeviceSupported(info))
116: return provider.getDevice(info);
117: } while (deviceProviders.hasNext());
118:
119: throw new IllegalArgumentException("MIDI device "
120: + info + " not available.");
121: }
122:
123:
130: public static Receiver getReceiver() throws MidiUnavailableException
131: {
132:
133:
134: MidiDevice.Info[] infos = getMidiDeviceInfo();
135: for (int i = 0; i < infos.length; i++)
136: {
137: MidiDevice device = getMidiDevice(infos[i]);
138: if (device instanceof Receiver)
139: return (Receiver) device;
140: }
141: throw new MidiUnavailableException("No Receiver device available");
142: }
143:
144:
151: public static Transmitter getTransmitter() throws MidiUnavailableException
152: {
153:
154:
155: MidiDevice.Info[] infos = getMidiDeviceInfo();
156: for (int i = 0; i < infos.length; i++)
157: {
158: MidiDevice device = getMidiDevice(infos[i]);
159: if (device instanceof Transmitter)
160: return (Transmitter) device;
161: }
162: throw new MidiUnavailableException("No Transmitter device available");
163: }
164:
165:
172: public static Synthesizer getSynthesizer() throws MidiUnavailableException
173: {
174:
175:
176: MidiDevice.Info[] infos = getMidiDeviceInfo();
177: for (int i = 0; i < infos.length; i++)
178: {
179: MidiDevice device = getMidiDevice(infos[i]);
180: if (device instanceof Synthesizer)
181: return (Synthesizer) device;
182: }
183: throw new MidiUnavailableException("No Synthesizer device available");
184: }
185:
186:
193: public static Sequencer getSequencer() throws MidiUnavailableException
194: {
195:
196:
197: MidiDevice.Info[] infos = getMidiDeviceInfo();
198: for (int i = 0; i < infos.length; i++)
199: {
200: MidiDevice device = getMidiDevice(infos[i]);
201: if (device instanceof Sequencer)
202: return (Sequencer) device;
203: }
204: throw new MidiUnavailableException("No Sequencer device available");
205: }
206:
207:
215: public static Soundbank getSoundbank(InputStream stream)
216: throws InvalidMidiDataException, IOException
217: {
218: Iterator<SoundbankReader> readers =
219: ServiceFactory.lookupProviders(SoundbankReader.class);
220: while (readers.hasNext())
221: {
222: SoundbankReader sr = readers.next();
223: Soundbank sb = sr.getSoundbank(stream);
224: if (sb != null)
225: return sb;
226: }
227: throw new InvalidMidiDataException("Cannot read soundbank from stream");
228: }
229:
230:
238: public static Soundbank getSoundbank(URL url)
239: throws InvalidMidiDataException, IOException
240: {
241: Iterator<SoundbankReader> readers =
242: ServiceFactory.lookupProviders(SoundbankReader.class);
243: while (readers.hasNext())
244: {
245: SoundbankReader sr = readers.next();
246: Soundbank sb = sr.getSoundbank(url);
247: if (sb != null)
248: return sb;
249: }
250: throw new InvalidMidiDataException("Cannot read from url " + url);
251: }
252:
253:
261: public static Soundbank getSoundbank(File file)
262: throws InvalidMidiDataException, IOException
263: {
264: Iterator<SoundbankReader> readers =
265: ServiceFactory.lookupProviders(SoundbankReader.class);
266: while (readers.hasNext())
267: {
268: SoundbankReader sr = (SoundbankReader) readers.next();
269: Soundbank sb = sr.getSoundbank(file);
270: if (sb != null)
271: return sb;
272: }
273: throw new InvalidMidiDataException("Cannot read soundbank from file "
274: + file);
275: }
276:
277:
285: public static MidiFileFormat getMidiFileFormat(InputStream stream)
286: throws InvalidMidiDataException, IOException
287: {
288: Iterator<MidiFileReader> readers =
289: ServiceFactory.lookupProviders(MidiFileReader.class);
290: while (readers.hasNext())
291: {
292: MidiFileReader sr = readers.next();
293: MidiFileFormat sb = sr.getMidiFileFormat(stream);
294: if (sb != null)
295: return sb;
296: }
297: throw new InvalidMidiDataException("Can't read MidiFileFormat from stream");
298: }
299:
300:
308: public static MidiFileFormat getMidiFileFormat(URL url)
309: throws InvalidMidiDataException, IOException
310: {
311: Iterator<MidiFileReader> readers =
312: ServiceFactory.lookupProviders(MidiFileReader.class);
313: while (readers.hasNext())
314: {
315: MidiFileReader sr = readers.next();
316: MidiFileFormat sb = sr.getMidiFileFormat(url);
317: if (sb != null)
318: return sb;
319: }
320: throw new InvalidMidiDataException("Cannot read from url " + url);
321: }
322:
323:
331: public static MidiFileFormat getMidiFileFormat(File file)
332: throws InvalidMidiDataException, IOException
333: {
334: Iterator<MidiFileReader> readers =
335: ServiceFactory.lookupProviders(MidiFileReader.class);
336: while (readers.hasNext())
337: {
338: MidiFileReader sr = readers.next();
339: MidiFileFormat sb = sr.getMidiFileFormat(file);
340: if (sb != null)
341: return sb;
342: }
343: throw new InvalidMidiDataException("Can't read MidiFileFormat from file "
344: + file);
345: }
346:
347:
355: public static Sequence getSequence(InputStream stream)
356: throws InvalidMidiDataException, IOException
357: {
358: Iterator<MidiFileReader> readers =
359: ServiceFactory.lookupProviders(MidiFileReader.class);
360: while (readers.hasNext())
361: {
362: MidiFileReader sr = readers.next();
363: Sequence sq = sr.getSequence(stream);
364: if (sq != null)
365: return sq;
366: }
367: throw new InvalidMidiDataException("Can't read Sequence from stream");
368: }
369:
370:
378: public static Sequence getSequence(URL url)
379: throws InvalidMidiDataException, IOException
380: {
381: Iterator<MidiFileReader> readers =
382: ServiceFactory.lookupProviders(MidiFileReader.class);
383: while (readers.hasNext())
384: {
385: MidiFileReader sr = readers.next();
386: Sequence sq = sr.getSequence(url);
387: if (sq != null)
388: return sq;
389: }
390: throw new InvalidMidiDataException("Cannot read from url " + url);
391: }
392:
393:
401: public static Sequence getSequence(File file)
402: throws InvalidMidiDataException, IOException
403: {
404: Iterator<MidiFileReader> readers =
405: ServiceFactory.lookupProviders(MidiFileReader.class);
406: while (readers.hasNext())
407: {
408: MidiFileReader sr = readers.next();
409: Sequence sq = sr.getSequence(file);
410: if (sq != null)
411: return sq;
412: }
413: throw new InvalidMidiDataException("Can't read Sequence from file "
414: + file);
415: }
416:
417:
422: public static int[] getMidiFileTypes()
423: {
424:
425: boolean supported[] = new boolean[3];
426:
427: int count = 0;
428: Iterator<MidiFileWriter> writers =
429: ServiceFactory.lookupProviders(MidiFileWriter.class);
430: while (writers.hasNext())
431: {
432: MidiFileWriter fw = writers.next();
433: int types[] = fw.getMidiFileTypes();
434: for (int i = types.length; i > 0;)
435: {
436: int type = types[--i];
437: if (supported[type] == false)
438: {
439: count++;
440: supported[type] = true;
441: }
442: }
443: }
444: int result[] = new int[count];
445: for (int i = supported.length; i > 0;)
446: {
447: if (supported[--i])
448: result[--count] = i;
449: }
450: return result;
451: }
452:
453:
459: public static boolean isFileTypeSupported(int fileType)
460: {
461: Iterator<MidiFileWriter> writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
462: while (writers.hasNext())
463: {
464: MidiFileWriter fw = writers.next();
465:
466: if (fw.isFileTypeSupported(fileType))
467: return true;
468: }
469: return false;
470: }
471:
472:
479: public static int[] getMidiFileTypes(Sequence sequence)
480: {
481:
482: boolean supported[] = new boolean[3];
483:
484: int count = 0;
485: Iterator<MidiFileWriter> writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
486: while (writers.hasNext())
487: {
488: MidiFileWriter fw = (MidiFileWriter) writers.next();
489: int types[] = fw.getMidiFileTypes(sequence);
490: for (int i = types.length; i > 0;)
491: {
492: int type = types[--i];
493: if (supported[type] == false)
494: {
495: count++;
496: supported[type] = true;
497: }
498: }
499: }
500: int result[] = new int[count];
501: for (int i = supported.length; i > 0;)
502: {
503: if (supported[--i])
504: result[--count] = i;
505: }
506: return result;
507: }
508:
509:
517: public static boolean isFileTypeSupported(int fileType, Sequence sequence)
518: {
519: Iterator<MidiFileWriter> writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
520: while (writers.hasNext())
521: {
522: MidiFileWriter fw = (MidiFileWriter) writers.next();
523:
524: if (fw.isFileTypeSupported(fileType, sequence))
525: return true;
526: }
527: return false;
528: }
529:
530:
540: public static int write(Sequence in, int fileType, OutputStream out)
541: throws IOException
542: {
543: Iterator<MidiFileWriter> writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
544: while (writers.hasNext())
545: {
546: MidiFileWriter fw = (MidiFileWriter) writers.next();
547:
548: if (fw.isFileTypeSupported(fileType, in))
549: return fw.write(in, fileType, out);
550: }
551: throw new IllegalArgumentException("File type "
552: + fileType + " is not supported");
553: }
554:
555:
565: public static int write(Sequence in, int fileType, File out)
566: throws IOException
567: {
568: Iterator<MidiFileWriter> writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
569: while (writers.hasNext())
570: {
571: MidiFileWriter fw = (MidiFileWriter) writers.next();
572:
573: if (fw.isFileTypeSupported(fileType, in))
574: return fw.write(in, fileType, out);
575: }
576: throw new IllegalArgumentException("File type "
577: + fileType + " is not supported");
578: }
579: }