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