Path: blob/master/test/jdk/javax/sound/sampled/Clip/bug6251460.java
41153 views
/*1* Copyright (c) 2005, 2016, 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 javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Clip;26import javax.sound.sampled.DataLine;27import javax.sound.sampled.LineEvent;28import javax.sound.sampled.LineListener;29import javax.sound.sampled.LineUnavailableException;3031/**32* @test33* @bug 6251460 804722234* @requires (os.family == "windows" | os.family == "mac")35* @summary Tests that JavaSound plays short sounds (less then 1 second)36*/37public class bug6251460 {38private static final class MutableBoolean {39public boolean value;4041public MutableBoolean(boolean initialValue) {42value = initialValue;43}44}4546// static helper routines47static long startTime = currentTimeMillis();48static long currentTimeMillis() {49return System.nanoTime() / 1000000L;50}51static void log(String s) {52long time = currentTimeMillis() - startTime;53long ms = time % 1000;54time /= 1000;55long sec = time % 60;56time /= 60;57long min = time % 60;58time /= 60;59System.out.println(""60+ (time < 10 ? "0" : "") + time61+ ":" + (min < 10 ? "0" : "") + min62+ ":" + (sec < 10 ? "0" : "") + sec63+ "." + (ms < 10 ? "00" : (ms < 100 ? "0" : "")) + ms64+ " " + s);65}666768static private int countErrors = 0;69static private final int LOOP_COUNT = 30;7071static AudioFormat format = new AudioFormat(8000, 16, 1, true, false);72// create a 250-ms clip73static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 0.25)];7475static protected void test()76throws LineUnavailableException, InterruptedException {77DataLine.Info info = new DataLine.Info(Clip.class, format);78Clip clip = (Clip)AudioSystem.getLine(info);79final MutableBoolean clipStoppedEvent = new MutableBoolean(false);80clip.addLineListener(new LineListener() {81@Override82public void update(LineEvent event) {83if (event.getType() == LineEvent.Type.STOP) {84synchronized (clipStoppedEvent) {85clipStoppedEvent.value = true;86clipStoppedEvent.notifyAll();87}88}89}90});91clip.open(format, soundData, 0, soundData.length);9293long lengthClip = clip.getMicrosecondLength() / 1000;94log("Clip length " + lengthClip + " ms");95log("Playing...");96for (int i=1; i<=LOOP_COUNT; i++) {97long startTime = currentTimeMillis();98log(" Loop " + i);99clip.start();100101synchronized (clipStoppedEvent) {102while (!clipStoppedEvent.value) {103clipStoppedEvent.wait();104}105clipStoppedEvent.value = false;106}107108long endTime = currentTimeMillis();109long lengthPlayed = endTime - startTime;110111if (lengthClip > lengthPlayed + 20) {112log(" ERR: Looks like sound didn't play: played " + lengthPlayed + " ms instead " + lengthClip);113countErrors++;114} else {115log(" OK: played " + lengthPlayed + " ms");116}117clip.setFramePosition(0);118119}120log("Played " + LOOP_COUNT + " times, " + countErrors + " errors detected.");121}122123public static void main(String[] args) throws InterruptedException {124try {125test();126} catch (LineUnavailableException | IllegalArgumentException127| IllegalStateException ignored) {128System.out.println("Test is not applicable. Automatically passed");129return;130}131if (countErrors > 0) {132throw new RuntimeException(133"Test FAILED: " + countErrors + " error detected (total "134+ LOOP_COUNT + ")");135}136}137}138139140