Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/AudioFloatConverter/ToFloatArray.java
38862 views
/*1* Copyright (c) 2007, 2013, 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*/2223/* @test24@summary Test AudioFloatConverter toFloatArray method */2526import javax.sound.sampled.*;2728import com.sun.media.sound.*;2930public class ToFloatArray {3132public static void main(String[] args) throws Exception {33float[] testarray = new float[1024];34for (int i = 0; i < 1024; i++) {35double ii = i / 1024.0;36ii = ii * ii;37testarray[i] = (float)Math.sin(10*ii*2*Math.PI);38testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);39testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);40testarray[i] *= 0.3;41}4243// Check conversion using PCM_FLOAT44for (int big = 0; big < 2; big+=1)45for (int bits = 32; bits <= 64; bits+=32) {46AudioFormat frm = new AudioFormat(47AudioFormat.Encoding.PCM_FLOAT,4844100, bits, 1, bits/8,4944100, big==1);50byte[] buff = new byte[testarray.length * frm.getFrameSize()];51float[] testarray2 = new float[testarray.length];52AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);53conv.toByteArray(testarray, buff);54conv.toFloatArray(buff, testarray2);55for (int i = 0; i < testarray2.length; i++) {56if(Math.abs(testarray[i] - testarray2[i]) > 0.05)57throw new RuntimeException("Conversion failed for " + frm +" , arrays not equal enough!\n");58}59}6061// Check conversion from float2byte and byte2float.62for (int big = 0; big < 2; big+=1)63for (int signed = 0; signed < 2; signed+=1)64for (int bits = 6; bits <= 40; bits+=2) {65AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);66byte[] buff = new byte[testarray.length * frm.getFrameSize()];67float[] testarray2 = new float[testarray.length];68AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);69conv.toByteArray(testarray, buff);70conv.toFloatArray(buff, testarray2);71for (int i = 0; i < testarray2.length; i++) {72if(Math.abs(testarray[i] - testarray2[i]) > 0.05)73throw new RuntimeException("Conversion failed for " + frm +" , arrays not equal enough!\n");74}75}7677// Check big/little78for (int big = 0; big < 2; big+=1)79for (int signed = 0; signed < 2; signed+=1)80for (int bits = 6; bits <= 40; bits+=2) {81AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);82byte[] buff = new byte[testarray.length * frm.getFrameSize()];83AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);84conv.toByteArray(testarray, buff);85byte[] buff2 = new byte[testarray.length * frm.getFrameSize()];86int fs = frm.getFrameSize();87for (int i = 0; i < buff2.length; i+=fs) {88for (int j = 0; j < fs; j++) {89buff2[i+(fs-j-1)] = buff[i+j];90}91}92float[] testarray2 = new float[testarray.length];93AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==1, big==0);94AudioFloatConverter.getConverter(frm2).toFloatArray(buff2, testarray2);95for (int i = 0; i < testarray2.length; i++) {96if(Math.abs(testarray[i] - testarray2[i]) > 0.05)97{98throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");99}100}101}102103// Check signed/unsigned104for (int big = 0; big < 2; big+=1)105for (int signed = 0; signed < 2; signed+=1)106for (int bits = 6; bits <= 40; bits+=2) {107AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);108byte[] b = new byte[testarray.length * frm.getFrameSize()];109AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);110conv.toByteArray(testarray, b);111int fs = frm.getFrameSize();112if(big==1)113{114for(int i=0; i < b.length; i+= fs )115b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);116}117else118{119for(int i=(0+fs-1); i < b.length; i+= fs )120b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);121}122float[] testarray2 = new float[testarray.length];123AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==0, big==1);124AudioFloatConverter.getConverter(frm2).toFloatArray(b, testarray2);125for (int i = 0; i < testarray2.length; i++) {126if(Math.abs(testarray[i] - testarray2[i]) > 0.05)127{128throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");129}130}131}132133// Check if conversion 32->24, 24->16, 16->8 result in same float data134AudioFormat frm = new AudioFormat(44100, 40, 1, true, true);135byte[] b = new byte[testarray.length * frm.getFrameSize()];136AudioFloatConverter.getConverter(frm).toByteArray(testarray, b);137for (int bits = 6; bits <= 40; bits+=2) {138AudioFormat frm2 = new AudioFormat(44100, bits, 1, true, true);139byte[] b2 = new byte[testarray.length * frm2.getFrameSize()];140int fs1 = frm.getFrameSize();141int fs2 = frm2.getFrameSize();142int ii = 0;143for (int i = 0; i < b.length; i+=fs1)144for (int j = 0; j < fs2; j++)145b2[ii++] = b[i+j];146float[] testarray2 = new float[testarray.length];147AudioFloatConverter.getConverter(frm2).toFloatArray(b2, testarray2);148for (int i = 0; i < testarray2.length; i++) {149if(Math.abs(testarray[i] - testarray2[i]) > 0.05)150{151throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");152}153}154}155}156157}158159160