API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.sound.sampled. AudioSystem View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614

/*
 * @(#)AudioSystem.java	1.83 06/04/07
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.sound.sampled;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.ArrayList;

import javax.sound.sampled.spi.AudioFileWriter;
import javax.sound.sampled.spi.AudioFileReader;
import javax.sound.sampled.spi.FormatConversionProvider;
import javax.sound.sampled.spi.MixerProvider;

import com.sun.media.sound.JDK13Services;

/* $fb TODO:
 * - consistent usage of (typed) collections
 */


/**
 * The <code>AudioSystem</code> class acts as the entry point to the
 * sampled-audio system resources. This class lets you query and
 * access the mixers that are installed on the system.
 * <code>AudioSystem</code> includes a number of
 * methods for converting audio data between different formats, and for
 * translating between audio files and streams. It also provides a method
 * for obtaining a <code>{@link Line}</code> directly from the
 * <code>AudioSystem</code> without dealing explicitly
 * with mixers.
 *
 * <p>Properties can be used to specify the default mixer
 * for specific line types.
 * Both system properties and a properties file are considered.
 * In the Sun reference implementation, the properties file is
 * &quot;lib/sound.properties&quot; in the JRE
 * directory. If a property exists both as a system property and in the
 * properties file, the system property takes precedence. If none is
 * specified, a suitable default is chosen among the available devices.
 * The syntax of the properties file is specified in
 * {@link java.util.Properties#load(InputStream) Properties.load}. The
 * following table lists the available property keys and which methods
 * consider them:
 *
 * <table border=0>
 *  <tr>
 *   <th>Property Key</th>
 *   <th>Interface</th>
 *   <th>Affected Method(s)</th>
 *  </tr>
 *  <tr>
 *   <td><code>javax.sound.sampled.Clip</code></td>
 *   <td>{@link Clip}</td>
 *   <td>{@link #getLine}, {@link #getClip}</td>
 *  </tr>
 *  <tr>
 *   <td><code>javax.sound.sampled.Port</code></td>
 *   <td>{@link Port}</td>
 *   <td>{@link #getLine}</td>
 *  </tr>
 *  <tr>
 *   <td><code>javax.sound.sampled.SourceDataLine</code></td>
 *   <td>{@link SourceDataLine}</td>
 *   <td>{@link #getLine}, {@link #getSourceDataLine}</td>
 *  </tr>
 *  <tr>
 *   <td><code>javax.sound.sampled.TargetDataLine</code></td>
 *   <td>{@link TargetDataLine}</td>
 *   <td>{@link #getLine}, {@link #getTargetDataLine}</td>
 *  </tr>
 * </table>
 *
 * The property value consists of the provider class name
 * and the mixer name, separated by the hash mark (&quot;#&quot;).
 * The provider class name is the fully-qualified
 * name of a concrete {@link javax.sound.sampled.spi.MixerProvider
 * mixer provider} class. The mixer name is matched against
 * the <code>String</code> returned by the <code>getName</code>
 * method of <code>Mixer.Info</code>.
 * Either the class name, or the mixer name may be omitted.
 * If only the class name is specified, the trailing hash mark
 * is optional.
 *
 * <p>If the provider class is specified, and it can be
 * successully retrieved from the installed providers, the list of
 * <code>Mixer.Info</code> objects is retrieved
 * from the provider. Otherwise, or when these mixers
 * do not provide a subsequent match, the list is retrieved
 * from {@link #getMixerInfo} to contain
 * all available <code>Mixer.Info</code> objects.
 *
 * <p>If a mixer name is specified, the resulting list of
 * <code>Mixer.Info</code> objects is searched:
 * the first one with a matching name, and whose
 * <code>Mixer</code> provides the
 * respective line interface, will be returned.
 * If no matching <code>Mixer.Info</code> object
 * is found, or the mixer name is not specified,
 * the first mixer from the resulting
 * list, which provides the respective line
 * interface, will be returned.
 *
 * For example, the property <code>javax.sound.sampled.Clip</code>
 * with a value
 * <code>&quot;com.sun.media.sound.MixerProvider#SunClip&quot;</code>
 * will have the following consequences when
 * <code>getLine</code> is called requesting a <code>Clip</code>
 * instance:
 * if the class <code>com.sun.media.sound.MixerProvider</code> exists
 * in the list of installed mixer providers,
 * the first <code>Clip</code> from the first mixer with name
 * <code>&quot;SunClip&quot;</code> will be returned. If it cannot
 * be found, the first <code>Clip</code> from the first mixer
 * of the specified provider will be returned, regardless of name.
 * If there is none, the first <code>Clip</code> from the first
 * <code>Mixer</code> with name
 * <code>&quot;SunClip&quot;</code> in the list of all mixers
 * (as returned by <code>getMixerInfo</code>) will be returned,
 * or, if not found, the first <code>Clip</code> of the first
 * <code>Mixer</code>that can be found in the list of all
 * mixers is returned.
 * If that fails, too, an <code>IllegalArgumentException</code>
 * is thrown.
 *
 * @author Kara Kytle
 * @author Florian Bomers
 * @author Matthias Pfisterer
 * @author Kevin P. Smith
 * @version 1.83, 06/04/07
 *
 * @see AudioFormat
 * @see AudioInputStream
 * @see Mixer
 * @see Line
 * @see Line.Info
 * @since 1.3
 */
public class AudioSystem {

    /**
     * An integer that stands for an unknown numeric value.
     * This value is appropriate only for signed quantities that do not
     * normally take negative values.  Examples include file sizes, frame
     * sizes, buffer sizes, and sample rates.
     * A number of Java Sound constructors accept
     * a value of <code>NOT_SPECIFIED</code> for such parameters.  Other
     * methods may also accept or return this value, as documented.
     */
    public static final int NOT_SPECIFIED = -1;

    /**
     * Private no-args constructor for ensuring against instantiation.
     */
    private AudioSystem() {
    }


    /**
     * Obtains an array of mixer info objects that represents
     * the set of audio mixers that are currently installed on the system.
     * @return an array of info objects for the currently installed mixers.  If no mixers
     * are available on the system, an array of length 0 is returned.
     * @see #getMixer
     */
    public static Mixer.Info[] getMixerInfo() {

	List infos = getMixerInfoList();
	Mixer.Info[] allInfos = (Mixer.Info[]) infos.toArray(new Mixer.Info[infos.size()]);
	return allInfos;
    }


    /**
     * Obtains the requested audio mixer.
     * @param info a <code>Mixer.Info</code> object representing the desired
     * mixer, or <code>null</code> for the system default mixer
     * @return the requested mixer
     * @throws SecurityException if the requested mixer
     * is unavailable because of security restrictions
     * @throws IllegalArgumentException if the info object does not represent
     * a mixer installed on the system
     * @see #getMixerInfo
     */
    public static Mixer getMixer(Mixer.Info info) {

	Mixer mixer = null;
	List providers = getMixerProviders();

	for(int i = 0; i < providers.size(); i++ ) {

	    try {
		return ((MixerProvider)providers.get(i)).getMixer(info);

	    } catch (IllegalArgumentException e) {
	    } catch (NullPointerException e) {
		// $$jb 08.20.99:  If the strings in the info object aren't
		// set, then Netscape (using jdk1.1.5) tends to throw
		// NPE's when doing some string manipulation.  This is
		// probably not the best fix, but is solves the problem
		// of the NPE in Netscape using local classes
		// $$jb 11.01.99: Replacing this patch.
	    }
	}

	//$$fb if looking for default mixer, and not found yet, add a round of looking
	if (info == null) {
	    for(int i = 0; i < providers.size(); i++ ) {
		try {
		    MixerProvider provider = (MixerProvider) providers.get(i);
		    Mixer.Info[] infos = provider.getMixerInfo();
		    // start from 0 to last device (do not reverse this order)
		    for (int ii = 0; ii < infos.length; ii++) {
			try {
			    return provider.getMixer(infos[ii]);
			} catch (IllegalArgumentException e) {
			    // this is not a good default device :)
			}
		    }
		} catch (IllegalArgumentException e) {
		} catch (NullPointerException e) {
		}
	    }
	}


	throw new IllegalArgumentException("Mixer not supported: "
					   + (info!=null?info.toString():"null"));
    }


    //$$fb 2002-11-26: fix for 4757930: DOC: AudioSystem.getTarget/SourceLineInfo() is ambiguous
    /**
     * Obtains information about all source lines of a particular type that are supported
     * by the installed mixers.
     * @param info a <code>Line.Info</code> object that specifies the kind of
     * lines about which information is requested
     * @return an array of <code>Line.Info</code> objects describing source lines matching
     * the type requested.  If no matching source lines are supported, an array of length 0
     * is returned.
     *
     * @see Mixer#getSourceLineInfo(Line.Info)
     */
    public static Line.Info[] getSourceLineInfo(Line.Info info) {

	Vector vector = new Vector();
	Line.Info[] currentInfoArray;

	Mixer mixer;
	Line.Info fullInfo = null;
	Mixer.Info[] infoArray = getMixerInfo();

	for (int i = 0; i < infoArray.length; i++) {

	    mixer = getMixer(infoArray[i]);

	    currentInfoArray = mixer.getSourceLineInfo(info);
	    for (int j = 0; j < currentInfoArray.length; j++) {
		vector.addElement(currentInfoArray[j]);
	    }
	}

	Line.Info[] returnedArray = new Line.Info[vector.size()];

	for (int i = 0; i < returnedArray.length; i++) {
	    returnedArray[i] = (Line.Info)vector.get(i);
	}

	return returnedArray;
    }


    /**
     * Obtains information about all target lines of a particular type that are supported
     * by the installed mixers.
     * @param info a <code>Line.Info</code> object that specifies the kind of
     * lines about which information is requested
     * @return an array of <code>Line.Info</code> objects describing target lines matching
     * the type requested.  If no matching target lines are supported, an array of length 0
     * is returned.
     *
     * @see Mixer#getTargetLineInfo(Line.Info)
     */
    public static Line.Info[] getTargetLineInfo(Line.Info info) {

	Vector vector = new Vector();
	Line.Info[] currentInfoArray;

	Mixer mixer;
	Line.Info fullInfo = null;
	Mixer.Info[] infoArray = getMixerInfo();

	for (int i = 0; i < infoArray.length; i++) {

	    mixer = getMixer(infoArray[i]);

	    currentInfoArray = mixer.getTargetLineInfo(info);
	    for (int j = 0; j < currentInfoArray.length; j++) {
		vector.addElement(currentInfoArray[j]);
	    }
	}

	Line.Info[] returnedArray = new Line.Info[vector.size()];

	for (int i = 0; i < returnedArray.length; i++) {
	    returnedArray[i] = (Line.Info)vector.get(i);
	}

	return returnedArray;
    }


    /**
     * Indicates whether the system supports any lines that match
     * the specified <code>Line.Info</code> object.  A line is supported if
     * any installed mixer supports it.
     * @param info a <code>Line.Info</code> object describing the line for which support is queried
     * @return <code>true</code> if at least one matching line is
     * supported, otherwise <code>false</code>
     *
     * @see Mixer#isLineSupported(Line.Info)
     */
    public static boolean isLineSupported(Line.Info info) {

	Mixer mixer;
	Mixer.Info[] infoArray = getMixerInfo();

	for (int i = 0; i < infoArray.length; i++) {

	    if( infoArray[i] != null ) {
		mixer = getMixer(infoArray[i]);
		if (mixer.isLineSupported(info)) {
		    return true;
		}
	    }
	}

	return false;
    }

    /**
     * Obtains a line that matches the description in the specified
     * <code>Line.Info</code> object.
     *
     * <p>If a <code>DataLine</code> is requested, and <code>info</code>
     * is an instance of <code>DataLine.Info</code> specifying at least
     * one fully qualified audio format, the last one
     * will be used as the default format of the returned
     * <code>DataLine</code>.
     *
     * <p>If system properties
     * <code>javax.sound.sampled.Clip</code>,
     * <code>javax.sound.sampled.Port</code>,
     * <code>javax.sound.sampled.SourceDataLine</code> and
     * <code>javax.sound.sampled.TargetDataLine</code> are defined
     * or they are defined in the file &quot;sound.properties&quot;,
     * they are used to retrieve default lines.
     * For details, refer to the {@link AudioSystem class description}.
     *
     * If the respective property is not set, or the mixer
     * requested in the property is not installed or does not provide the
     * requested line, all installed mixers are queried for the
     * requested line type. A Line will be returned from the first mixer
     * providing the requested line type.
     *
     * @param info a <code>Line.Info</code> object describing the desired kind of line
     * @return a line of the requested kind
     *
     * @throws LineUnavailableException if a matching line
     * is not available due to resource restrictions
     * @throws SecurityException if a matching line
     * is not available due to security restrictions
     * @throws IllegalArgumentException if the system does not
     * support at least one line matching the specified
     * <code>Line.Info</code> object
     * through any installed mixer
     */
    public static Line getLine(Line.Info info) throws LineUnavailableException {
	LineUnavailableException lue = null;
	List providers = getMixerProviders();


	// 1: try from default mixer for this line class
	try {
	    Mixer mixer = getDefaultMixer(providers, info);
	    if (mixer != null && mixer.isLineSupported(info)) {
		return mixer.getLine(info);
	    }
	} catch (LineUnavailableException e) {
	    lue = e;
	} catch (IllegalArgumentException iae) {
	    // must not happen... but better to catch it here,
	    // if plug-ins are badly written
	}


	// 2: if that doesn't work, try to find any mixing mixer
	for(int i = 0; i < providers.size(); i++) {
	    MixerProvider provider = (MixerProvider) providers.get(i);
	    Mixer.Info[] infos = provider.getMixerInfo();

	    for (int j = 0; j < infos.length; j++) {
 		try {
		    Mixer mixer = provider.getMixer(infos[j]);
		    // see if this is an appropriate mixer which can mix
		    if (isAppropriateMixer(mixer, info, true)) {
			return mixer.getLine(info);
		    }
		} catch (LineUnavailableException e) {
		    lue = e;
		} catch (IllegalArgumentException iae) {
		    // must not happen... but better to catch it here,
		    // if plug-ins are badly written
		}
	    }
	}


	// 3: if that didn't work, try to find any non-mixing mixer
	for(int i = 0; i < providers.size(); i++) {
	    MixerProvider provider = (MixerProvider) providers.get(i);
	    Mixer.Info[] infos = provider.getMixerInfo();
	    for (int j = 0; j < infos.length; j++) {
 		try {
		    Mixer mixer = provider.getMixer(infos[j]);
		    // see if this is an appropriate mixer which can mix
		    if (isAppropriateMixer(mixer, info, false)) {
			return mixer.getLine(info);
		    }
		} catch (LineUnavailableException e) {
		    lue = e;
		} catch (IllegalArgumentException iae) {
		    // must not happen... but better to catch it here,
		    // if plug-ins are badly written
		}
	    }
	}

	// if this line was supported but was not available, throw the last
	// LineUnavailableException we got (??).
	if (lue != null) {
	    throw lue;
	}

	// otherwise, the requested line was not supported, so throw
	// an Illegal argument exception
	throw new IllegalArgumentException("No line matching " +
					   info.toString() + " is supported.");
    }


    /**
     * Obtains a clip that can be used for playing back
     * an audio file or an audio stream. The returned clip
     * will be provided by the default system mixer, or,
     * if not possible, by any other mixer installed in the
     * system that supports a <code>Clip</code>
     * object.
     *
     * <p>The returned clip must be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioInputStream)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * <p>If the system property
     * <code>javax.sound.sampled.Clip</code>
     * is defined or it is defined in the file &quot;sound.properties&quot;,
     * it is used to retrieve the default clip.
     * For details, refer to the {@link AudioSystem class description}.
     *
     * @return the desired clip object
     *
     * @throws LineUnavailableException if a clip object
     * is not available due to resource restrictions
     * @throws SecurityException if a clip object
     * is not available due to security restrictions
     * @throws IllegalArgumentException if the system does not
     * support at least one clip instance through any installed mixer
     *
     * @see #getClip(Mixer.Info)
     * @since 1.5
     */
    public static Clip getClip() throws LineUnavailableException{
	AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
					     AudioSystem.NOT_SPECIFIED,
					     16, 2, 4,
					     AudioSystem.NOT_SPECIFIED, true);
	DataLine.Info info = new DataLine.Info(Clip.class, format);
	return (Clip) AudioSystem.getLine(info);
    }


    /**
     * Obtains a clip from the specified mixer that can be
     * used for playing back an audio file or an audio stream.
     *
     * <p>The returned clip must be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioInputStream)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * @param mixerInfo a <code>Mixer.Info</code> object representing the
     * desired mixer, or <code>null</code> for the system default mixer
     * @return a clip object from the specified mixer
     *
     * @throws LineUnavailableException if a clip
     * is not available from this mixer due to resource restrictions
     * @throws SecurityException if a clip
     * is not available from this mixer due to security restrictions
     * @throws IllegalArgumentException if the system does not
     * support at least one clip through the specified mixer
     *
     * @see #getClip()
     * @since 1.5
     */
    public static Clip getClip(Mixer.Info mixerInfo) throws LineUnavailableException{
	AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
					     AudioSystem.NOT_SPECIFIED,
					     16, 2, 4,
					     AudioSystem.NOT_SPECIFIED, true);
	DataLine.Info info = new DataLine.Info(Clip.class, format);
	Mixer mixer = AudioSystem.getMixer(mixerInfo);
	return (Clip) mixer.getLine(info);
    }


    /**
     * Obtains a source data line that can be used for playing back
     * audio data in the format specified by the
     * <code>AudioFormat</code> object. The returned line
     * will be provided by the default system mixer, or,
     * if not possible, by any other mixer installed in the
     * system that supports a matching
     * <code>SourceDataLine</code> object.
     *
     * <p>The returned line should be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioFormat, int)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * <p>The returned <code>SourceDataLine</code>'s default
     * audio format will be initialized with <code>format</code>.
     *
     * <p>If the system property
     * <code>javax.sound.sampled.SourceDataLine</code>
     * is defined or it is defined in the file &quot;sound.properties&quot;,
     * it is used to retrieve the default source data line.
     * For details, refer to the {@link AudioSystem class description}.
     *
     * @param format an <code>AudioFormat</code> object specifying
     *        the supported audio format of the returned line,
     *        or <code>null</code> for any audio format
     * @return the desired <code>SourceDataLine</code> object
     *
     * @throws LineUnavailableException if a matching source data line
     *         is not available due to resource restrictions
     * @throws SecurityException if a matching source data line
     *         is not available due to security restrictions
     * @throws IllegalArgumentException if the system does not
     *         support at least one source data line supporting the
     *         specified audio format through any installed mixer
     *
     * @see #getSourceDataLine(AudioFormat, Mixer.Info)
     * @since 1.5
     */
    public static SourceDataLine getSourceDataLine(AudioFormat format)
        throws LineUnavailableException{
	DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
	return (SourceDataLine) AudioSystem.getLine(info);
    }


    /**
     * Obtains a source data line that can be used for playing back
     * audio data in the format specified by the
     * <code>AudioFormat</code> object, provided by the mixer
     * specified by the <code>Mixer.Info</code> object.
     *
     * <p>The returned line should be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioFormat, int)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * <p>The returned <code>SourceDataLine</code>'s default
     * audio format will be initialized with <code>format</code>.
     *
     * @param format an <code>AudioFormat</code> object specifying
     *        the supported audio format of the returned line,
     *        or <code>null</code> for any audio format
     * @param mixerinfo a <code>Mixer.Info</code> object representing
     *        the desired mixer, or <code>null</code> for the system
     *        default mixer
     * @return the desired <code>SourceDataLine</code> object
     *
     * @throws LineUnavailableException if a matching source data
     *         line is not available from the specified mixer due
     *         to resource restrictions
     * @throws SecurityException if a matching source data line
     *         is not available from the specified mixer due to
     *         security restrictions
     * @throws IllegalArgumentException if the specified mixer does
     *         not support at least one source data line supporting
     *         the specified audio format
     *
     * @see #getSourceDataLine(AudioFormat)
     * @since 1.5
     */
    public static SourceDataLine getSourceDataLine(AudioFormat format,
						   Mixer.Info mixerinfo)
        throws LineUnavailableException{
	DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
	Mixer mixer = AudioSystem.getMixer(mixerinfo);
	return (SourceDataLine) mixer.getLine(info);
  }


    /**
     * Obtains a target data line that can be used for recording
     * audio data in the format specified by the
     * <code>AudioFormat</code> object. The returned line
     * will be provided by the default system mixer, or,
     * if not possible, by any other mixer installed in the
     * system that supports a matching
     * <code>TargetDataLine</code> object.
     *
     * <p>The returned line should be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioFormat, int)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * <p>The returned <code>TargetDataLine</code>'s default
     * audio format will be initialized with <code>format</code>.
     *
     * @param format an <code>AudioFormat</code> object specifying
     *        the supported audio format of the returned line,
     *        or <code>null</code> for any audio format
     * @return the desired <code>TargetDataLine</code> object
     *
     * @throws LineUnavailableException if a matching target data line
     *         is not available due to resource restrictions
     * @throws SecurityException if a matching target data line
     *         is not available due to security restrictions
     * @throws IllegalArgumentException if the system does not
     *         support at least one target data line supporting the
     *         specified audio format through any installed mixer
     *
     * @see #getTargetDataLine(AudioFormat, Mixer.Info)
     * @see AudioPermission
     * @since 1.5
     */
    public static TargetDataLine getTargetDataLine(AudioFormat format)
	throws LineUnavailableException{

	DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
	return (TargetDataLine) AudioSystem.getLine(info);
    }



    /**
     * Obtains a target data line that can be used for recording
     * audio data in the format specified by the
     * <code>AudioFormat</code> object, provided by the mixer
     * specified by the <code>Mixer.Info</code> object.
     *
     * <p>The returned line should be opened with the
     * <code>open(AudioFormat)</code> or
     * <code>open(AudioFormat, int)</code> method.
     *
     * <p>This is a high-level method that uses <code>getMixer</code>
     * and <code>getLine</code> internally.
     *
     * <p>The returned <code>TargetDataLine</code>'s default
     * audio format will be initialized with <code>format</code>.
     *
     * <p>If the system property
     * <code>javax.sound.sampled.TargetDataLine</code>
     * is defined or it is defined in the file &quot;sound.properties&quot;,
     * it is used to retrieve the default target data line.
     * For details, refer to the {@link AudioSystem class description}.
     *
     * @param format an <code>AudioFormat</code> object specifying
     *        the supported audio format of the returned line,
     *        or <code>null</code> for any audio format
     * @param mixerinfo a <code>Mixer.Info</code> object representing the
     *        desired mixer, or <code>null</code> for the system default mixer
     * @return the desired <code>TargetDataLine</code> object
     *
     * @throws LineUnavailableException if a matching target data
     *         line is not available from the specified mixer due
     *         to resource restrictions
     * @throws SecurityException if a matching target data line
     *         is not available from the specified mixer due to
     *         security restrictions
     * @throws IllegalArgumentException if the specified mixer does
     *         not support at least one target data line supporting
     *         the specified audio format
     *
     * @see #getTargetDataLine(AudioFormat)
     * @see AudioPermission
     * @since 1.5
     */
    public static TargetDataLine getTargetDataLine(AudioFormat format,
						   Mixer.Info mixerinfo)
	throws LineUnavailableException {

	DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
	Mixer mixer = AudioSystem.getMixer(mixerinfo);
	return (TargetDataLine) mixer.getLine(info);
    }


    // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
    /**
     * Obtains the encodings that the system can obtain from an
     * audio input stream with the specified encoding using the set
     * of installed format converters.
     * @param sourceEncoding the encoding for which conversion support
     * is queried
     * @return array of encodings.  If <code>sourceEncoding</code>is not supported,
     * an array of length 0 is returned. Otherwise, the array will have a length
     * of at least 1, representing <code>sourceEncoding</code> (no conversion).
     */
    public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat.Encoding sourceEncoding) {

	List codecs = getFormatConversionProviders();
	Vector encodings = new Vector();

	AudioFormat.Encoding encs[] = null;

	// gather from all the codecs
	for(int i=0; i<codecs.size(); i++ ) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    if( codec.isSourceEncodingSupported( sourceEncoding ) ) {
		encs = codec.getTargetEncodings();
		for (int j = 0; j < encs.length; j++) {
		    encodings.addElement( encs[j] );
		}
	    }
	}
	AudioFormat.Encoding encs2[] = (AudioFormat.Encoding[]) encodings.toArray(new AudioFormat.Encoding[0]);
	return encs2;
    }



    // $$fb 2002-04-12: fix for 4662082: behavior of AudioSystem.getTargetEncodings() methods doesn't match the spec
    /**
     * Obtains the encodings that the system can obtain from an
     * audio input stream with the specified format using the set
     * of installed format converters.
     * @param sourceFormat the audio format for which conversion
     * is queried
     * @return array of encodings. If <code>sourceFormat</code>is not supported,
     * an array of length 0 is returned. Otherwise, the array will have a length
     * of at least 1, representing the encoding of <code>sourceFormat</code> (no conversion).
     */
    public static AudioFormat.Encoding[] getTargetEncodings(AudioFormat sourceFormat) {


	List codecs = getFormatConversionProviders();
	Vector encodings = new Vector();

	int size = 0;
	int index = 0;
	AudioFormat.Encoding encs[] = null;

	// gather from all the codecs

	for(int i=0; i<codecs.size(); i++ ) {
	    encs = ((FormatConversionProvider) codecs.get(i)).getTargetEncodings(sourceFormat);
	    size += encs.length;
	    encodings.addElement( encs );
	}

	// now build a new array

	AudioFormat.Encoding encs2[] = new AudioFormat.Encoding[size];
	for(int i=0; i<encodings.size(); i++ ) {
	    encs = (AudioFormat.Encoding [])(encodings.get(i));
	    for(int j=0; j<encs.length; j++ ) {
		encs2[index++] = encs[j];
	    }
	}
	return encs2;
    }


    /**
     * Indicates whether an audio input stream of the specified encoding
     * can be obtained from an audio input stream that has the specified
     * format.
     * @param targetEncoding the desired encoding after conversion
     * @param sourceFormat the audio format before conversion
     * @return <code>true</code> if the conversion is supported,
     * otherwise <code>false</code>
     */
    public static boolean isConversionSupported(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {


	List codecs = getFormatConversionProviders();

	for(int i=0; i<codecs.size(); i++ ) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    if(codec.isConversionSupported(targetEncoding,sourceFormat) ) {
		return true;
	    }
	}
	return false;
    }


    /**
     * Obtains an audio input stream of the indicated encoding, by converting the
     * provided audio input stream.
     * @param targetEncoding the desired encoding after conversion
     * @param sourceStream the stream to be converted
     * @return an audio input stream of the indicated encoding
     * @throws IllegalArgumentException if the conversion is not supported
     * @see #getTargetEncodings(AudioFormat.Encoding)
     * @see #getTargetEncodings(AudioFormat)
     * @see #isConversionSupported(AudioFormat.Encoding, AudioFormat)
     * @see #getAudioInputStream(AudioFormat, AudioInputStream)
     */
    public static AudioInputStream getAudioInputStream(AudioFormat.Encoding targetEncoding,
						       AudioInputStream sourceStream) {

	List codecs = getFormatConversionProviders();

	for(int i = 0; i < codecs.size(); i++) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    if( codec.isConversionSupported( targetEncoding, sourceStream.getFormat() ) ) {
		return codec.getAudioInputStream( targetEncoding, sourceStream );
	    }
	}
	// we ran out of options, throw an exception
	throw new IllegalArgumentException("Unsupported conversion: " + targetEncoding + " from " + sourceStream.getFormat());
    }


    /**
     * Obtains the formats that have a particular encoding and that the system can
     * obtain from a stream of the specified format using the set of
     * installed format converters.
     * @param targetEncoding the desired encoding after conversion
     * @param sourceFormat the audio format before conversion
     * @return array of formats.  If no formats of the specified
     * encoding are supported, an array of length 0 is returned.
     */
    public static AudioFormat[] getTargetFormats(AudioFormat.Encoding targetEncoding, AudioFormat sourceFormat) {

	List codecs = getFormatConversionProviders();
	Vector formats = new Vector();

	int size = 0;
	int index = 0;
	AudioFormat fmts[] = null;

	// gather from all the codecs

	for(int i=0; i<codecs.size(); i++ ) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    fmts = codec.getTargetFormats(targetEncoding, sourceFormat);
	    size += fmts.length;
	    formats.addElement( fmts );
	}

	// now build a new array

	AudioFormat fmts2[] = new AudioFormat[size];
	for(int i=0; i<formats.size(); i++ ) {
	    fmts = (AudioFormat [])(formats.get(i));
	    for(int j=0; j<fmts.length; j++ ) {
		fmts2[index++] = fmts[j];
	    }
	}
	return fmts2;
    }


    /**
     * Indicates whether an audio input stream of a specified format
     * can be obtained from an audio input stream of another specified format.
     * @param targetFormat the desired audio format after conversion
     * @param sourceFormat the audio format before conversion
     * @return <code>true</code> if the conversion is supported,
     * otherwise <code>false</code>
     */

    public static boolean isConversionSupported(AudioFormat targetFormat, AudioFormat sourceFormat) {

	List codecs = getFormatConversionProviders();

	for(int i=0; i<codecs.size(); i++ ) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    if(codec.isConversionSupported(targetFormat, sourceFormat) ) {
		return true;
	    }
	}
	return false;
    }


    /**
     * Obtains an audio input stream of the indicated format, by converting the
     * provided audio input stream.
     * @param targetFormat the desired audio format after conversion
     * @param sourceStream the stream to be converted
     * @return an audio input stream of the indicated format
     * @throws IllegalArgumentException if the conversion is not supported
     * #see #getTargetEncodings(AudioFormat)
     * @see #getTargetFormats(AudioFormat.Encoding, AudioFormat)
     * @see #isConversionSupported(AudioFormat, AudioFormat)
     * @see #getAudioInputStream(AudioFormat.Encoding, AudioInputStream)
     */
    public static AudioInputStream getAudioInputStream(AudioFormat targetFormat,
						       AudioInputStream sourceStream) {

	if (sourceStream.getFormat().matches(targetFormat)) {
	    return sourceStream;
	}

	List codecs = getFormatConversionProviders();

	for(int i = 0; i < codecs.size(); i++) {
	    FormatConversionProvider codec = (FormatConversionProvider) codecs.get(i);
	    if(codec.isConversionSupported(targetFormat,sourceStream.getFormat()) ) {
		return codec.getAudioInputStream(targetFormat,sourceStream);
	    }
	}

	// we ran out of options...
	throw new IllegalArgumentException("Unsupported conversion: " + targetFormat + " from " + sourceStream.getFormat());
    }


    /**
     * Obtains the audio file format of the provided input stream.  The stream must
     * point to valid audio file data.  The implementation of this method may require
     * multiple parsers to examine the stream to determine whether they support it.
     * These parsers must be able to mark the stream, read enough data to determine whether they
     * support the stream, and, if not, reset the stream's read pointer to its original
     * position.  If the input stream does not support these operations, this method may fail
     * with an <code>IOException</code>.
     * @param stream the input stream from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the stream's audio file format
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an input/output exception occurs
     * @see InputStream#markSupported
     * @see InputStream#mark
     */
    public static AudioFileFormat getAudioFileFormat(InputStream stream)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioFileFormat format = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		format = reader.getAudioFileFormat( stream ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( format==null ) {
	    throw new UnsupportedAudioFileException("file is not a supported file type");
	} else {
	    return format;
	}
    }

    /**
     * Obtains the audio file format of the specified URL.  The URL must
     * point to valid audio file data.
     * @param url the URL from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an input/output exception occurs
     */
    public static AudioFileFormat getAudioFileFormat(URL url)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioFileFormat format = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		format = reader.getAudioFileFormat( url ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( format==null ) {
	    throw new UnsupportedAudioFileException("file is not a supported file type");
	} else {
	    return format;
	}
    }

    /**
     * Obtains the audio file format of the specified <code>File</code>.  The <code>File</code> must
     * point to valid audio file data.
     * @param file the <code>File</code> from which file format information should be
     * extracted
     * @return an <code>AudioFileFormat</code> object describing the audio file format
     * @throws UnsupportedAudioFileException if the <code>File</code> does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
    public static AudioFileFormat getAudioFileFormat(File file)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioFileFormat format = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		format = reader.getAudioFileFormat( file ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( format==null ) {
	    throw new UnsupportedAudioFileException("file is not a supported file type");
	} else {
	    return format;
	}
    }


    /**
     * Obtains an audio input stream from the provided input stream.  The stream must
     * point to valid audio file data.  The implementation of this method may
     * require multiple parsers to
     * examine the stream to determine whether they support it.  These parsers must
     * be able to mark the stream, read enough data to determine whether they
     * support the stream, and, if not, reset the stream's read pointer to its original
     * position.  If the input stream does not support these operation, this method may fail
     * with an <code>IOException</code>.
     * @param stream the input stream from which the <code>AudioInputStream</code> should be
     * constructed
     * @return an <code>AudioInputStream</code> object based on the audio file data contained
     * in the input stream.
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     * @see InputStream#markSupported
     * @see InputStream#mark
     */
    public static AudioInputStream getAudioInputStream(InputStream stream)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioInputStream audioStream = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		audioStream = reader.getAudioInputStream( stream ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( audioStream==null ) {
	    throw new UnsupportedAudioFileException("could not get audio input stream from input stream");
	} else {
	    return audioStream;
	}
    }

    /**
     * Obtains an audio input stream from the URL provided.  The URL must
     * point to valid audio file data.
     * @param url the URL for which the <code>AudioInputStream</code> should be
     * constructed
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
     * to by the URL
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
    public static AudioInputStream getAudioInputStream(URL url)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioInputStream audioStream = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		audioStream = reader.getAudioInputStream( url ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( audioStream==null ) {
	    throw new UnsupportedAudioFileException("could not get audio input stream from input URL");
	} else {
	    return audioStream;
	}
    }

    /**
     * Obtains an audio input stream from the provided <code>File</code>.  The <code>File</code> must
     * point to valid audio file data.
     * @param file the <code>File</code> for which the <code>AudioInputStream</code> should be
     * constructed
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
     * to by the <code>File</code>
     * @throws UnsupportedAudioFileException if the <code>File</code> does not point to valid audio
     * file data recognized by the system
     * @throws IOException if an I/O exception occurs
     */
    public static AudioInputStream getAudioInputStream(File file)
	throws UnsupportedAudioFileException, IOException {

	List providers = getAudioFileReaders();
	AudioInputStream audioStream = null;

	for(int i = 0; i < providers.size(); i++ ) {
	    AudioFileReader reader = (AudioFileReader) providers.get(i);
	    try {
		audioStream = reader.getAudioInputStream( file ); // throws IOException
		break;
	    } catch (UnsupportedAudioFileException e) {
		continue;
	    }
	}

	if( audioStream==null ) {
	    throw new UnsupportedAudioFileException("could not get audio input stream from input file");
	} else {
	    return audioStream;
	}
    }


    /**
     * Obtains the file types for which file writing support is provided by the system.
     * @return array of unique file types.  If no file types are supported,
     * an array of length 0 is returned.
     */
    public static AudioFileFormat.Type[] getAudioFileTypes() {
	List providers = getAudioFileWriters();
	Set returnTypesSet = new HashSet();

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes();
	    for(int j=0; j < fileTypes.length; j++) {
		returnTypesSet.add(fileTypes[j]);
	    }
	}
	AudioFileFormat.Type returnTypes[] = (AudioFileFormat.Type[])
	    returnTypesSet.toArray(new AudioFileFormat.Type[0]);
	return returnTypes;
    }


    /**
     * Indicates whether file writing support for the specified file type is provided
     * by the system.
     * @param fileType the file type for which write capabilities are queried
     * @return <code>true</code> if the file type is supported,
     * otherwise <code>false</code>
     */
    public static boolean isFileTypeSupported(AudioFileFormat.Type fileType) {

	List providers = getAudioFileWriters();

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    if (writer.isFileTypeSupported(fileType)) {
		return true;
	    }
	}
	return false;
    }


    /**
     * Obtains the file types that the system can write from the
     * audio input stream specified.
     * @param stream the audio input stream for which audio file type support
     * is queried
     * @return array of file types.  If no file types are supported,
     * an array of length 0 is returned.
     */
    public static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
	List providers = getAudioFileWriters();
	Set returnTypesSet = new HashSet();

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    AudioFileFormat.Type[] fileTypes = writer.getAudioFileTypes(stream);
	    for(int j=0; j < fileTypes.length; j++) {
		returnTypesSet.add(fileTypes[j]);
	    }
	}
	AudioFileFormat.Type returnTypes[] = (AudioFileFormat.Type[])
	    returnTypesSet.toArray(new AudioFileFormat.Type[0]);
	return returnTypes;
    }


    /**
     * Indicates whether an audio file of the specified file type can be written
     * from the indicated audio input stream.
     * @param fileType the file type for which write capabilities are queried
     * @param stream the stream for which file-writing support is queried
     * @return <code>true</code> if the file type is supported for this audio input stream,
     * otherwise <code>false</code>
     */
    public static boolean isFileTypeSupported(AudioFileFormat.Type fileType,
					      AudioInputStream stream) {

	List providers = getAudioFileWriters();

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    if(writer.isFileTypeSupported(fileType, stream)) {
		return true;
	    }
	}
	return false;
    }


    /**
     * Writes a stream of bytes representing an audio file of the specified file type
     * to the output stream provided.  Some file types require that
     * the length be written into the file header; such files cannot be written from
     * start to finish unless the length is known in advance.  An attempt
     * to write a file of such a type will fail with an IOException if the length in
     * the audio file type is <code>AudioSystem.NOT_SPECIFIED</code>.
     *
     * @param stream the audio input stream containing audio data to be
     * written to the file
     * @param fileType the kind of audio file to write
     * @param out the stream to which the file data should be written
     * @return the number of bytes written to the output stream
     * @throws IOException if an input/output exception occurs
     * @throws IllegalArgumentException if the file type is not supported by
     * the system
     * @see #isFileTypeSupported
     * @see	#getAudioFileTypes
     */
    public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
			    OutputStream out) throws IOException {

	List providers = getAudioFileWriters();
	int bytesWritten = 0;
	boolean flag = false;

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    try {
		bytesWritten = writer.write( stream, fileType, out ); // throws IOException
		flag = true;
		break;
	    } catch (IllegalArgumentException e) {
		// thrown if this provider cannot write the sequence, try the next
		continue;
	    }
	}
	if(!flag) {
	    throw new IllegalArgumentException("could not write audio file: file type not supported: " + fileType);
	} else {
	    return bytesWritten;
	}
    }


    /**
     * Writes a stream of bytes representing an audio file of the specified file type
     * to the external file provided.
     * @param stream the audio input stream containing audio data to be
     * written to the file
     * @param fileType the kind of audio file to write
     * @param out the external file to which the file data should be written
     * @return the number of bytes written to the file
     * @throws IOException if an I/O exception occurs
     * @throws IllegalArgumentException if the file type is not supported by
     * the system
     * @see #isFileTypeSupported
     * @see	#getAudioFileTypes
     */
    public static int write(AudioInputStream stream, AudioFileFormat.Type fileType,
			    File out) throws IOException {

	List providers = getAudioFileWriters();
	int bytesWritten = 0;
	boolean flag = false;

	for(int i=0; i < providers.size(); i++) {
	    AudioFileWriter writer = (AudioFileWriter) providers.get(i);
	    try {
		bytesWritten = writer.write( stream, fileType, out ); // throws IOException
		flag = true;
		break;
	    } catch (IllegalArgumentException e) {
		// thrown if this provider cannot write the sequence, try the next
		continue;
	    }
	}
	if (!flag) {
	    throw new IllegalArgumentException("could not write audio file: file type not supported: " + fileType);
	} else {
	    return bytesWritten;
	}
    }


    // METHODS FOR INTERNAL IMPLEMENTATION USE

    /**
     * Obtains the set of MixerProviders on the system.
     */
    private static List getMixerProviders() {
	return getProviders(MixerProvider.class);
    }


    /**
     * Obtains the set of format converters (codecs, transcoders, etc.)
     * that are currently installed on the system.
     * @return an array of
     * {@link javax.sound.sampled.spi.FormatConversionProvider
     * FormatConversionProvider}
     * objects representing the available format converters.  If no format
     * converters readers are available on the system, an array of length 0 is
     * returned.
     */
    private static List getFormatConversionProviders() {
	return getProviders(FormatConversionProvider.class);
    }


    /**
     * Obtains the set of audio file readers that are currently installed on the system.
     * @return a List of
     * {@link javax.sound.sampled.spi.AudioFileReader
     * AudioFileReader}
     * objects representing the installed audio file readers.  If no audio file
     * readers are available on the system, an empty List is returned.
     */
    private static List getAudioFileReaders() {
	return getProviders(AudioFileReader.class);
    }


    /**
     * Obtains the set of audio file writers that are currently installed on the system.
     * @return a List of
     * {@link javax.sound.samples.spi.AudioFileWriter AudioFileWriter}
     * objects representing the available audio file writers.  If no audio file
     * writers are available on the system, an empty List is returned.
     */
    private static List getAudioFileWriters() {
	return getProviders(AudioFileWriter.class);
    }



    /** Attempts to locate and return a default Mixer that provides lines
     * of the specified type.
     *
     * @param providers the installed mixer providers
     * @param info The requested line type
     * TargetDataLine.class, Clip.class or Port.class.
     * @return a Mixer that matches the requirements, or null if no default mixer found
     */
    private static Mixer getDefaultMixer(List providers, Line.Info info) {
	Class lineClass = info.getLineClass();
	String providerClassName = JDK13Services.getDefaultProviderClassName(lineClass);
	String instanceName = JDK13Services.getDefaultInstanceName(lineClass);
	Mixer mixer;

	if (providerClassName != null) {
	    MixerProvider defaultProvider = getNamedProvider(providerClassName, providers);
	    if (defaultProvider != null) {
		if (instanceName != null) {
		    mixer = getNamedMixer(instanceName, defaultProvider, info);
		    if (mixer != null) {
			return mixer;
		    }
		} else {
		    mixer = getFirstMixer(defaultProvider, info,
					  false /* mixing not required*/);
		    if (mixer != null) {
			return mixer;
		    }
		}

	    }
	}

	/* Provider class not specified or
	   provider class cannot be found, or
	   provider class and instance specified and instance cannot be found or is not appropriate */
	if (instanceName != null) {
	    mixer = getNamedMixer(instanceName, providers, info);
	    if (mixer != null) {
		return mixer;
	    }
	}


	/* No default are specified, or if something is specified, everything
	   failed. */
	return null;
    }



    /** Return a MixerProvider of a given class from the list of
	MixerProviders.

	This method never requires the returned Mixer to do mixing.
	@param providerClassName The class name of the provider to be returned.
	@param providers The list of MixerProviders that is searched.
	@return A MixerProvider of the requested class, or null if none is
	found.
     */
    private static MixerProvider getNamedProvider(String providerClassName,
						  List providers) {
	for(int i = 0; i < providers.size(); i++) {
	    MixerProvider provider = (MixerProvider) providers.get(i);
	    if (provider.getClass().getName().equals(providerClassName)) {
		return provider;
	    }
	}
	return null;
    }


    /** Return a Mixer with a given name from a given MixerProvider.
      This method never requires the returned Mixer to do mixing.
      @param mixerName The name of the Mixer to be returned.
      @param provider The MixerProvider to check for Mixers.
      @param info The type of line the returned Mixer is required to
      support.

      @return A Mixer matching the requirements, or null if none is found.
     */
    private static Mixer getNamedMixer(String mixerName,
				       MixerProvider provider,
				       Line.Info info) {
	Mixer.Info[] infos = provider.getMixerInfo();
	for (int i = 0; i < infos.length; i++) {
	    if (infos[i].getName().equals(mixerName)) {
		Mixer mixer = provider.getMixer(infos[i]);
		if (isAppropriateMixer(mixer, info, false)) {
		    return mixer;
		}
	    }
	}
	return null;
    }


    /** From a List of MixerProviders, return a Mixer with a given name.
	This method never requires the returned Mixer to do mixing.
	@param mixerName The name of the Mixer to be returned.
	@param providers The List of MixerProviders to check for Mixers.
	@param info The type of line the returned Mixer is required to
	support.
	@return A Mixer matching the requirements, or null if none is found.
     */
    private static Mixer getNamedMixer(String mixerName,
				       List providers,
				       Line.Info info) {
	for(int i = 0; i < providers.size(); i++) {
	    MixerProvider provider = (MixerProvider) providers.get(i);
	    Mixer mixer = getNamedMixer(mixerName, provider, info);
	    if (mixer != null) {
		return mixer;
	    }
	}
	return null;
    }


    /** From a given MixerProvider, return the first appropriate Mixer.
	@param provider The MixerProvider to check for Mixers.
	@param info The type of line the returned Mixer is required to
	support.
	@param isMixingRequired If true, only Mixers that support mixing are
	returned for line types of SourceDataLine and Clip.

	@return A Mixer that is considered appropriate, or null
	if none is found.
     */
    private static Mixer getFirstMixer(MixerProvider provider,
				       Line.Info info,
				       boolean isMixingRequired) {
	Mixer.Info[] infos = provider.getMixerInfo();
	for (int j = 0; j < infos.length; j++) {
	    Mixer mixer = provider.getMixer(infos[j]);
	    if (isAppropriateMixer(mixer, info, isMixingRequired)) {
		return mixer;
	    }
	}
	return null;
    }


    /** Checks if a Mixer is appropriate.
	A Mixer is considered appropriate if it support the given line type.
	If isMixingRequired is true and the line type is an output one
	(SourceDataLine, Clip), the mixer is appropriate if it supports
	at least 2 (concurrent) lines of the given type.

	@return true if the mixer is considered appropriate according to the
	rules given above, false otherwise.
     */
    private static boolean isAppropriateMixer(Mixer mixer,
					      Line.Info lineInfo,
					      boolean isMixingRequired) {
	if (! mixer.isLineSupported(lineInfo)) {
	    return false;
	}
	Class lineClass = lineInfo.getLineClass();
	if (isMixingRequired 
	    && (SourceDataLine.class.isAssignableFrom(lineClass) ||
		Clip.class.isAssignableFrom(lineClass))) {
	    int maxLines = mixer.getMaxLines(lineInfo);
	    return ((maxLines == NOT_SPECIFIED) || (maxLines > 1));
	}
	return true;
    }



    /**
     * Like getMixerInfo, but return List
     */
    private static List getMixerInfoList() {
	List providers = getMixerProviders();
	return getMixerInfoList(providers);
    }


    /**
     * Like getMixerInfo, but return List
     */
    private static List getMixerInfoList(List providers) {
	List infos = new ArrayList();

	Mixer.Info[] someInfos;	// per-mixer
	Mixer.Info[] allInfos;	// for all mixers

	for(int i = 0; i < providers.size(); i++ ) {
	    someInfos = (Mixer.Info[])
		((MixerProvider)providers.get(i)).getMixerInfo();

	    for (int j = 0; j < someInfos.length; j++) {
		infos.add(someInfos[j]);
	    }
	}

	return infos;
    }


    /**
     * Obtains the set of services currently installed on the system
     * using sun.misc.Service, the SPI mechanism in 1.3.
     * @return a List of instances of providers for the requested service.
     * If no providers are available, a vector of length 0 will be returned.
     */
    private static List getProviders(Class providerClass) {
	return JDK13Services.getProviders(providerClass);
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar