1/*
2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 6801206
27 * @summary Tests that Clip sets frame position
28 * @author Alex Menkov
29 */
30
31import javax.sound.sampled.AudioFormat;
32import javax.sound.sampled.AudioSystem;
33import javax.sound.sampled.Clip;
34import javax.sound.sampled.DataLine;
35import javax.sound.sampled.LineUnavailableException;
36
37public class ClipSetPos {
38
39    final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
40    final static int frameLength = 44100 * 2; // 2 seconds
41    final static byte[] dataBuffer
42            = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
43                       * audioFormat.getChannels()];
44    final static int MAX_FRAME_DELTA = 20;
45
46    public static void main(String[] args) {
47        boolean testPassed = true;
48        Clip clip = null;
49        try {
50            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