-
Notifications
You must be signed in to change notification settings - Fork 49
Description
import numpy as np
import simpleaudio as sa
import time
Définir le Morse pour chaque lettre
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': '----.'
}
Paramètres du son
frequency = 800 # Hz
fs = 44100 # échantillonnage
dot_duration = 0.2 # secondes
dash_duration = dot_duration * 3
intra_letter_gap = dot_duration
inter_letter_gap = dot_duration * 3
inter_word_gap = dot_duration * 7
Fonction pour générer un bip
def beep(duration):
t = np.linspace(0, duration, int(fs * duration), False)
audio = 0.5 * np.sin(2 * np.pi * frequency * t)
audio = (audio * 32767).astype(np.int16)
play_obj = sa.play_buffer(audio, 1, 2, fs)
play_obj.wait_done()
Convertir le texte en Morse
text = "J AIMERAI TOUJOURS AMINA"
for word in text.split():
for letter in word:
morse = MORSE_CODE_DICT.get(letter.upper())
if morse:
for symbol in morse:
if symbol == '.':
beep(dot_duration)
elif symbol == '-':
beep(dash_duration)
time.sleep(intra_letter_gap)
time.sleep(inter_letter_gap - intra_letter_gap)
time.sleep(inter_word_gap - inter_letter_gap)