Path: blob/master/test/jdk/javax/sound/sampled/Mixers/BothEndiansAndSigns.java
41153 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 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 493639732* @summary Verify that there'll for a given endianness, there's also the little33* endian version34*/35public class BothEndiansAndSigns {36static boolean failed = false;37static int testedFormats = 0;3839public static void main(String[] args) throws Exception {40out("4936397: Verify that there'll for a given endianness, there's also the little endian version");41out(" and the same for signed'ness for 8-bit formats");4243Mixer.Info[] aInfos = AudioSystem.getMixerInfo();44for (int i = 0; i < aInfos.length; i++) {45try {46Mixer mixer = AudioSystem.getMixer(aInfos[i]);47out("Mixer "+aInfos[i]);48checkLines(mixer, mixer.getSourceLineInfo());49checkLines(mixer, mixer.getTargetLineInfo());50} catch (Exception e) {51out("Unexpected exception when getting a mixer: "+e);52}53}54if (testedFormats == 0) {55out("[No appropriate lines available] - cannot exercise this test.");56} else {57if (failed) {58throw new Exception("Test FAILED!");59}60out("Test passed");61}62}6364public static void checkLines(Mixer mixer, Line.Info[] infos) {65for (int i = 0; i<infos.length; i++) {66try {67if (infos[i] instanceof DataLine.Info) {68DataLine.Info info = (DataLine.Info) infos[i];69System.out.println(" Line "+info+" (max. "+mixer.getMaxLines(info)+" simultaneously): ");70AudioFormat[] formats = info.getFormats();71for (int f = 0; f < formats.length; f++) {72try {73AudioFormat otherEndianOrSign = getOtherEndianOrSign(formats[f]);74if (otherEndianOrSign != null) {75checkFormat(formats, otherEndianOrSign);76}77} catch (Exception e1) {78out(" Unexpected exception when getting a format: "+e1);79}80}81}82} catch (Exception e) {83out(" Unexpected exception when getting a line: "+e);84}85}86}8788public static void checkFormat(AudioFormat[] formats, AudioFormat format) {89for (int i = 0; i < formats.length; i++) {90testedFormats++;91if (formats[i].matches(format)) {92return;93}94}95out(" ## expected this format: "+format96+" ("+format.getChannels()+" channels, "97+"frameSize="+format.getFrameSize()+", "98+(format.isBigEndian()?"big endian":"little endian")99+")");100failed = true;101}102103public static AudioFormat getOtherEndianOrSign(AudioFormat format) {104AudioFormat.Encoding newEnc = null;105boolean newEndian = format.isBigEndian();106boolean isSigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED);107boolean isUnsigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED);108if ((isSigned || isUnsigned) && format.getSampleSizeInBits() > 0) {109if (format.getSampleSizeInBits() == 8) {110// return the other signed'ness111if (isSigned) {112newEnc = AudioFormat.Encoding.PCM_UNSIGNED;113} else {114newEnc = AudioFormat.Encoding.PCM_SIGNED;115}116} else {117newEnc = format.getEncoding();118newEndian = !newEndian;119}120if (newEnc != null) {121return new AudioFormat(newEnc, format.getSampleRate(),122format.getSampleSizeInBits(),123format.getChannels(),124format.getFrameSize(),125format.getFrameRate(),126newEndian);127}128}129return null;130}131132static void out(String s) {133System.out.println(s); System.out.flush();134}135}136137138