mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-21 06:07:13 +08:00
formatting: run precommit on all files
This commit is contained in:
@@ -5,6 +5,7 @@ pytts voice service (offline)
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import pyttsx3
|
||||
|
||||
from bridge.reply import Reply, ReplyType
|
||||
@@ -12,6 +13,7 @@ from common.log import logger
|
||||
from common.tmp_dir import TmpDir
|
||||
from voice.voice import Voice
|
||||
|
||||
|
||||
class PyttsVoice(Voice):
|
||||
engine = pyttsx3.init()
|
||||
|
||||
@@ -20,7 +22,7 @@ class PyttsVoice(Voice):
|
||||
self.engine.setProperty("rate", 125)
|
||||
# 音量
|
||||
self.engine.setProperty("volume", 1.0)
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
for voice in self.engine.getProperty("voices"):
|
||||
if "Chinese" in voice.name:
|
||||
self.engine.setProperty("voice", voice.id)
|
||||
@@ -33,23 +35,23 @@ class PyttsVoice(Voice):
|
||||
def textToVoice(self, text):
|
||||
try:
|
||||
# avoid the same filename
|
||||
wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7fffffff) + ".wav"
|
||||
wavFileName = "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".wav"
|
||||
wavFile = TmpDir().path() + wavFileName
|
||||
logger.info("[Pytts] textToVoice text={} voice file name={}".format(text, wavFile))
|
||||
|
||||
self.engine.save_to_file(text, wavFile)
|
||||
|
||||
if sys.platform == 'win32':
|
||||
if sys.platform == "win32":
|
||||
self.engine.runAndWait()
|
||||
else:
|
||||
# In ubuntu, runAndWait do not really wait until the file created.
|
||||
# In ubuntu, runAndWait do not really wait until the file created.
|
||||
# It will return once the task queue is empty, but the task is still running in coroutine.
|
||||
# And if you call runAndWait() and time.sleep() twice, it will stuck, so do not use this.
|
||||
# If you want to fix this, add self._proxy.setBusy(True) in line 127 in espeak.py, at the beginning of the function save_to_file.
|
||||
# self.engine.runAndWait()
|
||||
|
||||
# Before espeak fix this problem, we iterate the generator and control the waiting by ourself.
|
||||
# But this is not the canonical way to use it, for example if the file already exists it also cannot wait.
|
||||
# But this is not the canonical way to use it, for example if the file already exists it also cannot wait.
|
||||
self.engine.iterate()
|
||||
while self.engine.isBusy() or wavFileName not in os.listdir(TmpDir().path()):
|
||||
time.sleep(0.1)
|
||||
|
||||
Reference in New Issue
Block a user