jdk/test/javax/sound/sampled/LinuxBlock/PlaySine.java
author serb
Mon, 31 Oct 2016 14:50:09 +0300
changeset 41905 e8e5df013c6e
permissions -rw-r--r--
8167615: Opensource unit/regression tests for JavaSound Reviewed-by: amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41905
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     1
/*
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     2
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     4
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     8
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    13
 * accompanied this code).
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    14
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    18
 *
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    21
 * questions.
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    22
 */
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    23
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    24
import java.io.File;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    25
import java.io.IOException;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    26
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    27
import javax.sound.sampled.AudioFormat;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    28
import javax.sound.sampled.AudioInputStream;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    29
import javax.sound.sampled.AudioSystem;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    30
import javax.sound.sampled.DataLine;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    31
import javax.sound.sampled.LineUnavailableException;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    32
import javax.sound.sampled.Mixer;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    33
import javax.sound.sampled.SourceDataLine;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    34
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    35
/**
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    36
 * @test
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    37
 * @bug 4834461
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    38
 * @summary Applet hang when you load it during sound card is in use
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    39
 * @run main/manual PlaySine
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    40
 */
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    41
public class PlaySine {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    42
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    43
    static int sampleRate = 8000;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    44
    static double frequency = 2000.0;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    45
    static double RAD = 2.0 * Math.PI;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    46
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    47
    static byte[] audioData = new byte[sampleRate/2];
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    48
    static SourceDataLine source;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    49
    static Mixer mixer = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    50
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    51
    static AudioInputStream ais = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    52
    static AudioFormat audioFormat;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    53
    static String filename;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    54
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    55
    public static void constructAIS() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    56
        try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    57
            ais = AudioSystem.getAudioInputStream(new File(filename));
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    58
        } catch (Exception e) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    59
            println("ERROR: could not open "+filename+": "+e.getMessage());
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    60
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    61
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    62
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    63
    public static void print(String s) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    64
        System.out.print(s);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    65
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    66
    public static void println(String s) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    67
        System.out.println(s);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    68
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    69
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    70
    public static void key() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    71
        println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    72
        print("Press ENTER to continue...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    73
        try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    74
            System.in.read();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    75
        } catch (IOException ioe) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    76
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    77
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    78
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    79
    static int audioLen = -1;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    80
    static int audioOffset = -1;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    81
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    82
    public static void writeData() {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    83
        if (audioLen == -1) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    84
            audioLen = audioData.length;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    85
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    86
        if (audioOffset < 0) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    87
            audioOffset = audioLen;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    88
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    89
        try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    90
            if (audioOffset >= audioLen) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    91
                audioOffset = 0;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    92
                if (ais!=null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    93
                    do {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    94
                        audioLen = ais.read(audioData, 0, audioData.length);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    95
                        if (audioLen < 0) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    96
                            constructAIS();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    97
                        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    98
                    } while (audioLen < 0);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
    99
                }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   100
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   101
            int toWrite = audioLen - audioOffset;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   102
            int written = source.write(audioData, audioOffset, toWrite);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   103
            audioOffset+=written;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   104
        } catch (Exception e) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   105
            e.printStackTrace();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   106
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   107
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   108
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   109
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   110
    public static int play(boolean shouldPlay) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   111
        int res = 0;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   112
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   113
        try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   114
            println("Getting line from mixer...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   115
            source = (SourceDataLine) mixer.getLine(info);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   116
            println("Opening line...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   117
            println("  -- if the program is hanging here, kill the process that has blocks the audio device now.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   118
            source.open(audioFormat);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   119
            println("Starting line...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   120
            source.start();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   121
            println("Writing audio data for 1 second...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   122
            long startTime = System.currentTimeMillis();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   123
            while (System.currentTimeMillis() - startTime < 1000) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   124
                writeData();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   125
                Thread.sleep(100);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   126
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   127
            res = 1;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   128
        } catch (IllegalArgumentException iae) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   129
            println("IllegalArgumentException: "+iae.getMessage());
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   130
            println("Sound device cannot handle this audio format.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   131
            println("ERROR: Test environment not correctly set up.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   132
            if (source!=null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   133
                source.close();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   134
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   135
            return 3;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   136
        } catch (LineUnavailableException lue) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   137
            println("LineUnavailableException: "+lue.getMessage());
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   138
            if (shouldPlay) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   139
                println("ERROR: the line should be available now!.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   140
                println("       Verify that you killed the other audio process.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   141
            } else {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   142
                println("Correct behavior! the bug is fixed.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   143
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   144
            res = 2;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   145
        } catch (Exception e) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   146
            println("Unexpected Exception: "+e.toString());
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   147
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   148
        if (source != null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   149
            println("Draining...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   150
            try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   151
                source.drain();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   152
            } catch (NullPointerException npe) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   153
                println("(NullPointerException: bug fixed in J2SE 1.4.2");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   154
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   155
            println("Stopping...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   156
            source.stop();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   157
            println("Closing...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   158
            source.close();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   159
            source = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   160
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   161
        return res;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   162
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   163
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   164
    public static void main(String[] args) throws Exception {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   165
        println("This is an interactive test. You can run it with a filename as argument");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   166
        println("It is only meant to be run on linux, with the (old) OSS kernel drivers (/dev/dsp)");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   167
        println("This test should not be run on systems with ALSA installed, or kernel 2.6 or higher.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   168
        println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   169
        println("The test verifies that Java Sound fails correctly if another process is blocking");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   170
        println("the audio device.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   171
        println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   172
        println("Checking sanity...");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   173
        Mixer.Info[] mixers=null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   174
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   175
        mixers = AudioSystem.getMixerInfo();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   176
        for (int i=0; i<mixers.length; i++) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   177
            try {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   178
                Mixer thisMixer = AudioSystem.getMixer(mixers[i]);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   179
                String mixerName = thisMixer.getMixerInfo().getName();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   180
                if (mixerName.indexOf("Java Sound")>=0
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   181
                    && mixerName.indexOf("Engine")>=0) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   182
                    mixer = thisMixer;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   183
                    break;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   184
                }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   185
            } catch (Exception e) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   186
                e.printStackTrace();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   187
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   188
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   189
        if (mixer == null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   190
            if (mixers.length==0) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   191
                System.out.println("ERROR: No mixers available!");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   192
            } else {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   193
                println("ERROR: Java Sound Engine could not be found.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   194
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   195
            println("Cannot run this test.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   196
            return;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   197
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   198
        println("  ...using mixer "+mixer.getMixerInfo());
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   199
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   200
        String osname = System.getProperty("os.name");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   201
        if ((osname == null) || (osname.toLowerCase().indexOf("linux")<0)) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   202
            println("ERROR: not running on linux (you are running on "+osname+")");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   203
            return;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   204
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   205
        println("  ...running on "+osname);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   206
        println("  ...sanity test OK.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   207
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   208
        filename = null;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   209
        if (args.length>0) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   210
            File f = new File(args[0]);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   211
            if (f.exists()) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   212
                filename = args[0];
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   213
                println("Opening "+filename);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   214
                constructAIS();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   215
                if (ais!=null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   216
                    audioFormat = ais.getFormat();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   217
                }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   218
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   219
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   220
        if (ais == null) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   221
            println("Using self-generated sine wave for playback");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   222
            audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   223
            for (int i=0; i<audioData.length; i++) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   224
                audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   225
            }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   226
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   227
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   228
        println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   229
        println("Now, on a second console, run the following command:");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   230
        println("    cat - < /dev/zero > /dev/dsp");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   231
        key();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   232
        println("After you press ENTER now, the mixer will be opened.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   233
        println("There are 3 possible cases that can occur:");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   234
        println("1) you'll hear a sine wave");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   235
        println("   -> you are running with mixing OSS drivers. ");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   236
        println("      Some soundcards only provide mixing OSS drivers.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   237
        println("      Test environment not valid. ");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   238
        println("      Repeat on another machine where you can reproduce the bug first.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   239
        println("2) this program stops doing anything after 'Opening line...'");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   240
        println("   -> this is the bug.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   241
        println("      Kill the command on the other console with Ctrl-C, this program");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   242
        println("      should continue working then.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   243
        println("3) this program reports a LineUnavailableException");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   244
        println("   -> bug is fixed.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   245
        println("      OR you run with non-blocking OSS drivers.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   246
        println("      make sure that you can reproduce this bug first with e.g. J2SE 1.4.1");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   247
        key();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   248
        int playedFirst = play(false);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   249
        int playedSecond = 0;
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   250
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   251
        if (playedFirst == 2) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   252
            println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   253
            println("Now kill the other process with Ctrl-C.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   254
            println("After that, this program should be able to play ");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   255
            println("the sine wave without problems.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   256
            key();
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   257
            playedSecond = play(true);
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   258
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   259
        println("");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   260
        if (playedFirst == 1) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   261
            println("Test FAILED.");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   262
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   263
        else if (playedFirst == 2 && playedSecond == 1) {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   264
            println("Test SUCCESSFUL");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   265
        } else {
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   266
            println("Test not failed (but not successful either...).");
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   267
        }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   268
    }
e8e5df013c6e 8167615: Opensource unit/regression tests for JavaSound
serb
parents:
diff changeset
   269
}