Path: blob/master/test/jdk/javax/sound/midi/Gervill/ModelByteBufferWavetable/OpenStream.java
41159 views
/*1* Copyright (c) 2010, 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*/2223/* @test24@summary Test AudioFloatInputStream.getFrameLength() returned from25ModelByteBufferWavetable openStream method26@modules java.desktop/com.sun.media.sound27*/2829import java.io.ByteArrayOutputStream;30import java.io.File;31import java.io.FileOutputStream;32import java.nio.file.Files;33import java.nio.file.Paths;3435import javax.sound.sampled.*;3637import com.sun.media.sound.*;3839public class OpenStream {4041static float[] testarray;4243static byte[] test_byte_array;4445static byte[] test_byte_array_8ext;4647static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);4849static AudioFormat format24 = new AudioFormat(44100, 24, 1, true, false);5051static ModelByteBuffer buffer;5253static ModelByteBuffer buffer_wave;5455static ModelByteBuffer buffer8;5657static ModelByteBuffer buffer16_8;5859static ModelByteBuffer buffer24;6061static File test_file;6263static ModelByteBuffer buffer_wave_ondisk;6465static void setUp() throws Exception {66testarray = new float[1024];67for (int i = 0; i < 1024; i++) {68double ii = i / 1024.0;69ii = ii * ii;70testarray[i] = (float) Math.sin(10 * ii * 2 * Math.PI);71testarray[i] += (float) Math.sin(1.731 + 2 * ii * 2 * Math.PI);72testarray[i] += (float) Math.sin(0.231 + 6.3 * ii * 2 * Math.PI);73testarray[i] *= 0.3;74}75test_byte_array = new byte[testarray.length * 2];76AudioFloatConverter.getConverter(format).toByteArray(testarray,77test_byte_array);78buffer = new ModelByteBuffer(test_byte_array);7980byte[] test_byte_array2 = new byte[testarray.length * 3];81buffer24 = new ModelByteBuffer(test_byte_array2);82test_byte_array_8ext = new byte[testarray.length];83byte[] test_byte_array_8_16 = new byte[testarray.length * 2];84AudioFloatConverter.getConverter(format24).toByteArray(testarray,85test_byte_array2);86int ix = 0;87int x = 0;88for (int i = 0; i < test_byte_array_8ext.length; i++) {89test_byte_array_8ext[i] = test_byte_array2[ix++];90test_byte_array_8_16[x++] = test_byte_array2[ix++];91test_byte_array_8_16[x++] = test_byte_array2[ix++];92}93buffer16_8 = new ModelByteBuffer(test_byte_array_8_16);94buffer8 = new ModelByteBuffer(test_byte_array_8ext);9596AudioInputStream ais = new AudioInputStream(buffer.getInputStream(),97format, testarray.length);98ByteArrayOutputStream baos = new ByteArrayOutputStream();99AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos);100buffer_wave = new ModelByteBuffer(baos.toByteArray());101102test_file = File.createTempFile("test", ".raw");103try (FileOutputStream fos = new FileOutputStream(test_file)) {104fos.write(baos.toByteArray());105}106buffer_wave_ondisk = new ModelByteBuffer(test_file);107108}109110static void tearDown() throws Exception {111Files.delete(Paths.get(test_file.getAbsolutePath()));112}113114public static void testOpenStream(ModelByteBufferWavetable wavetable)115throws Exception {116AudioFloatInputStream ais = wavetable.openStream();117long frames = wavetable.getBuffer().capacity()118/ wavetable.getFormat().getFrameSize();119long framelength = ais.getFrameLength();120ais.close();121if (frames != framelength) {122throw new Exception("Incorrect framelength returned (" + frames123+ " != " + framelength + ")");124}125}126127public static void main(String[] args) throws Exception {128129setUp();130131try {132testOpenStream(new ModelByteBufferWavetable(buffer, format));133testOpenStream(new ModelByteBufferWavetable(buffer_wave, format));134testOpenStream(new ModelByteBufferWavetable(buffer_wave_ondisk,135format));136} finally {137tearDown();138}139140}141142}143144145