How to Build a Morse Code Translator with Player in Python
Artiom
Posted on January 30, 2024
Morse code, a classic method of encoding text into sequences of dots and dashes, has been a cornerstone in the world of communication. While it's less common today, learning Morse code can be an exciting challenge, especially if you're interested in amateur radio or historical communication methods. To get a hands-on feel for Morse code, check out an online Morse code tool like convertmorse.com.
In this beginner-friendly tutorial, we'll walk through creating a Morse Code Translator and Player in Python. Let's dive in!
Step 1: Setting Up the Morse Code Dictionary
Our first task is to map each alphabet letter and number to its corresponding Morse code sequence. We use a Python dictionary to do this:
morse_code_dict = {
"A": ".-", "B": "-...", "C": "-.-.",
"D": "-..", "E": ".", "F": "..-.",
"G": "--.", "H": "....", "I": "..",
"J": ".---", "K": "-.-", "L": ".-..",
"M": "--", "N": "-.", "O": "---",
"P": ".--.", "Q": "--.-", "R": ".-.",
"S": "...", "T": "-", "U": "..-",
"V": "...-", "W": ".--", "X": "-..-",
"Y": "-.--", "Z": "--..", " ": "/"
}
Step 2: Input the Message
Now, let's get the message from the user:
message = input("Type a message to translate into Morse code: ")
Step 3: Translating to Morse Code
The next step is translating each character of the message into Morse code. We'll iterate through the message and convert each character:
morse_message = " ".join(morse_code_dict[char] for char in message.upper())
print("Morse Code:", morse_message)
Step 4: Play the Morse Code
To make our translator more interactive, we'll add functionality to play the Morse code using sound. We use the winsound module for Windows users. For each dot, we play a short beep, and for each dash, a longer beep:
import winsound
import time
for char in morse_message:
if char == ".":
winsound.Beep(1000, 200) # frequency, duration in ms
elif char == "-":
winsound.Beep(1000, 600)
elif char == " ":
time.sleep(0.5) # pause for space
time.sleep(0.2) # short pause between each signal
Complete Program
Putting all these steps together, you have a simple Morse code translator and player in Python:
# Complete program here combining all the above steps
import time
import winsound
# Morse Code Dictionary
morse_code_dict = {
"A": ".-", "B": "-...", "C": "-.-.",
"D": "-..", "E": ".", "F": "..-.",
"G": "--.", "H": "....", "I": "..",
"J": ".---", "K": "-.-", "L": ".-..",
"M": "--", "N": "-.", "O": "---",
"P": ".--.", "Q": "--.-", "R": ".-.",
"S": "...", "T": "-", "U": "..-",
"V": "...-", "W": ".--", "X": "-..-",
"Y": "-.--", "Z": "--..", " ": "/"
}
# Input Message
message = input("Type a message to translate into Morse code: ")
# Translate to Morse Code
morse_message = " ".join(morse_code_dict[char] for char in message.upper())
print("Morse Code:", morse_message)
# Play Morse Code
for char in morse_message:
if char == ".":
winsound.Beep(1000, 200) # frequency, duration in ms for a dot
elif char == "-":
winsound.Beep(1000, 600) # frequency, duration in ms for a dash
elif char == " ":
time.sleep(0.5) # pause for space
time.sleep(0.2) # short pause between each signal
Posted on January 30, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.