Path: blob/master/test/jdk/javax/sound/sampled/DataLine/LineDefFormat.java
41152 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.Clip;26import javax.sound.sampled.DataLine;27import javax.sound.sampled.Mixer;28import javax.sound.sampled.SourceDataLine;29import javax.sound.sampled.TargetDataLine;3031/**32* @test33* @bug 505338034* @summary Verify that getting a line initializes it with the format in35* DataLine.Info36*/37public class LineDefFormat {3839final static int samplerate = 22050;40static int passed = 0;41static int failed = 0;4243private static void doLine1(DataLine line, AudioFormat format) {44try {45System.out.println(" - got line: "+line);46System.out.println(" - line has format: "+line.getFormat());47if (!line.getFormat().matches(format)) {48System.out.println(" ## Error: expected this format: "+format);49failed++;50} else {51passed++;52}53} catch (Throwable t) {54System.out.println(" - Caught exception. Not failed.");55System.out.println(" - "+t.toString());56}57}5859private static void doLine2(DataLine line, AudioFormat format) {60try {61System.out.println(" - call to open()");62line.open();63try {64System.out.println(" - line has format: "+line.getFormat());65if (!line.getFormat().matches(format)) {66System.out.println("## Error: expected this format: "+format);67failed++;68} else {69passed++;70}71} finally {72line.close();73System.out.println(" - closed");74}75} catch (Throwable t) {76System.out.println(" - Caught exception. Not failed.");77System.out.println(" - "+t.toString());78}79}8081private static void doMixerClip(Mixer mixer, AudioFormat format) {82if (mixer==null) return;83try {84System.out.println("Clip from mixer "+mixer+":");85System.out.println(" "+mixer.getMixerInfo());86DataLine.Info info = new DataLine.Info(87Clip.class,88format);8990if (mixer.isLineSupported(info)) {91Clip clip = (Clip) mixer.getLine(info);92doLine1(clip, format);93} else {94System.out.println(" - Line not supported");95}96} catch (Throwable t) {97System.out.println(" - Caught exception. Not failed.");98System.out.println(" - "+t.toString());99}100}101102private static void doMixerSDL(Mixer mixer, AudioFormat format) {103if (mixer==null) return;104try {105System.out.println("SDL from mixer "+mixer+":");106DataLine.Info info = new DataLine.Info(107SourceDataLine.class,108format);109110if (mixer.isLineSupported(info)) {111SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);112doLine1(sdl, format);113doLine2(sdl, format);114} else {115System.out.println(" - Line not supported");116}117} catch (Throwable t) {118System.out.println(" - Caught exception. Not failed.");119System.out.println(" - "+t.toString());120}121}122123private static void doMixerTDL(Mixer mixer, AudioFormat format) {124if (mixer==null) return;125try {126System.out.println("TDL from mixer "+mixer+":");127DataLine.Info info = new DataLine.Info(128TargetDataLine.class,129format);130if (mixer.isLineSupported(info)) {131TargetDataLine tdl = (TargetDataLine) mixer.getLine(info);132doLine1(tdl, format);133doLine2(tdl, format);134} else {135System.out.println(" - Line not supported");136}137} catch (Throwable t) {138System.out.println(" - Caught exception. Not failed.");139System.out.println(" - "+t.toString());140}141}142143private static void doAll() throws Exception {144Mixer.Info[] mixers = AudioSystem.getMixerInfo();145AudioFormat pcm;146for (int i=0; i<mixers.length; i++) {147Mixer mixer = AudioSystem.getMixer(mixers[i]);148pcm = new AudioFormat(samplerate, 16, 1, true, false);149doMixerClip(mixer, pcm);150pcm = new AudioFormat(samplerate, 8, 1, false, false);151doMixerSDL(mixer, pcm);152pcm = new AudioFormat(samplerate, 16, 2, true, true);153doMixerTDL(mixer, pcm);154}155if (mixers.length==0) {156System.out.println("No mixers available!");157}158159}160161public static void main(String args[]) throws Exception{162doAll();163if (passed==0 && failed==0) {164System.out.println("Could not execute any of the tests. Test NOT failed.");165} else if (failed == 0) {166System.out.println("Test PASSED.");167} else {168throw new Exception("Test FAILED!");169}170}171}172173174