Skip to content

Instantly share code, notes, and snippets.

@kcal2845
Created January 13, 2019 17:08
Show Gist options
  • Save kcal2845/3f4a5f339d20533c6b93cf3bda28f233 to your computer and use it in GitHub Desktop.
Save kcal2845/3f4a5f339d20533c6b93cf3bda28f233 to your computer and use it in GitHub Desktop.
# 모스 코드를 음파로 변환합니다.
# 제작자 : kcal2845
# 구동 환경 : python 3.6(yaudio와 numpy 모듈 필요)
import pyaudio
import numpy as np
from time import sleep
def sin_wave(f,d): # f = 주파수, d = 실행 길이
a = 1 # 진폭
n = np.arange(fs*d) # n(0,1,2... fs*d)
return a*np.sin(2*np.pi*f/fs*n) # 샘플링
def translate(word,f):
morse_word = ''
for i in word:
if i == ' ':
morse_word += ' '
elif i == '-' or i == '.':
morse_word += i
else:
morse_word += morse_dict[i]
morse_word += ' '
print(morse_word)
for i in morse_word:
if i == ' ':
sleep(0.05)
elif i == '.':
samples = sin_wave(f,0.2)
stream.write(samples.astype(np.float32))
sleep(0.1)
elif i == '-':
samples = sin_wave(f,0.6)
stream.write(samples.astype(np.float32))
sleep(0.1)
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': '--..', '0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.'
}
fs = 44100 # 샘플링 주파수
p = pyaudio.PyAudio()
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
print('알파벳을 입력하면 모스부호로 변환합니다.')
print('나가려면 exit;를 입력하세요.')
while True:
words = input()
if words == 'exit;':
break
translate(words,880)
sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment