ear.py 4.2 KB

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