Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/ModelByteBufferWavetable/OpenStream.java
38860 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 method */2627import java.io.ByteArrayOutputStream;28import java.io.File;29import java.io.FileOutputStream;30import java.nio.file.Files;31import java.nio.file.Paths;3233import javax.sound.sampled.*;3435import com.sun.media.sound.*;3637public class OpenStream {3839static float[] testarray;4041static byte[] test_byte_array;4243static byte[] test_byte_array_8ext;4445static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);4647static AudioFormat format24 = new AudioFormat(44100, 24, 1, true, false);4849static ModelByteBuffer buffer;5051static ModelByteBuffer buffer_wave;5253static ModelByteBuffer buffer8;5455static ModelByteBuffer buffer16_8;5657static ModelByteBuffer buffer24;5859static File test_file;6061static ModelByteBuffer buffer_wave_ondisk;6263static void setUp() throws Exception {64testarray = new float[1024];65for (int i = 0; i < 1024; i++) {66double ii = i / 1024.0;67ii = ii * ii;68testarray[i] = (float) Math.sin(10 * ii * 2 * Math.PI);69testarray[i] += (float) Math.sin(1.731 + 2 * ii * 2 * Math.PI);70testarray[i] += (float) Math.sin(0.231 + 6.3 * ii * 2 * Math.PI);71testarray[i] *= 0.3;72}73test_byte_array = new byte[testarray.length * 2];74AudioFloatConverter.getConverter(format).toByteArray(testarray,75test_byte_array);76buffer = new ModelByteBuffer(test_byte_array);7778byte[] test_byte_array2 = new byte[testarray.length * 3];79buffer24 = new ModelByteBuffer(test_byte_array2);80test_byte_array_8ext = new byte[testarray.length];81byte[] test_byte_array_8_16 = new byte[testarray.length * 2];82AudioFloatConverter.getConverter(format24).toByteArray(testarray,83test_byte_array2);84int ix = 0;85int x = 0;86for (int i = 0; i < test_byte_array_8ext.length; i++) {87test_byte_array_8ext[i] = test_byte_array2[ix++];88test_byte_array_8_16[x++] = test_byte_array2[ix++];89test_byte_array_8_16[x++] = test_byte_array2[ix++];90}91buffer16_8 = new ModelByteBuffer(test_byte_array_8_16);92buffer8 = new ModelByteBuffer(test_byte_array_8ext);9394AudioInputStream ais = new AudioInputStream(buffer.getInputStream(),95format, testarray.length);96ByteArrayOutputStream baos = new ByteArrayOutputStream();97AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos);98buffer_wave = new ModelByteBuffer(baos.toByteArray());99100test_file = File.createTempFile("test", ".raw");101try (FileOutputStream fos = new FileOutputStream(test_file)) {102fos.write(baos.toByteArray());103}104buffer_wave_ondisk = new ModelByteBuffer(test_file);105106}107108static void tearDown() throws Exception {109Files.delete(Paths.get(test_file.getAbsolutePath()));110}111112public static void testOpenStream(ModelByteBufferWavetable wavetable)113throws Exception {114AudioFloatInputStream ais = wavetable.openStream();115long frames = wavetable.getBuffer().capacity()116/ wavetable.getFormat().getFrameSize();117long framelength = ais.getFrameLength();118ais.close();119if (frames != framelength) {120throw new Exception("Incorrect framelength returned (" + frames121+ " != " + framelength + ")");122}123}124125public static void main(String[] args) throws Exception {126127setUp();128129try {130testOpenStream(new ModelByteBufferWavetable(buffer, format));131testOpenStream(new ModelByteBufferWavetable(buffer_wave, format));132testOpenStream(new ModelByteBufferWavetable(buffer_wave_ondisk,133format));134} finally {135tearDown();136}137138}139140}141142143