Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/sound/sampled/AudioInputStream/SkipOnConvertSampleSize.java
41152 views
1
/*
2
* Copyright (c) 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
import java.io.ByteArrayInputStream;
25
26
import javax.sound.sampled.AudioFormat;
27
import javax.sound.sampled.AudioFormat.Encoding;
28
import javax.sound.sampled.AudioInputStream;
29
import javax.sound.sampled.AudioSystem;
30
31
/**
32
* @test
33
* @bug 6459818
34
* @summary Audio A-law and law decoder skip() method not implemented
35
* @author Klaus Jaensch
36
*/
37
public class SkipOnConvertSampleSize {
38
39
private static final int TEST_FRAME_LENGTH = 20000;
40
41
private static void testskipping(final Encoding encoding) throws Exception {
42
43
// create temporary PCM_SIGNED audio file
44
int pcmBufSize = TEST_FRAME_LENGTH * 2;
45
byte[] tempAudioBuf = new byte[pcmBufSize];
46
for (int i = 0; i < TEST_FRAME_LENGTH; i++) {
47
// fill with noise
48
tempAudioBuf[i * 2] = (byte) ((Math.random() - 1) * Byte.MAX_VALUE);
49
tempAudioBuf[i * 2 + 1] = (byte) ((Math.random() - 1)
50
* Byte.MAX_VALUE);
51
}
52
final ByteArrayInputStream bis = new ByteArrayInputStream(tempAudioBuf);
53
AudioFormat format = new AudioFormat(8000, 16, 1, true, false);
54
final AudioInputStream testAis = new AudioInputStream(bis, format,
55
TEST_FRAME_LENGTH);
56
final AudioFormat lawFormat;
57
final byte[] alawAudioBuf;
58
try (AudioInputStream lawStream = AudioSystem.getAudioInputStream(
59
encoding, testAis)) {
60
61
lawFormat = lawStream.getFormat();
62
int alawFrameSize = lawFormat.getFrameSize();
63
64
int lawBufSize = TEST_FRAME_LENGTH * alawFrameSize;
65
alawAudioBuf = new byte[lawBufSize];
66
int r1 = 0;
67
int totalRead = 0;
68
while ((r1 = lawStream.read(alawAudioBuf, totalRead,
69
lawBufSize - totalRead)) != -1) {
70
totalRead += r1;
71
}
72
}
73
74
// Convert back to PCM
75
76
ByteArrayInputStream alawBis = new ByteArrayInputStream(alawAudioBuf);
77
AudioInputStream lawAis = new AudioInputStream(alawBis, lawFormat,
78
TEST_FRAME_LENGTH);
79
try (AudioInputStream convPcmStream = AudioSystem.getAudioInputStream(
80
Encoding.PCM_SIGNED, lawAis)) {
81
final AudioFormat convPcmAudioFormat = convPcmStream.getFormat();
82
final int convPcmFrameSize = convPcmAudioFormat.getFrameSize();
83
84
// skip half of the stream
85
final long toSkip = (TEST_FRAME_LENGTH / 2) * convPcmFrameSize;
86
long skipped = 0;
87
do {
88
skipped += convPcmStream.skip(toSkip - skipped);
89
} while (skipped < toSkip);
90
int r2 = convPcmStream.read(new byte[convPcmFrameSize]);
91
// if skip is not correctly implemented we are at the end of the
92
// stream
93
if (r2 == -1) {
94
throw new RuntimeException(
95
"Skip method of decoder not correctly implemented!");
96
}
97
// otherwise we could read the rest ...
98
// we don't do it here
99
}
100
}
101
102
public static void main(final String[] args) throws Exception {
103
testskipping(Encoding.ALAW);
104
testskipping(Encoding.ULAW);
105
}
106
}
107
108