ClipSetPos.java revision 8729:0242fce0f717
175374Sbp/*
275374Sbp * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
375374Sbp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
475374Sbp *
5139825Simp * This code is free software; you can redistribute it and/or modify it
6139825Simp * under the terms of the GNU General Public License version 2 only, as
775374Sbp * published by the Free Software Foundation.
875374Sbp *
975374Sbp * This code is distributed in the hope that it will be useful, but WITHOUT
1075374Sbp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1175374Sbp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1275374Sbp * version 2 for more details (a copy is included in the LICENSE file that
1375374Sbp * accompanied this code).
1475374Sbp *
1575374Sbp * You should have received a copy of the GNU General Public License version
1675374Sbp * 2 along with this work; if not, write to the Free Software Foundation,
1775374Sbp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1875374Sbp *
1975374Sbp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2075374Sbp * or visit www.oracle.com if you need additional information or have any
2175374Sbp * questions.
2275374Sbp */
2375374Sbp
2475374Sbp/**
2575374Sbp * @test
2675374Sbp * @bug 6801206
2775374Sbp * @summary Tests that Clip sets frame position
2875374Sbp * @author Alex Menkov
2975374Sbp */
3075374Sbp
3175374Sbpimport javax.sound.sampled.AudioFormat;
3275374Sbpimport javax.sound.sampled.AudioSystem;
3375374Sbpimport javax.sound.sampled.Clip;
3475374Sbpimport javax.sound.sampled.DataLine;
3575374Sbpimport javax.sound.sampled.LineUnavailableException;
3675374Sbp
3775374Sbppublic class ClipSetPos {
3875374Sbp
3975374Sbp    final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
4075374Sbp    final static int frameLength = 44100 * 2; // 2 seconds
4175374Sbp    final static byte[] dataBuffer
42300773Scem            = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
43300774Scem                       * audioFormat.getChannels()];
44300774Scem    final static int MAX_FRAME_DELTA = 20;
45300774Scem
46300774Scem    public static void main(String[] args) {
47300774Scem        boolean testPassed = true;
4875374Sbp        Clip clip = null;
4975374Sbp        try {
5075374Sbp            clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
51            clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
52        } catch (LineUnavailableException ex) {
53            log(ex);
54            log("Cannot test (this is not failure)");
55            return;
56        } catch (IllegalArgumentException ex) {
57            log(ex);
58            log("Cannot test (this is not failure)");
59            return;
60        }
61
62        log("clip: " + clip.getClass().getName());
63
64        int len = clip.getFrameLength();
65        for (int pos=0; pos < len; pos += (len /100)) {
66            clip.setFramePosition(pos);
67            int curPos = clip.getFramePosition();
68            if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {
69                log("Tried to set pos to " + pos + ", but got back " + curPos);
70                testPassed = false;
71            } else {
72                log("Sucessfully set pos to " + pos);
73            }
74        }
75        clip.close();
76
77        if (testPassed) {
78            log("Test PASSED.");
79        } else {
80            log("Test FAILED.");
81            throw new RuntimeException("Test FAILED (see log)");
82        }
83    }
84
85    static void log(String s) {
86        System.out.println(s);
87    }
88
89    static void log(Exception ex) {
90        ex.printStackTrace(System.out);
91    }
92
93}
94