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:
72: public static MidiDevice.Info[] getMidiDeviceInfo()
73: {
74: Iterator deviceProviders =
75: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
76: List infoList = new ArrayList();
77:
78: while (deviceProviders.hasNext())
79: {
80: MidiDeviceProvider provider = (MidiDeviceProvider) deviceProviders.next();
81: MidiDevice.Info[] infos = provider.getDeviceInfo();
82: for (int i = infos.length; i > 0; )
83: infoList.add(infos[--i]);
84: }
85:
86: return (MidiDevice.Info[])
87: infoList.toArray(new MidiDevice.Info[infoList.size()]);
88: }
89:
90:
98: public static MidiDevice getMidiDevice(MidiDevice.Info info)
99: throws MidiUnavailableException
100: {
101: Iterator deviceProviders =
102: ServiceFactory.lookupProviders(MidiDeviceProvider.class);
103:
104: if (! deviceProviders.hasNext())
105: throw new MidiUnavailableException("No MIDI device providers available.");
106:
107: do
108: {
109: MidiDeviceProvider provider =
110: (MidiDeviceProvider) deviceProviders.next();
111: if (provider.isDeviceSupported(info))
112: return provider.getDevice(info);
113: } while (deviceProviders.hasNext());
114:
115: throw new IllegalArgumentException("MIDI device "
116: + info + " not available.");
117: }
118:
119:
126: public static Receiver getReceiver() throws MidiUnavailableException
127: {
128:
129:
130: MidiDevice.Info[] infos = getMidiDeviceInfo();
131: for (int i = 0; i < infos.length; i++)
132: {
133: MidiDevice device = getMidiDevice(infos[i]);
134: if (device instanceof Receiver)
135: return (Receiver) device;
136: }
137: throw new MidiUnavailableException("No Receiver device available");
138: }
139:
140:
147: public static Transmitter getTransmitter() throws MidiUnavailableException
148: {
149:
150:
151: MidiDevice.Info[] infos = getMidiDeviceInfo();
152: for (int i = 0; i < infos.length; i++)
153: {
154: MidiDevice device = getMidiDevice(infos[i]);
155: if (device instanceof Transmitter)
156: return (Transmitter) device;
157: }
158: throw new MidiUnavailableException("No Transmitter device available");
159: }
160:
161:
168: public static Synthesizer getSynthesizer() throws MidiUnavailableException
169: {
170:
171:
172: MidiDevice.Info[] infos = getMidiDeviceInfo();
173: for (int i = 0; i < infos.length; i++)
174: {
175: MidiDevice device = getMidiDevice(infos[i]);
176: if (device instanceof Synthesizer)
177: return (Synthesizer) device;
178: }
179: throw new MidiUnavailableException("No Synthesizer device available");
180: }
181:
182:
189: public static Sequencer getSequencer() throws MidiUnavailableException
190: {
191:
192:
193: MidiDevice.Info[] infos = getMidiDeviceInfo();
194: for (int i = 0; i < infos.length; i++)
195: {
196: MidiDevice device = getMidiDevice(infos[i]);
197: if (device instanceof Sequencer)
198: return (Sequencer) device;
199: }
200: throw new MidiUnavailableException("No Sequencer device available");
201: }
202:
203:
211: public static Soundbank getSoundbank(InputStream stream)
212: throws InvalidMidiDataException, IOException
213: {
214: Iterator readers = ServiceFactory.lookupProviders(SoundbankReader.class);
215: while (readers.hasNext())
216: {
217: SoundbankReader sr = (SoundbankReader) readers.next();
218: Soundbank sb = sr.getSoundbank(stream);
219: if (sb != null)
220: return sb;
221: }
222: throw new InvalidMidiDataException("Cannot read soundbank from stream");
223: }
224:
225:
233: public static Soundbank getSoundbank(URL url)
234: throws InvalidMidiDataException, IOException
235: {
236: Iterator readers = ServiceFactory.lookupProviders(SoundbankReader.class);
237: while (readers.hasNext())
238: {
239: SoundbankReader sr = (SoundbankReader) readers.next();
240: Soundbank sb = sr.getSoundbank(url);
241: if (sb != null)
242: return sb;
243: }
244: throw new InvalidMidiDataException("Cannot read from url " + url);
245: }
246:
247:
255: public static Soundbank getSoundbank(File file)
256: throws InvalidMidiDataException, IOException
257: {
258: Iterator readers = ServiceFactory.lookupProviders(SoundbankReader.class);
259: while (readers.hasNext())
260: {
261: SoundbankReader sr = (SoundbankReader) readers.next();
262: Soundbank sb = sr.getSoundbank(file);
263: if (sb != null)
264: return sb;
265: }
266: throw new InvalidMidiDataException("Cannot read soundbank from file "
267: + file);
268: }
269:
270:
278: public static MidiFileFormat getMidiFileFormat(InputStream stream)
279: throws InvalidMidiDataException, IOException
280: {
281: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
282: while (readers.hasNext())
283: {
284: MidiFileReader sr = (MidiFileReader) readers.next();
285: MidiFileFormat sb = sr.getMidiFileFormat(stream);
286: if (sb != null)
287: return sb;
288: }
289: throw new InvalidMidiDataException("Can't read MidiFileFormat from stream");
290: }
291:
292:
300: public static MidiFileFormat getMidiFileFormat(URL url)
301: throws InvalidMidiDataException, IOException
302: {
303: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
304: while (readers.hasNext())
305: {
306: MidiFileReader sr = (MidiFileReader) readers.next();
307: MidiFileFormat sb = sr.getMidiFileFormat(url);
308: if (sb != null)
309: return sb;
310: }
311: throw new InvalidMidiDataException("Cannot read from url " + url);
312: }
313:
314:
322: public static MidiFileFormat getMidiFileFormat(File file)
323: throws InvalidMidiDataException, IOException
324: {
325: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
326: while (readers.hasNext())
327: {
328: MidiFileReader sr = (MidiFileReader) readers.next();
329: MidiFileFormat sb = sr.getMidiFileFormat(file);
330: if (sb != null)
331: return sb;
332: }
333: throw new InvalidMidiDataException("Can't read MidiFileFormat from file "
334: + file);
335: }
336:
337:
345: public static Sequence getSequence(InputStream stream)
346: throws InvalidMidiDataException, IOException
347: {
348: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
349: while (readers.hasNext())
350: {
351: MidiFileReader sr = (MidiFileReader) readers.next();
352: Sequence sq = sr.getSequence(stream);
353: if (sq != null)
354: return sq;
355: }
356: throw new InvalidMidiDataException("Can't read Sequence from stream");
357: }
358:
359:
367: public static Sequence getSequence(URL url)
368: throws InvalidMidiDataException, IOException
369: {
370: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
371: while (readers.hasNext())
372: {
373: MidiFileReader sr = (MidiFileReader) readers.next();
374: Sequence sq = sr.getSequence(url);
375: if (sq != null)
376: return sq;
377: }
378: throw new InvalidMidiDataException("Cannot read from url " + url);
379: }
380:
381:
389: public static Sequence getSequence(File file)
390: throws InvalidMidiDataException, IOException
391: {
392: Iterator readers = ServiceFactory.lookupProviders(MidiFileReader.class);
393: while (readers.hasNext())
394: {
395: MidiFileReader sr = (MidiFileReader) readers.next();
396: Sequence sq = sr.getSequence(file);
397: if (sq != null)
398: return sq;
399: }
400: throw new InvalidMidiDataException("Can't read Sequence from file "
401: + file);
402: }
403:
404:
409: public static int[] getMidiFileTypes()
410: {
411:
412: boolean supported[] = new boolean[3];
413:
414: int count = 0;
415: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
416: while (writers.hasNext())
417: {
418: MidiFileWriter fw = (MidiFileWriter) writers.next();
419: int types[] = fw.getMidiFileTypes();
420: for (int i = types.length; i > 0;)
421: {
422: int type = types[--i];
423: if (supported[type] == false)
424: {
425: count++;
426: supported[type] = true;
427: }
428: }
429: }
430: int result[] = new int[count];
431: for (int i = supported.length; i > 0;)
432: {
433: if (supported[--i])
434: result[--count] = i;
435: }
436: return result;
437: }
438:
439:
445: public static boolean isFileTypeSupported(int fileType)
446: {
447: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
448: while (writers.hasNext())
449: {
450: MidiFileWriter fw = (MidiFileWriter) writers.next();
451:
452: if (fw.isFileTypeSupported(fileType))
453: return true;
454: }
455: return false;
456: }
457:
458:
465: public static int[] getMidiFileTypes(Sequence sequence)
466: {
467:
468: boolean supported[] = new boolean[3];
469:
470: int count = 0;
471: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
472: while (writers.hasNext())
473: {
474: MidiFileWriter fw = (MidiFileWriter) writers.next();
475: int types[] = fw.getMidiFileTypes(sequence);
476: for (int i = types.length; i > 0;)
477: {
478: int type = types[--i];
479: if (supported[type] == false)
480: {
481: count++;
482: supported[type] = true;
483: }
484: }
485: }
486: int result[] = new int[count];
487: for (int i = supported.length; i > 0;)
488: {
489: if (supported[--i])
490: result[--count] = i;
491: }
492: return result;
493: }
494:
495:
503: public static boolean isFileTypeSupported(int fileType, Sequence sequence)
504: {
505: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
506: while (writers.hasNext())
507: {
508: MidiFileWriter fw = (MidiFileWriter) writers.next();
509:
510: if (fw.isFileTypeSupported(fileType, sequence))
511: return true;
512: }
513: return false;
514: }
515:
516:
526: public static int write(Sequence in, int fileType, OutputStream out)
527: throws IOException
528: {
529: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
530: while (writers.hasNext())
531: {
532: MidiFileWriter fw = (MidiFileWriter) writers.next();
533:
534: if (fw.isFileTypeSupported(fileType, in))
535: return fw.write(in, fileType, out);
536: }
537: throw new IllegalArgumentException("File type "
538: + fileType + " is not supported");
539: }
540:
541:
551: public static int write(Sequence in, int fileType, File out)
552: throws IOException
553: {
554: Iterator writers = ServiceFactory.lookupProviders(MidiFileWriter.class);
555: while (writers.hasNext())
556: {
557: MidiFileWriter fw = (MidiFileWriter) writers.next();
558:
559: if (fw.isFileTypeSupported(fileType, in))
560: return fw.write(in, fileType, out);
561: }
562: throw new IllegalArgumentException("File type "
563: + fileType + " is not supported");
564: }
565: }