1/*
2 * Copyright (c) 2007, 2015, 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   @modules java.desktop/com.sun.media.sound
27*/
28
29import javax.sound.sampled.*;
30
31import com.sun.media.sound.*;
32
33public class ToFloatArray {
34
35    public static void main(String[] args) throws Exception {
36        float[] testarray = new float[1024];
37        for (int i = 0; i < 1024; i++) {
38            double ii = i / 1024.0;
39            ii = ii * ii;
40            testarray[i] = (float)Math.sin(10*ii*2*Math.PI);
41            testarray[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
42            testarray[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
43            testarray[i] *= 0.3;
44        }
45
46        // Check conversion using PCM_FLOAT
47        for (int big = 0; big < 2; big+=1)
48        for (int bits = 32; bits <= 64; bits+=32) {
49            AudioFormat frm = new AudioFormat(
50                    AudioFormat.Encoding.PCM_FLOAT,
51                    44100, bits, 1, bits/8,
52                    44100, big==1);
53            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
54            float[] testarray2 = new float[testarray.length];
55            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
56            conv.toByteArray(testarray, buff);
57            conv.toFloatArray(buff, testarray2);
58            for (int i = 0; i < testarray2.length; i++) {
59                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
60                    throw new RuntimeException("Conversion failed for " + frm  +" , arrays not equal enough!\n");
61            }
62        }
63
64        // Check conversion from float2byte and byte2float.
65        for (int big = 0; big < 2; big+=1)
66        for (int signed = 0; signed < 2; signed+=1)
67        for (int bits = 6; bits <= 40; bits+=2) {
68            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
69            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
70            float[] testarray2 = new float[testarray.length];
71            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
72            conv.toByteArray(testarray, buff);
73            conv.toFloatArray(buff, testarray2);
74            for (int i = 0; i < testarray2.length; i++) {
75                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
76                    throw new RuntimeException("Conversion failed for " + frm  +" , arrays not equal enough!\n");
77            }
78        }
79
80        // Check big/little
81        for (int big = 0; big < 2; big+=1)
82        for (int signed = 0; signed < 2; signed+=1)
83        for (int bits = 6; bits <= 40; bits+=2) {
84            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
85            byte[] buff = new byte[testarray.length * frm.getFrameSize()];
86            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
87            conv.toByteArray(testarray, buff);
88            byte[] buff2 = new byte[testarray.length * frm.getFrameSize()];
89            int fs = frm.getFrameSize();
90            for (int i = 0; i < buff2.length; i+=fs) {
91                for (int j = 0; j < fs; j++) {
92                    buff2[i+(fs-j-1)] = buff[i+j];
93                }
94            }
95            float[] testarray2 = new float[testarray.length];
96            AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==1, big==0);
97            AudioFloatConverter.getConverter(frm2).toFloatArray(buff2, testarray2);
98            for (int i = 0; i < testarray2.length; i++) {
99                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
100                {
101                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
102                }
103            }
104        }
105
106        // Check signed/unsigned
107        for (int big = 0; big < 2; big+=1)
108        for (int signed = 0; signed < 2; signed+=1)
109        for (int bits = 6; bits <= 40; bits+=2) {
110            AudioFormat frm = new AudioFormat(44100, bits, 1, signed==1, big==1);
111            byte[] b = new byte[testarray.length * frm.getFrameSize()];
112            AudioFloatConverter conv = AudioFloatConverter.getConverter(frm);
113            conv.toByteArray(testarray, b);
114            int fs = frm.getFrameSize();
115            if(big==1)
116            {
117                for(int i=0; i < b.length; i+= fs )
118                    b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
119            }
120            else
121            {
122                for(int i=(0+fs-1); i < b.length; i+= fs )
123                    b[i] = (b[i] >= 0) ? (byte)(0x80 | b[i]) : (byte)(0x7F & b[i]);
124            }
125            float[] testarray2 = new float[testarray.length];
126            AudioFormat frm2 = new AudioFormat(44100, bits, 1, signed==0, big==1);
127            AudioFloatConverter.getConverter(frm2).toFloatArray(b, testarray2);
128            for (int i = 0; i < testarray2.length; i++) {
129                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
130                {
131                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
132                }
133            }
134        }
135
136        // Check if conversion 32->24, 24->16, 16->8 result in same float data
137        AudioFormat frm = new AudioFormat(44100, 40, 1, true, true);
138        byte[] b = new byte[testarray.length * frm.getFrameSize()];
139        AudioFloatConverter.getConverter(frm).toByteArray(testarray, b);
140        for (int bits = 6; bits <= 40; bits+=2) {
141            AudioFormat frm2 = new AudioFormat(44100, bits, 1, true, true);
142            byte[] b2 = new byte[testarray.length * frm2.getFrameSize()];
143            int fs1 = frm.getFrameSize();
144            int fs2 = frm2.getFrameSize();
145            int ii = 0;
146            for (int i = 0; i < b.length; i+=fs1)
147                for (int j = 0; j < fs2; j++)
148                    b2[ii++] = b[i+j];
149            float[] testarray2 = new float[testarray.length];
150            AudioFloatConverter.getConverter(frm2).toFloatArray(b2, testarray2);
151            for (int i = 0; i < testarray2.length; i++) {
152                if(Math.abs(testarray[i] - testarray2[i]) > 0.05)
153                {
154                    throw new RuntimeException("Conversion failed for " + frm  +" to " + frm2 + " , arrays not equal enough!\n");
155                }
156            }
157        }
158    }
159
160}
161