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.DataLine;
  29 import javax.sound.sampled.LineUnavailableException;
  30 import javax.sound.sampled.Mixer;
  31 import javax.sound.sampled.SourceDataLine;
  32 
  33 /**
  34  * This is utility class for Test5032020.
  35  */
  36 public class DirectSoundUnderrunSilence {
  37 
  38     static int sampleRate = 8000;
  39     static double frequency = 1000.0;
  40     static double RAD = 2.0 * Math.PI;
  41 
  42     static byte[] audioData = new byte[sampleRate/8];
  43     static DataLine.Info info;
  44     static SourceDataLine 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         println("");
  65     }
  66 
  67     public static void play(Mixer mixer) {
  68         int res = 0;
  69         try {
  70             println("Getting SDL from mixer...");
  71             source = (SourceDataLine) mixer.getLine(info);
  72             println("Opening SDL...");
  73             source.open(audioFormat);
  74             println("Writing data to SDL...");
  75             source.write(audioData, 0, audioData.length);
  76             println("Starting SDL...");
  77             source.start();
  78             println("Now open your ears:");
  79             println("You should have heard a short tone,");
  80             println("followed by silence (no repeating tones).");
  81             key();
  82             source.write(audioData, 0, audioData.length);
  83             println("Now you should have heard another short tone.");
  84             println("If you did not hear a second tone, or more than 2 tones,");
  85             println("the test is FAILED.");
  86             println("otherwise, if you heard a total of 2 tones, the bug is fixed.");
  87             key();
  88         } catch (IllegalArgumentException iae) {
  89             println("IllegalArgumentException: "+iae.getMessage());
  90             println("Sound device cannot handle this audio format.");
  91             println("ERROR: Test environment not correctly set up.");
  92             if (source!=null) {
  93                 source.close();
  94                 source = null;
  95             }
  96             return;
  97         } catch (LineUnavailableException lue) {
  98             println("LineUnavailableException: "+lue.getMessage());
  99             println("This is normal for some mixers.");
 100         } catch (Exception e) {
 101             println("Unexpected Exception: "+e.toString());
 102         }
 103         if (source != null) {
 104             println("Stopping...");
 105             source.stop();
 106             println("Closing...");
 107             source.close();
 108             println("Closed.");
 109             source = null;
 110         }
 111     }
 112 
 113     public static void main(String[] args) throws Exception {
 114         println("This is an interactive test for DirectAudio.");
 115         println("If it's impossible to play data after an underun, the test fails.");
 116         println("");
 117         println("Make sure that you have speakers connected");
 118         println("and that the system mixer is not muted.");
 119         println("Also stop all other programs playing sounds:");
 120         println("It has been seen that it alters the results.");
 121         println("");
 122         println("Press a key to start the test.");
 123         key();
 124         Mixer.Info[] mixers=null;
 125 
 126         println("   ...using self-generated sine wave for playback");
 127         audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
 128         for (int i=0; i<audioData.length; i++) {
 129             audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
 130         }
 131         info = new DataLine.Info(SourceDataLine.class, audioFormat);
 132 
 133         mixers = AudioSystem.getMixerInfo();
 134         int succMixers = 0;
 135         for (int i=0; i<mixers.length; i++) {
 136             println(""+mixers[i]+":");
 137             if ((mixers[i].getName()+mixers[i].getDescription()+mixers[i].getVendor()).indexOf("Direct") < 0) {
 138                 println("  ->not a DirectAudio Mixer!");
 139             } else {
 140                 try {
 141                     Mixer mixer = AudioSystem.getMixer(mixers[i]);
 142                     if (!mixer.isLineSupported(info)) {
 143                         println("  ->doesn't support SourceDataLine!");
 144                     } else {
 145                         succMixers++;
 146                         println("  -> is getting tested.");
 147                         play(mixer);
 148                     }
 149                 } catch (Exception e) {
 150                     println("  -> Exception occured: "+e);
 151                     e.printStackTrace();
 152                 }
 153             }
 154         }
 155         if (succMixers == 0) {
 156             println("No DirectAudio mixers available! ");
 157             println("Cannot run test.");
 158         }
 159     }
 160 
 161 }