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<AudioFileFormat.Type> result
164: = new HashSet<AudioFileFormat.Type>();
165: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
166: while (i.hasNext())
167: {
168: AudioFileWriter writer = (AudioFileWriter) i.next();
169: AudioFileFormat.Type[] types = writer.getAudioFileTypes();
170: for (int j = 0; j < types.length; ++j)
171: result.add(types[j]);
172: }
173: return result.toArray(new AudioFileFormat.Type[result.size()]);
174: }
175:
176:
182: public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream ais)
183: {
184: HashSet<AudioFileFormat.Type> result
185: = new HashSet<AudioFileFormat.Type>();
186: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
187: while (i.hasNext())
188: {
189: AudioFileWriter writer = (AudioFileWriter) i.next();
190: AudioFileFormat.Type[] types = writer.getAudioFileTypes(ais);
191: for (int j = 0; j < types.length; ++j)
192: result.add(types[j]);
193: }
194: return result.toArray(new AudioFileFormat.Type[result.size()]);
195: }
196:
197:
206: public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targ,
207: AudioInputStream ais)
208: {
209: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
210: while (i.hasNext())
211: {
212: FormatConversionProvider prov = (FormatConversionProvider) i.next();
213: if (! prov.isConversionSupported(targ, ais.getFormat()))
214: continue;
215: return prov.getAudioInputStream(targ, ais);
216: }
217: throw new IllegalArgumentException("encoding not supported for stream");
218: }
219:
220:
229: public static AudioInputStream getAudioInputStream(AudioFormat targ,
230: AudioInputStream ais)
231: {
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<Mixer.Info> result = new HashSet<Mixer.Info>();
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 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<Line.Info> result = new HashSet<Line.Info>();
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 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<AudioFormat.Encoding> result
538: = new HashSet<AudioFormat.Encoding>();
539: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
540: while (i.hasNext())
541: {
542: FormatConversionProvider prov = (FormatConversionProvider) i.next();
543: if (! prov.isSourceEncodingSupported(source))
544: continue;
545: AudioFormat.Encoding[] es = prov.getTargetEncodings();
546: for (int j = 0; j < es.length; ++j)
547: result.add(es[j]);
548: }
549: return result.toArray(new AudioFormat.Encoding[result.size()]);
550: }
551:
552:
557: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat source)
558: {
559: HashSet<AudioFormat.Encoding> result
560: = new HashSet<AudioFormat.Encoding>();
561: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
562: while (i.hasNext())
563: {
564: FormatConversionProvider prov = (FormatConversionProvider) i.next();
565: AudioFormat.Encoding[] es = prov.getTargetEncodings(source);
566: for (int j = 0; j < es.length; ++j)
567: result.add(es[j]);
568: }
569: return result.toArray(new AudioFormat.Encoding[result.size()]);
570: }
571:
572:
578: public static AudioFormat[] getTargetFormats(AudioFormat.Encoding encoding,
579: AudioFormat sourceFmt)
580: {
581: HashSet<AudioFormat> result = new HashSet<AudioFormat>();
582: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
583: while (i.hasNext())
584: {
585: FormatConversionProvider prov = (FormatConversionProvider) i.next();
586: AudioFormat[] es = prov.getTargetFormats(encoding, sourceFmt);
587: for (int j = 0; j < es.length; ++j)
588: result.add(es[j]);
589: }
590: return result.toArray(new AudioFormat[result.size()]);
591: }
592:
593:
598: public static Line.Info[] getTargetLineInfo(Line.Info info)
599: {
600: HashSet<Line.Info> result = new HashSet<Line.Info>();
601: Mixer.Info[] infos = getMixerInfo();
602: for (int i = 0; i < infos.length; ++i)
603: {
604: Mixer mix = getMixer(infos[i]);
605: Line.Info[] targs = mix.getTargetLineInfo(info);
606: for (int j = 0; j < targs.length; ++j)
607: result.add(targs[j]);
608: }
609: return result.toArray(new Line.Info[result.size()]);
610: }
611:
612:
618: public static boolean isConversionSupported(AudioFormat.Encoding targ,
619: AudioFormat source)
620: {
621: Iterator i
622: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
623: while (i.hasNext())
624: {
625: FormatConversionProvider prov = (FormatConversionProvider) i.next();
626: if (prov.isConversionSupported(targ, source))
627: return true;
628: }
629: return false;
630: }
631:
632:
638: public static boolean isConversionSupported(AudioFormat targ,
639: AudioFormat source)
640: {
641: Iterator i
642: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
643: while (i.hasNext())
644: {
645: FormatConversionProvider prov = (FormatConversionProvider) i.next();
646: if (prov.isConversionSupported(targ, source))
647: return true;
648: }
649: return false;
650: }
651:
652: private static boolean isFileTypeSupported(AudioFileFormat.Type[] types,
653: AudioFileFormat.Type type)
654: {
655: for (int i = 0; i < types.length; ++i)
656: {
657: if (types[i].equals(type))
658: return true;
659: }
660: return false;
661: }
662:
663:
668: public static boolean isFileTypeSupported(AudioFileFormat.Type type)
669: {
670: return isFileTypeSupported(getAudioFileTypes(), type);
671: }
672:
673:
680: public static boolean isFileTypeSupported(AudioFileFormat.Type type,
681: AudioInputStream ais)
682: {
683: return isFileTypeSupported(getAudioFileTypes(ais), type);
684: }
685:
686:
691: public static boolean isLineSupported(Line.Info info)
692: {
693: Mixer.Info[] infos = getMixerInfo();
694: for (int i = 0; i < infos.length; ++i)
695: {
696: if (getMixer(infos[i]).isLineSupported(info))
697: return true;
698: }
699: return false;
700: }
701:
702:
713: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
714: File out)
715: throws IOException
716: {
717: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
718: while (i.hasNext())
719: {
720: AudioFileWriter w = (AudioFileWriter) i.next();
721: if (w.isFileTypeSupported(type, ais))
722: return w.write(ais, type, out);
723: }
724: throw new IllegalArgumentException("file type not supported by system");
725: }
726:
727:
738: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
739: OutputStream os)
740: throws IOException
741: {
742: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
743: while (i.hasNext())
744: {
745: AudioFileWriter w = (AudioFileWriter) i.next();
746: if (w.isFileTypeSupported(type, ais))
747: return w.write(ais, type, os);
748: }
749: throw new IllegalArgumentException("file type not supported by system");
750: }
751: }