Welcome, guest | Sign In | My Account | Store | Cart

This is not for the big guns, but for the Amateur coder AND radio enthusiast.

It is a DEMO Morse Code Generator and tutor that will generate the tones required for the original standard letters A to Z and numbers 0 to 9. Puctuation is NOT included but hey it is SOOOO simple to modify the code that I will let you, (the ones interested), do that for yourselves... ;o) Read the code for more informaton.

It is for at least standard text mode Python 2.5.2 to 2.7.2 using PCLinuxOS 2009 and Debian 6.0.0. It may well work at a much earlier version. I do have a version for Python 3.x.x but that will be uploaded elsewhere.

As it stands it _sends_ at around 8 WPM, (Words Per Minute).

It is written in such a way that youngsters can understand what is going on.

Enjoy finding simple solutions to often very difficult problems... ;o)

Bazza, G0LCU...

Python, 146 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# MCG2x.py
#
# A simple Morse Code Generator and tutor.
# (C)2010-2012, B.Walker, G0LCU.
#
# A DEMO fun Python program to generate the standard Morse Code tones
# out through the computer's speakers.
#
# Written in such a way that youngsters can understand what is going on.
#
# This is for Python Version 3.x.x using AT LEAST PClinuxOS 2009 and
# Debian 6.0.0...
#
# It is assumed that /dev/audio exists; if NOT, then install oss-compat
# from the distro`s repository.
#
# Ensure the sound system is not already in use.
#
# It is also asumed that the ?nix command "clear" is available.
#
# Copy the file to the Lib folder(/drawer/directory) or where the modules
# reside as "MCG2x.py"...
#
# For a quick way to run just use at the ">>>" prompt:-
#
# >>> import MCG2x<RETURN/ENTER>
#
# And away we go...
#
# Once running just press <ENTER/RETURN> for a random character or INPUT
# any single STANDRAD ASCII character the <RETURN/ENTER> to practice specific
# characters. Type EXIT or QUIT<RETURN/ENTER> to quit...
# Any __unknown__ character will generate the "?" character and display the
# this character along with the Morse Code tones for it...
# Read the Python code below for more information...
#
# Enjoy finding simple solutions to often VERY difficult questions... ;o)

# The standard imports required...
import os
import time
import random
import string

def main():
	# Make variables global, my choice... ;o)
	global delay
	global text
	global ascii_num
	global dah_dit
	global morse_code
	global count

	# Allocate start values...
	delay=225
	text="(C)2010-2012, B.Walker, G0LCU..."
	ascii_num=63
	dah_dit=0
	# The code below is the "?" character for any unknown character entered.
	morse_code="..--.."
	count=0

	while 1:
		# Enter a simple loop...
		# A basic ?nix type screen clear command...
		count=os.system("clear")
		# A simple user screen, QUIT or EXIT<RETURN/ENTER> to STOP...
		print "\nA simple Morse Code Generator and tutor.\n"
		print "(C)2010-2012, B.Walker, G0LCU.\n"
		print "It has a fixed speed of around 8 words per minute...\n"
		text=raw_input("Press <RETURN/ENTER> to continue:- ")
		text=string.upper(text)
		if text=="QUIT" or text=="EXIT": break
		# Don't allow any errors.
		if len(text)>=2: text=""
		if text=="": ascii_num=int(random.random()*43)+48
		if text!="": ascii_num=ord(text)
		# This is the "?" character...
		if ascii_num>=58 and ascii_num<=64:
			morse_code="..--.."
			ascii_num=63
		if ascii_num<=47 or ascii_num>=91:
			morse_code="..--.."
			ascii_num=63
		# Now to generate the relevant code according to the correct ASCII
		# character entered.
		if ascii_num==48: morse_code="-----"
		if ascii_num==49: morse_code=".----"
		if ascii_num==50: morse_code="..---"
		if ascii_num==51: morse_code="...--"
		if ascii_num==52: morse_code="....-"
		if ascii_num==53: morse_code="....."
		if ascii_num==54: morse_code="-...."
		if ascii_num==55: morse_code="--..."
		if ascii_num==56: morse_code="---.."
		if ascii_num==57: morse_code="----."
		if ascii_num==65: morse_code=".-"
		if ascii_num==66: morse_code="-..."
		if ascii_num==67: morse_code="-.-."
		if ascii_num==68: morse_code="-.."
		if ascii_num==69: morse_code="."
		if ascii_num==70: morse_code="..-."
		if ascii_num==71: morse_code="--."
		if ascii_num==72: morse_code="...."
		if ascii_num==73: morse_code=".."
		if ascii_num==74: morse_code=".---"
		if ascii_num==75: morse_code="-.-"
		if ascii_num==76: morse_code=".-.."
		if ascii_num==77: morse_code="--"
		if ascii_num==78: morse_code="-."
		if ascii_num==79: morse_code="---"
		if ascii_num==80: morse_code=".--."
		if ascii_num==81: morse_code="--.-"
		if ascii_num==82: morse_code=".-."
		if ascii_num==83: morse_code="..."
		if ascii_num==84: morse_code="-"
		if ascii_num==85: morse_code="..-"
		if ascii_num==86: morse_code="...-"
		if ascii_num==87: morse_code=".--"
		if ascii_num==88: morse_code="-..-"
		if ascii_num==89: morse_code="-.--"
		if ascii_num==90: morse_code="--.."
		# Now print the result(s)...
		print "\nASCII character "+chr(ascii_num)+"...\n"
		# Note the exclamation marks are NOT an error!
		print "Morse Code!   "+morse_code+"   !\n"
		# Open the "/dev/audio" device for writing...
		audio=open("/dev/audio", "wb")
		for dah_dit in range(0,len(morse_code),1):
			if morse_code[dah_dit]=="-": delay=225
			if morse_code[dah_dit]==".": delay=75
			# Play a crude sine-wave note at 1KHz of length "delay"...
			for count in range(0,delay,1):
				audio.write(chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3))
			# Add a gap roughly the same as a "dit" and NOT closing the audio device!
			for count in range(0,75,1):
				audio.write(chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0)+chr(0))
		# Ensure that the audio device is closed after each character!
		audio.close()
		# Add a short delay to see the code on screen before starting again.
		time.sleep(1)

main()

# End of MCG2x.py DEMO...
# Enjoy finding simple solutions to often VERY difficult questions... ;o)

You don't need a Maths or Physics Doctorate to do these things for yourselves, just someone to lay down the ideas so that you can expand and make them much better...

Good luck guys and gals, and......

Enjoy finding simple solutions to often VERY difficult problems...

Bazza, G0LCU...

2 comments

Lineolh 11 years, 12 months ago  # | flag

Hi,

I am actually using your code to transcript a string into morse code (the whole string, and not only character after character). I succeed in that but, I just have a question about this row :

<pre><code> audio.write(chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3)) </code></pre>

Could you explain me how does this function works ?

Thanks in advance.

Barry Walker (author) 11 years, 11 months ago  # | flag

Sorry about the delay in replying and apologies for nay typos...

The code below relies on Python Versions 2.7.x and LOWER.

audio.write(chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3))

This code is for Python 3.x.x to give the same result:-

audio.write(b'\x0f\x2d\x3f\x2d\x0f\x03\x00\x03')

The Linux audio devices /dev/audio and /dev/dsp default to 8 bit, mono, and 8KHz sampling speed. You can read and write to these devices using a CHARACTER string for Python 2.7.x and lower or as bytes for Python 3.x.x.

The values set in the string generate a crude sinewave allowing for the logarithmic relationship between the sound generating device and the waveform required as viewed on an oscilloscope. Although this empirical method of generating the waveform is consistent with all of the many computers I've used it may not hold true on every computer - hence "as viewed on an oscilloscope".

See other Python uploads I have used using the /dev/audio or dev/dsp.

dev/audio has a clean, working range from decimal 0 to 63 and dev/dsp from decimal 0 to 255...

These are all from experimentation using MANY different computers using Linux that have the two devices shown...

Note also that (sampling_rate_in_Hz/number_of_bytes_written) = frequency in Hz:-

8000/8 = 1000Hz or 1KHz...

Hope this helps...