AntonioVillanueva

professeur de télégraphie

Oct 30th, 2023 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. #Antonio Villanueva Segura cw python linux
  2. #pip install sounddevice
  3. #pip install pynput
  4. #sudo apt-get install libportaudio2
  5.  
  6. """
  7. python3 -m venv ~/simpleaudio-env
  8. source ~/simpleaudio-env/bin/activate
  9. """
  10.  
  11. import numpy as np
  12. import sounddevice as sd
  13.  
  14. import random
  15. import time
  16. import threading
  17. from pynput import keyboard
  18.  
  19. TONE=900 #Hz
  20. LIGNE=3 #Durée d'une ligne
  21. POINT=1 #Durée d'un point
  22. SPACE=1#Espace entre les symboles
  23. SPACE_MOTS=7 #Espace entre les mots
  24. VITESSE=0.1
  25.  
  26. correcte=0
  27. incorrecte=0
  28. erreur=True #répéter la lettre erreur
  29.  
  30. #dictionnaire de type clé (lettre), valeur (code cw)
  31. cw_dict = {
  32.     'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
  33.     'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
  34.     'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
  35.     'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
  36.     'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
  37.     'Z': '--..',
  38.     '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
  39.     '6': '-....', '7': '--...', '8': '---..', '9': '----.', '0': '-----',
  40.     '+': '.-.-.', '=': '-...-', ',': '--..--', '.': '.-.-.-', '?': '..--..',
  41.     '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...',
  42.     ':': '---...', ';': '-.-.-.', "'": '.----.', '-': '-....-',
  43.     '_': '..--.-', '"': '.-..-.', '$': '...-..-', '!': '-.-.--',
  44.     '@': '.--.-.'
  45. }
  46.  
  47.  
  48. touche=""
  49. press=False
  50.  
  51. def on_press(key):
  52.     """ touche enfoncée """
  53.     global touche
  54.     global press
  55.     """
  56.     if key == keyboard.Key.enter:
  57.         press=True
  58.         enter=True
  59.     else:
  60.     """
  61.  
  62.     try:
  63.         touche =format(key.char)
  64.         press=True
  65.     except AttributeError:
  66.         print("Erreur ".format(key))
  67.         touche=""
  68.  
  69. def on_release(key):
  70.     """touche relachee """
  71.     global press
  72.  
  73.     press=False
  74.  
  75.     if key == keyboard.Key.esc:
  76.         # Stop listener
  77.         return False
  78.        
  79. def cw_aleatoire(cw):
  80.     """ renvoie une clé aléatoire dans le dictionnaire"""
  81.     lettre=""
  82.     while (not lettre.isalpha()):
  83.         lettre=random.choice(list(cw.keys()))
  84.     return lettre
  85.  
  86.  
  87. def playSound(t):
  88.     """ joue un son de freq TONE et d'une durée t"""
  89.     # Génère une tonalité sinusoïdale de la fréquence et de la durée spécifiées
  90.     sin = np.linspace(0, t, int(t * 44100), endpoint=False)
  91.     audio_data = 0.5 * np.sin(2 * np.pi * TONE * sin)
  92.  
  93.     # jouer le son
  94.     sd.play(audio_data, 44100)
  95.  
  96.     # Attends la fin du son
  97.     sd.wait()
  98.  
  99.  
  100. def playChar(c,cw_dict):
  101.     """ joue la lettre """
  102.     lettre= cw_dict[c]
  103.     for s in lettre:
  104.         if s=='.':
  105.             playSound(POINT*VITESSE)
  106.         else:
  107.             playSound(LIGNE*VITESSE)
  108.         time.sleep(SPACE*VITESSE)
  109.  
  110. def charCw (c,cw_dict):
  111.     """ montre le code cw d'une lettre """
  112.     lettre= cw_dict[c]
  113.     print (" ( ",end="")
  114.     for s in lettre:
  115.         if s=='.':
  116.             print ('.',end="")
  117.         else:
  118.             print ('_',end="")
  119.         print(' ',end="")
  120.     print (" ) ",end="")       
  121.  
  122.    
  123. def run():
  124.     global press
  125.     global touche
  126.     global correcte
  127.     global incorrecte
  128.     global erreur
  129.     #boucle de fil principal
  130.     lettre=cw_aleatoire(cw_dict)
  131.     while True:
  132.  
  133.         if not erreur:#Repeter si erreur
  134.             lettre=cw_aleatoire(cw_dict)
  135.  
  136.         playChar(lettre,cw_dict)
  137.        
  138.         #attend qu'on appuie sur une touche
  139.        
  140.         while not press:
  141.             pass
  142.  
  143.    
  144.         """
  145.         #attend qu'on relache la touche    
  146.         while  press:
  147.             pass       
  148.         """
  149.         charCw(lettre,cw_dict)
  150.         if lettre==touche.upper():
  151.             print (" OK",end="")
  152.             correcte+=1
  153.             erreur=False
  154.         else:
  155.             print (" erreur, la lettre était ",lettre,end="")
  156.             incorrecte+=1
  157.             erreur=True #Repeter lettre    
  158.         print (" ( correctes = ",correcte," ,incorrectes =",incorrecte," )")
  159.  
  160.    
  161.    
  162. if __name__ == '__main__':
  163.  
  164.     print ("professeur de CW")
  165.     #fil de lecture des touches
  166.     with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
  167.        
  168.         fil = threading.Thread(target=run)
  169.         fil.start()
  170.         fil.join()
  171.         listener.join()
  172.  
  173.    
Advertisement
Add Comment
Please, Sign In to add comment