Path: blob/master/test/jdk/javax/sound/sampled/AudioInputStream/SkipOnConvertSampleSize.java
41152 views
/*1* Copyright (c) 2015, 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;2425import javax.sound.sampled.AudioFormat;26import javax.sound.sampled.AudioFormat.Encoding;27import javax.sound.sampled.AudioInputStream;28import javax.sound.sampled.AudioSystem;2930/**31* @test32* @bug 645981833* @summary Audio A-law and law decoder skip() method not implemented34* @author Klaus Jaensch35*/36public class SkipOnConvertSampleSize {3738private static final int TEST_FRAME_LENGTH = 20000;3940private static void testskipping(final Encoding encoding) throws Exception {4142// create temporary PCM_SIGNED audio file43int pcmBufSize = TEST_FRAME_LENGTH * 2;44byte[] tempAudioBuf = new byte[pcmBufSize];45for (int i = 0; i < TEST_FRAME_LENGTH; i++) {46// fill with noise47tempAudioBuf[i * 2] = (byte) ((Math.random() - 1) * Byte.MAX_VALUE);48tempAudioBuf[i * 2 + 1] = (byte) ((Math.random() - 1)49* Byte.MAX_VALUE);50}51final ByteArrayInputStream bis = new ByteArrayInputStream(tempAudioBuf);52AudioFormat format = new AudioFormat(8000, 16, 1, true, false);53final AudioInputStream testAis = new AudioInputStream(bis, format,54TEST_FRAME_LENGTH);55final AudioFormat lawFormat;56final byte[] alawAudioBuf;57try (AudioInputStream lawStream = AudioSystem.getAudioInputStream(58encoding, testAis)) {5960lawFormat = lawStream.getFormat();61int alawFrameSize = lawFormat.getFrameSize();6263int lawBufSize = TEST_FRAME_LENGTH * alawFrameSize;64alawAudioBuf = new byte[lawBufSize];65int r1 = 0;66int totalRead = 0;67while ((r1 = lawStream.read(alawAudioBuf, totalRead,68lawBufSize - totalRead)) != -1) {69totalRead += r1;70}71}7273// Convert back to PCM7475ByteArrayInputStream alawBis = new ByteArrayInputStream(alawAudioBuf);76AudioInputStream lawAis = new AudioInputStream(alawBis, lawFormat,77TEST_FRAME_LENGTH);78try (AudioInputStream convPcmStream = AudioSystem.getAudioInputStream(79Encoding.PCM_SIGNED, lawAis)) {80final AudioFormat convPcmAudioFormat = convPcmStream.getFormat();81final int convPcmFrameSize = convPcmAudioFormat.getFrameSize();8283// skip half of the stream84final long toSkip = (TEST_FRAME_LENGTH / 2) * convPcmFrameSize;85long skipped = 0;86do {87skipped += convPcmStream.skip(toSkip - skipped);88} while (skipped < toSkip);89int r2 = convPcmStream.read(new byte[convPcmFrameSize]);90// if skip is not correctly implemented we are at the end of the91// stream92if (r2 == -1) {93throw new RuntimeException(94"Skip method of decoder not correctly implemented!");95}96// otherwise we could read the rest ...97// we don't do it here98}99}100101public static void main(final String[] args) throws Exception {102testskipping(Encoding.ALAW);103testskipping(Encoding.ULAW);104}105}106107108