-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython code morce.py
More file actions
213 lines (187 loc) Β· 7.32 KB
/
Python code morce.py
File metadata and controls
213 lines (187 loc) Β· 7.32 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Enhanced Interactive Morse Code Translator
# Features:
# - Text β Morse Code translation
# - Play Morse code as audio beeps (using system beep or pygame)
# - Flash Morse code on screen (visual dots/dashes)
# - Save translations to file
# - Load previous text/Morse from file
# - Clean, colorful, user-friendly menu with input validation
# - Supports punctuation and prosigns
import os
import time
import sys
from datetime import datetime
# Try to use pygame for better audio, fall back to simple beep
try:
import pygame
pygame.init()
SOUND_AVAILABLE = True
except ImportError:
SOUND_AVAILABLE = False
# Morse code dictionary (International Morse Code + common punctuation)
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': '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
'5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
'.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
'/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...',
';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-',
'"': '.-..-.', '$': '...-..-', '@': '.--.-.',
' ': ' / ' # Word space
}
REVERSE_DICT = {v: k for k, v in MORSE_CODE_DICT.items() if k != ' '}
# Timing constants (in seconds)
DOT_DURATION = 0.2
DASH_DURATION = DOT_DURATION * 3
SYMBOL_GAP = DOT_DURATION
LETTER_GAP = DOT_DURATION * 3
WORD_GAP = DOT_DURATION * 7
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def beep(frequency=800, duration=0.1):
if SOUND_AVAILABLE:
sound = pygame.mixer.Sound(buffer=pygame.sndarray.make_sound(
pygame.sndarray.samples(pygame.mixer.Sound(pygame.mixer.Sound(buffer=b'\x00\x7F' * int(44100 * duration / 100))), (44100,)))
sound.set_volume(0.3)
sound.play()
time.sleep(duration)
else:
# Fallback to system beep (Windows) or print bell (others)
if os.name == 'nt':
import winsound
winsound.Beep(frequency, int(duration * 1000))
else:
print('\a', end='')
time.sleep(duration)
def flash_symbol(symbol):
if symbol == '.':
print("π΄", end='', flush=True)
time.sleep(DOT_DURATION)
print("\b \b", end='', flush=True)
elif symbol == '-':
print("π₯" * 3, end='', flush=True)
time.sleep(DASH_DURATION)
print("\b \b" * 3, end='', flush=True)
def play_morse_audio(morse):
print("\nPlaying Morse code audio...\n")
for symbol in morse.replace(' ', ''):
if symbol == '.':
beep(800, DOT_DURATION)
elif symbol == '-':
beep(800, DASH_DURATION)
time.sleep(SYMBOL_GAP)
print("Done playing.\n")
def flash_morse_visual(morse):
print("\nFlashing Morse code visually...\n")
for symbol in morse:
if symbol == '.':
flash_symbol('.')
time.sleep(SYMBOL_GAP)
elif symbol == '-':
flash_symbol('-')
time.sleep(SYMBOL_GAP)
elif symbol == ' ':
time.sleep(LETTER_GAP - SYMBOL_GAP)
elif symbol == '/':
time.sleep(WORD_GAP - LETTER_GAP)
print("\nDone flashing.\n")
def text_to_morse(text):
text = text.upper()
morse_parts = []
for char in text:
if char in MORSE_CODE_DICT:
morse_parts.append(MORSE_CODE_DICT[char])
else:
morse_parts.append('<?>') # Unknown
return ' '.join(morse_parts)
def morse_to_text(morse):
morse = morse.strip()
# Normalize separators
morse = morse.replace('/', ' / ').replace(' ', ' ')
words = morse.split(' / ')
result = []
for word in words:
letters = word.split()
decoded_word = ''.join(REVERSE_DICT.get(code, '?') for code in letters)
result.append(decoded_word)
return ' '.join(result)
def save_translation(text, morse):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("morse_history.txt", "a", encoding="utf-8") as f:
f.write(f"[{timestamp}]\nText: {text}\nMorse: {morse}\n{'-'*50}\n")
print("Translation saved to 'morse_history.txt'")
def load_history():
if os.path.exists("morse_history.txt"):
with open("morse_history.txt", "r", encoding="utf-8") as f:
print(f.read())
else:
print("No history file found.")
def main_menu():
while True:
clear_screen()
print("\033[96m" + "="*60)
print(" π¨ MORSE CODE TRANSLATOR PRO π¨".center(60))
print("="*60 + "\033[0m")
print("\n\033[1mMain Menu:\033[0m")
print("1. Text β Morse Code")
print("2. Morse Code β Text")
print("3. Play Morse Code (Audio)")
print("4. Flash Morse Code (Visual)")
print("5. Save Last Translation")
print("6. View History")
print("7. Exit")
choice = input("\n\033[93mEnter your choice (1-7): \033[0m").strip()
if choice == '1':
text = input("\n\033[92mEnter text to convert: \033[0m").strip()
if text:
morse = text_to_morse(text)
print(f"\n\033[95mMorse Code:\033[0m\n{morse}")
last_translation = (text, morse)
else:
print("\033[91mError: Empty input!\033[0m")
elif choice == '2':
morse = input("\n\033[92mEnter Morse code (. - / separators): \033[0m").strip()
if morse:
text = morse_to_text(morse)
print(f"\n\033[95mDecoded Text:\033[0m\n{text}")
last_translation = (text, morse)
else:
print("\033[91mError: Empty input!\033[0m")
elif choice == '3':
if 'last_translation' in locals() and last_translation[1]:
play_morse_audio(last_translation[1].replace('/', ' '))
else:
print("\033[91mNo Morse code to play! Convert text first.\033[0m")
elif choice == '4':
if 'last_translation' in locals() and last_translation[1]:
flash_morse_visual(last_translation[1])
else:
print("\033[91mNo No Morse code to flash! Convert text first.\033[0m")
elif choice == '5':
if 'last_translation' in locals():
save_translation(*last_translation)
else:
print("\033[91mNothing to save yet!\033[0m")
elif choice == '6':
load_history()
elif choice == '7':
print("\n\033[96mThank you for using Morse Code Translator Pro!\033[0m")
print("73 (Best regards) π\n")
break
else:
print("\033[91mInvalid choice! Please try again.\033[0m")
input("\nPress Enter to continue...")
if __name__ == "__main__":
try:
last_translation = None
main_menu()
except KeyboardInterrupt:
print("\n\n\033[96mGoodbye! 73 π\033[0m")
finally:
if SOUND_AVAILABLE:
pygame.quit()