ear.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from .__load__ import *
  4. from .snowboy import snowboydecoder
  5. # 耳朵,聆听者
  6. class Ear(object):
  7. def init(self, robot):
  8. # vecan机器人
  9. self.robot = robot
  10. # 勿扰模式控制 勿扰模式关闭
  11. self.bother = False
  12. # snowboy detector
  13. self.detector = None
  14. # 中断控制
  15. self.interrupted = False
  16. return self
  17. # 语音唤醒模式
  18. def wait(self):
  19. return
  20. self.robot.logger.debug(self.log('唤醒模式'))
  21. if self.detector is not None:
  22. self.detector.terminate()
  23. self.getConfig()
  24. self.detector = snowboydecoder.HotwordDetector(self.config['models'], sensitivity=self.config['sensitivity'], outname=Demeter.config['vecan']['outname'], temp=Demeter.config['vecan']['temp'])
  25. # main loop
  26. try:
  27. self.detector.start(detected_callback=self.config['callbacks'],
  28. audio_recorder_callback=self.robot.mouth.talk,
  29. interrupt_check=self.interruptCallback,
  30. silent_count_threshold=int(self.config['silent_threshold']),
  31. recording_timeout=int(self.config['recording_timeout']) * 4,
  32. sleep_time=0.03)
  33. self.detector.terminate()
  34. except Exception as e:
  35. self.robot.logger.critical(self.log('离线唤醒机制初始化失败:{}'.format(e)))
  36. # listen
  37. def listen(self, silent=False):
  38. self.robot.logger.debug(self.log('聆听模式'))
  39. try:
  40. if not silent:
  41. time.sleep(1)
  42. self.robot.mouth.speek(self.robot.data + 'beep_hi.wav')
  43. listener = snowboydecoder.ActiveListener([self.robot.data + self.config['hotword']], outname=Demeter.config['vecan']['outname'], temp=Demeter.config['vecan']['temp'])
  44. voice = listener.listen(
  45. silent_count_threshold=int(self.config['silent_threshold']),
  46. recording_timeout=int(self.config['recording_timeout']) * 4
  47. )
  48. if not silent:
  49. self.robot.mouth.speek(self.robot.data + 'beep_lo.wav')
  50. if voice:
  51. query = self.robot.tool['asr'].asr(voice)
  52. Demeter.remove(voice)
  53. return query
  54. except Exception as e:
  55. Demeter.logger.error(e)
  56. return ''
  57. # snowboy配置,后续改成从数据库读取
  58. def getConfig(self):
  59. self.config = Demeter.config['snowboy']
  60. if self.config['hotword_switch'] == True:
  61. self.config['models'] = [
  62. self.robot.data + self.config['hotword'],
  63. self.robot.data + self.config['on_hotword'],
  64. self.robot.data + self.config['off_hotword']
  65. ]
  66. self.config['callbacks'] = [
  67. self.detectedCallback,
  68. self.onHotwordCallback,
  69. self.offHotwordCallback
  70. ]
  71. else:
  72. self.config['models'] = self.robot.data + self.config['hotword']
  73. self.config['callbacks'] = self.detectedCallback
  74. # close
  75. def close(self):
  76. self.interrupted = True
  77. # callback
  78. def detectedCallback(self):
  79. if not self.isProperTime():
  80. self.robot.logger.warning(self.log('勿扰模式开启中'))
  81. return
  82. if self.robot.isRecording:
  83. self.robot.logger.warning(self.log('正在录音中,跳过'))
  84. return
  85. self.robot.mouth.speek(self.robot.data + 'beep_hi.wav')
  86. self.robot.logger.info(self.log('开始录音'))
  87. self.robot.interrupt()
  88. self.robot.isRecording = True;
  89. def onHotwordCallback(self):
  90. if self.config['hotword_switch'] == True:
  91. self.bother = True
  92. self.robot.mouth.speek(self.robot.data + 'off.wav')
  93. self.robot.logger.info(self.log('勿扰模式打开'))
  94. def offHotwordCallback(self):
  95. if self.config['hotword_switch'] == True:
  96. self.bother = False
  97. self.robot.mouth.speek(self.robot.data + 'on.wav')
  98. self.robot.logger.info(self.log('勿扰模式关闭'))
  99. def interruptCallback(self):
  100. return self.interrupted
  101. def isProperTime(self):
  102. if self.bother == True:
  103. return False
  104. if 'bother_enable' not in self.config:
  105. return True
  106. if self.config['bother_enable'] == False:
  107. return True
  108. if 'bother_since' not in self.config or 'bother_till' not in self.config:
  109. return True
  110. since = self.config['bother_since']
  111. till = self.config['bother_till']
  112. current = time.localtime(time.time()).tm_hour
  113. if till > since:
  114. return current not in range(since, till)
  115. else:
  116. return not (current in range(since, 25) or
  117. current in range(-1, till))
  118. def log(self, text):
  119. return 'Ear:' + text