Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits16ToFromFloatArray.java
41155 views
/*1* Copyright (c) 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 java.util.Arrays;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioFormat.Encoding;2728import com.sun.media.sound.AudioFloatConverter;2930import static javax.sound.sampled.AudioFormat.Encoding.*;3132/**33* @test34* @bug 815250135* @modules java.desktop/com.sun.media.sound36*/37public final class Bits16ToFromFloatArray {3839private static final int SIZE = 16;4041private static final float[] FLOATS = {-1.0f, 0, 1.0f};4243private static short MID_U = (short) (Short.MAX_VALUE + 1);44private static short MAX_U = -1;4546// BIG ENDIAN47private static final byte[] SIGNED_BIG = {48(byte) (Short.MIN_VALUE >> 8), (byte) (Short.MIN_VALUE & 0xff), 0,490, (byte) (Short.MAX_VALUE >> 8), (byte) (Short.MAX_VALUE & 0xff)50};5152private static final byte[] UNSIGNED_BIG = {530, 0, (byte) (MID_U >> 8), (byte) (MID_U & 0xff),54(byte) (MAX_U >> 8), (byte) (MAX_U >> 8)55};5657// LITTLE ENDIAN58private static final byte[] SIGNED_LITTLE = {59(byte) (Short.MIN_VALUE & 0xff), (byte) (Short.MIN_VALUE >> 8), 0,600, (byte) (Short.MAX_VALUE & 0xff), (byte) (Short.MAX_VALUE >> 8)61};6263private static final byte[] UNSIGNED_LITTLE = {640, 0, (byte) (MID_U & 0xff), (byte) (MID_U >> 8),65(byte) (MAX_U >> 8), (byte) (MAX_U >> 8)66};6768public static void main(final String[] args) {69test(PCM_UNSIGNED, UNSIGNED_BIG, true);70test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);71test(PCM_SIGNED, SIGNED_LITTLE, false);72test(PCM_SIGNED, SIGNED_BIG, true);73}7475private static void test(final Encoding enc, final byte[] expected,76boolean end) {77System.err.println("enc = " + enc);78AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,79end);80byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];81AudioFloatConverter conv = AudioFloatConverter.getConverter(af);8283conv.toByteArray(FLOATS, bytes);8485if (!Arrays.equals(bytes, expected)) {86System.err.println("Actual: " + Arrays.toString(bytes));87System.err.println("Expected: " + Arrays.toString(expected));88throw new RuntimeException();89}9091float[] floats = new float[bytes.length / af.getFrameSize()];92conv.toFloatArray(bytes, floats);9394if (!Arrays.equals(floats, FLOATS)) {95System.err.println("Actual: " + Arrays.toString(floats));96System.err.println("Expected: " + Arrays.toString(FLOATS));97throw new RuntimeException();98}99}100}101102103