Path: blob/master/test/jdk/javax/sound/sampled/Clip/Duration/ClipDuration.java
41159 views
/*1* Copyright (c) 2001, 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 java.io.ByteArrayInputStream;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioInputStream;27import javax.sound.sampled.AudioSystem;28import javax.sound.sampled.Clip;29import javax.sound.sampled.DataLine;30import javax.sound.sampled.LineUnavailableException;31import javax.sound.sampled.Mixer;3233/**34* @test35* @bug 423770336* @summary Check that Clip.getMicrosecondLength() returns correct value.37*/38public class ClipDuration {3940public static int run(Mixer m) {41int res=1; // failed42int frameCount = 441000; // lets say 10 seconds43AudioFormat f = new AudioFormat(44100.0f, 16, 2, true, false);44AudioInputStream audioInputStream =45new AudioInputStream(new ByteArrayInputStream(new byte[frameCount * f.getFrameSize()]),46f, frameCount);47AudioFormat format = audioInputStream.getFormat();48Clip m_clip = null;49try {50if (m == null) {51m_clip = (Clip) AudioSystem.getClip();52} else {53DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED);54m_clip = (Clip) m.getLine(info);55}56System.out.println("Got clip: "+m_clip);57m_clip.open(audioInputStream);58long microseconds=m_clip.getMicrosecondLength();59System.out.println("getFrameLength()="+m_clip.getFrameLength()+" frames");60System.out.println("getMicrosecondLength()="+microseconds+" us");61if (Math.abs(microseconds-10000000)<50) {62System.out.println("->Clip OK");63res=0; // passes if less than 50us error64}65} catch (LineUnavailableException luae) {66System.err.println(luae);67res = 3; // line not available, test not failed68} catch (Throwable t) {69System.out.println("->Exception:"+t);70t.printStackTrace();71res=2; // exception72}73if (m_clip != null) {74m_clip.close();75}76return res;77}78798081public static void main(String[] args) throws Exception {82if (isSoundcardInstalled()) {83int res=3;84res = run(null);85Mixer.Info[] infos = AudioSystem.getMixerInfo();86for (int i = 0; i<infos.length; i++) {87try {88Mixer m = AudioSystem.getMixer(infos[i]);89int r = run(m);90if (r == 1) res = 1;91} catch (Exception e) {92}93}94if (res!=1) {95System.out.println("Test passed");96} else {97if (res==2) {98System.err.println("Test could not execute: test threw unexpected Exception.");99throw new Exception("Test threw exception");100}101else if (res==3) {102System.err.println("Test could not execute: please install an audio device");103return;104}105throw new Exception("Test returned wrong length");106}107}108}109110/**111* Returns true if at least one soundcard is correctly installed112* on the system.113*/114public static boolean isSoundcardInstalled() {115boolean result = false;116try {117Mixer.Info[] mixers = AudioSystem.getMixerInfo();118if (mixers.length > 0) {119result = AudioSystem.getSourceDataLine(null) != null;120}121} catch (Exception e) {122System.err.println("Exception occured: "+e);123}124if (!result) {125System.err.println("Soundcard does not exist or sound drivers not installed!");126System.err.println("This test requires sound drivers for execution.");127}128return result;129}130}131132133