【python 2.7】输入任意字母数字,输出其对应的莫尔斯码并播放声音

时间:2022-11-04 22:29:35

 

 

 

#python 2.7
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import winsound,sys,time

__author__ = 'BH8ANK'


morse_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':'--..',
    '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':'----.',
    ' ':' ',
}

wave_dict = {
    'a':r'F:\personal\Radio\cw_sound\A.wav',
    'b':r'F:\personal\Radio\cw_sound\B.wav',
    'c':r'F:\personal\Radio\cw_sound\C.wav',
    'd':r'F:\personal\Radio\cw_sound\D.wav',
    'e':r'F:\personal\Radio\cw_sound\E.wav',
    'f':r'F:\personal\Radio\cw_sound\F.wav',
    'g':r'F:\personal\Radio\cw_sound\G.wav',
    'h':r'F:\personal\Radio\cw_sound\H.wav', 
    'i':r'F:\personal\Radio\cw_sound\I.wav',
    'j':r'F:\personal\Radio\cw_sound\J.wav',
    'k':r'F:\personal\Radio\cw_sound\K.wav',
    'l':r'F:\personal\Radio\cw_sound\L.wav',
    'm':r'F:\personal\Radio\cw_sound\M.wav',
    'n':r'F:\personal\Radio\cw_sound\N.wav',
    'o':r'F:\personal\Radio\cw_sound\O.wav',
    'p':r'F:\personal\Radio\cw_sound\P.wav',
    'q':r'F:\personal\Radio\cw_sound\Q.wav',
    'r':r'F:\personal\Radio\cw_sound\R.wav',
    's':r'F:\personal\Radio\cw_sound\S.wav',
    't':r'F:\personal\Radio\cw_sound\T.wav',
    'u':r'F:\personal\Radio\cw_sound\U.wav',
    'v':r'F:\personal\Radio\cw_sound\V.wav',
    'w':r'F:\personal\Radio\cw_sound\W.wav',
    'x':r'F:\personal\Radio\cw_sound\X.wav',
    'y':r'F:\personal\Radio\cw_sound\Y.wav',
    'z':r'F:\personal\Radio\cw_sound\Z.wav',
    '1':r'F:\personal\Radio\cw_sound\1.wav',
    '2':r'F:\personal\Radio\cw_sound\2.wav',
    '3':r'F:\personal\Radio\cw_sound\3.wav',
    '4':r'F:\personal\Radio\cw_sound\4.wav',
    '5':r'F:\personal\Radio\cw_sound\5.wav',
    '6':r'F:\personal\Radio\cw_sound\6.wav',
    '7':r'F:\personal\Radio\cw_sound\7.wav',
    '8':r'F:\personal\Radio\cw_sound\8.wav',
    '9':r'F:\personal\Radio\cw_sound\9.wav',
    '0':r'F:\personal\Radio\cw_sound\0.wav',
    ' ':' ',
    }

print("\n")
print("Please input a string(999 to exit):")
print("\n\n")
code = 'initial'

while(code!='999'):
    code = raw_input()
    if(code == '999'):
        print('Bye!')
        break
    for i in code:
        print str(morse_dict[i]),   
        if(' ' == i):
            time.sleep(0.25)
        if(' ' != i):
            winsound.PlaySound(wave_dict[i], winsound.SND_NODEFAULT)
#         os.system("paplay BEEP1.WAV")
    print('\n')
    
    

 

 

音频文件使用业余无线电常用的软件 CW Player逐个生成。

【python 2.7】输入任意字母数字,输出其对应的莫尔斯码并播放声音