User Tools

Site Tools


morsecode

This is an old revision of the document!


Morse Coder

ToneTester

This demonstrates how to generate a morse code tone. The timing is all based on the length of the “dit”

ToneTester.java
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 float FRQ=650F;
	public static final int GAP = 1;
	public static final int LETTER_GAP = 3 * GAP;
	public static final int WORD_GAP = 7 * GAP;
	public static final int VOLUME = 100;
 
 
	public static void main(String[] args)  {
		// 
		int dit= 90;
		int di = dit; // milisecs
		int dah=3*dit;
		int gap=1;
		int letterGap=3*gap;
		int wordGap=7*gap;
 
		int[] c = {dah, di, dah, dit};
		int[] q = {dah, dah, di, dah};
		for (int i = 0; i < 2; i++) {
			play(c);
			play(q);
		}
 
	}
 
	public static void play(int[] letter) {
		for(int t:letter) {
			generateTone(FRQ,t,VOLUME);
		}
		generateTone(0,LETTER_GAP,0);
	}
	public static void playWord(int[][] word) {
		for(int[] letter:word) {
			play(letter);
		}
		generateTone(0, WORD_GAP,0);	
	}
 
	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 tau = (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)* tau;
 
				buf[0] = (byte) (Math.sin(angle) * volume);
 
				sdl.write(buf, 0, 1);
			}
			sdl.drain();
			sdl.stop();
			sdl.close();
		} catch ( Exception e){
			e.printStackTrace();
		}
	}
}
morsecode.1778005630.txt.gz · Last modified: by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki