/* copyright (c) 1998-2009 Canadian Mind Products
* Roedy Green
* Canadian Mind Products
* #101 - 2536 Wark Street
* Victoria, BC Canada V8T 4G8
* tel: (250) 361-9093
* roedy g at mindprod dotcom
* http://mindprod.com
*/

// version 1.4 2007-05-23, add PAD and icon.  Use IntelliJ inspector.
// version 1.2 2001-03-26 - add documentation
// Version 1.1 1998-11-10 - add name and address.
// Version 1.0 1997-12-5
package com.mindprod.sound;

import sun.audio.AudioPlayer;

import java.io.ByteArrayInputStream;

/**
 * Sound.java Generate a sound created mathematically.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.4 2007-05-22 add PAD and icon, more IntelliJ inspect tidying.
 */
public final class Sound
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * @noinspection UnusedDeclaration
     */
    private static final String EMBEDDED_COPYRIGHT =
            "copyright (c) 1998-2009 Roedy Green, Canadian Mind Products, http://mindprod.com";

    /**
     * @noinspection UnusedDeclaration
     */
    private static final String RELEASE_DATE = "2007-05-24";

    /**
     * @noinspection UnusedDeclaration
     */
    private static final String VERSION_STRING = "1.4";

    // --------------------------- main() method ---------------------------

    public static void main( String[] args )
        {
        try
            {// make 4 seconds of sound
            int length = 4;

            // 8000 samples per second
            short[] pcm_linear = new short[8000 * length];

            for ( int i = 0; i < pcm_linear.length; i++ )
                {
                //  more interesting noise
                // pcm_linear[i] = (short)(16000*Math.sin((double)(i*i)/10000.0 * (2*3.1416)));

                // bump up the volume so range between -16,000 .. +16,000
                // make a sine wave of 120 Hz.
                // Since you are only sampling at 8000 samples per second,
                // can't expect this to work above 4000 Hz.
                // You will get a stroboscopic beat effect.
                pcm_linear[ i ] =
                        ( short ) ( 16000 * Math.sin( i * ( 120. * 2 * Math.PI
                                                            / 8000. ) ) );
                }
            System.out.println( "Creating U_Law" );
            U_Law ulaw = new U_Law( " Whoop, whoop, whoop", pcm_linear );
            byte[] newb = ulaw.toBytes();

            System.out.println( "Playing U_Law" );
            ByteArrayInputStream bis = new ByteArrayInputStream( newb );

            AudioPlayer.player.start( bis );
            Thread.sleep( length * 1000 );
            }
        catch ( Exception e )
            {
            e.printStackTrace();
            }
        }// end main
    }// end TestSound