Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits32ToFromFloatArray.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.PCM_SIGNED;31import static javax.sound.sampled.AudioFormat.Encoding.PCM_UNSIGNED;3233/**34* @test35* @bug 815250136* @modules java.desktop/com.sun.media.sound37*/38public final class Bits32ToFromFloatArray {3940private static final int SIZE = 32;4142private static final float[] FLOATS = {-1.0f, 0, 1.0f};4344private static int MID_U = (int) (Integer.MAX_VALUE + 1);45private static int MAX_U = -1;4647// BIG ENDIAN48private static final byte[] SIGNED_BIG = {49(byte) ((Integer.MIN_VALUE >> 24) & 0xff),50(byte) ((Integer.MIN_VALUE >> 16) & 0xff),51(byte) ((Integer.MIN_VALUE >> 8) & 0xff),52(byte) ((Integer.MIN_VALUE >> 0) & 0xff),530, 0, 0, 0,54(byte) ((Integer.MAX_VALUE >> 24) & 0xff),55(byte) ((Integer.MAX_VALUE >> 16) & 0xff),56(byte) ((Integer.MAX_VALUE >> 8) & 0xff),57(byte) ((Integer.MAX_VALUE >> 0) & 0xff),58};5960private static final byte[] UNSIGNED_BIG = {610, 0, 0, 0,62(byte) ((MID_U >> 24) & 0xff),63(byte) ((MID_U >> 16) & 0xff),64(byte) ((MID_U >> 8) & 0xff),65(byte) ((MID_U >> 0) & 0xff),66(byte) ((MAX_U >> 24) & 0xff),67(byte) ((MAX_U >> 16) & 0xff),68(byte) ((MAX_U >> 8) & 0xff),69(byte) ((MAX_U >> 0) & 0xff),7071};7273// LITTLE ENDIAN74private static final byte[] SIGNED_LITTLE = {75(byte) ((Integer.MIN_VALUE >> 0) & 0xff),76(byte) ((Integer.MIN_VALUE >> 8) & 0xff),77(byte) ((Integer.MIN_VALUE >> 16) & 0xff),78(byte) ((Integer.MIN_VALUE >> 24) & 0xff),790, 0, 0, 0,80(byte) ((Integer.MAX_VALUE >> 0) & 0xff),81(byte) ((Integer.MAX_VALUE >> 8) & 0xff),82(byte) ((Integer.MAX_VALUE >> 16) & 0xff),83(byte) ((Integer.MAX_VALUE >> 24) & 0xff),84};8586private static final byte[] UNSIGNED_LITTLE = {870, 0, 0, 0,88(byte) ((MID_U >> 0) & 0xff),89(byte) ((MID_U >> 8) & 0xff),90(byte) ((MID_U >> 16) & 0xff),91(byte) ((MID_U >> 24) & 0xff),92(byte) ((MAX_U >> 0) & 0xff),93(byte) ((MAX_U >> 8) & 0xff),94(byte) ((MAX_U >> 16) & 0xff),95(byte) ((MAX_U >> 24) & 0xff),96};9798public static void main(final String[] args) {99test(PCM_UNSIGNED, UNSIGNED_BIG, true);100test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);101test(PCM_SIGNED, SIGNED_LITTLE, false);102test(PCM_SIGNED, SIGNED_BIG, true);103}104105private static void test(final Encoding enc, final byte[] expected,106boolean end) {107AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,108end);109byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];110AudioFloatConverter conv = AudioFloatConverter.getConverter(af);111112conv.toByteArray(FLOATS, bytes);113114if (!Arrays.equals(bytes, expected)) {115System.err.println("Actual: " + Arrays.toString(bytes));116System.err.println("Expected: " + Arrays.toString(expected));117throw new RuntimeException();118}119120float[] floats = new float[bytes.length / af.getFrameSize()];121conv.toFloatArray(bytes, floats);122123if (!Arrays.equals(floats, FLOATS)) {124System.err.println("Actual: " + Arrays.toString(floats));125System.err.println("Expected: " + Arrays.toString(FLOATS));126throw new RuntimeException();127}128}129}130131132