Path: blob/master/test/jdk/javax/sound/sampled/Clip/ClipSetPos.java
41153 views
/*1* Copyright (c) 2011, 2013, 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*/2223/**24* @test25* @bug 680120626* @summary Tests that Clip sets frame position27* @author Alex Menkov28*/2930import javax.sound.sampled.AudioFormat;31import javax.sound.sampled.AudioSystem;32import javax.sound.sampled.Clip;33import javax.sound.sampled.DataLine;34import javax.sound.sampled.LineUnavailableException;3536public class ClipSetPos {3738final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);39final static int frameLength = 44100 * 2; // 2 seconds40final static byte[] dataBuffer41= new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)42* audioFormat.getChannels()];43final static int MAX_FRAME_DELTA = 20;4445public static void main(String[] args) {46boolean testPassed = true;47Clip clip = null;48try {49clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));50clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);51} catch (LineUnavailableException ex) {52log(ex);53log("Cannot test (this is not failure)");54return;55} catch (IllegalArgumentException ex) {56log(ex);57log("Cannot test (this is not failure)");58return;59}6061log("clip: " + clip.getClass().getName());6263int len = clip.getFrameLength();64for (int pos=0; pos < len; pos += (len /100)) {65clip.setFramePosition(pos);66int curPos = clip.getFramePosition();67if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {68log("Tried to set pos to " + pos + ", but got back " + curPos);69testPassed = false;70} else {71log("Sucessfully set pos to " + pos);72}73}74clip.close();7576if (testPassed) {77log("Test PASSED.");78} else {79log("Test FAILED.");80throw new RuntimeException("Test FAILED (see log)");81}82}8384static void log(String s) {85System.out.println(s);86}8788static void log(Exception ex) {89ex.printStackTrace(System.out);90}9192}939495