1 /*
   2  * Copyright (c) 2011, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /**
  27  * @test
  28  * @bug 6801206
  29  * @summary Tests that Clip sets frame position
  30  * @author Alex Menkov
  31  */
  32 
  33 import javax.sound.sampled.AudioFormat;
  34 import javax.sound.sampled.AudioSystem;
  35 import javax.sound.sampled.Clip;
  36 import javax.sound.sampled.DataLine;
  37 import javax.sound.sampled.LineUnavailableException;
  38 
  39 public class ClipSetPos {
  40 
  41     final static AudioFormat audioFormat = new AudioFormat(44100f, 16, 2, true, true);
  42     final static int frameLength = 44100 * 2; // 2 seconds
  43     final static byte[] dataBuffer
  44             = new byte[frameLength * (audioFormat.getSampleSizeInBits()/8)
  45                        * audioFormat.getChannels()];
  46     final static int MAX_FRAME_DELTA = 20;
  47 
  48     public static void main(String[] args) {
  49         boolean testPassed = true;
  50         Clip clip = null;
  51         try {
  52             clip = (Clip)AudioSystem.getLine(new DataLine.Info(Clip.class, audioFormat));
  53             clip.open(audioFormat, dataBuffer, 0, dataBuffer.length);
  54         } catch (LineUnavailableException ex) {
  55             log(ex);
  56             log("Cannot test (this is not failure)");
  57             return;
  58         } catch (IllegalArgumentException ex) {
  59             log(ex);
  60             log("Cannot test (this is not failure)");
  61             return;
  62         }
  63 
  64         log("clip: " + clip.getClass().getName());
  65 
  66         int len = clip.getFrameLength();
  67         for (int pos=0; pos < len; pos += (len /100)) {
  68             clip.setFramePosition(pos);
  69             int curPos = clip.getFramePosition();
  70             if (Math.abs(pos - curPos) > MAX_FRAME_DELTA) {
  71                 log("Tried to set pos to " + pos + ", but got back " + curPos);
  72                 testPassed = false;
  73             } else {
  74                 log("Sucessfully set pos to " + pos);
  75             }
  76         }
  77         clip.close();
  78 
  79         if (testPassed) {
  80             log("Test PASSED.");
  81         } else {
  82             log("Test FAILED.");
  83             throw new RuntimeException("Test FAILED (see log)");
  84         }
  85     }
  86 
  87     static void log(String s) {
  88         System.out.println(s);
  89     }
  90 
  91     static void log(Exception ex) {
  92         ex.printStackTrace(System.out);
  93     }
  94 
  95 }