-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
executable file
·185 lines (144 loc) · 4.32 KB
/
training.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/python3
import hashlib
import datetime
import eyed3
import os
import subprocess
import sqlite3
import time
from enum import Enum
from sqlite3 import Error
from gtts import gTTS
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame import mixer
DB_FOLDER = "database"
DB_FILE = "boxiot.db"
MP3_FOLDER = "mp3"
class SpeakType(Enum):
Introduction = 1
Combination = 2
Repetition = 3
#region database
def dict_factory(cursor, row):
dict = {}
for idx, col in enumerate(cursor.description):
dict[col[0]] = row[idx]
return dict
def create_connection(database_file):
connection = None
try:
connection = sqlite3.connect(database_file)
connection.row_factory = dict_factory
except Error as e:
print(e)
return connection
def get_training(connection, training_id):
sql = '''
SELECT
cns.Id
, cns.Pattern
, cns.Text
, tcs.Repetition
, cts.Name AS CombinationType
FROM
TrainingCombinations tcs
INNER JOIN
Combinations cns
ON
tcs.CombinationId = cns.Id
INNER JOIN
CombinationTypes cts
ON
cns.CombinationTypeId = cts.Id
WHERE
tcs.TrainingId = ?
ORDER BY
tcs.Sequence
'''
cursor = connection.cursor()
cursor.execute(sql, (training_id,))
training = cursor.fetchall()
return training
# def display_combination(pattern, text):
# with open("draw/combination.txt","w", encoding="utf-8") as cf:
# cf.write("FillScrren,0x0000\n")
# cf.write("SetFontDirection,3\n")
# cf.write(f"DrawUTF8String,G32,40,4,{pattern},0xf000\n")
# x = 64
# parts = text.split(", ")
# for part in parts:
# cf.write(f"DrawUTF8String,G24,{x},4,{part},0xffff\n")
# x += 24
# subprocess.call(["draw/draw", "draw/combination.txt"])
def wait_while_playing():
while mixer.music.get_busy() == True:
pass
def get_audio_file(combination):
speak_type_name = combination["SpeakType"].name[0:1].lower()
id = combination["Id"]
speak = combination["Speak"]
md5 = hashlib.md5(speak.encode()).hexdigest()
if combination["SpeakType"] == SpeakType.Repetition:
filename = f"{MP3_FOLDER}/{speak_type_name}-{md5}.mp3"
else:
filename = f"{MP3_FOLDER}/{speak_type_name}-{id}-{md5}.mp3"
if not os.path.isfile(filename):
tts = gTTS(speak)
tts.save(filename)
mp3 = eyed3.load(filename)
if not mp3.tag:
mp3.initTag()
if combination["SpeakType"] != SpeakType.Repetition:
mp3.tag.track_num = id
mp3.tag.title = speak
mp3.tag.save()
return filename
def speak(combination):
filename = get_audio_file(combination)
if "__Generate" in combination:
return
repetition = 1
if combination["SpeakType"] == SpeakType.Combination:
repetition = combination["Repetition"]
print(combination["Speak"])
start_time = datetime.datetime.now()
mixer.init(buffer=2048)
mixer.music.load(filename)
for _ in range(repetition):
time.sleep(0.5)
mixer.music.play()
wait_while_playing()
mixer.music.rewind()
end_time = datetime.datetime.now()
duration = end_time - start_time
return 1000 * duration.total_seconds()
def speak_combination(combination):
# intro
combination["SpeakType"] = SpeakType.Introduction
combination["Speak"] = f'{combination["CombinationType"]} combination {combination["Id"]}'
speak(combination)
# repetition
combination["SpeakType"] = SpeakType.Repetition
combination["Speak"] = f'{combination["Repetition"]} times'
speak(combination)
# combination
combination["SpeakType"] = SpeakType.Combination
combination["Speak"] = combination["Text"]
speak(combination)
def train(training_id):
connection = create_connection(os.path.join(DB_FOLDER, DB_FILE))
with connection:
training = get_training(connection, training_id)
for combination in training:
#combination["__Generate"] = True
speak_combination(combination)
def main():
# train(1)
# train(2)
# train(3)
# train(4)
# train(5)
# train(6)
train(7)
if __name__ == '__main__':
main()