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
24import java.io.ByteArrayInputStream;
25
26import javax.sound.sampled.AudioFormat;
27import javax.sound.sampled.AudioInputStream;
28import javax.sound.sampled.AudioSystem;
29import javax.sound.sampled.Clip;
30import javax.sound.sampled.DataLine;
31import javax.sound.sampled.LineUnavailableException;
32import javax.sound.sampled.Mixer;
33
34/**
35 * @test
36 * @bug 4237703
37 * @summary Check that Clip.getMicrosecondLength() returns correct value.
38 */
39public class ClipDuration {
40
41    public static int run(Mixer m) {
42        int res=1; // failed
43        int frameCount = 441000; // lets say 10 seconds
44        AudioFormat f = new AudioFormat(44100.0f, 16, 2, true, false);
45        AudioInputStream audioInputStream =
46            new AudioInputStream(new ByteArrayInputStream(new byte[frameCount * f.getFrameSize()]),
47                                 f, frameCount);
48        AudioFormat     format = audioInputStream.getFormat();
49        Clip m_clip = null;
50        try {
51            if (m == null) {
52                m_clip = (Clip) AudioSystem.getClip();
53            } else {
54                DataLine.Info   info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);
55                m_clip = (Clip) m.getLine(info);
56            }
57            System.out.println("Got clip: "+m_clip);
58            m_clip.open(audioInputStream);
59            long microseconds=m_clip.getMicrosecondLength();
60            System.out.println("getFrameLength()="+m_clip.getFrameLength()+" frames");
61            System.out.println("getMicrosecondLength()="+microseconds+" us");
62            if (Math.abs(microseconds-10000000)<50) {
63                System.out.println("->Clip OK");
64                res=0; // passes if less than 50us error
65            }
66        } catch (LineUnavailableException luae) {
67            System.err.println(luae);
68            res = 3; // line not available, test not failed
69        } catch (Throwable t) {
70            System.out.println("->Exception:"+t);
71            t.printStackTrace();
72            res=2; // exception
73        }
74        if (m_clip != null) {
75            m_clip.close();
76        }
77        return res;
78    }
79
80
81
82    public static void main(String[] args) throws Exception     {
83        if (isSoundcardInstalled()) {
84            int res=3;
85            res = run(null);
86            Mixer.Info[] infos = AudioSystem.getMixerInfo();
87            for (int i = 0; i<infos.length; i++) {
88                try {
89                        Mixer m = AudioSystem.getMixer(infos[i]);
90                        int r = run(m);
91                        if (r == 1) res = 1;
92                } catch (Exception e) {
93                }
94            }
95            if (res!=1) {
96                System.out.println("Test passed");
97            } else {
98                if (res==2) {
99                    System.err.println("Test could not execute: test threw unexpected Exception.");
100                    throw new Exception("Test threw exception");
101                }
102                else if (res==3) {
103                    System.err.println("Test could not execute: please install an audio device");
104                    return;
105                }
106                throw new Exception("Test returned wrong length");
107            }
108        }
109    }
110
111    /**
112     * Returns true if at least one soundcard is correctly installed
113     * on the system.
114     */
115    public static boolean isSoundcardInstalled() {
116        boolean result = false;
117        try {
118            Mixer.Info[] mixers = AudioSystem.getMixerInfo();
119            if (mixers.length > 0) {
120                result = AudioSystem.getSourceDataLine(null) != null;
121            }
122        } catch (Exception e) {
123            System.err.println("Exception occured: "+e);
124        }
125        if (!result) {
126            System.err.println("Soundcard does not exist or sound drivers not installed!");
127            System.err.println("This test requires sound drivers for execution.");
128        }
129        return result;
130    }
131}
132