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 }