Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits64ToFromFloatArray.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 Bits64ToFromFloatArray {3940private static final int SIZE = 64;4142private static final float[] FLOATS = {-1.0f, 0, 1.0f};4344private static long MID_U = (long) (Long.MAX_VALUE + 1);45private static long MAX_U = -1;4647// BIG ENDIAN48private static final byte[] SIGNED_BIG = {49(byte) ((Long.MIN_VALUE >> 56) & 0xff),50(byte) ((Long.MIN_VALUE >> 48) & 0xff),51(byte) ((Long.MIN_VALUE >> 40) & 0xff),52(byte) ((Long.MIN_VALUE >> 32) & 0xff),530, 0, 0, 0, // current javasound impl will ignore this540, 0, 0, 0,550, 0, 0, 0,56(byte) ((Long.MAX_VALUE >> 56) & 0xff),57(byte) ((Long.MAX_VALUE >> 48) & 0xff),58(byte) ((Long.MAX_VALUE >> 40) & 0xff),59(byte) ((Long.MAX_VALUE >> 32) & 0xff),600, 0, 0, 0, // current javasound impl will ignore this61};6263private static final byte[] UNSIGNED_BIG = {640, 0, 0, 0,650, 0, 0, 0,66(byte) ((MID_U >> 56) & 0xff),67(byte) ((MID_U >> 48) & 0xff),68(byte) ((MID_U >> 40) & 0xff),69(byte) ((MID_U >> 32) & 0xff),700, 0, 0, 0, // current javasound impl will ignore this71(byte) ((MAX_U >> 56) & 0xff),72(byte) ((MAX_U >> 48) & 0xff),73(byte) ((MAX_U >> 40) & 0xff),74(byte) ((MAX_U >> 32) & 0xff),750, 0, 0, 0, // current javasound impl will ignore this76};7778// LITTLE ENDIAN79private static final byte[] SIGNED_LITTLE = {800, 0, 0, 0, // current javasound impl will ignore this81(byte) ((Long.MIN_VALUE >> 32) & 0xff),82(byte) ((Long.MIN_VALUE >> 40) & 0xff),83(byte) ((Long.MIN_VALUE >> 48) & 0xff),84(byte) ((Long.MIN_VALUE >> 56) & 0xff),850, 0, 0, 0,860, 0, 0, 0,870, 0, 0, 0, // current javasound impl will ignore this88(byte) ((Long.MAX_VALUE >> 32) & 0xff),89(byte) ((Long.MAX_VALUE >> 40) & 0xff),90(byte) ((Long.MAX_VALUE >> 48) & 0xff),91(byte) ((Long.MAX_VALUE >> 56) & 0xff),92};9394private static final byte[] UNSIGNED_LITTLE = {950, 0, 0, 0,960, 0, 0, 0,970, 0, 0, 0, // current javasound impl will ignore this98(byte) ((MID_U >> 32) & 0xff),99(byte) ((MID_U >> 40) & 0xff),100(byte) ((MID_U >> 48) & 0xff),101(byte) ((MID_U >> 56) & 0xff),1020, 0, 0, 0, // current javasound impl will ignore this103(byte) ((MAX_U >> 32) & 0xff),104(byte) ((MAX_U >> 40) & 0xff),105(byte) ((MAX_U >> 48) & 0xff),106(byte) ((MAX_U >> 56) & 0xff),107};108109public static void main(final String[] args) {110test(PCM_UNSIGNED, UNSIGNED_BIG, true);111test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);112test(PCM_SIGNED, SIGNED_LITTLE, false);113test(PCM_SIGNED, SIGNED_BIG, true);114}115116private static void test(final Encoding enc, final byte[] expected,117boolean end) {118System.err.println(enc);119AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,120end);121byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];122AudioFloatConverter conv = AudioFloatConverter.getConverter(af);123124conv.toByteArray(FLOATS, bytes);125126if (!Arrays.equals(bytes, expected)) {127System.err.println("Actual: " + Arrays.toString(bytes));128System.err.println("Expected: " + Arrays.toString(expected));129throw new RuntimeException();130}131132float[] floats = new float[bytes.length / af.getFrameSize()];133conv.toFloatArray(bytes, floats);134135if (!Arrays.equals(floats, FLOATS)) {136System.err.println("Actual: " + Arrays.toString(floats));137System.err.println("Expected: " + Arrays.toString(FLOATS));138throw new RuntimeException();139}140}141}142143144