void commited on
Commit
17957f6
1 Parent(s): 3c3eb10

add DmPlayer

Browse files
Files changed (1) hide show
  1. dm_player.py +54 -0
dm_player.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from vits_tts import tts
2
+ import winsound
3
+ import threading
4
+ import time
5
+
6
+ def PlaySound(filename):
7
+ winsound.PlaySound(filename, winsound.SND_FILENAME)
8
+
9
+ class DmPlayer:
10
+
11
+ def __init__(self):
12
+ self.running = True
13
+ self.dm_text_list = []
14
+ self.th = None
15
+
16
+ def Start(self):
17
+ if self.th == None:
18
+ self.th = threading.Thread(target=self.Run, args=())
19
+ self.th.start()
20
+
21
+ def Add(self, text, notify = None, callback_before = None, *args):
22
+ self.dm_text_list.append((text, notify, callback_before, args))
23
+ print(f'queue len: {len(self.dm_text_list)}')
24
+
25
+ def LoadCharacter(self, name):
26
+ tts.LoadCharacter(name)
27
+
28
+ # ns "控制感情变化程度", minimum=0.1, maximum=1.0
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