User Tools

Site Tools


morsecode

Morse Coder

ToneTester

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

ToneTester demonstrates how to use the javax sound library to generate a morse code “CQ CQ” which is what a HAM radio operator will call to seek out a conversation from anyone who might be able to be listening

ToneTester.java
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
 
public class ToneTester {
 
	/**
	 * ToneTester demonstrates how to use the javax sound
	 * library to generate a morse code "CQ CQ"
	 * which is what a HAM radio operator will
	 * call to seek out a conversation from
	 * anyone who might be able to be listening
	 * 
	 * @author Chris Thiel, ofmCap
	 * @version May 5, 2026
	 */
 
	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};
		int[][] cq = {c,q};
		for (int i = 0; i < 2; i++) {
			playWord(cq);
		}
 
	}
 
	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.txt · Last modified: by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki