Available.java revision 829:b06c29386f63
1353944Sdim/*
2353944Sdim * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
3353944Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4353944Sdim *
5353944Sdim * This code is free software; you can redistribute it and/or modify it
6353944Sdim * under the terms of the GNU General Public License version 2 only, as
7353944Sdim * published by the Free Software Foundation.  Sun designates this
8353944Sdim * particular file as subject to the "Classpath" exception as provided
9353944Sdim * by Sun in the LICENSE file that accompanied this code.
10353944Sdim *
11353944Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
12353944Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13353944Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14353944Sdim * version 2 for more details (a copy is included in the LICENSE file that
15353944Sdim * accompanied this code).
16353944Sdim *
17353944Sdim * You should have received a copy of the GNU General Public License version
18353944Sdim * 2 along with this work; if not, write to the Free Software Foundation,
19353944Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20353944Sdim *
21353944Sdim * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22353944Sdim * CA 95054 USA or visit www.sun.com if you need additional information or
23353944Sdim * have any questions.
24353944Sdim */
25353944Sdim
26353944Sdim/* @test
27353944Sdim   @summary Test AudioFloatInputStream available method */
28353944Sdim
29353944Sdimimport java.io.*;
30353944Sdim
31353944Sdimimport javax.sound.sampled.*;
32353944Sdim
33353944Sdimimport com.sun.media.sound.*;
34353944Sdim
35353944Sdimpublic class Available {
36353944Sdim
37353944Sdim    static float[] test_float_array;
38353944Sdim    static byte[] test_byte_array;
39353944Sdim    static AudioFormat format = new AudioFormat(44100, 16, 1, true, false);
40353944Sdim
41353944Sdim    static AudioFloatInputStream getStream1()
42353944Sdim    {
43353944Sdim        return AudioFloatInputStream.getInputStream(format, test_byte_array, 0, test_byte_array.length);
44353944Sdim    }
45353944Sdim
46353944Sdim    static AudioFloatInputStream getStream2()
47353944Sdim    {
48353944Sdim        AudioInputStream strm = new AudioInputStream(new ByteArrayInputStream(test_byte_array), format, 1024);
49353944Sdim        return AudioFloatInputStream.getInputStream(strm);
50353944Sdim    }
51353944Sdim
52353944Sdim    static void setUp() throws Exception {
53353944Sdim        test_float_array = new float[1024];
54353944Sdim        test_byte_array = new byte[1024*format.getFrameSize()];
55353944Sdim        for (int i = 0; i < 1024; i++) {
56353944Sdim            double ii = i / 1024.0;
57353944Sdim            ii = ii * ii;
58353944Sdim            test_float_array[i] = (float)Math.sin(10*ii*2*Math.PI);
59353944Sdim            test_float_array[i] += (float)Math.sin(1.731 + 2*ii*2*Math.PI);
60353944Sdim            test_float_array[i] += (float)Math.sin(0.231 + 6.3*ii*2*Math.PI);
61353944Sdim            test_float_array[i] *= 0.3;
62353944Sdim        }
63353944Sdim        AudioFloatConverter.getConverter(format).toByteArray(test_float_array, test_byte_array);
64353944Sdim    }
65353944Sdim
66353944Sdim    public static void main(String[] args) throws Exception {
67353944Sdim        setUp();
68353944Sdim        for (int i = 0; i < 2; i++) {
69353944Sdim            AudioFloatInputStream stream = null;
70353944Sdim            if(i == 0) stream = getStream1();
71353944Sdim            if(i == 1) stream = getStream2();
72353944Sdim            float[] buff = new float[512];
73353944Sdim            if(stream.available() != 1024)
74353944Sdim                throw new RuntimeException("stream.available return incorrect value.");
75353944Sdim            stream.read(buff);
76353944Sdim            if(stream.available() != 512)
77353944Sdim                throw new RuntimeException("stream.available return incorrect value.");
78353944Sdim        }
79353944Sdim    }
80353944Sdim
81353944Sdim}
82353944Sdim