-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
97 lines (66 loc) · 2.25 KB
/
main.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
from biliLiveBroadcaster import *
from huoZiYinShua import *
from threading import Thread
from threading import Lock
#语音播报器
class voiceBroadcaster:
listToRead = []
locker = Lock()
#初始化
def __init__(self, numThread, voicePath, dictPath):
self.numThread = numThread
self.hzysProcesser = huoZiYinShua(voicePath, dictPath)
#添加需要朗读的文本
def appendText(self, data):
self.locker.acquire()
self.listToRead.append(data)
self.locker.release()
#单个线程的播报
def __broadcast(self, filePath):
while(True):
if(len(self.listToRead) != 0):
#从列表中移除要播报的文本
self.locker.acquire()
data = self.listToRead.pop(0)
self.locker.release()
#用活字印刷播报
self.hzysProcesser.playText(data, filePath)
#开始播报
def startOperation(self):
for n in range(1, self.numThread+1):
Thread(target=self.__broadcast, args=(".\\tempOutput\\"+str(n)+".wav", )).start()
#感谢礼物
def thank(voiceBroad, sender, quantity, giftName):
text = "感谢{}的{}个{}".format(sender, quantity, giftName)
print(text)
voiceBroad.appendText(text)
#欢迎观众
def welcome(voiceBroad, audience):
text = "欢迎{}进入直播间".format(audience)
print(text)
voiceBroad.appendText(text)
#传话太监
def chuanHua(voiceBroad, speaker, content):
text = "\"{}\"说\"{}\"".format(speaker, content)
print(text)
voiceBroad.appendText(text)
if __name__ == "__main__":
#打开设置文件
configFile = open(".\\settings.json", encoding="utf8")
settings = json.load(configFile)
configFile.close()
#读取设置
sourceDir = settings["sourceDirectory"] #音频文件存放目录
dictDir = settings["dictDirectory"] #词典存放目录
numOfThreads = settings["numOfThreads"] #线程数
#房间号
roomId = input("输入房间号:")
vb = voiceBroadcaster(numOfThreads, sourceDir, dictDir)
broadcaster = biliLiveBroadcaster(roomId,
partial(chuanHua, vb),
partial(thank, vb),
partial(welcome, vb))
#开始运行
vb.startOperation()
broadcaster.startBroadcasting()
#由于整个main线程被websocket占用,以下代码都不会被执行