Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits24ToFromFloatArray.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 Bits24ToFromFloatArray {3940private static final int SIZE = 24;4142private static final float[] FLOATS = {-1.0f, 0, 1.0f};4344private static int MIN_S = -8_388_608;45private static int MAX_S = 8_388_607;4647private static int MID_U = 0xFFFFFF / 2 + 1;48private static int MAX_U = 0xFFFFFF;4950// BIG ENDIAN51private static final byte[] SIGNED_BIG = {52(byte) ((MIN_S >> 16) & 0xff),53(byte) ((MIN_S >> 8) & 0xff),54(byte) ((MIN_S >> 0) & 0xff),550, 0, 0,56(byte) ((MAX_S >> 16) & 0xff),57(byte) ((MAX_S >> 8) & 0xff),58(byte) ((MAX_S >> 0) & 0xff),59};6061private static final byte[] UNSIGNED_BIG = {620, 0, 0,63(byte) ((MID_U >> 16) & 0xff),64(byte) ((MID_U >> 8) & 0xff),65(byte) ((MID_U >> 0) & 0xff),66(byte) ((MAX_U >> 16) & 0xff),67(byte) ((MAX_U >> 8) & 0xff),68(byte) ((MAX_U >> 0) & 0xff),6970};7172// LITTLE ENDIAN73private static final byte[] SIGNED_LITTLE = {74(byte) ((MIN_S >> 0) & 0xff),75(byte) ((MIN_S >> 8) & 0xff),76(byte) ((MIN_S >> 16) & 0xff),770, 0, 0,78(byte) ((MAX_S >> 0) & 0xff),79(byte) ((MAX_S >> 8) & 0xff),80(byte) ((MAX_S >> 16) & 0xff),81};8283private static final byte[] UNSIGNED_LITTLE = {840, 0, 0,85(byte) ((MID_U >> 0) & 0xff),86(byte) ((MID_U >> 8) & 0xff),87(byte) ((MID_U >> 16) & 0xff),88(byte) ((MAX_U >> 0) & 0xff),89(byte) ((MAX_U >> 8) & 0xff),90(byte) ((MAX_U >> 16) & 0xff),91};9293public static void main(final String[] args) {94test(PCM_UNSIGNED, UNSIGNED_BIG, true);95test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);96test(PCM_SIGNED, SIGNED_LITTLE, false);97test(PCM_SIGNED, SIGNED_BIG, true);98}99100private static void test(final Encoding enc, final byte[] expected,101boolean end) {102System.err.println(enc);103AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,104end);105byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];106AudioFloatConverter conv = AudioFloatConverter.getConverter(af);107108conv.toByteArray(FLOATS, bytes);109110if (!Arrays.equals(bytes, expected)) {111System.err.println("Actual: " + Arrays.toString(bytes));112System.err.println("Expected: " + Arrays.toString(expected));113throw new RuntimeException();114}115116float[] floats = new float[bytes.length / af.getFrameSize()];117conv.toFloatArray(bytes, floats);118119if (!Arrays.equals(floats, FLOATS)) {120System.err.println("Actual: " + Arrays.toString(floats));121System.err.println("Expected: " + Arrays.toString(FLOATS));122throw new RuntimeException();123}124}125}126127128