rabin 4 years ago
parent
commit
d05d0350c7

+ 4 - 4
conf/dev.conf

@@ -39,11 +39,11 @@ on_hotword			= 悟空别吵.pmdl
 off_hotword			= 悟空醒醒.pmdl
 sensitivity			= 0.5
 silent_threshold	= 15
-recording_timeout	= 5
+recording_timeout	= 50
 ;勿扰模式 开始时间-结束时间
-bother_enable		= True
-bother_since		= 1
-bother_till			= 100
+bother_enable		= False
+;bother_since		= 1
+;bother_till			= 100
 ;自动聊天
 active_mode			= True
 

+ 2 - 0
service/api/tencent.py

@@ -22,6 +22,8 @@ class Tencent(Base):
 		r = self.engine.ASR(file, 'mp3', '1', self.region)
 		File.remove(file)
 		res = json.loads(r)
+		print(file)
+		print(res)
 		if 'Response' in res and 'Result' in res['Response']:
 			return self.asrLog(1, res['Response']['Result'])
 		else:

+ 1 - 1
service/brain.py

@@ -137,7 +137,7 @@ class Brain(object):
 	# 没听清
 	def pardon(self):
 		if not self.hasPardon:
-			self.robot.say('抱歉,刚刚没听清,能再说一遍吗?', completed=lambda: self.response(self.robot.ear.listen()))
+			self.robot.say('抱歉,刚刚没听清,能再说一遍吗?', completed=lambda: self.think(self.robot.ear.listen()))
 			self.hasPardon = True
 		else:
 			self.robot.say('没听清呢')

+ 40 - 12
service/ear.py

@@ -23,18 +23,18 @@ class Ear(object):
 			self.detector.terminate()
 
 		self.getConfig()
-		self.detector = snowboydecoder.HotwordDetector(self.config['models'], sensitivity=self.config['sensitivity'], outname=Demeter.config['vecan']['outname'], temp=Demeter.config['vecan']['temp'])
+		self.detector = snowboydecoder.HotwordDetector(self.config['models'], sensitivity=self.config['sensitivity'], outname=Demeter.config['vecan']['outname'], temp=Demeter.config['vecan']['temp'],robot=self.robot)
 		# main loop
-		try:
-			self.detector.start(detected_callback=self.config['callbacks'],
-								audio_recorder_callback=self.robot.mouth.talk,
+		#try:
+		self.detector.start(detected_callback=self.config['callbacks'],
+								audio_recorder_callback=self.recorderCallback,
 								interrupt_check=self.interruptCallback,
 								silent_count_threshold=int(self.config['silent_threshold']),
 								recording_timeout=int(self.config['recording_timeout']) * 4,
 								sleep_time=0.03)
-			self.detector.terminate()
-		except Exception as e:
-			self.robot.logger.critical(self.log('离线唤醒机制初始化失败:{}'.format(e)))
+		self.detector.terminate()
+		#except Exception as e:
+			#self.robot.logger.critical(self.log('离线唤醒机制初始化失败:{}'.format(e)))
 
 	# listen
 	def listen(self, silent=False):
@@ -52,7 +52,7 @@ class Ear(object):
 				self.robot.mouth.speek(self.robot.data + 'beep_lo.wav')
 			if voice:
 				query = self.robot.tool['asr'].asr(voice)
-				Demeter.remove(voice)
+				File.remove(voice)
 				return query
 		except Exception as e:			
 			Demeter.logger.error(e)
@@ -91,8 +91,36 @@ class Ear(object):
 			return
 		self.robot.mouth.speek(self.robot.data + 'beep_hi.wav')
 		self.robot.logger.info(self.log('开始录音'))
-		self.robot.interrupt()
-		self.robot.isRecording = True;
+		self.robot.stop()
+		self.robot.isRecording = True
+
+	def recorderCallback(self, fp, callback=None):
+		self.robot.mouth.speek(self.robot.data + 'beep_lo.wav')
+		self.robot.logger.info(self.log('结束录音'))
+		self.robot.isRecording = False
+		if self.robot.profiling:
+			self.robot.logger.info(self.log('性能调试已打开'))
+			pr = cProfile.Profile()
+			pr.enable()
+			self.recorder(fp, callback)
+			pr.disable()
+			s = io.StringIO()
+			sortby = 'cumulative'
+			ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
+			ps.print_stats()
+			print(s.getvalue())
+		else:
+			self.recorder(fp, callback)
+
+	def recorder(self, fp, callback=None, onSay=None):
+		try:
+			self.robot.stop()
+			query = self.robot.tool['asr'].asr(fp)
+			File.remove(fp)
+			self.robot.brain.think(query, callback, onSay)
+		except Exception as e:
+			self.robot.logger.critical(self.log(e))
+			clean()
 
 	def onHotwordCallback(self):
 		if self.config['hotword_switch'] == True:
@@ -118,8 +146,8 @@ class Ear(object):
 			return True
 		if 'bother_since' not in self.config or 'bother_till' not in self.config:
 			return True
-		since = self.config['bother_since']
-		till = self.config['bother_till']
+		since = int(self.config['bother_since'])
+		till = int(self.config['bother_till'])
 		current = time.localtime(time.time()).tm_hour
 		if till > since:
 			return current not in range(since, till)

+ 4 - 32
service/mouth.py

@@ -46,9 +46,9 @@ class Mouth(object):
 		self.speek(voice, not cache, completed=completed)
 
 	def speek(self, voice, delete=False, completed=None, volume=1):
-		if self.speaker == None:
-			self.speaker = Demeter.service('sox', 'player')
-			self.speaker.init(self)
+		#if self.speaker == None:
+		self.speaker = Demeter.service('sox', 'player')
+		self.speaker.init(self)
 		self.speaker.play(voice, delete=delete, onCompleted=completed, volume=volume)
 
 	def checkSpeeker(self):
@@ -58,35 +58,7 @@ class Mouth(object):
 	def close(self):
 		if self.checkSpeeker():
 			self.speaker.stop()
-			#self.speaker = None
-
-	def talk(self, fp, callback=None):
-		self.speek(self.robot.data + 'beep_lo.wav')
-		self.robot.logger.info(self.log('结束录音'))
-		self.robot.isRecording = False
-		if self.robot.profiling:
-			self.robot.logger.info(self.log('性能调试已打开'))
-			pr = cProfile.Profile()
-			pr.enable()
-			self.onTalk(fp, callback)
-			pr.disable()
-			s = io.StringIO()
-			sortby = 'cumulative'
-			ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
-			ps.print_stats()
-			print(s.getvalue())
-		else:
-			self.onTalk(fp, callback)
-
-	def onTalk(self, fp, callback=None, onSay=None):
-		try:
-			self.robot.stop()
-			query = self.robot.tool['asr'].asr(fp)
-			File.remove(fp)
-			self.robot.brain.think(query, callback, onSay)
-		except Exception as e:
-			self.robot.logger.critical(self.log(e))
-			clean()
+			self.speaker = None
 
 	def log(self, text):
 		return 'Mouth:' + text

+ 1 - 0
service/player/sox.py

@@ -15,6 +15,7 @@ class Sox(Base):
 
 	def run(self):
 		cmd = ['play', '-v', str(self.volume), str(self.src)]
+		cmd = ['aplay', str(self.src)]
 		self.logger.info(self.mouth.log('播放语音 ' + ' '.join(cmd)))
 
 		self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

BIN
service/snowboy/_snowboydetect.so


+ 9 - 2
service/snowboy/snowboydecoder.py

@@ -78,6 +78,8 @@ class ActiveListener(object):
                  resource=RESOURCE_FILE,
                  outname = 'output',
                  temp = ''):
+        self.outname = outname
+        self.temp = temp
         logger.debug("activeListen __init__()")
         self.recordedData = []
         model_str = ",".join(decoder_model)
@@ -216,9 +218,14 @@ class HotwordDetector(object):
                  audio_gain=1,
                  apply_frontend=False,
                  outname='output',
-                 temp=''):
+                 temp='',
+                 robot=False
+                 ):
 
         self._running = False
+        self.robot = robot
+        self.outname = outname
+        self.temp = temp
 
         tm = type(decoder_model)
         ts = type(sensitivity)
@@ -343,7 +350,7 @@ class HotwordDetector(object):
                     if callback is not None:
                         callback()
 
-                    if audio_recorder_callback is not None and status == 1 and utils.is_proper_time():
+                    if audio_recorder_callback is not None and status == 1 and self.robot.ear.isProperTime():
                         state = "ACTIVE"
                     continue
 

+ 1 - 1
vecan.py

@@ -18,7 +18,7 @@ class Vecan(object):
 		self.robot = Demeter.service('robot')
 		self.robot.init(profiling=Demeter.option['profiling'], logout=Demeter.option['logout'])
 		#self.robot.say('亲爱的,我是神算终端机器人vecan,很高兴为你服务', True, completed=lambda: self.robot.wait())
-		self.robot.say('亲爱的,我是神算终端机器人vecan,很高兴为你服务', True)
+		#self.robot.say('亲爱的,我是神算终端机器人vecan,很高兴为你服务', True)
 		self.robot.wait()
 
 		Demeter.dog = WatchDog.init()