1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
63: public class AudioSystem
64: {
65:
69: public static final int NOT_SPECIFIED = -1;
70:
71:
72: private AudioSystem()
73: {
74: }
75:
76:
84: public static AudioFileFormat getAudioFileFormat(File f)
85: throws UnsupportedAudioFileException, IOException
86: {
87: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
88: while (i.hasNext())
89: {
90: AudioFileReader reader = (AudioFileReader) i.next();
91: try
92: {
93: return reader.getAudioFileFormat(f);
94: }
95: catch (UnsupportedAudioFileException _)
96: {
97:
98: }
99: }
100: throw new UnsupportedAudioFileException("file type not recognized");
101: }
102:
103:
111: public static AudioFileFormat getAudioFileFormat(InputStream is)
112: throws UnsupportedAudioFileException, IOException
113: {
114: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
115: while (i.hasNext())
116: {
117: AudioFileReader reader = (AudioFileReader) i.next();
118: try
119: {
120: return reader.getAudioFileFormat(is);
121: }
122: catch (UnsupportedAudioFileException _)
123: {
124:
125: }
126: }
127: throw new UnsupportedAudioFileException("input stream type not recognized");
128: }
129:
130:
138: public static AudioFileFormat getAudioFileFormat(URL url)
139: throws UnsupportedAudioFileException, IOException
140: {
141: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
142: while (i.hasNext())
143: {
144: AudioFileReader reader = (AudioFileReader) i.next();
145: try
146: {
147: return reader.getAudioFileFormat(url);
148: }
149: catch (UnsupportedAudioFileException _)
150: {
151:
152: }
153: }
154: throw new UnsupportedAudioFileException("URL type not recognized");
155: }
156:
157:
161: public static AudioFileFormat.Type[] getAudioFileTypes()
162: {
163: HashSet result = new HashSet();
164: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
165: while (i.hasNext())
166: {
167: AudioFileWriter writer = (AudioFileWriter) i.next();
168: AudioFileFormat.Type[] types = writer.getAudioFileTypes();
169: for (int j = 0; j < types.length; ++j)
170: result.add(types[j]);
171: }
172: return (AudioFileFormat.Type[]) result.toArray(new AudioFileFormat.Type[result.size()]);
173: }
174:
175:
181: public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream ais)
182: {
183: HashSet result = new HashSet();
184: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
185: while (i.hasNext())
186: {
187: AudioFileWriter writer = (AudioFileWriter) i.next();
188: AudioFileFormat.Type[] types = writer.getAudioFileTypes(ais);
189: for (int j = 0; j < types.length; ++j)
190: result.add(types[j]);
191: }
192: return (AudioFileFormat.Type[]) result.toArray(new AudioFileFormat.Type[result.size()]);
193: }
194:
195:
204: public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targ,
205: AudioInputStream ais)
206: {
207: HashSet result = new HashSet();
208: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
209: while (i.hasNext())
210: {
211: FormatConversionProvider prov = (FormatConversionProvider) i.next();
212: if (! prov.isConversionSupported(targ, ais.getFormat()))
213: continue;
214: return prov.getAudioInputStream(targ, ais);
215: }
216: throw new IllegalArgumentException("encoding not supported for stream");
217: }
218:
219:
228: public static AudioInputStream getAudioInputStream(AudioFormat targ,
229: AudioInputStream ais)
230: {
231: HashSet result = new HashSet();
232: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
233: while (i.hasNext())
234: {
235: FormatConversionProvider prov = (FormatConversionProvider) i.next();
236: if (! prov.isConversionSupported(targ, ais.getFormat()))
237: continue;
238: return prov.getAudioInputStream(targ, ais);
239: }
240: throw new IllegalArgumentException("format not supported for stream");
241: }
242:
243:
251: public static AudioInputStream getAudioInputStream(File f)
252: throws UnsupportedAudioFileException, IOException
253: {
254: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
255: while (i.hasNext())
256: {
257: AudioFileReader reader = (AudioFileReader) i.next();
258: try
259: {
260: return reader.getAudioInputStream(f);
261: }
262: catch (UnsupportedAudioFileException _)
263: {
264:
265: }
266: }
267: throw new UnsupportedAudioFileException("file type not recognized");
268: }
269:
270:
278: public static AudioInputStream getAudioInputStream(InputStream is)
279: throws UnsupportedAudioFileException, IOException
280: {
281: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
282: while (i.hasNext())
283: {
284: AudioFileReader reader = (AudioFileReader) i.next();
285: try
286: {
287: return reader.getAudioInputStream(is);
288: }
289: catch (UnsupportedAudioFileException _)
290: {
291:
292: }
293: }
294: throw new UnsupportedAudioFileException("input stream type not recognized");
295: }
296:
297:
305: public static AudioInputStream getAudioInputStream(URL url)
306: throws UnsupportedAudioFileException, IOException
307: {
308: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
309: while (i.hasNext())
310: {
311: AudioFileReader reader = (AudioFileReader) i.next();
312: try
313: {
314: return reader.getAudioInputStream(url);
315: }
316: catch (UnsupportedAudioFileException _)
317: {
318:
319: }
320: }
321: throw new UnsupportedAudioFileException("URL type not recognized");
322: }
323:
324:
331: public static Clip getClip()
332: throws LineUnavailableException
333: {
334: Mixer.Info[] infos = getMixerInfo();
335: for (int i = 0; i < infos.length; ++i)
336: {
337: Mixer mix = getMixer(infos[i]);
338: Line[] lines = mix.getSourceLines();
339: for (int j = 0; j < lines.length; ++j)
340: {
341: if (lines[j] instanceof Clip)
342: return (Clip) lines[j];
343: }
344: }
345: throw new LineUnavailableException("no Clip available");
346: }
347:
348:
357: public static Clip getClip(Mixer.Info info)
358: throws LineUnavailableException
359: {
360: Mixer mix = getMixer(info);
361: Line[] lines = mix.getSourceLines();
362: for (int j = 0; j < lines.length; ++j)
363: {
364: if (lines[j] instanceof Clip)
365: return (Clip) lines[j];
366: }
367: throw new LineUnavailableException("no Clip available");
368: }
369:
370:
377: public static Line getLine(Line.Info info) throws LineUnavailableException
378: {
379: Mixer.Info[] infos = getMixerInfo();
380: for (int i = 0; i < infos.length; ++i)
381: {
382: Mixer mix = getMixer(infos[i]);
383: try
384: {
385: return mix.getLine(info);
386: }
387: catch (LineUnavailableException _)
388: {
389:
390: }
391: }
392: throw new LineUnavailableException("no Clip available");
393: }
394:
395:
402: public static Mixer getMixer(Mixer.Info info)
403: {
404: Iterator i = ServiceFactory.lookupProviders(MixerProvider.class);
405: while (i.hasNext())
406: {
407: MixerProvider prov = (MixerProvider) i.next();
408: if (prov.isMixerSupported(info))
409: return prov.getMixer(info);
410: }
411: throw new IllegalArgumentException("mixer not found");
412: }
413:
414:
417: public static Mixer.Info[] getMixerInfo()
418: {
419: HashSet result = new HashSet();
420: Iterator i = ServiceFactory.lookupProviders(MixerProvider.class);
421: while (i.hasNext())
422: {
423: MixerProvider prov = (MixerProvider) i.next();
424: Mixer.Info[] is = prov.getMixerInfo();
425: for (int j = 0; j < is.length; ++j)
426: result.add(is[j]);
427: }
428: return (Mixer.Info[]) result.toArray(new Mixer.Info[result.size()]);
429: }
430:
431:
438: public static SourceDataLine getSourceDataLine(AudioFormat fmt)
439: throws LineUnavailableException
440: {
441: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
442: Mixer.Info[] mixers = getMixerInfo();
443: for (int i = 0; i < mixers.length; ++i)
444: {
445: Mixer mix = getMixer(mixers[i]);
446: if (mix.isLineSupported(info))
447: return (SourceDataLine) mix.getLine(info);
448: }
449: throw new LineUnavailableException("source data line not found");
450: }
451:
452:
459: public static SourceDataLine getSourceDataLine(AudioFormat fmt,
460: Mixer.Info mixer)
461: throws LineUnavailableException
462: {
463: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
464: Mixer mix = getMixer(mixer);
465: if (mix.isLineSupported(info))
466: return (SourceDataLine) mix.getLine(info);
467: throw new LineUnavailableException("source data line not found");
468: }
469:
470:
475: public static Line.Info[] getSourceLineInfo(Line.Info info)
476: {
477: HashSet result = new HashSet();
478: Mixer.Info[] infos = getMixerInfo();
479: for (int i = 0; i < infos.length; ++i)
480: {
481: Mixer mix = getMixer(infos[i]);
482: Line.Info[] srcs = mix.getSourceLineInfo(info);
483: for (int j = 0; j < srcs.length; ++j)
484: result.add(srcs[j]);
485: }
486: return (Line.Info[]) result.toArray(new Line.Info[result.size()]);
487: }
488:
489:
495: public static TargetDataLine getTargetDataLine(AudioFormat fmt)
496: throws LineUnavailableException
497: {
498: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
499: Mixer.Info[] mixers = getMixerInfo();
500: for (int i = 0; i < mixers.length; ++i)
501: {
502: Mixer mix = getMixer(mixers[i]);
503: if (mix.isLineSupported(info))
504: return (TargetDataLine) mix.getLine(info);
505: }
506: throw new LineUnavailableException("target data line not found");
507: }
508:
509:
519: public static TargetDataLine getTargetDataLine(AudioFormat fmt,
520: Mixer.Info mixer)
521: throws LineUnavailableException
522: {
523: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
524: Mixer mix = getMixer(mixer);
525: if (mix.isLineSupported(info))
526: return (TargetDataLine) mix.getLine(info);
527: throw new LineUnavailableException("target data line not found");
528: }
529:
530:
535: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding source)
536: {
537: HashSet result = new HashSet();
538: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
539: while (i.hasNext())
540: {
541: FormatConversionProvider prov = (FormatConversionProvider) i.next();
542: if (! prov.isSourceEncodingSupported(source))
543: continue;
544: AudioFormat.Encoding[] es = prov.getTargetEncodings();
545: for (int j = 0; j < es.length; ++j)
546: result.add(es[j]);
547: }
548: return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]);
549: }
550:
551:
556: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat source)
557: {
558: HashSet result = new HashSet();
559: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
560: while (i.hasNext())
561: {
562: FormatConversionProvider prov = (FormatConversionProvider) i.next();
563: AudioFormat.Encoding[] es = prov.getTargetEncodings(source);
564: for (int j = 0; j < es.length; ++j)
565: result.add(es[j]);
566: }
567: return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]);
568: }
569:
570:
576: public static AudioFormat[] getTargetFormats(AudioFormat.Encoding encoding,
577: AudioFormat sourceFmt)
578: {
579: HashSet result = new HashSet();
580: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
581: while (i.hasNext())
582: {
583: FormatConversionProvider prov = (FormatConversionProvider) i.next();
584: AudioFormat[] es = prov.getTargetFormats(encoding, sourceFmt);
585: for (int j = 0; j < es.length; ++j)
586: result.add(es[j]);
587: }
588: return (AudioFormat[]) result.toArray(new AudioFormat[result.size()]);
589: }
590:
591:
596: public static Line.Info[] getTargetLineInfo(Line.Info info)
597: {
598: HashSet result = new HashSet();
599: Mixer.Info[] infos = getMixerInfo();
600: for (int i = 0; i < infos.length; ++i)
601: {
602: Mixer mix = getMixer(infos[i]);
603: Line.Info[] targs = mix.getTargetLineInfo(info);
604: for (int j = 0; j < targs.length; ++j)
605: result.add(targs[j]);
606: }
607: return (Line.Info[]) result.toArray(new Line.Info[result.size()]);
608: }
609:
610:
616: public static boolean isConversionSupported(AudioFormat.Encoding targ,
617: AudioFormat source)
618: {
619: Iterator i
620: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
621: while (i.hasNext())
622: {
623: FormatConversionProvider prov = (FormatConversionProvider) i.next();
624: if (prov.isConversionSupported(targ, source))
625: return true;
626: }
627: return false;
628: }
629:
630:
636: public static boolean isConversionSupported(AudioFormat targ,
637: AudioFormat source)
638: {
639: Iterator i
640: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
641: while (i.hasNext())
642: {
643: FormatConversionProvider prov = (FormatConversionProvider) i.next();
644: if (prov.isConversionSupported(targ, source))
645: return true;
646: }
647: return false;
648: }
649:
650: private static boolean isFileTypeSupported(AudioFileFormat.Type[] types,
651: AudioFileFormat.Type type)
652: {
653: for (int i = 0; i < types.length; ++i)
654: {
655: if (types[i].equals(type))
656: return true;
657: }
658: return false;
659: }
660:
661:
666: public static boolean isFileTypeSupported(AudioFileFormat.Type type)
667: {
668: return isFileTypeSupported(getAudioFileTypes(), type);
669: }
670:
671:
678: public static boolean isFileTypeSupported(AudioFileFormat.Type type,
679: AudioInputStream ais)
680: {
681: return isFileTypeSupported(getAudioFileTypes(ais), type);
682: }
683:
684:
689: public static boolean isLineSupported(Line.Info info)
690: {
691: Mixer.Info[] infos = getMixerInfo();
692: for (int i = 0; i < infos.length; ++i)
693: {
694: if (getMixer(infos[i]).isLineSupported(info))
695: return true;
696: }
697: return false;
698: }
699:
700:
711: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
712: File out)
713: throws IOException
714: {
715: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
716: while (i.hasNext())
717: {
718: AudioFileWriter w = (AudioFileWriter) i.next();
719: if (w.isFileTypeSupported(type, ais))
720: return w.write(ais, type, out);
721: }
722: throw new IllegalArgumentException("file type not supported by system");
723: }
724:
725:
736: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
737: OutputStream os)
738: throws IOException
739: {
740: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
741: while (i.hasNext())
742: {
743: AudioFileWriter w = (AudioFileWriter) i.next();
744: if (w.isFileTypeSupported(type, ais))
745: return w.write(ais, type, os);
746: }
747: throw new IllegalArgumentException("file type not supported by system");
748: }
749: }