Skip to content

Commit

Permalink
fix: pytts second round not work
Browse files Browse the repository at this point in the history
  • Loading branch information
JS00000 committed Apr 20, 2023
1 parent 1e58c1a commit 3420902
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions voice/pytts/pytts_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,34 @@
import os

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

def __init__(self):
self.engine = pyttsx3.init()
# 语速
self.engine.setProperty("rate", 125)
# 音量
self.engine.setProperty("volume", 1.0)
for voice in self.engine.getProperty("voices"):
if "Chinese" in voice.name:
self.engine.setProperty("voice", voice.id)

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

def textToVoice(self, text):
try:
mp3FileName = "reply-" + str(int(time.time())) + ".mp3"
mp3FileName = "reply-" + str(int(time.time()*100)) + ".mp3"
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(
"[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)
except Exception as e:
print(e)
reply = Reply(ReplyType.ERROR, str(e))
finally:
return reply

0 comments on commit 3420902

Please sign in to comment.