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 deviceProviders =
80: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
81: List infoList = new ArrayList();
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 (MidiDevice.Info[])
92: infoList.toArray(new MidiDevice.Info[infoList.size()]);
93: }
94:
95:
103: public static MidiDevice getMidiDevice(MidiDevice.Info info)
104: throws MidiUnavailableException
105: {
106: Iterator deviceProviders =
107: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
108:
109: if (! deviceProviders.hasNext())
110: throw new MidiUnavailableException("No MIDI device providers available.");
111:
112: do
113: {
114: MidiDeviceProvider provider =
115: (MidiDeviceProvider) deviceProviders.next();
116: if (provider.isDeviceSupported(info))
117: return provider.getDevice(info);
118: } while (deviceProviders.hasNext());
119:
120: throw new IllegalArgumentException("MIDI device "
121: + info + " not available.");
122: }
123:
124:
131: public static Receiver getReceiver() throws MidiUnavailableException
132: {
133:
134:
135: MidiDevice.Info[] infos = getMidiDeviceInfo();
136: for (int i = 0; i < infos.length; i++)
137: {
138: MidiDevice device = getMidiDevice(infos[i]);
139: if (device instanceof Receiver)
140: return (Receiver) device;
141: }
142: throw new MidiUnavailableException("No Receiver device available");
143: }
144:
145:
152: public static Transmitter getTransmitter() throws MidiUnavailableException
153: {
154:
155:
156: MidiDevice.Info[] infos = getMidiDeviceInfo();
157: for (int i = 0; i < infos.length; i++)
158: {
159: MidiDevice device = getMidiDevice(infos[i]);
160: if (device instanceof Transmitter)
161: return (Transmitter) device;
162: }
163: throw new MidiUnavailableException("No Transmitter device available");
164: }
165:
166:
173: public static Synthesizer getSynthesizer() throws MidiUnavailableException
174: {
175:
176:
177: MidiDevice.Info[] infos = getMidiDeviceInfo();
178: for (int i = 0; i < infos.length; i++)
179: {
180: MidiDevice device = getMidiDevice(infos[i]);
181: if (device instanceof Synthesizer)
182: return (Synthesizer) device;
183: }
184: throw new MidiUnavailableException("No Synthesizer device available");
185: }
186:
187:
194: public static Sequencer getSequencer() throws MidiUnavailableException
195: {
196:
197:
198: MidiDevice.Info[] infos = getMidiDeviceInfo();
199: for (int i = 0; i < infos.length; i++)
200: {
201: MidiDevice device = getMidiDevice(infos[i]);
202: if (device instanceof Sequencer)
203: return (Sequencer) device;
204: }
205: throw new MidiUnavailableException("No Sequencer device available");
206: }
207:
208:
216: public static Soundbank getSoundbank(InputStream stream)
217: throws InvalidMidiDataException, IOException
218: {
219: Iterator readers = ServiceFactory.lookupProviders(SoundbankReader.class);
220: while (readers.hasNext())
221: {
222: SoundbankReader sr = (SoundbankReader) 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 readers = ServiceFactory.lookupProviders(SoundbankReader.class);
242: while (readers.hasNext())
243: {
244: SoundbankReader sr = (SoundbankReader) readers.next();
245: Soundbank sb = sr.getSoundbank(url);
246: if (sb != null)
247: return sb;
248: }
249: throw new InvalidMidiDataException("Cannot read from url " + url);
250: }
251:
252:
260: public static Soundbank getSoundbank(File file)
261: throws InvalidMidiDataException, IOException
262: {
263: Iterator readers = ServiceFactory.lookupProviders(SoundbankReader.class);
264: while (readers.hasNext())
265: {
266: SoundbankReader sr = (SoundbankReader) readers.next();
267: Soundbank sb = sr.getSoundbank(file);
268: if (sb != null)
269: return sb;
270: }
271: throw new InvalidMidiDataException("Cannot read soundbank from file "
272: + file);
273: }
274:
275:
283: public static MidiFileFormat getMidiFileFormat(InputStream stream)
284: throws InvalidMidiDataException, IOException
285: {
286: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
287: while (readers.hasNext())
288: {
289: MidiFileReader sr = (MidiFileReader) readers.next();
290: MidiFileFormat sb = sr.getMidiFileFormat(stream);
291: if (sb != null)
292: return sb;
293: }
294: throw new InvalidMidiDataException("Can't read MidiFileFormat from stream");
295: }
296:
297:
305: public static MidiFileFormat getMidiFileFormat(URL url)
306: throws InvalidMidiDataException, IOException
307: {
308: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
309: while (readers.hasNext())
310: {
311: MidiFileReader sr = (MidiFileReader) readers.next();
312: MidiFileFormat sb = sr.getMidiFileFormat(url);
313: if (sb != null)
314: return sb;
315: }
316: throw new InvalidMidiDataException("Cannot read from url " + url);
317: }
318:
319:
327: public static MidiFileFormat getMidiFileFormat(File file)
328: throws InvalidMidiDataException, IOException
329: {
330: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
331: while (readers.hasNext())
332: {
333: MidiFileReader sr = (MidiFileReader) readers.next();
334: MidiFileFormat sb = sr.getMidiFileFormat(file);
335: if (sb != null)
336: return sb;
337: }
338: throw new InvalidMidiDataException("Can't read MidiFileFormat from file "
339: + file);
340: }
341:
342:
350: public static Sequence getSequence(InputStream stream)
351: throws InvalidMidiDataException, IOException
352: {
353: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
354: while (readers.hasNext())
355: {
356: MidiFileReader sr = (MidiFileReader) readers.next();
357: Sequence sq = sr.getSequence(stream);
358: if (sq != null)
359: return sq;
360: }
361: throw new InvalidMidiDataException("Can't read Sequence from stream");
362: }
363:
364:
372: public static Sequence getSequence(URL url)
373: throws InvalidMidiDataException, IOException
374: {
375: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
376: while (readers.hasNext())
377: {
378: MidiFileReader sr = (MidiFileReader) readers.next();
379: Sequence sq = sr.getSequence(url);
380: if (sq != null)
381: return sq;
382: }
383: throw new InvalidMidiDataException("Cannot read from url " + url);
384: }
385:
386:
394: public static Sequence getSequence(File file)
395: throws InvalidMidiDataException, IOException
396: {
397: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
398: while (readers.hasNext())
399: {
400: MidiFileReader sr = (MidiFileReader) readers.next();
401: Sequence sq = sr.getSequence(file);
402: if (sq != null)
403: return sq;
404: }
405: throw new InvalidMidiDataException("Can't read Sequence from file "
406: + file);
407: }
408:
409:
414: public static int[] getMidiFileTypes()
415: {
416:
417: boolean supported[] = new boolean[3];
418:
419: int count = 0;
420: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
421: while (writers.hasNext())
422: {
423: MidiFileWriter fw = (MidiFileWriter) writers.next();
424: int types[] = fw.getMidiFileTypes();
425: for (int i = types.length; i > 0;)
426: {
427: int type = types[--i];
428: if (supported[type] == false)
429: {
430: count++;
431: supported[type] = true;
432: }
433: }
434: }
435: int result[] = new int[count];
436: for (int i = supported.length; i > 0;)
437: {
438: if (supported[--i])
439: result[--count] = i;
440: }
441: return result;
442: }
443:
444:
450: public static boolean isFileTypeSupported(int fileType)
451: {
452: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
453: while (writers.hasNext())
454: {
455: MidiFileWriter fw = (MidiFileWriter) writers.next();
456:
457: if (fw.isFileTypeSupported(fileType))
458: return true;
459: }
460: return false;
461: }
462:
463:
470: public static int[] getMidiFileTypes(Sequence sequence)
471: {
472:
473: boolean supported[] = new boolean[3];
474:
475: int count = 0;
476: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
477: while (writers.hasNext())
478: {
479: MidiFileWriter fw = (MidiFileWriter) writers.next();
480: int types[] = fw.getMidiFileTypes(sequence);
481: for (int i = types.length; i > 0;)
482: {
483: int type = types[--i];
484: if (supported[type] == false)
485: {
486: count++;
487: supported[type] = true;
488: }
489: }
490: }
491: int result[] = new int[count];
492: for (int i = supported.length; i > 0;)
493: {
494: if (supported[--i])
495: result[--count] = i;
496: }
497: return result;
498: }
499:
500:
508: public static boolean isFileTypeSupported(int fileType, Sequence sequence)
509: {
510: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
511: while (writers.hasNext())
512: {
513: MidiFileWriter fw = (MidiFileWriter) writers.next();
514:
515: if (fw.isFileTypeSupported(fileType, sequence))
516: return true;
517: }
518: return false;
519: }
520:
521:
531: public static int write(Sequence in, int fileType, OutputStream out)
532: throws IOException
533: {
534: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
535: while (writers.hasNext())
536: {
537: MidiFileWriter fw = (MidiFileWriter) writers.next();
538:
539: if (fw.isFileTypeSupported(fileType, in))
540: return fw.write(in, fileType, out);
541: }
542: throw new IllegalArgumentException("File type "
543: + fileType + " is not supported");
544: }
545:
546:
556: public static int write(Sequence in, int fileType, File out)
557: throws IOException
558: {
559: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
560: while (writers.hasNext())
561: {
562: MidiFileWriter fw = (MidiFileWriter) writers.next();
563:
564: if (fw.isFileTypeSupported(fileType, in))
565: return fw.write(in, fileType, out);
566: }
567: throw new IllegalArgumentException("File type "
568: + fileType + " is not supported");
569: }
570: }