You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pytts_voice.py 1.1KB

1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. pytts voice service (offline)
  3. """
  4. import time
  5. import pyttsx3
  6. from bridge.reply import Reply, ReplyType
  7. from common.log import logger
  8. from common.tmp_dir import TmpDir
  9. from voice.voice import Voice
  10. class PyttsVoice(Voice):
  11. engine = pyttsx3.init()
  12. def __init__(self):
  13. # 语速
  14. self.engine.setProperty("rate", 125)
  15. # 音量
  16. self.engine.setProperty("volume", 1.0)
  17. for voice in self.engine.getProperty("voices"):
  18. if "Chinese" in voice.name:
  19. self.engine.setProperty("voice", voice.id)
  20. def textToVoice(self, text):
  21. try:
  22. wavFile = TmpDir().path() + "reply-" + str(int(time.time())) + ".wav"
  23. self.engine.save_to_file(text, wavFile)
  24. self.engine.runAndWait()
  25. logger.info(
  26. "[Pytts] textToVoice text={} voice file name={}".format(text, wavFile)
  27. )
  28. reply = Reply(ReplyType.VOICE, wavFile)
  29. except Exception as e:
  30. reply = Reply(ReplyType.ERROR, str(e))
  31. finally:
  32. return reply