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