1/* 2 * Copyright (c) 2010, 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/* @test 25 @summary Test AudioFloatInputStream.getFrameLength() returned from 26 ModelByteBufferWavetable openStream method 27 @modules java.desktop/com.sun.media.sound 28 */ 29 30import java.io.ByteArrayOutputStream; 31import java.io.File; 32import java.io.FileOutputStream; 33import java.nio.file.Files; 34import java.nio.file.Paths; 35 36import javax.sound.sampled.*; 37 38import com.sun.media.sound.*; 39 40public class OpenStream { 41 42 static float[] testarray; 43 44 static byte[] test_byte_array; 45 46 static byte[] test_byte_array_8ext; 47 48 static AudioFormat format = new AudioFormat(44100, 16, 1, true, false); 49 50 static AudioFormat format24 = new AudioFormat(44100, 24, 1, true, false); 51 52 static ModelByteBuffer buffer; 53 54 static ModelByteBuffer buffer_wave; 55 56 static ModelByteBuffer buffer8; 57 58 static ModelByteBuffer buffer16_8; 59 60 static ModelByteBuffer buffer24; 61 62 static File test_file; 63 64 static ModelByteBuffer buffer_wave_ondisk; 65 66 static void setUp() throws Exception { 67 testarray = new float[1024]; 68 for (int i = 0; i < 1024; i++) { 69 double ii = i / 1024.0; 70 ii = ii * ii; 71 testarray[i] = (float) Math.sin(10 * ii * 2 * Math.PI); 72 testarray[i] += (float) Math.sin(1.731 + 2 * ii * 2 * Math.PI); 73 testarray[i] += (float) Math.sin(0.231 + 6.3 * ii * 2 * Math.PI); 74 testarray[i] *= 0.3; 75 } 76 test_byte_array = new byte[testarray.length * 2]; 77 AudioFloatConverter.getConverter(format).toByteArray(testarray, 78 test_byte_array); 79 buffer = new ModelByteBuffer(test_byte_array); 80 81 byte[] test_byte_array2 = new byte[testarray.length * 3]; 82 buffer24 = new ModelByteBuffer(test_byte_array2); 83 test_byte_array_8ext = new byte[testarray.length]; 84 byte[] test_byte_array_8_16 = new byte[testarray.length * 2]; 85 AudioFloatConverter.getConverter(format24).toByteArray(testarray, 86 test_byte_array2); 87 int ix = 0; 88 int x = 0; 89 for (int i = 0; i < test_byte_array_8ext.length; i++) { 90 test_byte_array_8ext[i] = test_byte_array2[ix++]; 91 test_byte_array_8_16[x++] = test_byte_array2[ix++]; 92 test_byte_array_8_16[x++] = test_byte_array2[ix++]; 93 } 94 buffer16_8 = new ModelByteBuffer(test_byte_array_8_16); 95 buffer8 = new ModelByteBuffer(test_byte_array_8ext); 96 97 AudioInputStream ais = new AudioInputStream(buffer.getInputStream(), 98 format, testarray.length); 99 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 100 AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos); 101 buffer_wave = new ModelByteBuffer(baos.toByteArray()); 102 103 test_file = File.createTempFile("test", ".raw"); 104 try (FileOutputStream fos = new FileOutputStream(test_file)) { 105 fos.write(baos.toByteArray()); 106 } 107 buffer_wave_ondisk = new ModelByteBuffer(test_file); 108 109 } 110 111 static void tearDown() throws Exception { 112 Files.delete(Paths.get(test_file.getAbsolutePath())); 113 } 114 115 public static void testOpenStream(ModelByteBufferWavetable wavetable) 116 throws Exception { 117 AudioFloatInputStream ais = wavetable.openStream(); 118 long frames = wavetable.getBuffer().capacity() 119 / wavetable.getFormat().getFrameSize(); 120 long framelength = ais.getFrameLength(); 121 ais.close(); 122 if (frames != framelength) { 123 throw new Exception("Incorrect framelength returned (" + frames 124 + " != " + framelength + ")"); 125 } 126 } 127 128 public static void main(String[] args) throws Exception { 129 130 setUp(); 131 132 try { 133 testOpenStream(new ModelByteBufferWavetable(buffer, format)); 134 testOpenStream(new ModelByteBufferWavetable(buffer_wave, format)); 135 testOpenStream(new ModelByteBufferWavetable(buffer_wave_ondisk, 136 format)); 137 } finally { 138 tearDown(); 139 } 140 141 } 142 143} 144