Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/ToFloatArray.java
41155 views
/*1* Copyright (c) 2007, 2015, 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 method25@modules java.desktop/com.sun.media.sound26*/2728import javax.sound.sampled.*;2930import com.sun.media.sound.*;3132public class ToFloatArray {3334public static void main(String[] args) throws Exception {35float[] testarray = new float[1024];36for (int i = 0; i < 1024; i++) {37double ii = i / 1024.0;38ii = ii * ii;39testarray[i] = (float)Math.sin(10*ii*2*Math.PI);40testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);41testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);42testarray[i] *= 0.3;43}4445// Check conversion using PCM_FLOAT46for (int big = 0; big < 2; big+=1)47for (int bits = 32; bits <= 64; bits+=32) {48AudioFormat frm = new AudioFormat(49AudioFormat.Encoding.PCM_FLOAT,5044100, bits, 1, bits/8,5144100, big==1);52byte[] buff = new byte[testarray.length * frm.getFrameSize()];53float[] testarray2 = new float[testarray.length];54AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);55conv.toByteArray(testarray, buff);56conv.toFloatArray(buff, testarray2);57for (int i = 0; i < testarray2.length; i++) {58if(Math.abs(testarray[i] - testarray2[i]) > 0.05)59throw new RuntimeException("Conversion failed for " + frm +" , arrays not equal enough!\n");60}61}6263// Check conversion from float2byte and byte2float.64for (int big = 0; big < 2; big+=1)65for (int signed = 0; signed < 2; signed+=1)66for (int bits = 6; bits <= 40; bits+=2) {67AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);68byte[] buff = new byte[testarray.length * frm.getFrameSize()];69float[] testarray2 = new float[testarray.length];70AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);71conv.toByteArray(testarray, buff);72conv.toFloatArray(buff, testarray2);73for (int i = 0; i < testarray2.length; i++) {74if(Math.abs(testarray[i] - testarray2[i]) > 0.05)75throw new RuntimeException("Conversion failed for " + frm +" , arrays not equal enough!\n");76}77}7879// Check big/little80for (int big = 0; big < 2; big+=1)81for (int signed = 0; signed < 2; signed+=1)82for (int bits = 6; bits <= 40; bits+=2) {83AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);84byte[] buff = new byte[testarray.length * frm.getFrameSize()];85AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);86conv.toByteArray(testarray, buff);87byte[] buff2 = new byte[testarray.length * frm.getFrameSize()];88int fs = frm.getFrameSize();89for (int i = 0; i < buff2.length; i+=fs) {90for (int j = 0; j < fs; j++) {91buff2[i+(fs-j-1)] = buff[i+j];92}93}94float[] testarray2 = new float[testarray.length];95AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==1, big==0);96AudioFloatConverter.getConverter(frm2).toFloatArray(buff2, testarray2);97for (int i = 0; i < testarray2.length; i++) {98if(Math.abs(testarray[i] - testarray2[i]) > 0.05)99{100throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");101}102}103}104105// Check signed/unsigned106for (int big = 0; big < 2; big+=1)107for (int signed = 0; signed < 2; signed+=1)108for (int bits = 6; bits <= 40; bits+=2) {109AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);110byte[] b = new byte[testarray.length * frm.getFrameSize()];111AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);112conv.toByteArray(testarray, b);113int fs = frm.getFrameSize();114if(big==1)115{116for(int i=0; i < b.length; i+= fs )117b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);118}119else120{121for(int i=(0+fs-1); i < b.length; i+= fs )122b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);123}124float[] testarray2 = new float[testarray.length];125AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==0, big==1);126AudioFloatConverter.getConverter(frm2).toFloatArray(b, testarray2);127for (int i = 0; i < testarray2.length; i++) {128if(Math.abs(testarray[i] - testarray2[i]) > 0.05)129{130throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");131}132}133}134135// Check if conversion 32->24, 24->16, 16->8 result in same float data136AudioFormat frm = new AudioFormat(44100, 40, 1, true, true);137byte[] b = new byte[testarray.length * frm.getFrameSize()];138AudioFloatConverter.getConverter(frm).toByteArray(testarray, b);139for (int bits = 6; bits <= 40; bits+=2) {140AudioFormat frm2 = new AudioFormat(44100, bits, 1, true, true);141byte[] b2 = new byte[testarray.length * frm2.getFrameSize()];142int fs1 = frm.getFrameSize();143int fs2 = frm2.getFrameSize();144int ii = 0;145for (int i = 0; i < b.length; i+=fs1)146for (int j = 0; j < fs2; j++)147b2[ii++] = b[i+j];148float[] testarray2 = new float[testarray.length];149AudioFloatConverter.getConverter(frm2).toFloatArray(b2, testarray2);150for (int i = 0; i < testarray2.length; i++) {151if(Math.abs(testarray[i] - testarray2[i]) > 0.05)152{153throw new RuntimeException("Conversion failed for " + frm +" to " + frm2 + " , arrays not equal enough!\n");154}155}156}157}158159}160161162