Path: blob/master/test/jdk/javax/sound/sampled/Clip/bug5070081.java
41153 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.concurrent.TimeUnit;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioSystem;27import javax.sound.sampled.Clip;28import javax.sound.sampled.DataLine;29import javax.sound.sampled.LineUnavailableException;3031/*32* @test33* @bug 507008134* @summary Tests that javax.sound.sampled.Clip does not loses position through35* stop/start36*/37public class bug5070081 {3839static AudioFormat format = new AudioFormat(22050, 8, 1, false, false);40// create a 3-second file41static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 3)];4243static final int LOOP_COUNT = 5;4445static boolean test() throws Exception {46DataLine.Info info = new DataLine.Info(Clip.class, format);47Clip clip = null;48boolean bSuccess = true;49try {50clip = (Clip) AudioSystem.getLine(info);51clip.open(format, soundData, 0, soundData.length);52} catch (LineUnavailableException | IllegalArgumentException ignored) {53// the test is not applicable54return bSuccess;55}5657long nLengthMS = clip.getMicrosecondLength()/1000;5859System.out.println(" Clip length:");60System.out.println(" frames: " + clip.getFrameLength());61System.out.println(" seconds: " + nLengthMS/1000.0);6263clip.start(); // start playing64Thread.sleep(1000); // wait a sec65long time1 = currentTimeMillis();66long pos1 = clip.getFramePosition(); // store the position67clip.stop(); // and then stop68long pos2 = clip.getFramePosition(); // 2nd try69long time2 = currentTimeMillis();7071System.out.println(" Position before stop: " + pos1);72System.out.println(" Position after stop: " + pos2);7374long timeDiff = Math.abs(time2 - time1);75// sample rate is 22050 per second, so 22.05 per ms76long posDiff = (long) (Math.abs(pos2 - pos1) / 22.05);77System.out.println(" d(time): " + timeDiff + " ms;"78+ "d(clip pos time): " + posDiff + " ms.");7980long nDerivation = posDiff - timeDiff;81// add 50 ms for deviation (delay for stopping and errors due timer precision)82if (nDerivation > 50) {83System.out.println(" ERROR(1): The deviation is too much: " + nDerivation + " ms");84bSuccess = false;85}8687Thread.sleep(1000);88clip.start(); // start again89Thread.sleep(100);90while(clip.isRunning()); // wait for the sound to finish9192int nEndPos = clip.getFramePosition();93System.out.println(" Position at end: " + nEndPos);94if (nEndPos > clip.getFrameLength()) {95System.out.println(" ERROR(2): end position if out of range");96bSuccess = false;97}9899clip.close();100101return bSuccess;102}103104public static void main(String[] args) throws Exception {105for (int count=1; count <= LOOP_COUNT; count++)106{107System.out.println("loop " + count + "/" + LOOP_COUNT);108if (!test())109{110System.out.println("Test FAILED");111throw new RuntimeException("Test FAILED.");112}113}114115System.out.println("Test passed sucessfully");116}117118private static long currentTimeMillis() {119return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());120}121}122123124