Browse Source

fix: pytts second round not work

master
JS00000 1 year ago
parent
commit
34209021c8
1 changed files with 9 additions and 10 deletions
  1. +9
    -10
      voice/pytts/pytts_voice.py

+ 9
- 10
voice/pytts/pytts_voice.py View File

@@ -13,9 +13,9 @@ from voice.voice import Voice
import os import os


class PyttsVoice(Voice): class PyttsVoice(Voice):
engine = pyttsx3.init()


def __init__(self): def __init__(self):
self.engine = pyttsx3.init()
# 语速 # 语速
self.engine.setProperty("rate", 125) self.engine.setProperty("rate", 125)
# 音量 # 音量
@@ -23,25 +23,24 @@ class PyttsVoice(Voice):
for voice in self.engine.getProperty("voices"): for voice in self.engine.getProperty("voices"):
if "Chinese" in voice.name: if "Chinese" in voice.name:
self.engine.setProperty("voice", voice.id) self.engine.setProperty("voice", voice.id)

self.engine.setProperty("voice", "zh") self.engine.setProperty("voice", "zh")
self.engine.startLoop(useDriverLoop=False)


def textToVoice(self, text): def textToVoice(self, text):
try: try:
mp3FileName = "reply-" + str(int(time.time())) + ".mp3"
mp3FileName = "reply-" + str(int(time.time()*100)) + ".mp3"
mp3File = TmpDir().path() + mp3FileName mp3File = TmpDir().path() + mp3FileName
self.engine.save_to_file(text, mp3File)
self.engine.runAndWait()

# engine.runAndWait() will return before the file created
while mp3FileName not in os.listdir(TmpDir().path()):
time.sleep(0.1)

logger.info( logger.info(
"[Pytts] textToVoice text={} voice file name={}".format(text, mp3File) "[Pytts] textToVoice text={} voice file name={}".format(text, mp3File)
) )
self.engine.save_to_file(text, mp3File)
self.engine.iterate()
while self.engine.isBusy() or mp3FileName not in os.listdir(TmpDir().path()):
time.sleep(0.1)
logger.debug("[Pytts] Task finished")
reply = Reply(ReplyType.VOICE, mp3File) reply = Reply(ReplyType.VOICE, mp3File)
except Exception as e: except Exception as e:
print(e)
reply = Reply(ReplyType.ERROR, str(e)) reply = Reply(ReplyType.ERROR, str(e))
finally: finally:
return reply return reply

Loading…
Cancel
Save