1 /*
   2  * Copyright (c) 2003, 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 java.io.IOException;
  25 
  26 import javax.sound.sampled.AudioFormat;
  27 import javax.sound.sampled.AudioSystem;
  28 import javax.sound.sampled.Clip;
  29 import javax.sound.sampled.DataLine;
  30 import javax.sound.sampled.LineUnavailableException;
  31 import javax.sound.sampled.Mixer;
  32 
  33 /**
  34  * This is utility class for Test4218609.
  35  */
  36 public class ClickInPlay {
  37 
  38     static int sampleRate = 8000;
  39     static double frequency = 2000.0;
  40     static double RAD = 2.0 * Math.PI;
  41 
  42     static byte[] audioData = new byte[sampleRate/2];
  43     static DataLine.Info info;
  44     static Clip source;
  45 
  46     //static AudioInputStream ais = null;
  47     static AudioFormat audioFormat;
  48     //static String filename;
  49 
  50     public static void print(String s) {
  51         System.out.print(s);
  52     }
  53     public static void println(String s) {
  54         System.out.println(s);
  55     }
  56 
  57     public static void key() {
  58         println("");
  59         print("Press ENTER to continue...");
  60         try {
  61             System.in.read();
  62         } catch (IOException ioe) {
  63         }
  64     }
  65 
  66     public static void play(Mixer mixer) {
  67         int res = 0;
  68         try {
  69             println("Getting clip from mixer...");
  70             source = (Clip) mixer.getLine(info);
  71             println("Opening clip...");
  72             source.open(audioFormat, audioData, 0, audioData.length);
  73             println("Starting clip...");
  74             source.loop(Clip.LOOP_CONTINUOUSLY);
  75             println("Now open your ears:");
  76             println("- if you hear a sine wave playing,");
  77             println("  listen carefully if you can hear clicks.");
  78             println("  If no, the bug is fixed.");
  79             println("- if you don't hear anything, it's possible");
  80             println("  that this mixer is not connected to an ");
  81             println("  amplifier, or that its volume is set to 0");
  82             key();
  83         } catch (IllegalArgumentException iae) {
  84             println("IllegalArgumentException: "+iae.getMessage());
  85             println("Sound device cannot handle this audio format.");
  86             println("ERROR: Test environment not correctly set up.");
  87             if (source!=null) {
  88                 source.close();
  89                 source = null;
  90             }
  91             return;
  92         } catch (LineUnavailableException lue) {
  93             println("LineUnavailableException: "+lue.getMessage());
  94             println("This is normal for some mixers.");
  95         } catch (Exception e) {
  96             println("Unexpected Exception: "+e.toString());
  97         }
  98         if (source != null) {
  99             println("Stopping...");
 100             source.stop();
 101             println("Closing...");
 102             source.close();
 103             println("Closed.");
 104             source = null;
 105         }
 106     }
 107 
 108     public static void main(String[] args) throws Exception {
 109         println("This is an interactive test.");
 110         println("If you can hear clicks during playback in");
 111         println("any mixer, the test is failed.");
 112         println("");
 113         println("Make sure that you have speakers connected");
 114         println("and that the system mixer is not muted.");
 115         println("");
 116         println("Press a key to start the test.");
 117         key();
 118         Mixer.Info[] mixers=null;
 119 
 120             println("   ...using self-generated sine wave for playback");
 121             audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
 122             for (int i=0; i<audioData.length; i++) {
 123                 audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
 124             }
 125         info = new DataLine.Info(Clip.class, audioFormat);
 126 
 127         mixers = AudioSystem.getMixerInfo();
 128         int succMixers = 0;
 129         for (int i=0; i<mixers.length; i++) {
 130             try {
 131                 Mixer mixer = AudioSystem.getMixer(mixers[i]);
 132                 if (mixer.isLineSupported(info)) {
 133                     succMixers++;
 134                     println("  ...using mixer "+mixer.getMixerInfo());
 135                     play(mixer);
 136                 }
 137             } catch (Exception e) {
 138                 e.printStackTrace();
 139             }
 140         }
 141         if (succMixers == 0) {
 142                 println("No mixers available! ");
 143                 println("Cannot run test.");
 144         }
 145     }
 146 
 147 }