-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_app.py
118 lines (87 loc) · 4.07 KB
/
start_app.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
from PyQt5.QtWidgets import QMainWindow, QWidget, QApplication
from PyQt5.QtWidgets import QLabel, QPlainTextEdit, QTextBrowser, QPushButton, QSpinBox
from PyQt5 import uic
from morse.morse_generator import MorseGenerator
from trainer.training_generator import TrainingDataGenerator
from playsound import playsound
import sys
import os
class AppUI(QMainWindow):
def __init__(self) -> None:
super(AppUI, self).__init__()
# load ui file
uic.loadUi("app.ui", self)
# create generators
self.morse_gen = MorseGenerator()
self.training_gen = TrainingDataGenerator()
# Koch mode variables
# Same word order as is used by lcwo.net
# First level uses first 2 characters, each level beyond that adds 1
self.koch_char_order = [
"K", "M", "U", "R", "E", "S", "N", "A", "P", "T", "L", "W", "I", ".",
"J", "Z", "=", "F", "O", "Y", ",", "V", "G", "5", "/", "Q", "9", "2",
"H", "3", "8", "B", "?", "4", "7", "C", "1", "D", "6", "0", "X"
]
# changing labels
self.koch_label_grade_percent = self.findChild(QLabel, "kochGradePercent")
self.koch_label_level_newchars = self.findChild(QLabel, "kochNewChars")
# spins
self.koch_spin_level = self.findChild(QSpinBox, "kochLevelSpin")
self.koch_spin_char_wpm = self.findChild(QSpinBox, "kochCharWpmSpin")
self.koch_spin_farn_wpm = self.findChild(QSpinBox, "kochFarnsworthWpmSpin")
# buttons
self.koch_btn_listen_newchars = self.findChild(QPushButton, "kochNewCharsListenBtn")
self.koch_btn_listen_newchars.clicked.connect(self.koch_listen_newchars)
self.koch_btn_listen_level = self.findChild(QPushButton, "kochStartLevelBtn")
self.koch_btn_listen_level.clicked.connect(self.koch_listen_level)
self.koch_btn_auto_grade = self.findChild(QPushButton, "kochAutoGradeBtn")
self.koch_btn_auto_grade.clicked.connect(self.koch_auto_grade)
# user decoded input
self.koch_input_decoded = self.findChild(QPlainTextEdit, "decodedUserInput")
# transmission output
self.koch_output_decoded = self.findChild(QTextBrowser, "transmittedCharOutput")
# runtime variables
self.current_level_text = ""
self.show()
def get_all_level_chars(self) -> str:
level = self.get_level()
# maximum level is 40?
allchars = self.koch_char_order[0:level+1]
return "".join(allchars)
def get_new_level_chars(self) -> str:
level = self.get_level()
if level == 1:
return "".join(self.koch_char_order[:2])
else:
return self.koch_char_order[level+1]
def get_level(self):
return self.koch_spin_level.value()
def koch_listen_newchars(self):
message = ""
new_chars = self.get_new_level_chars()
print(new_chars)
for char in str(new_chars):
segment = ("" + char + " ") * 3
message += segment
print(message)
morse = self.morse_gen.encode_morse(message)
timing = self.morse_gen.generate_timing(morse)
self.morse_gen.generate_audio(timing, "newchars.wav", wpm=18, farnsworth=15)
playsound("newchars.wav", block=False) #TODO: replace this with a thread
# os.remove("newchars.wav")
def koch_listen_level(self):
current_chars = self.get_all_level_chars()
self.current_level_text = self.training_gen.random_chars(current_chars)
morse = self.morse_gen.encode_morse(self.current_level_text)
timing = self.morse_gen.generate_timing(morse)
self.morse_gen.generate_audio(timing, "level.wav", wpm=18, farnsworth=15)
playsound("level.wav", block=False) #TODO: replace this with a thread
# os.remove("level.wav")
def koch_auto_grade(self):
#TODO: complete this method once auto-grading is available
self.koch_output_decoded.clear()
self.koch_output_decoded.append(self.current_level_text)
# init app
app = QApplication(sys.argv)
window = AppUI()
sys.exit(app.exec_())