Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits8ToFromFloatArray.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 Bits8ToFromFloatArray {3839private static final int SIZE = 8;4041private static final float[] FLOATS = {-1.0f, 0, 1.0f};4243private static final byte[] SIGNED = {Byte.MIN_VALUE, 0, Byte.MAX_VALUE};4445private static final byte[] UNSIGNED = {460, (byte) (Byte.MAX_VALUE + 1), (byte) -147};4849public static void main(final String[] args) {50test(PCM_UNSIGNED, UNSIGNED);51test(PCM_SIGNED, SIGNED);52}5354private static void test(final Encoding enc, final byte[] expected) {55AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,56true);57byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];58AudioFloatConverter conv = AudioFloatConverter.getConverter(af);5960conv.toByteArray(FLOATS, bytes);6162if (!Arrays.equals(bytes, expected)) {63System.err.println("Actual: " + Arrays.toString(bytes));64System.err.println("Expected: " + Arrays.toString(expected));65throw new RuntimeException();66}6768float[] floats = new float[bytes.length / af.getFrameSize()];69conv.toFloatArray(bytes, floats);7071if (!Arrays.equals(floats, FLOATS)) {72System.err.println("Actual: " + Arrays.toString(floats));73System.err.println("Expected: " + Arrays.toString(FLOATS));74throw new RuntimeException();75}76}77}787980