1 /* 2 * Copyright (c) 2010, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* @test 27 @summary Test AudioFloatInputStream.getFrameLength() returned from 28 ModelByteBufferWavetable openStream method */ 29 30 import java.io.ByteArrayOutputStream; 31 import java.io.File; 32 import java.io.FileOutputStream; 33 34 import javax.sound.sampled.*; 35 36 import com.sun.media.sound.*; 37 38 public class OpenStream { 39 40 static float[] testarray; 41 42 static byte[] test_byte_array; 43 44 static byte[] test_byte_array_8ext; 45 46 static AudioFormat format = new AudioFormat(44100, 16, 1, true, false); 47 48 static AudioFormat format24 = new AudioFormat(44100, 24, 1, true, false); 49 50 static ModelByteBuffer buffer; 51 52 static ModelByteBuffer buffer_wave; 53 54 static ModelByteBuffer buffer8; 55 56 static ModelByteBuffer buffer16_8; 57 58 static ModelByteBuffer buffer24; 59 60 static File test_file; 61 62 static ModelByteBuffer buffer_wave_ondisk; 63 64 static void setUp() throws Exception { 65 testarray = new float[1024]; 66 for (int i = 0; i < 1024; i++) { 67 double ii = i / 1024.0; 68 ii = ii * ii; 69 testarray[i] = (float) Math.sin(10 * ii * 2 * Math.PI); 70 testarray[i] += (float) Math.sin(1.731 + 2 * ii * 2 * Math.PI); 71 testarray[i] += (float) Math.sin(0.231 + 6.3 * ii * 2 * Math.PI); 72 testarray[i] *= 0.3; 73 } 74 test_byte_array = new byte[testarray.length * 2]; 75 AudioFloatConverter.getConverter(format).toByteArray(testarray, 76 test_byte_array); 77 buffer = new ModelByteBuffer(test_byte_array); 78 79 byte[] test_byte_array2 = new byte[testarray.length * 3]; 80 buffer24 = new ModelByteBuffer(test_byte_array2); 81 test_byte_array_8ext = new byte[testarray.length]; 82 byte[] test_byte_array_8_16 = new byte[testarray.length * 2]; 83 AudioFloatConverter.getConverter(format24).toByteArray(testarray, 84 test_byte_array2); 85 int ix = 0; 86 int x = 0; 87 for (int i = 0; i < test_byte_array_8ext.length; i++) { 88 test_byte_array_8ext[i] = test_byte_array2[ix++]; 89 test_byte_array_8_16[x++] = test_byte_array2[ix++]; 90 test_byte_array_8_16[x++] = test_byte_array2[ix++]; 91 } 92 buffer16_8 = new ModelByteBuffer(test_byte_array_8_16); 93 buffer8 = new ModelByteBuffer(test_byte_array_8ext); 94 95 AudioInputStream ais = new AudioInputStream(buffer.getInputStream(), 96 format, testarray.length); 97 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 98 AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos); 99 buffer_wave = new ModelByteBuffer(baos.toByteArray()); 100 101 test_file = File.createTempFile("test", ".raw"); 102 FileOutputStream fos = new FileOutputStream(test_file); 103 fos.write(baos.toByteArray()); 104 fos.close(); 105 buffer_wave_ondisk = new ModelByteBuffer(test_file); 106 107 } 108 109 static void tearDown() throws Exception { 110 if (!test_file.delete()) 111 test_file.deleteOnExit(); 112 } 113 114 public static void testOpenStream(ModelByteBufferWavetable wavetable) 115 throws Exception { 116 AudioFloatInputStream ais = wavetable.openStream(); 117 long frames = wavetable.getBuffer().capacity() 118 / wavetable.getFormat().getFrameSize(); 119 long framelength = ais.getFrameLength(); 120 ais.close(); 121 if (frames != framelength) { 122 throw new Exception("Incorrect framelength returned (" + frames 123 + " != " + framelength + ")"); 124 } 125 } 126 127 public static void main(String[] args) throws Exception { 128 129 setUp(); 130 131 try { 132 testOpenStream(new ModelByteBufferWavetable(buffer, format)); 133 testOpenStream(new ModelByteBufferWavetable(buffer_wave, format)); 134 testOpenStream(new ModelByteBufferWavetable(buffer_wave_ondisk, 135 format)); 136 } finally { 137 tearDown(); 138 } 139 140 } 141 142 }