2713N/A/*
2713N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2713N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2713N/A *
2713N/A * This code is free software; you can redistribute it and/or modify it
2713N/A * under the terms of the GNU General Public License version 2 only, as
2713N/A * published by the Free Software Foundation. Oracle designates this
2713N/A * particular file as subject to the "Classpath" exception as provided
2713N/A * by Oracle in the LICENSE file that accompanied this code.
2713N/A *
2713N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2713N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2713N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2713N/A * version 2 for more details (a copy is included in the LICENSE file that
2713N/A * accompanied this code).
2713N/A *
2713N/A * You should have received a copy of the GNU General Public License version
2713N/A * 2 along with this work; if not, write to the Free Software Foundation,
2713N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2713N/A *
2713N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2713N/A * or visit www.oracle.com if you need additional information or have any
2713N/A * questions.
2713N/A */
2713N/A
2713N/A/* @test
2713N/A @summary Test AudioFloatInputStream.getFrameLength() returned from
2713N/A ModelByteBufferWavetable openStream method */
2713N/A
2713N/Aimport java.io.ByteArrayOutputStream;
2713N/Aimport java.io.File;
2713N/Aimport java.io.FileOutputStream;
2713N/A
2713N/Aimport javax.sound.sampled.*;
2713N/A
2713N/Aimport com.sun.media.sound.*;
2713N/A
2713N/Apublic class OpenStream {
2713N/A
2713N/A static float[] testarray;
2713N/A
2713N/A static byte[] test_byte_array;
2713N/A
2713N/A static byte[] test_byte_array_8ext;
2713N/A
2713N/A static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
2713N/A
2713N/A static AudioFormat format24 = new AudioFormat(44100, 24, 1, true, false);
2713N/A
2713N/A static ModelByteBuffer buffer;
2713N/A
2713N/A static ModelByteBuffer buffer_wave;
2713N/A
2713N/A static ModelByteBuffer buffer8;
2713N/A
2713N/A static ModelByteBuffer buffer16_8;
2713N/A
2713N/A static ModelByteBuffer buffer24;
2713N/A
2713N/A static File test_file;
2713N/A
2713N/A static ModelByteBuffer buffer_wave_ondisk;
2713N/A
2713N/A static void setUp() throws Exception {
2713N/A testarray = new float[1024];
2713N/A for (int i = 0; i < 1024; i++) {
2713N/A double ii = i / 1024.0;
2713N/A ii = ii * ii;
2713N/A testarray[i] = (float) Math.sin(10 * ii * 2 * Math.PI);
2713N/A testarray[i] += (float) Math.sin(1.731 + 2 * ii * 2 * Math.PI);
2713N/A testarray[i] += (float) Math.sin(0.231 + 6.3 * ii * 2 * Math.PI);
2713N/A testarray[i] *= 0.3;
2713N/A }
2713N/A test_byte_array = new byte[testarray.length * 2];
2713N/A AudioFloatConverter.getConverter(format).toByteArray(testarray,
2713N/A test_byte_array);
2713N/A buffer = new ModelByteBuffer(test_byte_array);
2713N/A
2713N/A byte[] test_byte_array2 = new byte[testarray.length * 3];
2713N/A buffer24 = new ModelByteBuffer(test_byte_array2);
2713N/A test_byte_array_8ext = new byte[testarray.length];
2713N/A byte[] test_byte_array_8_16 = new byte[testarray.length * 2];
2713N/A AudioFloatConverter.getConverter(format24).toByteArray(testarray,
2713N/A test_byte_array2);
2713N/A int ix = 0;
2713N/A int x = 0;
2713N/A for (int i = 0; i < test_byte_array_8ext.length; i++) {
2713N/A test_byte_array_8ext[i] = test_byte_array2[ix++];
2713N/A test_byte_array_8_16[x++] = test_byte_array2[ix++];
2713N/A test_byte_array_8_16[x++] = test_byte_array2[ix++];
2713N/A }
2713N/A buffer16_8 = new ModelByteBuffer(test_byte_array_8_16);
2713N/A buffer8 = new ModelByteBuffer(test_byte_array_8ext);
2713N/A
2713N/A AudioInputStream ais = new AudioInputStream(buffer.getInputStream(),
2713N/A format, testarray.length);
2713N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
2713N/A AudioSystem.write(ais, AudioFileFormat.Type.WAVE, baos);
2713N/A buffer_wave = new ModelByteBuffer(baos.toByteArray());
2713N/A
2713N/A test_file = File.createTempFile("test", ".raw");
2713N/A FileOutputStream fos = new FileOutputStream(test_file);
2713N/A fos.write(baos.toByteArray());
2713N/A fos.close();
2713N/A buffer_wave_ondisk = new ModelByteBuffer(test_file);
2713N/A
2713N/A }
2713N/A
2713N/A static void tearDown() throws Exception {
2713N/A if (!test_file.delete())
2713N/A test_file.deleteOnExit();
2713N/A }
2713N/A
2713N/A public static void testOpenStream(ModelByteBufferWavetable wavetable)
2713N/A throws Exception {
2713N/A AudioFloatInputStream ais = wavetable.openStream();
2713N/A long frames = wavetable.getBuffer().capacity()
2713N/A / wavetable.getFormat().getFrameSize();
2713N/A long framelength = ais.getFrameLength();
2713N/A ais.close();
2713N/A if (frames != framelength) {
2713N/A throw new Exception("Incorrect framelength returned (" + frames
2713N/A + " != " + framelength + ")");
2713N/A }
2713N/A }
2713N/A
2713N/A public static void main(String[] args) throws Exception {
2713N/A
2713N/A setUp();
2713N/A
2713N/A try {
2713N/A testOpenStream(new ModelByteBufferWavetable(buffer, format));
2713N/A testOpenStream(new ModelByteBufferWavetable(buffer_wave, format));
2713N/A testOpenStream(new ModelByteBufferWavetable(buffer_wave_ondisk,
2713N/A format));
2713N/A } finally {
2713N/A tearDown();
2713N/A }
2713N/A
2713N/A }
2713N/A
2713N/A}