jdk/test/javax/sound/sampled/Clip/bug5070081.java
changeset 41905 e8e5df013c6e
child 44647 c87e29c083eb
equal deleted inserted replaced
41904:524d908e49ea 41905:e8e5df013c6e
       
     1 /*
       
     2  * Copyright (c) 2005, 2016, 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 import javax.sound.sampled.AudioFormat;
       
    25 import javax.sound.sampled.AudioSystem;
       
    26 import javax.sound.sampled.Clip;
       
    27 import javax.sound.sampled.DataLine;
       
    28 
       
    29 /*
       
    30  * @test
       
    31  * @bug 5070081
       
    32  * @summary Tests that javax.sound.sampled.Clip does not loses position through
       
    33  *          stop/start
       
    34  * @key headful
       
    35  */
       
    36 public class bug5070081 {
       
    37 
       
    38     static AudioFormat format = new AudioFormat(22050, 8, 1, false, false);
       
    39     // create a 3-second file
       
    40     static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 3)];
       
    41 
       
    42     static final int LOOP_COUNT = 5;
       
    43 
       
    44     static boolean test() throws Exception {
       
    45         DataLine.Info info = new DataLine.Info(Clip.class, format);
       
    46         Clip clip = (Clip)AudioSystem.getLine(info);
       
    47         clip.open(format, soundData, 0, soundData.length);
       
    48 
       
    49         boolean bSuccess = true;
       
    50 
       
    51         long nLengthMS = clip.getMicrosecondLength()/1000;
       
    52 
       
    53         System.out.println("  Clip length:");
       
    54         System.out.println("    frames: " + clip.getFrameLength());
       
    55         System.out.println("    seconds: " + nLengthMS/1000.0);
       
    56 
       
    57         clip.start();                               // start playing
       
    58         Thread.sleep(1000);                         // wait a sec
       
    59         long time1 = System.currentTimeMillis();
       
    60         long pos1 = clip.getFramePosition();        // store the position
       
    61         System.out.println("  Position before stop: " + pos1);
       
    62         clip.stop();                                // and then stop
       
    63         long pos2 = clip.getFramePosition();        // 2nd try
       
    64         long time2 = System.currentTimeMillis();
       
    65         System.out.println("  Position after stop: " + pos2);
       
    66 
       
    67         System.out.println("  d(time): " + Math.abs(time2-time1) + " ms;"
       
    68                 + "d(clip pos): " + Math.abs(pos2 - pos1) + " ms.");
       
    69 
       
    70         long nDerivation = Math.abs(pos2 - pos1) - Math.abs(time2-time1);
       
    71         // add 50 ms for deviation (delay for stopping and errors due timer precision)
       
    72         if (nDerivation > 50) {
       
    73             System.out.println("  ERROR(1): The deviation is too much: " + nDerivation + " ms");
       
    74             bSuccess = false;
       
    75         }
       
    76 
       
    77         Thread.sleep(1000);
       
    78         clip.start();                               // start again
       
    79         Thread.sleep(100);
       
    80         while(clip.isRunning());                    // wait for the sound to finish
       
    81 
       
    82         int nEndPos = clip.getFramePosition();
       
    83         System.out.println("  Position at end: " + nEndPos);
       
    84         if (nEndPos > clip.getFrameLength()) {
       
    85             System.out.println("  ERROR(2): end position if out of range");
       
    86             bSuccess = false;
       
    87         }
       
    88 
       
    89         clip.close();
       
    90 
       
    91         return bSuccess;
       
    92     }
       
    93 
       
    94     public static void main(String[] args) throws Exception {
       
    95         for (int count=1; count <= LOOP_COUNT; count++)
       
    96         {
       
    97             System.out.println("loop " + count + "/" + LOOP_COUNT);
       
    98             if (!test())
       
    99             {
       
   100                 System.out.println("Test FAILED");
       
   101                 throw new RuntimeException("Test FAILED.");
       
   102             }
       
   103         }
       
   104 
       
   105         System.out.println("Test passed sucessfully");
       
   106     }
       
   107 }