1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import javax.sound.sampled.AudioFormat;
  25 import javax.sound.sampled.AudioSystem;
  26 import javax.sound.sampled.DataLine;
  27 import javax.sound.sampled.Line;
  28 import javax.sound.sampled.Mixer;
  29 
  30 /**
  31  * @test
  32  * @bug 4936397
  33  * @summary Verify that there'll for a given endianness, there's also the little
  34  *          endian version
  35  */
  36 public class BothEndiansAndSigns {
  37     static boolean failed = false;
  38     static int testedFormats = 0;
  39 
  40     public static void main(String[] args) throws Exception {
  41         out("4936397: Verify that there'll for a given endianness, there's also the little endian version");
  42         out("         and the same for signed'ness for 8-bit formats");
  43 
  44         Mixer.Info[] aInfos = AudioSystem.getMixerInfo();
  45         for (int i = 0; i < aInfos.length; i++) {
  46             try {
  47                 Mixer mixer = AudioSystem.getMixer(aInfos[i]);
  48                 out("Mixer "+aInfos[i]);
  49                 checkLines(mixer, mixer.getSourceLineInfo());
  50                 checkLines(mixer, mixer.getTargetLineInfo());
  51             } catch (Exception e) {
  52                 out("Unexpected exception when getting a mixer: "+e);
  53             }
  54         }
  55         if (testedFormats == 0) {
  56             out("[No appropriate lines available] - cannot exercise this test.");
  57         } else {
  58             if (failed) {
  59                 throw new Exception("Test FAILED!");
  60             }
  61             out("Test passed");
  62         }
  63     }
  64 
  65     public static void checkLines(Mixer mixer, Line.Info[] infos) {
  66         for (int i = 0; i<infos.length; i++) {
  67             try {
  68                 if (infos[i] instanceof DataLine.Info) {
  69                     DataLine.Info info = (DataLine.Info) infos[i];
  70                     System.out.println(" Line "+info+" (max. "+mixer.getMaxLines(info)+" simultaneously): ");
  71                     AudioFormat[] formats = info.getFormats();
  72                     for (int f = 0; f < formats.length; f++) {
  73                         try {
  74                             AudioFormat otherEndianOrSign = getOtherEndianOrSign(formats[f]);
  75                             if (otherEndianOrSign != null) {
  76                                 checkFormat(formats, otherEndianOrSign);
  77                             }
  78                         } catch (Exception e1) {
  79                             out("  Unexpected exception when getting a format: "+e1);
  80                         }
  81                     }
  82                 }
  83             } catch (Exception e) {
  84                 out(" Unexpected exception when getting a line: "+e);
  85             }
  86         }
  87     }
  88 
  89     public static void checkFormat(AudioFormat[] formats, AudioFormat format) {
  90         for (int i = 0; i < formats.length; i++) {
  91             testedFormats++;
  92             if (formats[i].matches(format)) {
  93                 return;
  94             }
  95         }
  96         out("  ## expected this format: "+format
  97             +" ("+format.getChannels()+" channels, "
  98             +"frameSize="+format.getFrameSize()+", "
  99             +(format.isBigEndian()?"big endian":"little endian")
 100             +")");
 101         failed = true;
 102     }
 103 
 104     public static AudioFormat getOtherEndianOrSign(AudioFormat format) {
 105         AudioFormat.Encoding newEnc = null;
 106         boolean newEndian = format.isBigEndian();
 107         boolean isSigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED);
 108         boolean isUnsigned = format.getEncoding().equals(AudioFormat.Encoding.PCM_UNSIGNED);
 109         if ((isSigned || isUnsigned) && format.getSampleSizeInBits() > 0) {
 110             if (format.getSampleSizeInBits() == 8) {
 111                 // return the other signed'ness
 112                 if (isSigned) {
 113                     newEnc = AudioFormat.Encoding.PCM_UNSIGNED;
 114                 } else {
 115                     newEnc = AudioFormat.Encoding.PCM_SIGNED;
 116                 }
 117             } else {
 118                 newEnc = format.getEncoding();
 119                 newEndian = !newEndian;
 120             }
 121             if (newEnc != null) {
 122                 return new AudioFormat(newEnc, format.getSampleRate(),
 123                                        format.getSampleSizeInBits(),
 124                                        format.getChannels(),
 125                                        format.getFrameSize(),
 126                                        format.getFrameRate(),
 127                                        newEndian);
 128             }
 129         }
 130         return null;
 131     }
 132 
 133     static void out(String s) {
 134         System.out.println(s); System.out.flush();
 135     }
 136 }