-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_code.py
More file actions
executable file
·65 lines (55 loc) · 2.29 KB
/
morse_code.py
File metadata and controls
executable file
·65 lines (55 loc) · 2.29 KB
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
from playsound import playsound
from time import sleep
from pydub import AudioSegment
# Dictionary representing the international morse code
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': '--..',
'1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....',
'7': '--...', '8': '---..', '9': '----.',
'0': '-----', ' ': ' '}
REVERSE_MORSE_CODE_DICT = {v: k for k, v in MORSE_CODE_DICT.items()}
def encode(message):
cipher = ''
for letter in message.upper():
if letter.isalnum():
cipher += MORSE_CODE_DICT[letter] + ' '
else:
cipher += '/ '
return cipher
def decode(message):
symbols = message.split()
decipher = [REVERSE_MORSE_CODE_DICT[s] if (
s != '/') else ' ' for s in symbols]
return ''.join(decipher)
def play_morse_code(message):
for character in encode(message.upper()):
if character == '.':
playsound('sounds/dit.wav')
sleep(0.2)
if character == '-':
playsound('sounds/dah.wav')
sleep(0.2)
if character == ' ' or character == '/':
sleep(0.32)
def generate_morse_code_wav(message):
dah = AudioSegment.from_wav('sounds/dah.wav')
dit = AudioSegment.from_wav('sounds/dit.wav')
space_05 = AudioSegment.from_wav('sounds/silent_quarter-second.wav')
space_10 = AudioSegment.from_wav('sounds/silent_half-second.wav')
combined = AudioSegment.empty()
for character in encode(message.upper()):
if character == '.':
combined += (dit + space_05)
if character == '-':
combined += (dah + space_05)
if character == ' ' or character == '/':
combined += space_10
combined.export("output.wav", format="wav")