Path: blob/master/test/jdk/javax/sound/sampled/DirectAudio/TickAtEndOfPlay.java
41153 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 javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.DataLine;26import javax.sound.sampled.SourceDataLine;2728/**29* @test30* @bug 500195931* @summary Short tick sound after finished playing with SourceDataLine32* @run main/manual TickAtEndOfPlay33*/34public class TickAtEndOfPlay {3536static boolean WorkAround1 = false;37static boolean WorkAround2 = false;3839public static void main(String[] args) throws Exception {40System.out.println("This test should only be run on Windows.");41System.out.println("Make sure that the speakers are connected and the volume is up.");42System.out.println("Close all other programs that may use the soundcard.");4344System.out.println("You'll hear a 2-second tone. when the tone finishes,");45System.out.println(" there should be no noise. If you hear a short tick/noise,");46System.out.println(" the bug still applies.");4748System.out.println("Press ENTER to continue.");49System.in.read();5051for (int i = 0; i < args.length; i++) {52if (args[i].equals("1")) WorkAround1 = true;53if (args[i].equals("2")) WorkAround2 = true;54}55if (WorkAround1) System.out.println("Using work around1: appending silence");56if (WorkAround2) System.out.println("Using work around2: waiting before close");5758int zerolen = 0; // how many 0-bytes will be appended to playback59if (WorkAround1) zerolen = 1000;60int seconds = 2;61int sampleRate = 8000;62double frequency = 1000.0;63double RAD = 2.0 * Math.PI;64AudioFormat af = new AudioFormat((float)sampleRate,8,1,true,true);65System.out.println("Format: "+af);66DataLine.Info info = new DataLine.Info(SourceDataLine.class,af);67SourceDataLine source = (SourceDataLine)AudioSystem.getLine(info);68System.out.println("Line: "+source);69if (source.toString().indexOf("MixerSourceLine")>=0) {70System.out.println("This test only applies to non-Java Sound Audio Engine!");71return;72}73System.out.println("Opening...");74source.open(af);75System.out.println("Starting...");76source.start();77int datalen = sampleRate * seconds;78byte[] buf = new byte[datalen+zerolen];79for (int i=0; i<datalen; i++) {80buf[i] = (byte)(Math.sin(RAD*frequency/sampleRate*i)*127.0);81}82System.out.println("Writing...");83source.write(buf,0,buf.length);84System.out.println("Draining...");85source.drain();86System.out.println("Stopping...");87source.stop();88if (WorkAround2) {89System.out.println("Waiting 200 millis...");90Thread.sleep(200);91}92System.out.println("Closing...");93source.close();94System.out.println("Done.");95}96}979899