Path: blob/master/test/jdk/javax/sound/sampled/AudioInputStream/FrameLengthAfterConversion.java
41152 views
/*1* Copyright (c) 2016, 2017, 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 java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.File;26import java.io.IOException;27import java.io.InputStream;28import java.nio.file.Files;29import java.nio.file.Paths;30import java.util.ArrayList;31import java.util.List;3233import javax.sound.sampled.AudioFileFormat;34import javax.sound.sampled.AudioFormat;35import javax.sound.sampled.AudioInputStream;36import javax.sound.sampled.AudioSystem;37import javax.sound.sampled.UnsupportedAudioFileException;38import javax.sound.sampled.spi.AudioFileWriter;39import javax.sound.sampled.spi.FormatConversionProvider;4041import static java.util.ServiceLoader.load;42import static javax.sound.sampled.AudioFileFormat.Type.AIFC;43import static javax.sound.sampled.AudioFileFormat.Type.AIFF;44import static javax.sound.sampled.AudioFileFormat.Type.AU;45import static javax.sound.sampled.AudioFileFormat.Type.SND;46import static javax.sound.sampled.AudioFileFormat.Type.WAVE;47import static javax.sound.sampled.AudioSystem.NOT_SPECIFIED;4849/**50* @test51* @bug 8038139 817840152*/53public final class FrameLengthAfterConversion {5455/**56* We will try to use all formats, in this case all our providers will be57* covered by supported/unsupported formats.58*/59private static final List<AudioFormat> formats = new ArrayList<>(23000);6061private static final AudioFormat.Encoding[] encodings = {62AudioFormat.Encoding.ALAW, AudioFormat.Encoding.ULAW,63AudioFormat.Encoding.PCM_SIGNED, AudioFormat.Encoding.PCM_UNSIGNED,64AudioFormat.Encoding.PCM_FLOAT, new AudioFormat.Encoding("Test")65};6667private static final int[] sampleBits = {681, 4, 8, 11, 16, 20, 24, 3269};7071private static final int[] channels = {721, 2, 3, 4, 573};7475private static final AudioFileFormat.Type[] types = {76WAVE, AU, AIFF, AIFC, SND,77new AudioFileFormat.Type("TestName", "TestExt")78};7980private static final int FRAME_LENGTH = 10;8182static {83for (final int sampleSize : sampleBits) {84for (final int channel : channels) {85for (final AudioFormat.Encoding enc : encodings) {86final int frameSize = ((sampleSize + 7) / 8) * channel;87formats.add(new AudioFormat(enc, 44100, sampleSize, channel,88frameSize, 44100, true));89formats.add(new AudioFormat(enc, 44100, sampleSize, channel,90frameSize, 44100, false));91}92}93}94}9596public static void main(final String[] args) throws IOException {97for (final FormatConversionProvider fcp : load(98FormatConversionProvider.class)) {99System.out.println("fcp = " + fcp);100for (final AudioFormat from : formats) {101for (final AudioFormat to : formats) {102testAfterConversion(fcp, to, getStream(from, true));103}104}105}106107for (final AudioFileWriter afw : load(AudioFileWriter.class)) {108System.out.println("afw = " + afw);109for (final AudioFileFormat.Type type : types) {110for (final AudioFormat from : formats) {111testAfterSaveToStream(afw, type, getStream(from, true));112}113}114}115116for (final AudioFileWriter afw : load(AudioFileWriter.class)) {117System.out.println("afw = " + afw);118for (final AudioFileFormat.Type type : types) {119for (final AudioFormat from : formats) {120testAfterSaveToFile(afw, type, getStream(from, true));121}122}123}124125for (final AudioFileWriter afw : load(AudioFileWriter.class)) {126System.out.println("afw = " + afw);127for (final AudioFileFormat.Type type : types) {128for (final AudioFormat from : formats) {129testAfterSaveToFile(afw, type, getStream(from, false));130}131}132}133}134135/**136* Verifies the frame length after the stream was saved/read to/from137* stream.138*/139private static void testAfterSaveToStream(final AudioFileWriter afw,140final AudioFileFormat.Type type,141final AudioInputStream ais)142throws IOException {143try {144final ByteArrayOutputStream out = new ByteArrayOutputStream();145afw.write(ais, type, out);146final InputStream input = new ByteArrayInputStream(147out.toByteArray());148validate(AudioSystem.getAudioInputStream(input).getFrameLength());149} catch (IllegalArgumentException | UnsupportedAudioFileException150ignored) {151}152}153154/**155* Verifies the frame length after the stream was saved/read to/from file.156*/157private static void testAfterSaveToFile(final AudioFileWriter afw,158final AudioFileFormat.Type type,159AudioInputStream ais)160throws IOException {161final File temp = File.createTempFile("sound", ".tmp");162try {163afw.write(ais, type, temp);164ais = AudioSystem.getAudioInputStream(temp);165final long frameLength = ais.getFrameLength();166ais.close();167validate(frameLength);168} catch (IllegalArgumentException | UnsupportedAudioFileException169ignored) {170} finally {171Files.delete(Paths.get(temp.getAbsolutePath()));172}173}174175/**176* Verifies the frame length after the stream was converted to other177* stream.178*179* @see FormatConversionProvider#getAudioInputStream(AudioFormat,180* AudioInputStream)181*/182private static void testAfterConversion(final FormatConversionProvider fcp,183final AudioFormat to,184final AudioInputStream ais) {185if (fcp.isConversionSupported(to, ais.getFormat())) {186validate(fcp.getAudioInputStream(to, ais).getFrameLength());187}188}189190/**191* Throws an exception if the frameLength is specified and is not equal to192* the gold value.193*/194private static void validate(final long frameLength) {195if (frameLength != FRAME_LENGTH) {196System.err.println("Expected: " + FRAME_LENGTH);197System.err.println("Actual: " + frameLength);198throw new RuntimeException();199}200}201202private static AudioInputStream getStream(final AudioFormat format,203final boolean frameLength) {204final int dataSize = FRAME_LENGTH * format.getFrameSize();205final InputStream in = new ByteArrayInputStream(new byte[dataSize]);206if (frameLength) {207return new AudioInputStream(in, format, FRAME_LENGTH);208} else {209return new AudioInputStream(in, format, NOT_SPECIFIED);210}211}212}213214215