DEV Community

Cover image for Morse code
Iniyan
Iniyan

Posted on

Morse code

How to Build a Simple Morse Code Translator and Player in Python

Morse code is a system of communication that uses dots, dashes, and spaces to represent letters and numbers. It was widely used in the past for long-distance communication, and it is still used today by amateur radio operators and enthusiasts. In this tutorial, we will build a simple Morse code translator and player in Python using the winsoundmodule.

Define the Morse Code Dictionary

We start by defining a dictionary that maps each letter to its Morse code representation. We will also include a space character to separate words in the message. Here is an example of what the dictionary may look like:

trans_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":'--...'," ":' '
}
Enter fullscreen mode Exit fullscreen mode

Take User Input

Next, we ask the user to enter a message to be translated into Morse code. We can use the input() function for this:

message = input("Enter your message:\n")
Enter fullscreen mode Exit fullscreen mode

Convert Message to Morse Code

We can now convert the user's message to Morse code using the trans_dict dictionary we defined earlier. We can loop through each character in the message and use the dictionary to look up its Morse code representation. We can join these Morse code characters together into a single string using the join() method:

trans = " ".join(trans_dict[c] for c in message.upper())
print(trans)
Enter fullscreen mode Exit fullscreen mode

Play Morse Code as Beeps

Finally, we can play the Morse code as beeps using the winsound module. We can loop through each Morse code character in the trans string and use conditional statements to determine whether it is a dot, dash, or space. We can then use the time module to create time delays between beeps to distinguish between dots, dashes, and spaces. Here is an example of what the code may look like:

import time
import winsound

for c in trans:
    if c == '.':
        time.sleep(0.25)
        winsound.Beep(1500, 500)
    elif c == '-':
        time.sleep(0.25)
        winsound.Beep(1500, 900)
    elif c  == ' ':
        time.sleep(0.50)
Enter fullscreen mode Exit fullscreen mode

Overall code

import time
import winsound
trans_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":'--...'," ":' '
}
message = input("enter your message:\n")
trans = " ".join(trans_dict[c] for c in message.upper())
print(trans)
for c in trans:
    if c == '.':
        time.sleep(0.25)
        winsound.Beep(1500, 500)
    elif c == '-':
        time.sleep(0.25)
        winsound.Beep(1500, 900)
    elif c  == ' ':
        time.sleep(0.50)
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we learned how to build a simple Morse code translator and player in Python using the winsound module. We started by defining a dictionary that maps each letter to its Morse code representation. We then took user input, converted the message to Morse code, and played it as beeps using the winsound module. With some additional improvements, this code could be used as a useful tool for learning and practicing Morse code.

Top comments (0)