|
1 /* |
|
2 * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import javax.sound.sampled.AudioFormat; |
|
25 import javax.sound.sampled.AudioSystem; |
|
26 import javax.sound.sampled.DataLine; |
|
27 import javax.sound.sampled.Line; |
|
28 import javax.sound.sampled.Mixer; |
|
29 |
|
30 /** |
|
31 * @test |
|
32 * @bug 5013897 |
|
33 * @summary Verify that plughw: provides mono and 8-bit lines |
|
34 */ |
|
35 public class PlugHwMonoAnd8bitAvailable { |
|
36 static int failed = 0; |
|
37 static int testedFormats = 0; |
|
38 |
|
39 public static void main(String[] args) throws Exception { |
|
40 out("5013897: Verify that plughw: provides mono and 8-bit lines"); |
|
41 |
|
42 Mixer.Info[] aInfos = AudioSystem.getMixerInfo(); |
|
43 for (int i = 0; i < aInfos.length; i++) { |
|
44 try { |
|
45 Mixer mixer = AudioSystem.getMixer(aInfos[i]); |
|
46 out("Mixer "+aInfos[i]); |
|
47 if (aInfos[i].getName().contains("plughw")) { |
|
48 checkLines(mixer, mixer.getSourceLineInfo()); |
|
49 checkLines(mixer, mixer.getTargetLineInfo()); |
|
50 } else { |
|
51 out(" -> not plughw, ignored."); |
|
52 } |
|
53 } catch (Exception e) { |
|
54 out("Unexpected exception when getting a mixer: "+e); |
|
55 } |
|
56 } |
|
57 if (testedFormats == 0) { |
|
58 out("[No appropriate lines available] - cannot exercise this test."); |
|
59 } else { |
|
60 if (failed>0) { |
|
61 throw new Exception("Test FAILED!"); |
|
62 } |
|
63 out("Successfully verified "+testedFormats+" formats."); |
|
64 out("Test passed"); |
|
65 } |
|
66 } |
|
67 |
|
68 public static void checkLines(Mixer mixer, Line.Info[] infos) { |
|
69 for (int i = 0; i<infos.length; i++) { |
|
70 try { |
|
71 System.out.println(" Line "+infos[i]+" (max. "+mixer.getMaxLines(infos[i])+" simultaneously): "); |
|
72 if (infos[i] instanceof DataLine.Info) { |
|
73 DataLine.Info info = (DataLine.Info) infos[i]; |
|
74 int thisTestedFormats = testedFormats; |
|
75 int thisFailed = failed; |
|
76 AudioFormat[] formats = info.getFormats(); |
|
77 for (int f = 0; f < formats.length; f++) { |
|
78 if (formats[f].getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED) |
|
79 || formats[f].getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) { |
|
80 try { |
|
81 if (formats[f].getSampleSizeInBits() > 16) { |
|
82 // if a bit size larger than 16 is available, also 16-bit must be there |
|
83 checkFormat(formats, getOtherBits(formats[f], 16)); |
|
84 } else |
|
85 if (formats[f].getSampleSizeInBits() > 8) { |
|
86 // if a bit size larger than 8 is available, also 8-bit must be there |
|
87 checkFormat(formats, getOtherBits(formats[f], 8)); |
|
88 } |
|
89 if (formats[f].getChannels() > 2) { |
|
90 // if more than 2 channels, also 2 channels must be there |
|
91 checkFormat(formats, getOtherChannels(formats[f], 2)); |
|
92 } else |
|
93 if (formats[f].getChannels() > 1) { |
|
94 // if more than 1 channel, also 1 channel must be there |
|
95 checkFormat(formats, getOtherChannels(formats[f], 1)); |
|
96 } |
|
97 } catch (Exception e1) { |
|
98 out(" Unexpected exception when getting a format: "+e1); |
|
99 } |
|
100 } |
|
101 } |
|
102 if (testedFormats - thisTestedFormats == 0) { |
|
103 out(" -->could not test any formats"); |
|
104 } else if (failed - thisFailed == 0) { |
|
105 out(" -->"+(testedFormats - thisTestedFormats)+" formats tested OK"); |
|
106 } |
|
107 |
|
108 } else { |
|
109 out(" --> not a DataLine"); |
|
110 } |
|
111 } catch (Exception e) { |
|
112 out(" Unexpected exception when getting a line: "+e); |
|
113 } |
|
114 } |
|
115 } |
|
116 |
|
117 public static void checkFormat(AudioFormat[] formats, AudioFormat format) { |
|
118 testedFormats++; |
|
119 for (int i = 0; i < formats.length; i++) { |
|
120 if (formats[i].matches(format)) { |
|
121 return; |
|
122 } |
|
123 } |
|
124 out(" ## expected this format: "+format |
|
125 +" ("+format.getChannels()+" channels, " |
|
126 +"frameSize="+format.getFrameSize()+", " |
|
127 +(format.isBigEndian()?"big endian":"little endian") |
|
128 +")"); |
|
129 failed++; |
|
130 } |
|
131 |
|
132 // only works for PCM encodings |
|
133 public static AudioFormat getOtherBits(AudioFormat format, int newBits) { |
|
134 boolean isSigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED); |
|
135 return new AudioFormat(format.getSampleRate(), |
|
136 newBits, |
|
137 format.getChannels(), |
|
138 isSigned, |
|
139 (newBits>8)?format.isBigEndian():false); |
|
140 } |
|
141 |
|
142 // only works for PCM encodings |
|
143 public static AudioFormat getOtherChannels(AudioFormat format, int newChannels) { |
|
144 int newFrameSize; |
|
145 if (newChannels <= 0 || format.getChannels() <= 0 || format.getFrameSize() <= 0) { |
|
146 newFrameSize = -1; |
|
147 } else { |
|
148 newFrameSize = format.getFrameSize() / format.getChannels() * newChannels; |
|
149 } |
|
150 return new AudioFormat(format.getEncoding(), |
|
151 format.getSampleRate(), |
|
152 format.getSampleSizeInBits(), |
|
153 newChannels, |
|
154 newFrameSize, |
|
155 format.getFrameRate(), |
|
156 format.isBigEndian()); |
|
157 } |
|
158 |
|
159 |
|
160 static void out(String s) { |
|
161 System.out.println(s); System.out.flush(); |
|
162 } |
|
163 } |