Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileWriter/AiffSampleRate.java
41159 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.InputStream;2627import javax.sound.sampled.AudioFileFormat;28import javax.sound.sampled.AudioFormat;29import javax.sound.sampled.AudioInputStream;30import javax.sound.sampled.AudioSystem;3132/**33* @test34* @bug 491463935* @summary JavaSound writes wrong sample rates to AIFF files36*/37public class AiffSampleRate {3839private static final float[] testSampleRates =40{8000.0F, 8000.0F + 0.011F, 8193.975F, 10000.0F, 11025.0F, 12000.0F,4116000.0F, 22050.0F, 24000.0F, 32000.0F, 44100.0F - 1.22222F, 44100.0F,4247888.888F, 48000.0F, 96000.0F, 192000.0F};4344public static void main(String[] args) throws Exception {45boolean isTestPassed = true;4647out("#4914639: JavaSound writes wrong sample rates to AIFF files");48for (int i = 0; i < testSampleRates.length; i++) {49isTestPassed &= testSampleRate(testSampleRates[i]);50}51if (isTestPassed) {52out("Test PASSED.");53} else {54throw new Exception("Test FAILED.");55}56}5758private static boolean testSampleRate(float sampleRate) {59boolean result = true;6061try {62// create AudioInputStream with sample rate of 10000 Hz63ByteArrayInputStream data = new ByteArrayInputStream(new byte[1]);64AudioFormat format = new AudioFormat(sampleRate, 8, 1, true, true);65AudioInputStream stream = new AudioInputStream(data, format, 1);6667// write to AIFF file68ByteArrayOutputStream outputStream = new ByteArrayOutputStream();69AudioSystem.write(stream, AudioFileFormat.Type.AIFF, outputStream);70byte[] fileData = outputStream.toByteArray();71InputStream inputStream = new ByteArrayInputStream(fileData);72AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);73if (! equals(sampleRate, aff.getFormat().getFrameRate())) {74out("error for sample rate " + sampleRate);75result = false;76}77} catch (Exception e) {78out(e);79out("Test NOT FAILED");80}81return result;82}8384private static boolean equals(float f1, float f2) {85return Math.abs(f2 - f1) < 1.0E-9;86}8788private static void out(Throwable t) {89t.printStackTrace(System.out);90}9192private static void out(String message) {93System.out.println(message);94}95}969798