/* * [Sound.java] * * Summary: Generate a sound created mathematically. * * Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1997-12-05 * 1.1 1998-11-10 add name and address. * 1.2 2001-03-26 add documentation * 1.3 2007-01-01 add documentation * 1.4 2007-05-23 add PAD and icon. Use IntelliJ inspector. */ package com.mindprod.sound; import sun.audio.AudioPlayer; import java.io.ByteArrayInputStream; import static java.lang.System.*; /** * 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. * @since 1997-12-05 */ public final class Sound { private static final int FIRST_COPYRIGHT_YEAR = 1998; /** * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1998-2017 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"; 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. ) ) ); } out.println( "Creating U_Law" ); U_Law ulaw = new U_Law( " Whoop, whoop, whoop", pcm_linear ); byte[] newb = ulaw.toBytes(); out.println( "Playing U_Law" ); ByteArrayInputStream bis = new ByteArrayInputStream( newb ); AudioPlayer.player.start( bis ); Thread.sleep( length * 1000 ); } catch ( Exception e ) { e.printStackTrace( System.err ); err.println(); } } // end main } // end TestSound