41905
|
1 |
/*
|
|
2 |
* Copyright (c) 2002, 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.io.ByteArrayInputStream;
|
|
25 |
import java.io.ByteArrayOutputStream;
|
|
26 |
import java.io.InputStream;
|
|
27 |
|
|
28 |
import javax.sound.sampled.AudioFileFormat;
|
|
29 |
import javax.sound.sampled.AudioFormat;
|
|
30 |
import javax.sound.sampled.AudioInputStream;
|
|
31 |
import javax.sound.sampled.AudioSystem;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* @test
|
|
35 |
* @bug 4914639
|
|
36 |
* @summary JavaSound writes wrong sample rates to AIFF files
|
|
37 |
*/
|
|
38 |
public class AiffSampleRate {
|
|
39 |
|
|
40 |
private static final float[] testSampleRates =
|
|
41 |
{8000.0F, 8000.0F + 0.011F, 8193.975F, 10000.0F, 11025.0F, 12000.0F,
|
|
42 |
16000.0F, 22050.0F, 24000.0F, 32000.0F, 44100.0F - 1.22222F, 44100.0F,
|
|
43 |
47888.888F, 48000.0F, 96000.0F, 192000.0F};
|
|
44 |
|
|
45 |
public static void main(String[] args) throws Exception {
|
|
46 |
boolean isTestPassed = true;
|
|
47 |
|
|
48 |
out("#4914639: JavaSound writes wrong sample rates to AIFF files");
|
|
49 |
for (int i = 0; i < testSampleRates.length; i++) {
|
|
50 |
isTestPassed &= testSampleRate(testSampleRates[i]);
|
|
51 |
}
|
|
52 |
if (isTestPassed) {
|
|
53 |
out("Test PASSED.");
|
|
54 |
} else {
|
|
55 |
throw new Exception("Test FAILED.");
|
|
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
private static boolean testSampleRate(float sampleRate) {
|
|
60 |
boolean result = true;
|
|
61 |
|
|
62 |
try {
|
|
63 |
// create AudioInputStream with sample rate of 10000 Hz
|
|
64 |
ByteArrayInputStream data = new ByteArrayInputStream(new byte[1]);
|
|
65 |
AudioFormat format = new AudioFormat(sampleRate, 8, 1, true, true);
|
|
66 |
AudioInputStream stream = new AudioInputStream(data, format, 1);
|
|
67 |
|
|
68 |
// write to AIFF file
|
|
69 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
70 |
AudioSystem.write(stream, AudioFileFormat.Type.AIFF, outputStream);
|
|
71 |
byte[] fileData = outputStream.toByteArray();
|
|
72 |
InputStream inputStream = new ByteArrayInputStream(fileData);
|
|
73 |
AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);
|
|
74 |
if (! equals(sampleRate, aff.getFormat().getFrameRate())) {
|
|
75 |
out("error for sample rate " + sampleRate);
|
|
76 |
result = false;
|
|
77 |
}
|
|
78 |
} catch (Exception e) {
|
|
79 |
out(e);
|
|
80 |
out("Test NOT FAILED");
|
|
81 |
}
|
|
82 |
return result;
|
|
83 |
}
|
|
84 |
|
|
85 |
private static boolean equals(float f1, float f2) {
|
|
86 |
return Math.abs(f2 - f1) < 1.0E-9;
|
|
87 |
}
|
|
88 |
|
|
89 |
private static void out(Throwable t) {
|
|
90 |
t.printStackTrace(System.out);
|
|
91 |
}
|
|
92 |
|
|
93 |
private static void out(String message) {
|
|
94 |
System.out.println(message);
|
|
95 |
}
|
|
96 |
}
|