Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits32ToFromFloatArray.java
41155 views
1
/*
2
* Copyright (c) 2016, 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
import java.util.Arrays;
25
26
import javax.sound.sampled.AudioFormat;
27
import javax.sound.sampled.AudioFormat.Encoding;
28
29
import com.sun.media.sound.AudioFloatConverter;
30
31
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
32
import static javax.sound.sampled.AudioFormat.Encoding.PCM_UNSIGNED;
33
34
/**
35
* @test
36
* @bug 8152501
37
* @modules java.desktop/com.sun.media.sound
38
*/
39
public final class Bits32ToFromFloatArray {
40
41
private static final int SIZE = 32;
42
43
private static final float[] FLOATS = {-1.0f, 0, 1.0f};
44
45
private static int MID_U = (int) (Integer.MAX_VALUE + 1);
46
private static int MAX_U = -1;
47
48
// BIG ENDIAN
49
private static final byte[] SIGNED_BIG = {
50
(byte) ((Integer.MIN_VALUE >> 24) & 0xff),
51
(byte) ((Integer.MIN_VALUE >> 16) & 0xff),
52
(byte) ((Integer.MIN_VALUE >> 8) & 0xff),
53
(byte) ((Integer.MIN_VALUE >> 0) & 0xff),
54
0, 0, 0, 0,
55
(byte) ((Integer.MAX_VALUE >> 24) & 0xff),
56
(byte) ((Integer.MAX_VALUE >> 16) & 0xff),
57
(byte) ((Integer.MAX_VALUE >> 8) & 0xff),
58
(byte) ((Integer.MAX_VALUE >> 0) & 0xff),
59
};
60
61
private static final byte[] UNSIGNED_BIG = {
62
0, 0, 0, 0,
63
(byte) ((MID_U >> 24) & 0xff),
64
(byte) ((MID_U >> 16) & 0xff),
65
(byte) ((MID_U >> 8) & 0xff),
66
(byte) ((MID_U >> 0) & 0xff),
67
(byte) ((MAX_U >> 24) & 0xff),
68
(byte) ((MAX_U >> 16) & 0xff),
69
(byte) ((MAX_U >> 8) & 0xff),
70
(byte) ((MAX_U >> 0) & 0xff),
71
72
};
73
74
// LITTLE ENDIAN
75
private static final byte[] SIGNED_LITTLE = {
76
(byte) ((Integer.MIN_VALUE >> 0) & 0xff),
77
(byte) ((Integer.MIN_VALUE >> 8) & 0xff),
78
(byte) ((Integer.MIN_VALUE >> 16) & 0xff),
79
(byte) ((Integer.MIN_VALUE >> 24) & 0xff),
80
0, 0, 0, 0,
81
(byte) ((Integer.MAX_VALUE >> 0) & 0xff),
82
(byte) ((Integer.MAX_VALUE >> 8) & 0xff),
83
(byte) ((Integer.MAX_VALUE >> 16) & 0xff),
84
(byte) ((Integer.MAX_VALUE >> 24) & 0xff),
85
};
86
87
private static final byte[] UNSIGNED_LITTLE = {
88
0, 0, 0, 0,
89
(byte) ((MID_U >> 0) & 0xff),
90
(byte) ((MID_U >> 8) & 0xff),
91
(byte) ((MID_U >> 16) & 0xff),
92
(byte) ((MID_U >> 24) & 0xff),
93
(byte) ((MAX_U >> 0) & 0xff),
94
(byte) ((MAX_U >> 8) & 0xff),
95
(byte) ((MAX_U >> 16) & 0xff),
96
(byte) ((MAX_U >> 24) & 0xff),
97
};
98
99
public static void main(final String[] args) {
100
test(PCM_UNSIGNED, UNSIGNED_BIG, true);
101
test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);
102
test(PCM_SIGNED, SIGNED_LITTLE, false);
103
test(PCM_SIGNED, SIGNED_BIG, true);
104
}
105
106
private static void test(final Encoding enc, final byte[] expected,
107
boolean end) {
108
AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,
109
end);
110
byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];
111
AudioFloatConverter conv = AudioFloatConverter.getConverter(af);
112
113
conv.toByteArray(FLOATS, bytes);
114
115
if (!Arrays.equals(bytes, expected)) {
116
System.err.println("Actual: " + Arrays.toString(bytes));
117
System.err.println("Expected: " + Arrays.toString(expected));
118
throw new RuntimeException();
119
}
120
121
float[] floats = new float[bytes.length / af.getFrameSize()];
122
conv.toFloatArray(bytes, floats);
123
124
if (!Arrays.equals(floats, FLOATS)) {
125
System.err.println("Actual: " + Arrays.toString(floats));
126
System.err.println("Expected: " + Arrays.toString(FLOATS));
127
throw new RuntimeException();
128
}
129
}
130
}
131
132