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<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
88: while (i.hasNext())
89: {
90: AudioFileReader reader = 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<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
115: while (i.hasNext())
116: {
117: AudioFileReader reader = 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<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
142: while (i.hasNext())
143: {
144: AudioFileReader reader = 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<AudioFileWriter> i = ServiceFactory.lookupProviders(AudioFileWriter.class);
166: while (i.hasNext())
167: {
168: AudioFileWriter writer = 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<AudioFileWriter> i = ServiceFactory.lookupProviders(AudioFileWriter.class);
187: while (i.hasNext())
188: {
189: AudioFileWriter writer = 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<FormatConversionProvider> i =
210: ServiceFactory.lookupProviders(FormatConversionProvider.class);
211: while (i.hasNext())
212: {
213: FormatConversionProvider prov = i.next();
214: if (! prov.isConversionSupported(targ, ais.getFormat()))
215: continue;
216: return prov.getAudioInputStream(targ, ais);
217: }
218: throw new IllegalArgumentException("encoding not supported for stream");
219: }
220:
221:
230: public static AudioInputStream getAudioInputStream(AudioFormat targ,
231: AudioInputStream ais)
232: {
233: Iterator<FormatConversionProvider> i =
234: ServiceFactory.lookupProviders(FormatConversionProvider.class);
235: while (i.hasNext())
236: {
237: FormatConversionProvider prov = i.next();
238: if (! prov.isConversionSupported(targ, ais.getFormat()))
239: continue;
240: return prov.getAudioInputStream(targ, ais);
241: }
242: throw new IllegalArgumentException("format not supported for stream");
243: }
244:
245:
253: public static AudioInputStream getAudioInputStream(File f)
254: throws UnsupportedAudioFileException, IOException
255: {
256: Iterator<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
257: while (i.hasNext())
258: {
259: AudioFileReader reader = i.next();
260: try
261: {
262: return reader.getAudioInputStream(f);
263: }
264: catch (UnsupportedAudioFileException _)
265: {
266:
267: }
268: }
269: throw new UnsupportedAudioFileException("file type not recognized");
270: }
271:
272:
280: public static AudioInputStream getAudioInputStream(InputStream is)
281: throws UnsupportedAudioFileException, IOException
282: {
283: Iterator<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
284: while (i.hasNext())
285: {
286: AudioFileReader reader = i.next();
287: try
288: {
289: return reader.getAudioInputStream(is);
290: }
291: catch (UnsupportedAudioFileException _)
292: {
293:
294: }
295: }
296: throw new UnsupportedAudioFileException("input stream type not recognized");
297: }
298:
299:
307: public static AudioInputStream getAudioInputStream(URL url)
308: throws UnsupportedAudioFileException, IOException
309: {
310: Iterator<AudioFileReader> i = ServiceFactory.lookupProviders(AudioFileReader.class);
311: while (i.hasNext())
312: {
313: AudioFileReader reader = i.next();
314: try
315: {
316: return reader.getAudioInputStream(url);
317: }
318: catch (UnsupportedAudioFileException _)
319: {
320:
321: }
322: }
323: throw new UnsupportedAudioFileException("URL type not recognized");
324: }
325:
326:
333: public static Clip getClip()
334: throws LineUnavailableException
335: {
336: Mixer.Info[] infos = getMixerInfo();
337: for (int i = 0; i < infos.length; ++i)
338: {
339: Mixer mix = getMixer(infos[i]);
340: Line[] lines = mix.getSourceLines();
341: for (int j = 0; j < lines.length; ++j)
342: {
343: if (lines[j] instanceof Clip)
344: return (Clip) lines[j];
345: }
346: }
347: throw new LineUnavailableException("no Clip available");
348: }
349:
350:
359: public static Clip getClip(Mixer.Info info)
360: throws LineUnavailableException
361: {
362: Mixer mix = getMixer(info);
363: Line[] lines = mix.getSourceLines();
364: for (int j = 0; j < lines.length; ++j)
365: {
366: if (lines[j] instanceof Clip)
367: return (Clip) lines[j];
368: }
369: throw new LineUnavailableException("no Clip available");
370: }
371:
372:
379: public static Line getLine(Line.Info info) throws LineUnavailableException
380: {
381: Mixer.Info[] infos = getMixerInfo();
382: for (int i = 0; i < infos.length; ++i)
383: {
384: Mixer mix = getMixer(infos[i]);
385: try
386: {
387: return mix.getLine(info);
388: }
389: catch (LineUnavailableException _)
390: {
391:
392: }
393: }
394: throw new LineUnavailableException("no Clip available");
395: }
396:
397:
404: public static Mixer getMixer(Mixer.Info info)
405: {
406: Iterator<MixerProvider> i = ServiceFactory.lookupProviders(MixerProvider.class);
407: while (i.hasNext())
408: {
409: MixerProvider prov = i.next();
410: if (prov.isMixerSupported(info))
411: return prov.getMixer(info);
412: }
413: throw new IllegalArgumentException("mixer not found");
414: }
415:
416:
419: public static Mixer.Info[] getMixerInfo()
420: {
421: HashSet<Mixer.Info> result = new HashSet<Mixer.Info>();
422: Iterator<MixerProvider> i = ServiceFactory.lookupProviders(MixerProvider.class);
423: while (i.hasNext())
424: {
425: MixerProvider prov = i.next();
426: Mixer.Info[] is = prov.getMixerInfo();
427: for (int j = 0; j < is.length; ++j)
428: result.add(is[j]);
429: }
430: return result.toArray(new Mixer.Info[result.size()]);
431: }
432:
433:
440: public static SourceDataLine getSourceDataLine(AudioFormat fmt)
441: throws LineUnavailableException
442: {
443: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
444: Mixer.Info[] mixers = getMixerInfo();
445: for (int i = 0; i < mixers.length; ++i)
446: {
447: Mixer mix = getMixer(mixers[i]);
448: if (mix.isLineSupported(info))
449: return (SourceDataLine) mix.getLine(info);
450: }
451: throw new LineUnavailableException("source data line not found");
452: }
453:
454:
461: public static SourceDataLine getSourceDataLine(AudioFormat fmt,
462: Mixer.Info mixer)
463: throws LineUnavailableException
464: {
465: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
466: Mixer mix = getMixer(mixer);
467: if (mix.isLineSupported(info))
468: return (SourceDataLine) mix.getLine(info);
469: throw new LineUnavailableException("source data line not found");
470: }
471:
472:
477: public static Line.Info[] getSourceLineInfo(Line.Info info)
478: {
479: HashSet<Line.Info> result = new HashSet<Line.Info>();
480: Mixer.Info[] infos = getMixerInfo();
481: for (int i = 0; i < infos.length; ++i)
482: {
483: Mixer mix = getMixer(infos[i]);
484: Line.Info[] srcs = mix.getSourceLineInfo(info);
485: for (int j = 0; j < srcs.length; ++j)
486: result.add(srcs[j]);
487: }
488: return result.toArray(new Line.Info[result.size()]);
489: }
490:
491:
497: public static TargetDataLine getTargetDataLine(AudioFormat fmt)
498: throws LineUnavailableException
499: {
500: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
501: Mixer.Info[] mixers = getMixerInfo();
502: for (int i = 0; i < mixers.length; ++i)
503: {
504: Mixer mix = getMixer(mixers[i]);
505: if (mix.isLineSupported(info))
506: return (TargetDataLine) mix.getLine(info);
507: }
508: throw new LineUnavailableException("target data line not found");
509: }
510:
511:
521: public static TargetDataLine getTargetDataLine(AudioFormat fmt,
522: Mixer.Info mixer)
523: throws LineUnavailableException
524: {
525: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
526: Mixer mix = getMixer(mixer);
527: if (mix.isLineSupported(info))
528: return (TargetDataLine) mix.getLine(info);
529: throw new LineUnavailableException("target data line not found");
530: }
531:
532:
537: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding source)
538: {
539: HashSet<AudioFormat.Encoding> result
540: = new HashSet<AudioFormat.Encoding>();
541: Iterator<FormatConversionProvider> i =
542: ServiceFactory.lookupProviders(FormatConversionProvider.class);
543: while (i.hasNext())
544: {
545: FormatConversionProvider prov = i.next();
546: if (! prov.isSourceEncodingSupported(source))
547: continue;
548: AudioFormat.Encoding[] es = prov.getTargetEncodings();
549: for (int j = 0; j < es.length; ++j)
550: result.add(es[j]);
551: }
552: return result.toArray(new AudioFormat.Encoding[result.size()]);
553: }
554:
555:
560: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat source)
561: {
562: HashSet<AudioFormat.Encoding> result
563: = new HashSet<AudioFormat.Encoding>();
564: Iterator<FormatConversionProvider> i =
565: ServiceFactory.lookupProviders(FormatConversionProvider.class);
566: while (i.hasNext())
567: {
568: FormatConversionProvider prov = i.next();
569: AudioFormat.Encoding[] es = prov.getTargetEncodings(source);
570: for (int j = 0; j < es.length; ++j)
571: result.add(es[j]);
572: }
573: return result.toArray(new AudioFormat.Encoding[result.size()]);
574: }
575:
576:
582: public static AudioFormat[] getTargetFormats(AudioFormat.Encoding encoding,
583: AudioFormat sourceFmt)
584: {
585: HashSet<AudioFormat> result = new HashSet<AudioFormat>();
586: Iterator<FormatConversionProvider> i =
587: ServiceFactory.lookupProviders(FormatConversionProvider.class);
588: while (i.hasNext())
589: {
590: FormatConversionProvider prov = i.next();
591: AudioFormat[] es = prov.getTargetFormats(encoding, sourceFmt);
592: for (int j = 0; j < es.length; ++j)
593: result.add(es[j]);
594: }
595: return result.toArray(new AudioFormat[result.size()]);
596: }
597:
598:
603: public static Line.Info[] getTargetLineInfo(Line.Info info)
604: {
605: HashSet<Line.Info> result = new HashSet<Line.Info>();
606: Mixer.Info[] infos = getMixerInfo();
607: for (int i = 0; i < infos.length; ++i)
608: {
609: Mixer mix = getMixer(infos[i]);
610: Line.Info[] targs = mix.getTargetLineInfo(info);
611: for (int j = 0; j < targs.length; ++j)
612: result.add(targs[j]);
613: }
614: return result.toArray(new Line.Info[result.size()]);
615: }
616:
617:
623: public static boolean isConversionSupported(AudioFormat.Encoding targ,
624: AudioFormat source)
625: {
626: Iterator<FormatConversionProvider> i
627: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
628: while (i.hasNext())
629: {
630: FormatConversionProvider prov = i.next();
631: if (prov.isConversionSupported(targ, source))
632: return true;
633: }
634: return false;
635: }
636:
637:
643: public static boolean isConversionSupported(AudioFormat targ,
644: AudioFormat source)
645: {
646: Iterator<FormatConversionProvider> i
647: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
648: while (i.hasNext())
649: {
650: FormatConversionProvider prov = i.next();
651: if (prov.isConversionSupported(targ, source))
652: return true;
653: }
654: return false;
655: }
656:
657: private static boolean isFileTypeSupported(AudioFileFormat.Type[] types,
658: AudioFileFormat.Type type)
659: {
660: for (int i = 0; i < types.length; ++i)
661: {
662: if (types[i].equals(type))
663: return true;
664: }
665: return false;
666: }
667:
668:
673: public static boolean isFileTypeSupported(AudioFileFormat.Type type)
674: {
675: return isFileTypeSupported(getAudioFileTypes(), type);
676: }
677:
678:
685: public static boolean isFileTypeSupported(AudioFileFormat.Type type,
686: AudioInputStream ais)
687: {
688: return isFileTypeSupported(getAudioFileTypes(ais), type);
689: }
690:
691:
696: public static boolean isLineSupported(Line.Info info)
697: {
698: Mixer.Info[] infos = getMixerInfo();
699: for (int i = 0; i < infos.length; ++i)
700: {
701: if (getMixer(infos[i]).isLineSupported(info))
702: return true;
703: }
704: return false;
705: }
706:
707:
718: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
719: File out)
720: throws IOException
721: {
722: Iterator<AudioFileWriter> i = ServiceFactory.lookupProviders(AudioFileWriter.class);
723: while (i.hasNext())
724: {
725: AudioFileWriter w = i.next();
726: if (w.isFileTypeSupported(type, ais))
727: return w.write(ais, type, out);
728: }
729: throw new IllegalArgumentException("file type not supported by system");
730: }
731:
732:
743: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
744: OutputStream os)
745: throws IOException
746: {
747: Iterator<AudioFileWriter> i = ServiceFactory.lookupProviders(AudioFileWriter.class);
748: while (i.hasNext())
749: {
750: AudioFileWriter w = i.next();
751: if (w.isFileTypeSupported(type, ais))
752: return w.write(ais, type, os);
753: }
754: throw new IllegalArgumentException("file type not supported by system");
755: }
756: }