Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/midi/Gervill/AudioFloatConverter/Bits16ToFromFloatArray.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.*;
32
33
/**
34
* @test
35
* @bug 8152501
36
* @modules java.desktop/com.sun.media.sound
37
*/
38
public final class Bits16ToFromFloatArray {
39
40
private static final int SIZE = 16;
41
42
private static final float[] FLOATS = {-1.0f, 0, 1.0f};
43
44
private static short MID_U = (short) (Short.MAX_VALUE + 1);
45
private static short MAX_U = -1;
46
47
// BIG ENDIAN
48
private static final byte[] SIGNED_BIG = {
49
(byte) (Short.MIN_VALUE >> 8), (byte) (Short.MIN_VALUE & 0xff), 0,
50
0, (byte) (Short.MAX_VALUE >> 8), (byte) (Short.MAX_VALUE & 0xff)
51
};
52
53
private static final byte[] UNSIGNED_BIG = {
54
0, 0, (byte) (MID_U >> 8), (byte) (MID_U & 0xff),
55
(byte) (MAX_U >> 8), (byte) (MAX_U >> 8)
56
};
57
58
// LITTLE ENDIAN
59
private static final byte[] SIGNED_LITTLE = {
60
(byte) (Short.MIN_VALUE & 0xff), (byte) (Short.MIN_VALUE >> 8), 0,
61
0, (byte) (Short.MAX_VALUE & 0xff), (byte) (Short.MAX_VALUE >> 8)
62
};
63
64
private static final byte[] UNSIGNED_LITTLE = {
65
0, 0, (byte) (MID_U & 0xff), (byte) (MID_U >> 8),
66
(byte) (MAX_U >> 8), (byte) (MAX_U >> 8)
67
};
68
69
public static void main(final String[] args) {
70
test(PCM_UNSIGNED, UNSIGNED_BIG, true);
71
test(PCM_UNSIGNED, UNSIGNED_LITTLE, false);
72
test(PCM_SIGNED, SIGNED_LITTLE, false);
73
test(PCM_SIGNED, SIGNED_BIG, true);
74
}
75
76
private static void test(final Encoding enc, final byte[] expected,
77
boolean end) {
78
System.err.println("enc = " + enc);
79
AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100,
80
end);
81
byte[] bytes = new byte[FLOATS.length * af.getFrameSize()];
82
AudioFloatConverter conv = AudioFloatConverter.getConverter(af);
83
84
conv.toByteArray(FLOATS, bytes);
85
86
if (!Arrays.equals(bytes, expected)) {
87
System.err.println("Actual: " + Arrays.toString(bytes));
88
System.err.println("Expected: " + Arrays.toString(expected));
89
throw new RuntimeException();
90
}
91
92
float[] floats = new float[bytes.length / af.getFrameSize()];
93
conv.toFloatArray(bytes, floats);
94
95
if (!Arrays.equals(floats, FLOATS)) {
96
System.err.println("Actual: " + Arrays.toString(floats));
97
System.err.println("Expected: " + Arrays.toString(FLOATS));
98
throw new RuntimeException();
99
}
100
}
101
}
102
103