Path: blob/master/test/jdk/javax/sound/sampled/Mixers/DirectSoundRepeatingBuffer/DirectSoundRepeatingBuffer.java
41161 views
/*1* Copyright (c) 2004, 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 Test4997635.34*/35public class DirectSoundRepeatingBuffer {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.");80println("- if after a while you hear repeated tones,");81println(" the bug is NOT fixed.");82println("- if the program remains silent after the ");83println(" initial tone, the bug is fixed.");84key();85} catch (IllegalArgumentException iae) {86println("IllegalArgumentException: "+iae.getMessage());87println("Sound device cannot handle this audio format.");88println("ERROR: Test environment not correctly set up.");89if (source!=null) {90source.close();91source = null;92}93return;94} catch (LineUnavailableException lue) {95println("LineUnavailableException: "+lue.getMessage());96println("This is normal for some mixers.");97} catch (Exception e) {98println("Unexpected Exception: "+e.toString());99}100if (source != null) {101println("Stopping...");102source.stop();103println("Closing...");104source.close();105println("Closed.");106source = null;107}108}109110public static void main(String[] args) throws Exception {111println("This is an interactive test for DirectAudio.");112println("If the tone repeats, the test is failed.");113println("");114println("Make sure that you have speakers connected");115println("and that the system mixer is not muted.");116println("");117println("Press a key to start the test.");118key();119Mixer.Info[] mixers=null;120121println(" ...using self-generated sine wave for playback");122audioFormat = new AudioFormat((float)sampleRate, 8, 1, true, true);123for (int i=0; i<audioData.length; i++) {124audioData[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);125}126info = new DataLine.Info(SourceDataLine.class, audioFormat);127128mixers = AudioSystem.getMixerInfo();129int succMixers = 0;130for (int i=0; i<mixers.length; i++) {131println(""+mixers[i]+":");132if ((mixers[i].getName()+mixers[i].getDescription()+mixers[i].getVendor()).indexOf("Direct") < 0) {133println(" ->not a DirectAudio Mixer!");134} else {135try {136Mixer mixer = AudioSystem.getMixer(mixers[i]);137if (!mixer.isLineSupported(info)) {138println(" ->doesn't support SourceDataLine!");139} else {140succMixers++;141println(" -> is getting tested.");142play(mixer);143}144} catch (Exception e) {145println(" -> Exception occured: "+e);146e.printStackTrace();147}148}149}150if (succMixers == 0) {151println("No DirectAudio mixers available! ");152println("Cannot run test.");153}154}155}156157158