ToFloatArray.java revision 9330:8b1f1c2a400f
1/*
2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @summary Test AudioFloatConverter toFloatArray method */
26
27import javax.sound.sampled.*;
28
29import com.sun.media.sound.*;
30
31public class ToFloatArray {
32
33    public static void main(String[] args) throws Exception {
34        float[] testarray = new float[1024];
35        for (int i = 0; i < 1024; i++) {
36            double ii = i / 1024.0;
37            ii = ii * ii;
38            testarray[i] = (float)Math.sin(10*ii*2*Math.PI);
39            testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
40            testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
41            testarray[i] *= 0.3;
42        }
43
44        // Check conversion using PCM_FLOAT
45        for (int big = 0; big < 2; big+=1)
46        for (int bits = 32; bits <= 64; bits+=32) {
47            AudioFormat frm = new AudioFormat(
48                    AudioFormat.Encoding.PCM_FLOAT,
49                    44100, bits, 1, bits/8,
50                    44100, big==1);
51            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
52            float[] testarray2 = new float[testarray.length];
53            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
54            conv.toByteArray(testarray, buff);
55            conv.toFloatArray(buff, testarray2);
56            for (int i = 0; i < testarray2.length; i++) {
57                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
58                    throw new RuntimeException("Conversion failed for " + frm  +" , arrays not equal enough!\n");
59            }
60        }
61
62        // Check conversion from float2byte and byte2float.
63        for (int big = 0; big < 2; big+=1)
64        for (int signed = 0; signed < 2; signed+=1)
65        for (int bits = 6; bits <= 40; bits+=2) {
66            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
67            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
68            float[] testarray2 = new float[testarray.length];
69            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
70            conv.toByteArray(testarray, buff);
71            conv.toFloatArray(buff, testarray2);
72            for (int i = 0; i < testarray2.length; i++) {
73                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
74                    throw new RuntimeException("Conversion failed for " + frm  +" , arrays not equal enough!\n");
75            }
76        }
77
78        // Check big/little
79        for (int big = 0; big < 2; big+=1)
80        for (int signed = 0; signed < 2; signed+=1)
81        for (int bits = 6; bits <= 40; bits+=2) {
82            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
83            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
84            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
85            conv.toByteArray(testarray, buff);
86            byte[] buff2 = new byte[testarray.length * frm.getFrameSize()];
87            int fs = frm.getFrameSize();
88            for (int i = 0; i < buff2.length; i+=fs) {
89                for (int j = 0; j < fs; j++) {
90                    buff2[i+(fs-j-1)] = buff[i+j];
91                }
92            }
93            float[] testarray2 = new float[testarray.length];
94            AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==1, big==0);
95            AudioFloatConverter.getConverter(frm2).toFloatArray(buff2, testarray2);
96            for (int i = 0; i < testarray2.length; i++) {
97                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
98                {
99                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
100                }
101            }
102        }
103
104        // Check signed/unsigned
105        for (int big = 0; big < 2; big+=1)
106        for (int signed = 0; signed < 2; signed+=1)
107        for (int bits = 6; bits <= 40; bits+=2) {
108            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
109            byte[] b = new byte[testarray.length * frm.getFrameSize()];
110            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
111            conv.toByteArray(testarray, b);
112            int fs = frm.getFrameSize();
113            if(big==1)
114            {
115                for(int i=0; i < b.length; i+= fs )
116                    b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
117            }
118            else
119            {
120                for(int i=(0+fs-1); i < b.length; i+= fs )
121                    b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
122            }
123            float[] testarray2 = new float[testarray.length];
124            AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==0, big==1);
125            AudioFloatConverter.getConverter(frm2).toFloatArray(b, testarray2);
126            for (int i = 0; i < testarray2.length; i++) {
127                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
128                {
129                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
130                }
131            }
132        }
133
134        // Check if conversion 32->24, 24->16, 16->8 result in same float data
135        AudioFormat frm = new AudioFormat(44100, 40, 1, true, true);
136        byte[] b = new byte[testarray.length * frm.getFrameSize()];
137        AudioFloatConverter.getConverter(frm).toByteArray(testarray, b);
138        for (int bits = 6; bits <= 40; bits+=2) {
139            AudioFormat frm2 = new AudioFormat(44100, bits, 1, true, true);
140            byte[] b2 = new byte[testarray.length * frm2.getFrameSize()];
141            int fs1 = frm.getFrameSize();
142            int fs2 = frm2.getFrameSize();
143            int ii = 0;
144            for (int i = 0; i < b.length; i+=fs1)
145                for (int j = 0; j < fs2; j++)
146                    b2[ii++] = b[i+j];
147            float[] testarray2 = new float[testarray.length];
148            AudioFloatConverter.getConverter(frm2).toFloatArray(b2, testarray2);
149            for (int i = 0; i < testarray2.length; i++) {
150                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
151                {
152                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
153                }
154            }
155        }
156    }
157
158}
159