1 /*
2  * Copyright (c) 2001, 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 java.io.ByteArrayInputStream;
25 
26 import javax.sound.sampled.AudioFormat;
27 import javax.sound.sampled.AudioInputStream;
28 import javax.sound.sampled.AudioSystem;
29 import javax.sound.sampled.Clip;
30 import javax.sound.sampled.DataLine;
31 import javax.sound.sampled.FloatControl;
32 import javax.sound.sampled.LineUnavailableException;
33 import javax.sound.sampled.Mixer;
34 
35 /**
36  * @test
37  * @bug 4385654
38  * @summary Check that the MASTER_GAIN control has a valid precision
39  */
40 //public class test047 extends TRTest
41 public class FloatControlBug {
42 
43     private Clip theClip;
44 
45     boolean testPassed = true;
46 
47     private AudioFormat.Encoding theEncoding = AudioFormat.Encoding.PCM_SIGNED;
48 
49     private float theSampleRate = 44100;
50 
51     private int theSampleSize = 16;
52 
53     private int theNumberOfChannels = 1;
54 
55     private int theFrameSize = 2;
56 
57     private float theFrameRate = 44100;
58 
59     private boolean isBigEndian = false;
60 
61     //_______________________________________________
62     //      Method: runTest
63     //_______________________________________________
runTest()64     public boolean runTest() {
65         AudioInputStream theAudioInputStream = new AudioInputStream(
66                 new ByteArrayInputStream(new byte[0]),
67                 new AudioFormat(44100.0f, 16, 2, true, false), 441000);
68 
69         AudioFormat theAudioFormat = theAudioInputStream.getFormat();
70 
71         DataLine.Info info = new DataLine.Info(Clip.class, theAudioFormat,
72                                                AudioSystem.NOT_SPECIFIED);
73         try {
74             theClip = (Clip) AudioSystem.getLine(info);
75             theClip.open(theAudioInputStream);
76             FloatControl theFloatControl = (FloatControl) (theClip.getControl(
77                     FloatControl.Type.MASTER_GAIN));
78             float theFloatControlPrecision = theFloatControl.getPrecision();
79             System.out.println(
80                     "theFloatControlPrecision: " + theFloatControlPrecision);
81             System.out.println("Minimum: " + theFloatControl.getMinimum());
82             System.out.println("Maximum: " + theFloatControl.getMaximum());
83             System.out.println("Value  : " + theFloatControl.getValue());
84             testPassed = theFloatControlPrecision > 0;
85         } catch (LineUnavailableException e) {
86             e.printStackTrace();
87             testPassed = true;
88         } catch (Exception e) {
89             e.printStackTrace();
90             testPassed = false;
91         }
92         return testPassed;
93     }
94 
95     //_______________________________________________
96     //      Method: main
97     //_______________________________________________
main(String[] args)98     public static void main(String[] args) throws Exception {
99         //test047 thisTest = new test047();
100         if (isSoundcardInstalled()) {
101             FloatControlBug thisTest = new FloatControlBug();
102             boolean testResult = thisTest.runTest();
103             if (testResult) {
104                 System.out.println("Test passed");
105             } else {
106                 System.out.println("Test failed");
107                 throw new Exception("Test failed");
108             }
109         }
110     }
111 
112     /**
113      * Returns true if at least one soundcard is correctly installed
114      * on the system.
115      */
isSoundcardInstalled()116     public static boolean isSoundcardInstalled() {
117         boolean result = false;
118         try {
119             Mixer.Info[] mixers = AudioSystem.getMixerInfo();
120             if (mixers.length > 0) {
121                 result = AudioSystem.getSourceDataLine(null) != null;
122             }
123         } catch (Exception e) {
124             System.err.println("Exception occured: " + e);
125         }
126         if (!result) {
127             System.err.println(
128                     "Soundcard does not exist or sound drivers not installed!");
129             System.err.println(
130                     "This test requires sound drivers for execution.");
131         }
132         return result;
133     }
134 }
135