-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
117 lines (80 loc) · 2.8 KB
/
start.py
File metadata and controls
117 lines (80 loc) · 2.8 KB
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
import os
import time
import yaml
import sys
import random
from google.cloud import texttospeech
# module_path = os.path.dirname(__file__)
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = module_path + "/*.json"
client = "" # texttospeech.TextToSpeechClient()
ZH = "zh-CN"
EN = "en-GB"
def getAudioFromApi(msg, lang="zh-CN", audio_file=""):
if client == "":
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(text=msg)
voice = texttospeech.VoiceSelectionParams(
language_code=lang,
ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
if audio_file == "":
audio_file = "./audio/" + msg + ".mp3"
with open(audio_file, "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "{0}"'.format(audio_file))
def say(msg, lang="zh-cn"):
#os.system("say " + msg)
audio_file = "./audio/" + msg + ".mp3"
if not os.path.isfile(audio_file):
getAudioFromApi(msg, lang, audio_file)
print("play file: \"{0}\"".format(audio_file))
os.system("play -q \"{0}\" ".format(audio_file))
def getDataFromYaml():
file_name = "dict.yaml"
stream = open(file_name, 'r')
return yaml.safe_load(stream)
def getChapterSay(chapter):
chapters = chapter.split("-")
result = "小学" + chapters[0] + "年级"
result += ["上学期", "下学期"][int(chapters[1])]
result += "第" + chapters[2] + "模块"
return result
def examine(chapter):
# 数据
data = getDataFromYaml()
test = {}
if chapter == "all":
test = data
else:
test[chapter] = data[chapter]
print(test)
# 提示语
say("现在开始听力考试", ZH)
for chapter in test:
say(getChapterSay(chapter), ZH)
time.sleep(2)
# 随机排序
random.shuffle(test[chapter])
for item in test[chapter]:
item = item.split("|")
w = item[0].strip()
c = item[1].strip()
say(w, EN)
time.sleep(1)
say(c, ZH)
time.sleep(1)
say(w, EN)
time.sleep(3)
os.system("play -q \"{0}\" ".format("./aff80e3f644640fbbd4d1ce2cede4113.mp3"))
time.sleep(3)
say("听力考试结束") #,不及格没有饭吃")
if len(sys.argv) == 1:
sys.argv.append("all")
if __name__ == "__main__":
examine(sys.argv[1])