-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr2morse.py
76 lines (63 loc) · 2.06 KB
/
str2morse.py
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
import array
import math
import time
import pyaudio
morse = ["","",".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
showdebug = str(input("Show debug prints? (Y/N): "))
if showdebug == "Y":
showdbug = True
else:
showdbug = False
def Str2Morse(string):
tablestr = list(string)
endstrtable = []
try:
for i in tablestr:
if i != " ":
cletter = int(i, 36) - 8
if showdbug:
print("CHARACTER " + str(cletter - 1) + " (" + i + ") GET")
endstrtable.append(morse[cletter])
else:
if showdbug:
print("CHARACTER -1 ( ) GET")
endstrtable.append("/")
except ValueError:
print(f"You inputted a illegal base 36 to base 10 ({i}) character!")
except:
print("Plz debug, something is wrong...")
newstring = " ".join(endstrtable)
return newstring
while True:
instring = str(input("Words: "))
endstr = Str2Morse(instring)
print(endstr)
for i in list(endstr):
p = pyaudio.PyAudio()
volume = 0.5 # range [0.0, 1.0]
fs = 44100
if i == "-":
duration = 0.5
if showdbug:
print("DASH READ")
elif i == ".":
duration = 0.2
if showdbug:
print("DOT READ")
else:
duration = 0
if showdbug:
print("NULL READ")
f = 1000
num_samples = int(fs * duration)
samples = [volume * math.sin(2 * math.pi * k * f / fs) for k in range(0, num_samples)]
output_bytes = array.array('f', samples).tobytes()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=fs,
output=True)
start_time = time.time()
stream.write(output_bytes)
stream.stop_stream()
stream.close()
p.terminate()