Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

47 linhas
1.4KB

  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. import os
  11. class PyttsVoice(Voice):
  12. def __init__(self):
  13. self.engine = pyttsx3.init()
  14. # 语速
  15. self.engine.setProperty("rate", 125)
  16. # 音量
  17. self.engine.setProperty("volume", 1.0)
  18. for voice in self.engine.getProperty("voices"):
  19. if "Chinese" in voice.name:
  20. self.engine.setProperty("voice", voice.id)
  21. self.engine.setProperty("voice", "zh")
  22. self.engine.startLoop(useDriverLoop=False)
  23. def textToVoice(self, text):
  24. try:
  25. mp3FileName = "reply-" + str(int(time.time()*100)) + ".mp3"
  26. mp3File = TmpDir().path() + mp3FileName
  27. logger.info(
  28. "[Pytts] textToVoice text={} voice file name={}".format(text, mp3File)
  29. )
  30. self.engine.save_to_file(text, mp3File)
  31. self.engine.iterate()
  32. while self.engine.isBusy() or mp3FileName not in os.listdir(TmpDir().path()):
  33. time.sleep(0.1)
  34. logger.debug("[Pytts] Task finished")
  35. reply = Reply(ReplyType.VOICE, mp3File)
  36. except Exception as e:
  37. print(e)
  38. reply = Reply(ReplyType.ERROR, str(e))
  39. finally:
  40. return reply