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