1 /*
   2  * Copyright (c) 2004, 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.DataLine;
  27 import javax.sound.sampled.SourceDataLine;
  28 
  29 /**
  30  * @test
  31  * @bug 5001959
  32  * @summary Short tick sound after finished playing with SourceDataLine
  33  * @run main/manual TickAtEndOfPlay
  34  */
  35 public class TickAtEndOfPlay {
  36 
  37     static boolean WorkAround1 = false;
  38     static boolean WorkAround2 = false;
  39 
  40     public static void main(String[] args) throws Exception {
  41         System.out.println("This test should only be run on Windows.");
  42         System.out.println("Make sure that the speakers are connected and the volume is up.");
  43         System.out.println("Close all other programs that may use the soundcard.");
  44 
  45         System.out.println("You'll hear a 2-second tone. when the tone finishes,");
  46         System.out.println("  there should be no noise. If you hear a short tick/noise,");
  47         System.out.println("  the bug still applies.");
  48 
  49         System.out.println("Press ENTER to continue.");
  50         System.in.read();
  51 
  52         for (int i = 0; i < args.length; i++) {
  53             if (args[i].equals("1")) WorkAround1 = true;
  54             if (args[i].equals("2")) WorkAround2 = true;
  55         }
  56         if (WorkAround1) System.out.println("Using work around1: appending silence");
  57         if (WorkAround2) System.out.println("Using work around2: waiting before close");
  58 
  59         int zerolen = 0; // how many 0-bytes will be appended to playback
  60         if (WorkAround1) zerolen = 1000;
  61         int seconds = 2;
  62         int sampleRate = 8000;
  63         double frequency = 1000.0;
  64         double RAD = 2.0 * Math.PI;
  65         AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true);
  66         System.out.println("Format: "+af);
  67         DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);
  68         SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info);
  69         System.out.println("Line: "+source);
  70         if (source.toString().indexOf("MixerSourceLine")>=0) {
  71             System.out.println("This test only applies to non-Java Sound Audio Engine!");
  72             return;
  73         }
  74         System.out.println("Opening...");
  75         source.open(af);
  76         System.out.println("Starting...");
  77         source.start();
  78         int datalen = sampleRate * seconds;
  79         byte[] buf = new byte[datalen+zerolen];
  80         for (int i=0; i<datalen; i++) {
  81             buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
  82         }
  83         System.out.println("Writing...");
  84         source.write(buf,0,buf.length);
  85         System.out.println("Draining...");
  86         source.drain();
  87         System.out.println("Stopping...");
  88         source.stop();
  89         if (WorkAround2) {
  90             System.out.println("Waiting 200 millis...");
  91             Thread.sleep(200);
  92         }
  93         System.out.println("Closing...");
  94         source.close();
  95         System.out.println("Done.");
  96     }
  97 }