Morse Code Tone

<< Mr Potato Head | OtherProjectsTrailIndex | Birds >>

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;


public class ToneTester {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static final int DIT=90;
	public static final float FRQ=650F;

	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		int dah=3*DIT;
		int gap=1;
		int letterGap=3*gap;
		int wordGap=7*gap;
		generateTone(FRQ,dah,100);
		generateTone(FRQ,DIT,100);
		generateTone(FRQ,dah,100);
		generateTone(FRQ,DIT,100);
	}

	public static void generateTone(float hz1, int msecs, int volume)

			{
					float frequency = 44100.0F;
					int samplesize = 8;
					int channels;
					boolean signed = true;
					boolean bigendian = false;
					byte[] buf;
					double ttpi = (2.0 * Math.PI);
					AudioFormat format;
					buf = new byte[1];
					channels = 1;
					format = new AudioFormat(frequency, samplesize, channels, signed,
					                         bigendian);
					try {
						SourceDataLine sdl = AudioSystem.getSourceDataLine(format);

						sdl.open(format);
						sdl.start();
						for (int i = 0; i < msecs * frequency / 1000; i++)
						{
						    double angle = i / (frequency / hz1)* ttpi;

						    buf[0] = (byte) (Math.sin(angle) * volume);

						    sdl.write(buf, 0, 1);
						}
						sdl.drain();
						sdl.stop();
						sdl.close();
					} catch ( Exception e){
						e.printStackTrace();
					}
			}
}