jdk/test/javax/sound/sampled/AudioInputStream/FrameLengthAfterConversion.java
author serb
Thu, 18 Feb 2016 22:11:29 +0300
changeset 36454 d2853d1fc614
child 40718 fe2adbe4d101
permissions -rw-r--r--
8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36454
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     1
/*
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     4
 *
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     8
 *
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    13
 * accompanied this code).
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    14
 *
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    18
 *
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    21
 * questions.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    22
 */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    23
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    26
import java.io.File;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    27
import java.io.IOException;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    28
import java.io.InputStream;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    29
import java.util.ArrayList;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    30
import java.util.List;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    31
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    32
import javax.sound.sampled.AudioFileFormat;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    33
import javax.sound.sampled.AudioFormat;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    34
import javax.sound.sampled.AudioInputStream;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    35
import javax.sound.sampled.AudioSystem;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    36
import javax.sound.sampled.UnsupportedAudioFileException;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    37
import javax.sound.sampled.spi.AudioFileWriter;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    38
import javax.sound.sampled.spi.FormatConversionProvider;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    39
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    40
import static java.util.ServiceLoader.load;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    41
import static javax.sound.sampled.AudioFileFormat.Type.AIFC;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    42
import static javax.sound.sampled.AudioFileFormat.Type.AIFF;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    43
import static javax.sound.sampled.AudioFileFormat.Type.AU;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    44
import static javax.sound.sampled.AudioFileFormat.Type.SND;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    45
import static javax.sound.sampled.AudioFileFormat.Type.WAVE;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    46
import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    47
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    48
/**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    49
 * @test
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    50
 * @bug 8038139
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    51
 */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    52
public final class FrameLengthAfterConversion {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    53
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    54
    /**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    55
     * We will try to use all formats, in this case all our providers will be
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    56
     * covered by supported/unsupported formats.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    57
     */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    58
    private static final List<AudioFormat> formats = new ArrayList<>(23000);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    59
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    60
    private static final AudioFormat.Encoding[] encodings = {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    61
            AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    62
            AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    63
            AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    64
    };
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    65
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    66
    private static final int[] sampleBits = {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    67
            1, 4, 8, 11, 16, 20, 24, 32
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    68
    };
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    69
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    70
    private static final int[] channels = {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    71
            1, 2, 3, 4, 5
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    72
    };
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    73
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    74
    private static final AudioFileFormat.Type[] types = {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    75
            WAVE, AU, AIFF, AIFC, SND,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    76
            new AudioFileFormat.Type("TestName", "TestExt")
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    77
    };
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    78
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    79
    private static final int FRAME_LENGTH = 10;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    80
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    81
    static {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    82
        for (final int sampleSize : sampleBits) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    83
            for (final int channel : channels) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    84
                for (final AudioFormat.Encoding enc : encodings) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    85
                    final int frameSize = ((sampleSize + 7) / 8) * channel;
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    86
                    formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    87
                                                frameSize, 44100, true));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    88
                    formats.add(new AudioFormat(enc, 44100, sampleSize, channel,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    89
                                                frameSize, 44100, false));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    90
                }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    91
            }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    92
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    93
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    94
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    95
    public static void main(final String[] args) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    96
        for (final FormatConversionProvider fcp : load(
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    97
                FormatConversionProvider.class)) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    98
            System.out.println("fcp = " + fcp);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
    99
            for (final AudioFormat from : formats) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   100
                for (final AudioFormat to : formats) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   101
                    testAfterConversion(fcp, to, getStream(from, true));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   102
                }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   103
            }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   104
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   105
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   106
        for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   107
            System.out.println("afw = " + afw);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   108
            for (final AudioFileFormat.Type type : types) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   109
                for (final AudioFormat from : formats) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   110
                    testAfterSaveToStream(afw, type, getStream(from, true));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   111
                }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   112
            }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   113
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   114
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   115
        for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   116
            System.out.println("afw = " + afw);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   117
            for (final AudioFileFormat.Type type : types) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   118
                for (final AudioFormat from : formats) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   119
                    testAfterSaveToFile(afw, type, getStream(from, true));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   120
                }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   121
            }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   122
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   123
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   124
        for (final AudioFileWriter afw : load(AudioFileWriter.class)) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   125
            System.out.println("afw = " + afw);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   126
            for (final AudioFileFormat.Type type : types) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   127
                for (final AudioFormat from : formats) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   128
                    testAfterSaveToFile(afw, type, getStream(from, false));
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   129
                }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   130
            }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   131
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   132
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   133
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   134
    /**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   135
     * Verifies the frame length after the stream was saved/read to/from
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   136
     * stream.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   137
     */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   138
    private static void testAfterSaveToStream(final AudioFileWriter afw,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   139
                                              final AudioFileFormat.Type type,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   140
                                              final AudioInputStream ais) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   141
        try {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   142
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   143
            afw.write(ais, type, out);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   144
            final InputStream input = new ByteArrayInputStream(
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   145
                    out.toByteArray());
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   146
            validate(AudioSystem.getAudioInputStream(input).getFrameLength());
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   147
        } catch (IllegalArgumentException | UnsupportedAudioFileException
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   148
                | IOException ignored) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   149
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   150
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   151
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   152
    /**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   153
     * Verifies the frame length after the stream was saved/read to/from file.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   154
     */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   155
    private static void testAfterSaveToFile(final AudioFileWriter afw,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   156
                                            final AudioFileFormat.Type type,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   157
                                            AudioInputStream ais) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   158
        try {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   159
            final File temp = File.createTempFile("sound", ".tmp");
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   160
            temp.deleteOnExit();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   161
            afw.write(ais, type, temp);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   162
            ais = AudioSystem.getAudioInputStream(temp);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   163
            final long frameLength = ais.getFrameLength();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   164
            ais.close();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   165
            temp.delete();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   166
            validate(frameLength);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   167
        } catch (IllegalArgumentException | UnsupportedAudioFileException
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   168
                | IOException ignored) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   169
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   170
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   171
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   172
    /**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   173
     * Verifies the frame length after the stream was converted to other
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   174
     * stream.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   175
     *
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   176
     * @see FormatConversionProvider#getAudioInputStream(AudioFormat,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   177
     * AudioInputStream)
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   178
     */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   179
    private static void testAfterConversion(final FormatConversionProvider fcp,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   180
                                            final AudioFormat to,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   181
                                            final AudioInputStream ais) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   182
        if (fcp.isConversionSupported(to, ais.getFormat())) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   183
            validate(fcp.getAudioInputStream(to, ais).getFrameLength());
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   184
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   185
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   186
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   187
    /**
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   188
     * Throws an exception if the frameLength is specified and is not equal to
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   189
     * the gold value.
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   190
     */
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   191
    private static void validate(final long frameLength) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   192
        if (frameLength != FRAME_LENGTH) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   193
            System.err.println("Expected: " + FRAME_LENGTH);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   194
            System.err.println("Actual: " + frameLength);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   195
            throw new RuntimeException();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   196
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   197
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   198
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   199
    private static AudioInputStream getStream(final AudioFormat format,
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   200
                                              final boolean frameLength) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   201
        final int dataSize = FRAME_LENGTH * format.getFrameSize();
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   202
        final InputStream in = new ByteArrayInputStream(new byte[dataSize]);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   203
        if (frameLength) {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   204
            return new AudioInputStream(in, format, FRAME_LENGTH);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   205
        } else {
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   206
            return new AudioInputStream(in, format, NOT_SPECIFIED);
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   207
        }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   208
    }
d2853d1fc614 8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV
serb
parents:
diff changeset
   209
}