Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/sampled/Clip/bug5070081.java
38855 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;2930/*31* @test32* @bug 507008133* @summary Tests that javax.sound.sampled.Clip does not loses position through34* stop/start35* @key headful36*/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 = (Clip)AudioSystem.getLine(info);48clip.open(format, soundData, 0, soundData.length);4950boolean bSuccess = true;5152long nLengthMS = clip.getMicrosecondLength()/1000;5354System.out.println(" Clip length:");55System.out.println(" frames: " + clip.getFrameLength());56System.out.println(" seconds: " + nLengthMS/1000.0);5758clip.start(); // start playing59Thread.sleep(1000); // wait a sec60long time1 = currentTimeMillis();61long pos1 = clip.getFramePosition(); // store the position62clip.stop(); // and then stop63long pos2 = clip.getFramePosition(); // 2nd try64long time2 = currentTimeMillis();6566System.out.println(" Position before stop: " + pos1);67System.out.println(" Position after stop: " + pos2);6869long timeDiff = Math.abs(time2 - time1);70// sample rate is 22050 per second, so 22.05 per ms71long posDiff = (long) (Math.abs(pos2 - pos1) / 22.05);72System.out.println(" d(time): " + timeDiff + " ms;"73+ "d(clip pos time): " + posDiff + " ms.");7475long nDerivation = posDiff - timeDiff;76// add 50 ms for deviation (delay for stopping and errors due timer precision)77if (nDerivation > 50) {78System.out.println(" ERROR(1): The deviation is too much: " + nDerivation + " ms");79bSuccess = false;80}8182Thread.sleep(1000);83clip.start(); // start again84Thread.sleep(100);85while(clip.isRunning()); // wait for the sound to finish8687int nEndPos = clip.getFramePosition();88System.out.println(" Position at end: " + nEndPos);89if (nEndPos > clip.getFrameLength()) {90System.out.println(" ERROR(2): end position if out of range");91bSuccess = false;92}9394clip.close();9596return bSuccess;97}9899public static void main(String[] args) throws Exception {100for (int count=1; count <= LOOP_COUNT; count++)101{102System.out.println("loop " + count + "/" + LOOP_COUNT);103if (!test())104{105System.out.println("Test FAILED");106throw new RuntimeException("Test FAILED.");107}108}109110System.out.println("Test passed sucessfully");111}112113private static long currentTimeMillis() {114return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());115}116}117118119