1/*
2 * Copyright (c) 2005, 2017, 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.util.concurrent.TimeUnit;
25
26import javax.sound.sampled.AudioFormat;
27import javax.sound.sampled.AudioSystem;
28import javax.sound.sampled.Clip;
29import javax.sound.sampled.DataLine;
30import javax.sound.sampled.LineUnavailableException;
31
32/*
33 * @test
34 * @bug 5070081
35 * @summary Tests that javax.sound.sampled.Clip does not loses position through
36 *          stop/start
37 */
38public class bug5070081 {
39
40    static AudioFormat format = new AudioFormat(22050, 8, 1, false, false);
41    // create a 3-second file
42    static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 3)];
43
44    static final int LOOP_COUNT = 5;
45
46    static boolean test() throws Exception {
47        DataLine.Info info = new DataLine.Info(Clip.class, format);
48        Clip clip = null;
49        boolean bSuccess = true;
50        try {
51            clip = (Clip) AudioSystem.getLine(info);
52            clip.open(format, soundData, 0, soundData.length);
53        } catch (LineUnavailableException | IllegalArgumentException ignored) {
54            // the test is not applicable
55            return bSuccess;
56        }
57
58        long nLengthMS = clip.getMicrosecondLength()/1000;
59
60        System.out.println("  Clip length:");
61        System.out.println("    frames: " + clip.getFrameLength());
62        System.out.println("    seconds: " + nLengthMS/1000.0);
63
64        clip.start();                               // start playing
65        Thread.sleep(1000);                         // wait a sec
66        long time1 = currentTimeMillis();
67        long pos1 = clip.getFramePosition();        // store the position
68        clip.stop();                                // and then stop
69        long pos2 = clip.getFramePosition();        // 2nd try
70        long time2 = currentTimeMillis();
71
72        System.out.println("  Position before stop: " + pos1);
73        System.out.println("  Position after stop: " + pos2);
74
75        long timeDiff = Math.abs(time2 - time1);
76        // sample rate is 22050 per second, so 22.05 per ms
77        long posDiff = (long) (Math.abs(pos2 - pos1) / 22.05);
78        System.out.println("  d(time): " + timeDiff + " ms;"
79                + "d(clip pos time): " + posDiff + " ms.");
80
81        long nDerivation = posDiff - timeDiff;
82        // add 50 ms for deviation (delay for stopping and errors due timer precision)
83        if (nDerivation > 50) {
84            System.out.println("  ERROR(1): The deviation is too much: " + nDerivation + " ms");
85            bSuccess = false;
86        }
87
88        Thread.sleep(1000);
89        clip.start();                               // start again
90        Thread.sleep(100);
91        while(clip.isRunning());                    // wait for the sound to finish
92
93        int nEndPos = clip.getFramePosition();
94        System.out.println("  Position at end: " + nEndPos);
95        if (nEndPos > clip.getFrameLength()) {
96            System.out.println("  ERROR(2): end position if out of range");
97            bSuccess = false;
98        }
99
100        clip.close();
101
102        return bSuccess;
103    }
104
105    public static void main(String[] args) throws Exception {
106        for (int count=1; count <= LOOP_COUNT; count++)
107        {
108            System.out.println("loop " + count + "/" + LOOP_COUNT);
109            if (!test())
110            {
111                System.out.println("Test FAILED");
112                throw new RuntimeException("Test FAILED.");
113            }
114        }
115
116        System.out.println("Test passed sucessfully");
117    }
118
119    private static long currentTimeMillis() {
120        return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
121    }
122}
123