Path: blob/master/test/jdk/javax/sound/sampled/Mixers/PlugHwMonoAnd8bitAvailable.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.Line;27import javax.sound.sampled.Mixer;2829/**30* @test31* @bug 501389732* @summary Verify that plughw: provides mono and 8-bit lines33*/34public class PlugHwMonoAnd8bitAvailable {35static int failed = 0;36static int testedFormats = 0;3738public static void main(String[] args) throws Exception {39out("5013897: Verify that plughw: provides mono and 8-bit lines");4041Mixer.Info[] aInfos = AudioSystem.getMixerInfo();42for (int i = 0; i < aInfos.length; i++) {43try {44Mixer mixer = AudioSystem.getMixer(aInfos[i]);45out("Mixer "+aInfos[i]);46if (aInfos[i].getName().contains("plughw")) {47checkLines(mixer, mixer.getSourceLineInfo());48checkLines(mixer, mixer.getTargetLineInfo());49} else {50out(" -> not plughw, ignored.");51}52} catch (Exception e) {53out("Unexpected exception when getting a mixer: "+e);54}55}56if (testedFormats == 0) {57out("[No appropriate lines available] - cannot exercise this test.");58} else {59if (failed>0) {60throw new Exception("Test FAILED!");61}62out("Successfully verified "+testedFormats+" formats.");63out("Test passed");64}65}6667public static void checkLines(Mixer mixer, Line.Info[] infos) {68for (int i = 0; i<infos.length; i++) {69try {70System.out.println(" Line "+infos[i]+" (max. "+mixer.getMaxLines(infos[i])+" simultaneously): ");71if (infos[i] instanceof DataLine.Info) {72DataLine.Info info = (DataLine.Info) infos[i];73int thisTestedFormats = testedFormats;74int thisFailed = failed;75AudioFormat[] formats = info.getFormats();76for (int f = 0; f < formats.length; f++) {77if (formats[f].getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)78|| formats[f].getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED)) {79try {80if (formats[f].getSampleSizeInBits() > 16) {81// if a bit size larger than 16 is available, also 16-bit must be there82checkFormat(formats, getOtherBits(formats[f], 16));83} else84if (formats[f].getSampleSizeInBits() > 8) {85// if a bit size larger than 8 is available, also 8-bit must be there86checkFormat(formats, getOtherBits(formats[f], 8));87}88if (formats[f].getChannels() > 2) {89// if more than 2 channels, also 2 channels must be there90checkFormat(formats, getOtherChannels(formats[f], 2));91} else92if (formats[f].getChannels() > 1) {93// if more than 1 channel, also 1 channel must be there94checkFormat(formats, getOtherChannels(formats[f], 1));95}96} catch (Exception e1) {97out(" Unexpected exception when getting a format: "+e1);98}99}100}101if (testedFormats - thisTestedFormats == 0) {102out(" -->could not test any formats");103} else if (failed - thisFailed == 0) {104out(" -->"+(testedFormats - thisTestedFormats)+" formats tested OK");105}106107} else {108out(" --> not a DataLine");109}110} catch (Exception e) {111out(" Unexpected exception when getting a line: "+e);112}113}114}115116public static void checkFormat(AudioFormat[] formats, AudioFormat format) {117testedFormats++;118for (int i = 0; i < formats.length; i++) {119if (formats[i].matches(format)) {120return;121}122}123out(" ## expected this format: "+format124+" ("+format.getChannels()+" channels, "125+"frameSize="+format.getFrameSize()+", "126+(format.isBigEndian()?"big endian":"little endian")127+")");128failed++;129}130131// only works for PCM encodings132public static AudioFormat getOtherBits(AudioFormat format, int newBits) {133boolean isSigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED);134return new AudioFormat(format.getSampleRate(),135newBits,136format.getChannels(),137isSigned,138(newBits>8)?format.isBigEndian():false);139}140141// only works for PCM encodings142public static AudioFormat getOtherChannels(AudioFormat format, int newChannels) {143int newFrameSize;144if (newChannels <= 0 || format.getChannels() <= 0 || format.getFrameSize() <= 0) {145newFrameSize = -1;146} else {147newFrameSize = format.getFrameSize() / format.getChannels() * newChannels;148}149return new AudioFormat(format.getEncoding(),150format.getSampleRate(),151format.getSampleSizeInBits(),152newChannels,153newFrameSize,154format.getFrameRate(),155format.isBigEndian());156}157158159static void out(String s) {160System.out.println(s); System.out.flush();161}162}163164165