mouth.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from .__load__ import *
  4. # 口 说话
  5. class Mouth(object):
  6. tool = {}
  7. def init(self, robot):
  8. self.robot = robot
  9. # 播放器
  10. self.speaker = None
  11. # 对话
  12. self.onSay = None
  13. return self
  14. def say(self, msg, cache=False, plugin='', completed=None):
  15. if self.onSay:
  16. self.robot.logger.info(self.log('onSay: {}'.format(msg)))
  17. if plugin != '':
  18. self.onSay("[{}] {}".format(plugin, msg))
  19. else:
  20. self.onSay(msg)
  21. self.onSay = None
  22. if plugin != '':
  23. self.robot.brain.addMemory(1, "[{}] {}".format(plugin, msg))
  24. else:
  25. self.robot.brain.addMemory(1, msg)
  26. pattern = r'^https?://.+'
  27. if re.match(pattern, msg):
  28. self.robot.logger.info(self.log('内容包含URL,无法读取'))
  29. return
  30. voice = ''
  31. self.robot.logger.info(self.log('获取文字 - ' + msg))
  32. if getCache(msg):
  33. self.robot.logger.info(self.log('命中缓存,播放缓存语音'))
  34. voice = getCache(msg)
  35. else:
  36. try:
  37. voice = self.robot.tool['tts'].tts(msg)
  38. if cache:
  39. saveCache(voice, msg)
  40. except Exception as e:
  41. self.robot.logger.error(self.log('保存缓存失败:{}'.format(e)))
  42. if completed is None:
  43. completed = lambda: self.robot.brain.doubt(msg)
  44. self.speek(voice, not cache, completed=completed)
  45. def speek(self, voice, delete=False, completed=None, volume=1):
  46. #if self.speaker == None:
  47. self.speaker = Demeter.service('sox', 'player')
  48. self.speaker.init(self)
  49. self.speaker.play(voice, delete=delete, onCompleted=completed, volume=volume)
  50. def checkSpeeker(self):
  51. return self.speaker is not None and self.speaker.is_playing()
  52. # 中断、停止
  53. def close(self):
  54. if self.checkSpeeker():
  55. self.speaker.stop()
  56. self.speaker = None
  57. def log(self, text):
  58. return 'Mouth:' + text