jdk/test/javax/sound/sampled/Clip/bug5070081.java
changeset 44647 c87e29c083eb
parent 41905 e8e5df013c6e
child 44656 72decb1e4935
equal deleted inserted replaced
44646:df364d57eeb2 44647:c87e29c083eb
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    18  *
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
       
    23 
       
    24 import java.util.concurrent.TimeUnit;
    23 
    25 
    24 import javax.sound.sampled.AudioFormat;
    26 import javax.sound.sampled.AudioFormat;
    25 import javax.sound.sampled.AudioSystem;
    27 import javax.sound.sampled.AudioSystem;
    26 import javax.sound.sampled.Clip;
    28 import javax.sound.sampled.Clip;
    27 import javax.sound.sampled.DataLine;
    29 import javax.sound.sampled.DataLine;
    54         System.out.println("    frames: " + clip.getFrameLength());
    56         System.out.println("    frames: " + clip.getFrameLength());
    55         System.out.println("    seconds: " + nLengthMS/1000.0);
    57         System.out.println("    seconds: " + nLengthMS/1000.0);
    56 
    58 
    57         clip.start();                               // start playing
    59         clip.start();                               // start playing
    58         Thread.sleep(1000);                         // wait a sec
    60         Thread.sleep(1000);                         // wait a sec
    59         long time1 = System.currentTimeMillis();
    61         long time1 = currentTimeMillis();
    60         long pos1 = clip.getFramePosition();        // store the position
    62         long pos1 = clip.getFramePosition();        // store the position
    61         System.out.println("  Position before stop: " + pos1);
       
    62         clip.stop();                                // and then stop
    63         clip.stop();                                // and then stop
    63         long pos2 = clip.getFramePosition();        // 2nd try
    64         long pos2 = clip.getFramePosition();        // 2nd try
    64         long time2 = System.currentTimeMillis();
    65         long time2 = currentTimeMillis();
       
    66 
       
    67         System.out.println("  Position before stop: " + pos1);
    65         System.out.println("  Position after stop: " + pos2);
    68         System.out.println("  Position after stop: " + pos2);
    66 
    69 
    67         System.out.println("  d(time): " + Math.abs(time2-time1) + " ms;"
    70         long timeDiff = Math.abs(time2 - time1);
    68                 + "d(clip pos): " + Math.abs(pos2 - pos1) + " ms.");
    71         // sample rate is 22050 per second, so 22.05 per ms
       
    72         long posDiff = (long) (Math.abs(pos2 - pos1) / 22.05);
       
    73         System.out.println("  d(time): " + timeDiff + " ms;"
       
    74                 + "d(clip pos time): " + posDiff + " ms.");
    69 
    75 
    70         long nDerivation = Math.abs(pos2 - pos1) - Math.abs(time2-time1);
    76         long nDerivation = posDiff - timeDiff;
    71         // add 50 ms for deviation (delay for stopping and errors due timer precision)
    77         // add 50 ms for deviation (delay for stopping and errors due timer precision)
    72         if (nDerivation > 50) {
    78         if (nDerivation > 50) {
    73             System.out.println("  ERROR(1): The deviation is too much: " + nDerivation + " ms");
    79             System.out.println("  ERROR(1): The deviation is too much: " + nDerivation + " ms");
    74             bSuccess = false;
    80             bSuccess = false;
    75         }
    81         }
   102             }
   108             }
   103         }
   109         }
   104 
   110 
   105         System.out.println("Test passed sucessfully");
   111         System.out.println("Test passed sucessfully");
   106     }
   112     }
       
   113 
       
   114     private static long currentTimeMillis() {
       
   115         return TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
       
   116     }
   107 }
   117 }