Path: blob/master/test/jdk/javax/sound/sampled/Lines/ClickInPlay/ClickInPlay.java
41155 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioSystem;27import javax.sound.sampled.Clip;28import javax.sound.sampled.DataLine;29import javax.sound.sampled.LineUnavailableException;30import javax.sound.sampled.Mixer;3132/**33* This is utility class for Test4218609.34*/35public class ClickInPlay {3637static int sampleRate = 8000;38static double frequency = 2000.0;39static double RAD = 2.0 * Math.PI;4041static byte[] audioData = new byte[sampleRate/2];42static DataLine.Info info;43static Clip source;4445//static AudioInputStream ais = null;46static AudioFormat audioFormat;47//static String filename;4849public static void print(String s) {50System.out.print(s);51}52public static void println(String s) {53System.out.println(s);54}5556public static void key() {57println("");58print("Press ENTER to continue...");59try {60System.in.read();61} catch (IOException ioe) {62}63}6465public static void play(Mixer mixer) {66int res = 0;67try {68println("Getting clip from mixer...");69source = (Clip) mixer.getLine(info);70println("Opening clip...");71source.open(audioFormat, audioData, 0, audioData.length);72println("Starting clip...");73source.loop(Clip.LOOP_CONTINUOUSLY);74println("Now open your ears:");75println("- if you hear a sine wave playing,");76println(" listen carefully if you can hear clicks.");77println(" If no, the bug is fixed.");78println("- if you don't hear anything, it's possible");79println(" that this mixer is not connected to an ");80println(" amplifier, or that its volume is set to 0");81key();82} catch (IllegalArgumentException iae) {83println("IllegalArgumentException: "+iae.getMessage());84println("Sound device cannot handle this audio format.");85println("ERROR: Test environment not correctly set up.");86if (source!=null) {87source.close();88source = null;89}90return;91} catch (LineUnavailableException lue) {92println("LineUnavailableException: "+lue.getMessage());93println("This is normal for some mixers.");94} catch (Exception e) {95println("Unexpected Exception: "+e.toString());96}97if (source != null) {98println("Stopping...");99source.stop();100println("Closing...");101source.close();102println("Closed.");103source = null;104}105}106107public static void main(String[] args) throws Exception {108println("This is an interactive test.");109println("If you can hear clicks during playback in");110println("any mixer, the test is failed.");111println("");112println("Make sure that you have speakers connected");113println("and that the system mixer is not muted.");114println("");115println("Press a key to start the test.");116key();117Mixer.Info[] mixers=null;118119println(" ...using self-generated sine wave for playback");120audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);121for (int i=0; i<audioData.length; i++) {122audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);123}124info = new DataLine.Info(Clip.class, audioFormat);125126mixers = AudioSystem.getMixerInfo();127int succMixers = 0;128for (int i=0; i<mixers.length; i++) {129try {130Mixer mixer = AudioSystem.getMixer(mixers[i]);131if (mixer.isLineSupported(info)) {132succMixers++;133println(" ...using mixer "+mixer.getMixerInfo());134play(mixer);135}136} catch (Exception e) {137e.printStackTrace();138}139}140if (succMixers == 0) {141println("No mixers available! ");142println("Cannot run test.");143}144}145146}147148149