Spaces:
Configuration error
Configuration error
void
commited on
Commit
•
4bc647b
1
Parent(s):
c462179
rewrite DmPlayer to avoid "while"
Browse files- app.py +6 -7
- dm_player.py +29 -35
app.py
CHANGED
@@ -63,7 +63,7 @@ class MyHandler(blivedm.BaseHandler):
|
|
63 |
uname = command['data']['uname']
|
64 |
msg_type = command['data']['msg_type']
|
65 |
if msg_type == 2:
|
66 |
-
dmplayer.
|
67 |
|
68 |
_CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback
|
69 |
|
@@ -71,20 +71,19 @@ class MyHandler(blivedm.BaseHandler):
|
|
71 |
pass
|
72 |
|
73 |
def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
|
74 |
-
dmplayer.
|
75 |
|
76 |
def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
|
77 |
-
dmplayer.
|
78 |
|
79 |
def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
|
80 |
-
dmplayer.
|
81 |
|
82 |
def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
|
83 |
-
dmplayer.
|
84 |
|
85 |
|
86 |
if __name__ == '__main__':
|
87 |
-
dmplayer.
|
88 |
-
dmplayer.LoadCharacter("圣园未花")
|
89 |
dmplayer.SetVoiceOption(0.6, 0.668, 0.9)
|
90 |
asyncio.run(dm_monitor())
|
|
|
63 |
uname = command['data']['uname']
|
64 |
msg_type = command['data']['msg_type']
|
65 |
if msg_type == 2:
|
66 |
+
dmplayer.Pend(f'谢谢{uname}的关注喵', "bubu.wav")
|
67 |
|
68 |
_CMD_CALLBACK_DICT['INTERACT_WORD'] = __interact_word_callback
|
69 |
|
|
|
71 |
pass
|
72 |
|
73 |
def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMessage):
|
74 |
+
dmplayer.Pend(message.msg, "bubu.wav", test_callback, message.uname, message.msg) # 带有callback的加入队列
|
75 |
|
76 |
def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage):
|
77 |
+
dmplayer.Pend(f'感谢{message.uname}赠送的{message.num}个{message.gift_name},阿里嘎多!', "po.wav") # 普通的加入队列
|
78 |
|
79 |
def _on_buy_guard(self, client: blivedm.BLiveClient, message: web_models.GuardBuyMessage):
|
80 |
+
dmplayer.Pend(f'感谢{message.uname}的{message.gift_name},阿里嘎多!', "po.wav")
|
81 |
|
82 |
def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperChatMessage):
|
83 |
+
dmplayer.Pend(f'{message.uname}说:{message.message}', "dingding.wav")
|
84 |
|
85 |
|
86 |
if __name__ == '__main__':
|
87 |
+
dmplayer.LoadCharacter("德丽莎")
|
|
|
88 |
dmplayer.SetVoiceOption(0.6, 0.668, 0.9)
|
89 |
asyncio.run(dm_monitor())
|
dm_player.py
CHANGED
@@ -6,21 +6,37 @@ import time
|
|
6 |
def PlaySound(filename):
|
7 |
winsound.PlaySound(filename, winsound.SND_FILENAME)
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
class DmPlayer:
|
10 |
|
11 |
def __init__(self):
|
12 |
-
self.
|
13 |
-
self.
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
if self.
|
18 |
-
self.
|
19 |
-
self.
|
20 |
-
|
21 |
-
def
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def LoadCharacter(self, name):
|
26 |
tts.LoadCharacter(name)
|
@@ -29,26 +45,4 @@ class DmPlayer:
|
|
29 |
# nsw "控制音素发音长度", minimum=0.1, maximum=1.0
|
30 |
# ls "控制整体语速", minimum=0.1, maximum=2.0
|
31 |
def SetVoiceOption(self, ns, nsw, ls):
|
32 |
-
tts.SetVoiceOption(ns, nsw, ls)
|
33 |
-
|
34 |
-
def Run(self):
|
35 |
-
text = ""
|
36 |
-
while self.running:
|
37 |
-
try:
|
38 |
-
(text, notify, cb_before, args) = self.dm_text_list.pop(0)
|
39 |
-
except:
|
40 |
-
time.sleep(1)
|
41 |
-
else:
|
42 |
-
if cb_before != None:
|
43 |
-
cb_before(*args)
|
44 |
-
|
45 |
-
print(f'Generating TTS {text}')
|
46 |
-
result, wav = tts.GenerateTTS(text)
|
47 |
-
if result == True:
|
48 |
-
if notify != None:
|
49 |
-
PlaySound(f"{notify}")
|
50 |
-
PlaySound(wav)
|
51 |
-
|
52 |
-
def Terminate(self):
|
53 |
-
self.running = False
|
54 |
-
self.th = None
|
|
|
6 |
def PlaySound(filename):
|
7 |
winsound.PlaySound(filename, winsound.SND_FILENAME)
|
8 |
|
9 |
+
def Play(text, notify, cb_before_play, args):
|
10 |
+
if cb_before_play != None:
|
11 |
+
cb_before_play(*args)
|
12 |
+
result, wav = tts.GenerateTTS(text)
|
13 |
+
if result == True:
|
14 |
+
if notify != None:
|
15 |
+
PlaySound(f"{notify}")
|
16 |
+
PlaySound(wav)
|
17 |
+
|
18 |
class DmPlayer:
|
19 |
|
20 |
def __init__(self):
|
21 |
+
self.pending_list = []
|
22 |
+
self.playing = False
|
23 |
+
|
24 |
+
def Pend(self, text, notify = None, cb_before_play = None, *args):
|
25 |
+
self.pending_list.append((text, notify, cb_before_play, args))
|
26 |
+
if self.playing == False:
|
27 |
+
self.playing = True
|
28 |
+
threading.Thread(target=self.Play, args=()).start()
|
29 |
+
|
30 |
+
def Play(self):
|
31 |
+
try:
|
32 |
+
(text, notify, cb_before_play, args) = self.pending_list.pop(0)
|
33 |
+
except:
|
34 |
+
self.playing = False
|
35 |
+
print('Queue empty')
|
36 |
+
else:
|
37 |
+
Play(text, notify, cb_before_play, args)
|
38 |
+
time.sleep(0.5)
|
39 |
+
self.Play()
|
40 |
|
41 |
def LoadCharacter(self, name):
|
42 |
tts.LoadCharacter(name)
|
|
|
45 |
# nsw "控制音素发音长度", minimum=0.1, maximum=1.0
|
46 |
# ls "控制整体语速", minimum=0.1, maximum=2.0
|
47 |
def SetVoiceOption(self, ns, nsw, ls):
|
48 |
+
tts.SetVoiceOption(ns, nsw, ls)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|