-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse.py
executable file
·131 lines (119 loc) · 4.84 KB
/
morse.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
# coding: utf-8
import numpy
from tone import tone
class morse:
length = None
hz = None
t = None
string = None
audio = None
time = 0
def dit(self):
self.audio = numpy.hstack((self.audio, self.t.sine(self.hz, self.length)))
self.time += self.t.time
self.audio = numpy.hstack((self.audio, self.t.sine(0, self.length)))
self.time += self.t.time
def dah(self):
self.audio = numpy.hstack((self.audio, self.t.sine(self.hz, self.length *3)))
self.time += self.t.time
self.audio = numpy.hstack((self.audio, self.t.sine(0, self.length)))
self.time += self.t.time
def space(self):
self.audio = numpy.hstack((self.audio, self.t.sine(0, self.length)))
self.time += self.t.time
def stop(self):
self.audio = numpy.hstack((self.audio, self.t.sine(0, self.length *3)))
self.time += self.t.time
def morse_a(self, char):
if char == ' ':
self.space(), self.stop()
if char == 'a':
self.dit(), self.dah(), self.stop()
if char == 'b':
self.dah(), self.dit(), self.dit(), self.dit(), self.stop()
if char == 'c':
self.dah(), self.dit(), self.dah(), self.dit(), self.stop()
if char == 'd':
self.dah(), self.dit(), self.dit(), self.stop()
if char == 'e':
self.dit(), self.stop()
if char == 'f':
self.dit(), self.dit(), self.dah(), self.dit(), self.stop()
if char == 'g':
self.dah(), self.dah(), self.dit(), self.stop()
if char == 'h':
self.dit(), self.dit(), self.dit(), self.dit(), self.stop()
if char == 'i':
self.dit(), self.dit(), self.stop()
if char == 'j':
self.dit(), self.dah(), self.dah(), self.dah(), self.stop()
if char == 'k':
self.dah(), self.dit(), self.dah(), self.stop()
if char == 'l':
self.dit(), self.dah(), self.dit(), self.dit(), self.stop()
if char == 'm':
self.dah(), self.dah(), self.stop()
if char == 'n':
self.dah(), self.dit(), self.stop()
if char == 'o':
self.dah(), self.dah(), self.dah(), self.stop()
if char == 'p':
self.dit(), self.dah(), self.dah(), self.dit(), self.stop()
if char == 'q':
self.dah(), self.dah(), self.dit(), self.dah(), self.stop()
if char == 'r':
self.dit(), self.dah(), self.dit(), self.stop()
if char == 's':
self.dit(), self.dit(), self.dit(), self.stop()
if char == 't':
self.dah(), self.stop()
if char == 'u':
self.dit(), self.dit(), self.dah(), self.stop()
if char == 'v':
self.dit(), self.dit(), self.dit(), self.dah(), self.stop()
if char == 'w':
self.dit(), self.dah(), self.dah(), self.stop()
if char == 'x':
self.dah(), self.dit(), self.dit(), self.dah(), self.stop()
if char == 'y':
self.dah(), self.dit(), self.dah(), self.dah(), self.stop()
if char == 'z':
self.dah(), self.dah(), self.dit(), self.dit(), self.stop()
if char == '0':
self.dah(), self.dah(), self.dah(), self.dah(), self.dah(), self.stop()
if char == '1':
self.dit(), self.dah(), self.dah(), self.dah(), self.dah(), self.stop()
if char == '2':
self.dit(), self.dit(), self.dah(), self.dah(), self.dah(), self.stop()
if char == '3':
self.dit(), self.dit(), self.dit(), self.dah(), self.dah(), self.stop()
if char == '4':
self.dit(), self.dit(), self.dit(), self.dit(), self.dah(), self.stop()
if char == '5':
self.dit(), self.dit(), self.dit(), self.dit(), self.dit(), self.stop()
if char == '6':
self.dah(), self.dit(), self.dit(), self.dit(), self.dit(), self.stop()
if char == '7':
self.dah(), self.dah(), self.dit(), self.dit(), self.dit(), self.stop()
if char == '8':
self.dah(), self.dah(), self.dah(), self.dit(), self.dit(), self.stop()
if char == '9':
self.dah(), self.dah(), self.dah(), self.dah(), self.dit(), self.stop()
return
def play(self):
print(self.string)
self.t.play(self.audio)
def __init__(self, string, hz, speed):
self.string = string
self.length = (1.2 / speed)
self.hz = hz
self.t = tone() # Declare t as tone with default rate of 44100 Hz, for 8000 Hz do t = tone(8000)
self.audio = self.t.sine(0, 0)
length = len(self.string)
for x in range(length):
self.morse_a(self.string[x].lower())
if __name__ == '__main__':
test = morse("e", 1000, 22)
print(test.time)
test.play()