-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHelloTTSGenerator.py
More file actions
73 lines (57 loc) · 2.2 KB
/
HelloTTSGenerator.py
File metadata and controls
73 lines (57 loc) · 2.2 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
import threading
import nls
from nls.token import getToken
from Config import Config
config = Config.get_instance()
TEST_ACCESS_APPKEY = config.get("app_key") # 使用Config获取app_key
TEXT = '围,您好,我是王先生的私人秘书,您找他有什么事情吗?'
class HelloTTSGenerator:
def __init__(self, tid, test_file):
self.__th = threading.Thread(target=self.__test_run)
self.__id = tid
self.__test_file = test_file
def start(self, text):
self.__text = text
self.__f = open(self.__test_file, "wb")
self.__th.start()
def test_on_metainfo(self, message, *args):
print("on_metainfo message=>{}".format(message))
def test_on_error(self, message, *args):
print("on_error args=>{}".format(args))
def test_on_close(self, *args):
print("on_close: args=>{}".format(args))
try:
self.__f.close()
except Exception as e:
print("close file failed since:", e)
def test_on_data(self, data, *args):
try:
self.__f.write(data)
except Exception as e:
print("write data failed:", e)
def test_on_completed(self, message, *args):
print("on_completed:args=>{} message=>{}".format(args, message))
def __test_run(self):
ak_id = config.get("ak_id")
ak_secret = config.get("ak_secret")
info = getToken(ak_id, ak_secret)
print(info)
print("thread:{} start..".format(self.__id))
tts = nls.NlsSpeechSynthesizer(
token=info,
appkey=TEST_ACCESS_APPKEY,
long_tts=False,
on_metainfo=self.test_on_metainfo,
on_data=self.test_on_data,
on_completed=self.test_on_completed,
on_error=self.test_on_error,
on_close=self.test_on_close,
callback_args=[self.__id]
)
print("{}: session start".format(self.__id))
r = tts.start(self.__text, sample_rate=8000, voice="zhiyuan", ex={'enable_subtitle': False})
print("{}: tts done with result:{}".format(self.__id, r))
if __name__ == '__main__':
nls.enableTrace(True)
t = HelloTTSGenerator("thread1", "say_hello.pcm")
t.start(TEXT)