Skip to content

Commit

Permalink
fix: 修复极客模式下的若干bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wzpan committed Apr 8, 2023
1 parent 6b0826d commit 5d79aca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
4 changes: 3 additions & 1 deletion plugins/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ def handle(self, text, parsed):
self.say("拍照失败,请检查相机是否连接正确", cache=True)

def isValid(self, text, parsed):
return any(word in text for word in ["拍照", "拍张照"])
return any(word in text for word in ["拍照", "拍张照"]) and not any(
word in text for word in ["拍照成功", "拍照失败", "后启动拍照"]
)
7 changes: 3 additions & 4 deletions plugins/Gossip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ENTRY_WORDS = ["进入", "打开", "激活", "开启", "一下"]
CLOSE_WORDS = ["退出", "结束", "停止"]


class Plugin(AbstractPlugin):

IS_IMMERSIVE = True
Expand All @@ -22,11 +23,9 @@ def handle(self, text, parsed):
else:
self.clearImmersive() # 去掉沉浸式
self.say("结束闲聊", cache=True)

def isValidImmersive(self, text, parsed):
return (
"闲聊" in text and any(word in text for word in CLOSE_WORDS)
)
return "闲聊" in text and any(word in text for word in CLOSE_WORDS)

def isValid(self, text, parsed):
return "闲聊" in text and any(word in text for word in ENTRY_WORDS)
31 changes: 19 additions & 12 deletions robot/Conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, profiling=False):
def _lastCompleted(self, index, onCompleted):
# logger.debug(f"{index}, {self.tts_index}, {self.tts_count}")
if index >= self.tts_count - 1:
logger.debug(f"执行onCompleted")
# logger.debug(f"执行onCompleted")
onCompleted and onCompleted()

def _ttsAction(self, msg, cache, index, onCompleted=None):
Expand All @@ -85,9 +85,9 @@ def _ttsAction(self, msg, cache, index, onCompleted=None):
logger.info(f"第{index}段TTS合成成功。msg: {msg}")
while index != self.tts_index:
# 阻塞直到轮到这个音频播放
continue
continue
with self.play_lock:
# logger.debug(f"即将播放第{index}段TTS。msg: {msg}")
logger.info(f"即将播放第{index}段TTS。msg: {msg}")
self.player.play(
voice,
not cache,
Expand Down Expand Up @@ -129,7 +129,7 @@ def checkRestore(self):
self.brain.restore()

def _InGossip(self, query):
return self.immersiveMode == "Gossip"
return self.immersiveMode in ["Gossip"] and not "闲聊" in query

def doResponse(self, query, UUID="", onSay=None, onStream=None):
"""
Expand Down Expand Up @@ -287,12 +287,10 @@ def _tts_line(self, line, cache, index=0, onCompleted=None):
pattern = r"http[s]?://.+"
if re.match(pattern, line):
logger.info("内容包含URL,屏蔽后续内容")
self.tts_count -= 1
return None
if line:
result = self._ttsAction(line, cache, index, onCompleted)
return result
self.tts_count -= 1
return None

def _tts(self, lines, cache, onCompleted=None):
Expand Down Expand Up @@ -352,20 +350,29 @@ def stream_say(self, stream, cache=False, onCompleted=None):
if onCompleted is None:
onCompleted = lambda: self._onCompleted(msg)
self.tts_index = 0
self.tts_count = 0
index = 0
skip_tts = False
for data in stream():
if self.onStream:
self.onStream(data, resp_uuid)
line += data
if any(char in data for char in utils.getPunctuations()):
audio = self._tts_line(line.strip(), cache, index, onCompleted)
if audio:
lines.append(line)
audios.append(audio)
if "```" in line.strip():
skip_tts = True
if not skip_tts:
audio = self._tts_line(line.strip(), cache, index, onCompleted)
if audio:
self.tts_count += 1
audios.append(audio)
index += 1
else:
logger.info(f"{line} 属于代码段,跳过朗读")
lines.append(line)
line = ""
index += 1
if skip_tts:
self._tts_line("内容包含代码,我就不念了", True, index, onCompleted)
msg = "".join(lines)
self.tts_count = len(lines)
self.appendHistory(1, msg, UUID=resp_uuid, plugin="")
self._after_play(msg, audios, "")

Expand Down
22 changes: 15 additions & 7 deletions robot/Player.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import asyncio
import subprocess
import os
import platform
Expand Down Expand Up @@ -81,6 +82,16 @@ def __init__(self, **kwargs):
self.play_queue = queue.Queue() # 播放队列
self.consumer_thread = threading.Thread(target=self.playLoop)
self.consumer_thread.start()
self.loop = asyncio.new_event_loop() # 创建事件循环
self.thread_loop = threading.Thread(target=self.loop.run_forever)
self.thread_loop.start()

def executeOnCompleted(self, onCompleted):
# 全部播放完成,播放统一的 onCompleted()
onCompleted and onCompleted()
if self.play_queue.empty():
for onCompleted in self.onCompleteds:
onCompleted and onCompleted()

def playLoop(self):
while True:
Expand All @@ -89,13 +100,10 @@ def playLoop(self):
with self.play_lock:
logger.info(f"开始播放音频:{src}")
self.src = src
self.doPlay(src)
self.doPlay(src)
# 将 onCompleted() 方法的调用放到事件循环的线程中执行
self.play_queue.task_done()
onCompleted and onCompleted()
# 全部播放完成,播放统一的 onCompleted()
if self.play_queue.empty():
for onCompleted in self.onCompleteds:
onCompleted and onCompleted()
self.loop.call_soon_threadsafe(self.executeOnCompleted, onCompleted)

def doPlay(self, src):
system = platform.system()
Expand All @@ -112,7 +120,7 @@ def doPlay(self, src):
self.playing = False
if self.delete:
utils.check_and_delete(src)
logger.info(f"播放完成:{src}")
logger.info(f"播放完成:{src}")

def play(self, src, delete=False, onCompleted=None):
if src and (os.path.exists(src) or src.startswith("http")):
Expand Down
11 changes: 10 additions & 1 deletion wukong.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class Wukong(object):

_profiling = False
_debug = False

def init(self):
self.detector = None
Expand Down Expand Up @@ -88,7 +89,7 @@ def run(self):
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, self._signal_handler)
# 后台管理端
server.run(self.conversation, self)
server.run(self.conversation, self, debug=self._debug)
try:
# 初始化离线唤醒
detector.initDetector(self)
Expand Down Expand Up @@ -172,6 +173,14 @@ def profiling(self):
self._profiling = True
self.run()

def debug(self):
"""
调试模式启动服务
"""
logger.info("进入调试模式")
self._debug = True
self.run()


if __name__ == "__main__":
if len(sys.argv) == 1:
Expand Down

0 comments on commit 5d79aca

Please sign in to comment.