1 /*
   2  * Copyright (c) 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 java.io.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import javax.sound.sampled.AudioFileFormat;
  33 import javax.sound.sampled.AudioFormat;
  34 import javax.sound.sampled.AudioInputStream;
  35 import javax.sound.sampled.AudioSystem;
  36 import javax.sound.sampled.UnsupportedAudioFileException;
  37 import javax.sound.sampled.spi.AudioFileWriter;
  38 import javax.sound.sampled.spi.FormatConversionProvider;
  39 
  40 import static java.util.ServiceLoader.load;
  41 import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
  42 import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
  43 import static javax.sound.sampled.AudioFileFormat.Type.AU;
  44 import static javax.sound.sampled.AudioFileFormat.Type.SND;
  45 import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
  46 import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;
  47 
  48 /**
  49  * @test
  50  * @bug 8038139
  51  */
  52 public final class FrameLengthAfterConversion {
  53 
  54     /**
  55      * We will try to use all formats, in this case all our providers will be
  56      * covered by supported/unsupported formats.
  57      */
  58     private static final List<AudioFormat> formats = new ArrayList<>(23000);
  59 
  60     private static final AudioFormat.Encoding[] encodings = {
  61             AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
  62             AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
  63             AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
  64     };
  65 
  66     private static final int[] sampleBits = {
  67             1, 4, 8, 11, 16, 20, 24, 32
  68     };
  69 
  70     private static final int[] channels = {
  71             1, 2, 3, 4, 5
  72     };
  73 
  74     private static final AudioFileFormat.Type[] types = {
  75             WAVE, AU, AIFF, AIFC, SND,
  76             new AudioFileFormat.Type("TestName", "TestExt")
  77     };
  78 
  79     private static final int FRAME_LENGTH = 10;
  80 
  81     static {
  82         for (final int sampleSize : sampleBits) {
  83             for (final int channel : channels) {
  84                 for (final AudioFormat.Encoding enc : encodings) {
  85                     final int frameSize = ((sampleSize + 7) / 8) * channel;
  86                     formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
  87                                                 frameSize, 44100, true));
  88                     formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
  89                                                 frameSize, 44100, false));
  90                 }
  91             }
  92         }
  93     }
  94 
  95     public static void main(final String[] args) {
  96         for (final FormatConversionProvider fcp : load(
  97                 FormatConversionProvider.class)) {
  98             System.out.println("fcp = " + fcp);
  99             for (final AudioFormat from : formats) {
 100                 for (final AudioFormat to : formats) {
 101                     testAfterConversion(fcp, to, getStream(from, true));
 102                 }
 103             }
 104         }
 105 
 106         for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
 107             System.out.println("afw = " + afw);
 108             for (final AudioFileFormat.Type type : types) {
 109                 for (final AudioFormat from : formats) {
 110                     testAfterSaveToStream(afw, type, getStream(from, true));
 111                 }
 112             }
 113         }
 114 
 115         for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
 116             System.out.println("afw = " + afw);
 117             for (final AudioFileFormat.Type type : types) {
 118                 for (final AudioFormat from : formats) {
 119                     testAfterSaveToFile(afw, type, getStream(from, true));
 120                 }
 121             }
 122         }
 123 
 124         for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
 125             System.out.println("afw = " + afw);
 126             for (final AudioFileFormat.Type type : types) {
 127                 for (final AudioFormat from : formats) {
 128                     testAfterSaveToFile(afw, type, getStream(from, false));
 129                 }
 130             }
 131         }
 132     }
 133 
 134     /**
 135      * Verifies the frame length after the stream was saved/read to/from
 136      * stream.
 137      */
 138     private static void testAfterSaveToStream(final AudioFileWriter afw,
 139                                               final AudioFileFormat.Type type,
 140                                               final AudioInputStream ais) {
 141         try {
 142             final ByteArrayOutputStream out = new ByteArrayOutputStream();
 143             afw.write(ais, type, out);
 144             final InputStream input = new ByteArrayInputStream(
 145                     out.toByteArray());
 146             validate(AudioSystem.getAudioInputStream(input).getFrameLength());
 147         } catch (IllegalArgumentException | UnsupportedAudioFileException
 148                 | IOException ignored) {
 149         }
 150     }
 151 
 152     /**
 153      * Verifies the frame length after the stream was saved/read to/from file.
 154      */
 155     private static void testAfterSaveToFile(final AudioFileWriter afw,
 156                                             final AudioFileFormat.Type type,
 157                                             AudioInputStream ais) {
 158         try {
 159             final File temp = File.createTempFile("sound", ".tmp");
 160             temp.deleteOnExit();
 161             afw.write(ais, type, temp);
 162             ais = AudioSystem.getAudioInputStream(temp);
 163             final long frameLength = ais.getFrameLength();
 164             ais.close();
 165             temp.delete();
 166             validate(frameLength);
 167         } catch (IllegalArgumentException | UnsupportedAudioFileException
 168                 | IOException ignored) {
 169         }
 170     }
 171 
 172     /**
 173      * Verifies the frame length after the stream was converted to other
 174      * stream.
 175      *
 176      * @see FormatConversionProvider#getAudioInputStream(AudioFormat,
 177      * AudioInputStream)
 178      */
 179     private static void testAfterConversion(final FormatConversionProvider fcp,
 180                                             final AudioFormat to,
 181                                             final AudioInputStream ais) {
 182         if (fcp.isConversionSupported(to, ais.getFormat())) {
 183             validate(fcp.getAudioInputStream(to, ais).getFrameLength());
 184         }
 185     }
 186 
 187     /**
 188      * Throws an exception if the frameLength is specified and is not equal to
 189      * the gold value.
 190      */
 191     private static void validate(final long frameLength) {
 192         if (frameLength != FRAME_LENGTH) {
 193             System.err.println("Expected: " + FRAME_LENGTH);
 194             System.err.println("Actual: " + frameLength);
 195             throw new RuntimeException();
 196         }
 197     }
 198 
 199     private static AudioInputStream getStream(final AudioFormat format,
 200                                               final boolean frameLength) {
 201         final int dataSize = FRAME_LENGTH * format.getFrameSize();
 202         final InputStream in = new ByteArrayInputStream(new byte[dataSize]);
 203         if (frameLength) {
 204             return new AudioInputStream(in, format, FRAME_LENGTH);
 205         } else {
 206             return new AudioInputStream(in, format, NOT_SPECIFIED);
 207         }
 208     }
 209 }