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:
79: public static AudioFileFormat getAudioFileFormat(File f)
80: throws UnsupportedAudioFileException, IOException
81: {
82: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
83: while (i.hasNext())
84: {
85: AudioFileReader reader = (AudioFileReader) i.next();
86: try
87: {
88: return reader.getAudioFileFormat(f);
89: }
90: catch (UnsupportedAudioFileException _)
91: {
92:
93: }
94: }
95: throw new UnsupportedAudioFileException("file type not recognized");
96: }
97:
98:
106: public static AudioFileFormat getAudioFileFormat(InputStream is)
107: throws UnsupportedAudioFileException, IOException
108: {
109: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
110: while (i.hasNext())
111: {
112: AudioFileReader reader = (AudioFileReader) i.next();
113: try
114: {
115: return reader.getAudioFileFormat(is);
116: }
117: catch (UnsupportedAudioFileException _)
118: {
119:
120: }
121: }
122: throw new UnsupportedAudioFileException("input stream type not recognized");
123: }
124:
125:
133: public static AudioFileFormat getAudioFileFormat(URL url)
134: throws UnsupportedAudioFileException, IOException
135: {
136: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
137: while (i.hasNext())
138: {
139: AudioFileReader reader = (AudioFileReader) i.next();
140: try
141: {
142: return reader.getAudioFileFormat(url);
143: }
144: catch (UnsupportedAudioFileException _)
145: {
146:
147: }
148: }
149: throw new UnsupportedAudioFileException("URL type not recognized");
150: }
151:
152:
156: public static AudioFileFormat.Type[] getAudioFileTypes()
157: {
158: HashSet result = new HashSet();
159: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
160: while (i.hasNext())
161: {
162: AudioFileWriter writer = (AudioFileWriter) i.next();
163: AudioFileFormat.Type[] types = writer.getAudioFileTypes();
164: for (int j = 0; j < types.length; ++j)
165: result.add(types[j]);
166: }
167: return (AudioFileFormat.Type[]) result.toArray(new AudioFileFormat.Type[result.size()]);
168: }
169:
170:
176: public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream ais)
177: {
178: HashSet result = new HashSet();
179: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
180: while (i.hasNext())
181: {
182: AudioFileWriter writer = (AudioFileWriter) i.next();
183: AudioFileFormat.Type[] types = writer.getAudioFileTypes(ais);
184: for (int j = 0; j < types.length; ++j)
185: result.add(types[j]);
186: }
187: return (AudioFileFormat.Type[]) result.toArray(new AudioFileFormat.Type[result.size()]);
188: }
189:
190:
199: public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targ,
200: AudioInputStream ais)
201: {
202: HashSet result = new HashSet();
203: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
204: while (i.hasNext())
205: {
206: FormatConversionProvider prov = (FormatConversionProvider) i.next();
207: if (! prov.isConversionSupported(targ, ais.getFormat()))
208: continue;
209: return prov.getAudioInputStream(targ, ais);
210: }
211: throw new IllegalArgumentException("encoding not supported for stream");
212: }
213:
214:
223: public static AudioInputStream getAudioInputStream(AudioFormat targ,
224: AudioInputStream ais)
225: {
226: HashSet result = new HashSet();
227: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
228: while (i.hasNext())
229: {
230: FormatConversionProvider prov = (FormatConversionProvider) i.next();
231: if (! prov.isConversionSupported(targ, ais.getFormat()))
232: continue;
233: return prov.getAudioInputStream(targ, ais);
234: }
235: throw new IllegalArgumentException("format not supported for stream");
236: }
237:
238:
246: public static AudioInputStream getAudioInputStream(File f)
247: throws UnsupportedAudioFileException, IOException
248: {
249: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
250: while (i.hasNext())
251: {
252: AudioFileReader reader = (AudioFileReader) i.next();
253: try
254: {
255: return reader.getAudioInputStream(f);
256: }
257: catch (UnsupportedAudioFileException _)
258: {
259:
260: }
261: }
262: throw new UnsupportedAudioFileException("file type not recognized");
263: }
264:
265:
273: public static AudioInputStream getAudioInputStream(InputStream is)
274: throws UnsupportedAudioFileException, IOException
275: {
276: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
277: while (i.hasNext())
278: {
279: AudioFileReader reader = (AudioFileReader) i.next();
280: try
281: {
282: return reader.getAudioInputStream(is);
283: }
284: catch (UnsupportedAudioFileException _)
285: {
286:
287: }
288: }
289: throw new UnsupportedAudioFileException("input stream type not recognized");
290: }
291:
292:
300: public static AudioInputStream getAudioInputStream(URL url)
301: throws UnsupportedAudioFileException, IOException
302: {
303: Iterator i = ServiceFactory.lookupProviders(AudioFileReader.class);
304: while (i.hasNext())
305: {
306: AudioFileReader reader = (AudioFileReader) i.next();
307: try
308: {
309: return reader.getAudioInputStream(url);
310: }
311: catch (UnsupportedAudioFileException _)
312: {
313:
314: }
315: }
316: throw new UnsupportedAudioFileException("URL type not recognized");
317: }
318:
319:
326: public static Clip getClip()
327: throws LineUnavailableException
328: {
329: Mixer.Info[] infos = getMixerInfo();
330: for (int i = 0; i < infos.length; ++i)
331: {
332: Mixer mix = getMixer(infos[i]);
333: Line[] lines = mix.getSourceLines();
334: for (int j = 0; j < lines.length; ++j)
335: {
336: if (lines[j] instanceof Clip)
337: return (Clip) lines[j];
338: }
339: }
340: throw new LineUnavailableException("no Clip available");
341: }
342:
343:
352: public static Clip getClip(Mixer.Info info)
353: throws LineUnavailableException
354: {
355: Mixer mix = getMixer(info);
356: Line[] lines = mix.getSourceLines();
357: for (int j = 0; j < lines.length; ++j)
358: {
359: if (lines[j] instanceof Clip)
360: return (Clip) lines[j];
361: }
362: throw new LineUnavailableException("no Clip available");
363: }
364:
365:
372: public static Line getLine(Line.Info info) throws LineUnavailableException
373: {
374: Mixer.Info[] infos = getMixerInfo();
375: for (int i = 0; i < infos.length; ++i)
376: {
377: Mixer mix = getMixer(infos[i]);
378: try
379: {
380: return mix.getLine(info);
381: }
382: catch (LineUnavailableException _)
383: {
384:
385: }
386: }
387: throw new LineUnavailableException("no Clip available");
388: }
389:
390:
397: public static Mixer getMixer(Mixer.Info info)
398: {
399: Iterator i = ServiceFactory.lookupProviders(MixerProvider.class);
400: while (i.hasNext())
401: {
402: MixerProvider prov = (MixerProvider) i.next();
403: if (prov.isMixerSupported(info))
404: return prov.getMixer(info);
405: }
406: throw new IllegalArgumentException("mixer not found");
407: }
408:
409:
412: public static Mixer.Info[] getMixerInfo()
413: {
414: HashSet result = new HashSet();
415: Iterator i = ServiceFactory.lookupProviders(MixerProvider.class);
416: while (i.hasNext())
417: {
418: MixerProvider prov = (MixerProvider) i.next();
419: Mixer.Info[] is = prov.getMixerInfo();
420: for (int j = 0; j < is.length; ++j)
421: result.add(is[j]);
422: }
423: return (Mixer.Info[]) result.toArray(new Mixer.Info[result.size()]);
424: }
425:
426:
433: public static SourceDataLine getSourceDataLine(AudioFormat fmt)
434: throws LineUnavailableException
435: {
436: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
437: Mixer.Info[] mixers = getMixerInfo();
438: for (int i = 0; i < mixers.length; ++i)
439: {
440: Mixer mix = getMixer(mixers[i]);
441: if (mix.isLineSupported(info))
442: return (SourceDataLine) mix.getLine(info);
443: }
444: throw new LineUnavailableException("source data line not found");
445: }
446:
447:
454: public static SourceDataLine getSourceDataLine(AudioFormat fmt,
455: Mixer.Info mixer)
456: throws LineUnavailableException
457: {
458: DataLine.Info info = new DataLine.Info(SourceDataLine.class, fmt);
459: Mixer mix = getMixer(mixer);
460: if (mix.isLineSupported(info))
461: return (SourceDataLine) mix.getLine(info);
462: throw new LineUnavailableException("source data line not found");
463: }
464:
465:
470: public static Line.Info[] getSourceLineInfo(Line.Info info)
471: {
472: HashSet result = new HashSet();
473: Mixer.Info[] infos = getMixerInfo();
474: for (int i = 0; i < infos.length; ++i)
475: {
476: Mixer mix = getMixer(infos[i]);
477: Line.Info[] srcs = mix.getSourceLineInfo(info);
478: for (int j = 0; j < srcs.length; ++j)
479: result.add(srcs[j]);
480: }
481: return (Line.Info[]) result.toArray(new Line.Info[result.size()]);
482: }
483:
484:
490: public static TargetDataLine getTargetDataLine(AudioFormat fmt)
491: throws LineUnavailableException
492: {
493: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
494: Mixer.Info[] mixers = getMixerInfo();
495: for (int i = 0; i < mixers.length; ++i)
496: {
497: Mixer mix = getMixer(mixers[i]);
498: if (mix.isLineSupported(info))
499: return (TargetDataLine) mix.getLine(info);
500: }
501: throw new LineUnavailableException("target data line not found");
502: }
503:
504:
514: public static TargetDataLine getTargetDataLine(AudioFormat fmt,
515: Mixer.Info mixer)
516: throws LineUnavailableException
517: {
518: DataLine.Info info = new DataLine.Info(TargetDataLine.class, fmt);
519: Mixer mix = getMixer(mixer);
520: if (mix.isLineSupported(info))
521: return (TargetDataLine) mix.getLine(info);
522: throw new LineUnavailableException("target data line not found");
523: }
524:
525:
530: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding source)
531: {
532: HashSet result = new HashSet();
533: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
534: while (i.hasNext())
535: {
536: FormatConversionProvider prov = (FormatConversionProvider) i.next();
537: if (! prov.isSourceEncodingSupported(source))
538: continue;
539: AudioFormat.Encoding[] es = prov.getTargetEncodings();
540: for (int j = 0; j < es.length; ++j)
541: result.add(es[j]);
542: }
543: return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]);
544: }
545:
546:
551: public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat source)
552: {
553: HashSet result = new HashSet();
554: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
555: while (i.hasNext())
556: {
557: FormatConversionProvider prov = (FormatConversionProvider) i.next();
558: AudioFormat.Encoding[] es = prov.getTargetEncodings(source);
559: for (int j = 0; j < es.length; ++j)
560: result.add(es[j]);
561: }
562: return (AudioFormat.Encoding[]) result.toArray(new AudioFormat.Encoding[result.size()]);
563: }
564:
565:
571: public static AudioFormat[] getTargetFormats(AudioFormat.Encoding encoding,
572: AudioFormat sourceFmt)
573: {
574: HashSet result = new HashSet();
575: Iterator i = ServiceFactory.lookupProviders(FormatConversionProvider.class);
576: while (i.hasNext())
577: {
578: FormatConversionProvider prov = (FormatConversionProvider) i.next();
579: AudioFormat[] es = prov.getTargetFormats(encoding, sourceFmt);
580: for (int j = 0; j < es.length; ++j)
581: result.add(es[j]);
582: }
583: return (AudioFormat[]) result.toArray(new AudioFormat[result.size()]);
584: }
585:
586:
591: public static Line.Info[] getTargetLineInfo(Line.Info info)
592: {
593: HashSet result = new HashSet();
594: Mixer.Info[] infos = getMixerInfo();
595: for (int i = 0; i < infos.length; ++i)
596: {
597: Mixer mix = getMixer(infos[i]);
598: Line.Info[] targs = mix.getTargetLineInfo(info);
599: for (int j = 0; j < targs.length; ++j)
600: result.add(targs[j]);
601: }
602: return (Line.Info[]) result.toArray(new Line.Info[result.size()]);
603: }
604:
605:
611: public static boolean isConversionSupported(AudioFormat.Encoding targ,
612: AudioFormat source)
613: {
614: Iterator i
615: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
616: while (i.hasNext())
617: {
618: FormatConversionProvider prov = (FormatConversionProvider) i.next();
619: if (prov.isConversionSupported(targ, source))
620: return true;
621: }
622: return false;
623: }
624:
625:
631: public static boolean isConversionSupported(AudioFormat targ,
632: AudioFormat source)
633: {
634: Iterator i
635: = ServiceFactory.lookupProviders(FormatConversionProvider.class);
636: while (i.hasNext())
637: {
638: FormatConversionProvider prov = (FormatConversionProvider) i.next();
639: if (prov.isConversionSupported(targ, source))
640: return true;
641: }
642: return false;
643: }
644:
645: private static boolean isFileTypeSupported(AudioFileFormat.Type[] types,
646: AudioFileFormat.Type type)
647: {
648: for (int i = 0; i < types.length; ++i)
649: {
650: if (types[i].equals(type))
651: return true;
652: }
653: return false;
654: }
655:
656:
661: public static boolean isFileTypeSupported(AudioFileFormat.Type type)
662: {
663: return isFileTypeSupported(getAudioFileTypes(), type);
664: }
665:
666:
673: public static boolean isFileTypeSupported(AudioFileFormat.Type type,
674: AudioInputStream ais)
675: {
676: return isFileTypeSupported(getAudioFileTypes(ais), type);
677: }
678:
679:
684: public static boolean isLineSupported(Line.Info info)
685: {
686: Mixer.Info[] infos = getMixerInfo();
687: for (int i = 0; i < infos.length; ++i)
688: {
689: if (getMixer(infos[i]).isLineSupported(info))
690: return true;
691: }
692: return false;
693: }
694:
695:
706: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
707: File out)
708: throws IOException
709: {
710: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
711: while (i.hasNext())
712: {
713: AudioFileWriter w = (AudioFileWriter) i.next();
714: if (w.isFileTypeSupported(type, ais))
715: return w.write(ais, type, out);
716: }
717: throw new IllegalArgumentException("file type not supported by system");
718: }
719:
720:
731: public static int write(AudioInputStream ais, AudioFileFormat.Type type,
732: OutputStream os)
733: throws IOException
734: {
735: Iterator i = ServiceFactory.lookupProviders(AudioFileWriter.class);
736: while (i.hasNext())
737: {
738: AudioFileWriter w = (AudioFileWriter) i.next();
739: if (w.isFileTypeSupported(type, ais))
740: return w.write(ais, type, os);
741: }
742: throw new IllegalArgumentException("file type not supported by system");
743: }
744: }