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