voice: wake-word barge-in (interrupt a spoken reply)
While a reply is speaking the wake word is still scored, so the user can
interrupt it. On a wake the daemon aborts the playback, mutes further
speech while the interrupt is captured/handled, and hands the following
command to the front end. A bare "stop"/"cancel" aborts the reply and
drops anything queued behind it; anything else runs as a new command.
- voice_transport: AudioIO.play(pcm, rate, stop_event); SoundDeviceAudio
streams in ~50 ms chunks and aborts the output stream on a stop.
VoiceDaemon gains barge_in.{enabled,wait_s,threshold} and the
on_barge_in(active) callback; speak() drops text while muted and plays
through the interruptible path.
- gateway/voice: tags the interrupt utterance (raw["barge_in"]),
classifies stop vs new command (_is_stop_command), drops queued replies
on stop, and unmutes once handled.
- config: gateway.voice.barge_in block; config.example.json regenerated.
- tests: wake-abort, mute release, threshold gate, stop/new-command
classification, sentinel preservation.
This commit is contained in:
1 parent
a8ca65a6e4
commit
faebe8a53a
6 files changed
+542
-57
No files matched your search
@@ -27,6 +27,55 @@ from semif_agent.voice_transport import VoiceDaemon
|
||||
|
||||
from .base import GatewayAdapter, InboundMessage, OutboundMessage
|
||||
|
||||
#: Words/phrases that abort a spoken reply when heard after a barge-in. The
|
||||
#: match is on the whole normalized utterance (so "stop the timer" stays a real
|
||||
#: command) with a first-word fallback only for a short utterance with trailing
|
||||
#: filler ("stop please", "cancel that").
|
||||
_STOP_PHRASES = {
|
||||
"stop",
|
||||
"stop it",
|
||||
"stop talking",
|
||||
"stop please",
|
||||
"cancel",
|
||||
"cancel that",
|
||||
"cancel please",
|
||||
"quiet",
|
||||
"quiet down",
|
||||
"be quiet",
|
||||
"silence",
|
||||
"hush",
|
||||
"shut up",
|
||||
"never mind",
|
||||
"nevermind",
|
||||
"enough",
|
||||
}
|
||||
_STOP_FIRST_WORDS = {"stop", "cancel", "quiet", "silence", "hush", "shut", "enough"}
|
||||
_STOP_FILLER = {"please", "now", "it", "that", "talking", "down", "up", "already"}
|
||||
|
||||
#: Characters stripped from a spoken utterance's edges before matching.
|
||||
_PUNCT = " \t\r\n.,!?;:\"'()[]{}"
|
||||
|
||||
|
||||
def _is_stop_command(text: str) -> bool:
|
||||
"""Whether an interrupt utterance means "stop talking" (vs a new command).
|
||||
|
||||
Deterministic vocabulary, no model: a bare "stop"/"cancel" (or a short
|
||||
variant like "stop please") aborts the spoken reply. Anything longer is a
|
||||
real command and is submitted as a new request.
|
||||
"""
|
||||
cleaned = (text or "").strip().lower().strip(_PUNCT)
|
||||
if not cleaned:
|
||||
return False
|
||||
if cleaned in _STOP_PHRASES:
|
||||
return True
|
||||
words = cleaned.split()
|
||||
if len(words) > 3:
|
||||
return False
|
||||
first = words[0].strip(_PUNCT)
|
||||
if first not in _STOP_FIRST_WORDS:
|
||||
return False
|
||||
return all(word.strip(_PUNCT) in _STOP_FILLER for word in words[1:])
|
||||
|
||||
|
||||
class VoiceAdapter(GatewayAdapter):
|
||||
name = "voice"
|
||||
@@ -41,12 +90,19 @@ class VoiceAdapter(GatewayAdapter):
|
||||
self.display_name = str(cfg.get("display_name") or self.chat_id)
|
||||
#: `run()` blocks on the mic; this daemon owns capture/wake/STT/TTS.
|
||||
self.daemon = VoiceDaemon(cfg, trace=trace, name=self.name)
|
||||
#: Set by the daemon while a barge-in command is being captured, so the
|
||||
#: transcribed utterance is classified as an interrupt.
|
||||
self._barged_in = False
|
||||
self.daemon.on_barge_in = self._on_barge_in
|
||||
self._on_inbound = None
|
||||
#: Utterances are handed to a worker thread, not run on the audio
|
||||
#: thread: the scheduler is synchronous and can block on the decision
|
||||
#: engine, and a slow or failing task must neither stop the mic being
|
||||
#: read nor kill the front end. `None` asks the worker to stop.
|
||||
self._inbound: "queue.Queue[InboundMessage | None]" = queue.Queue()
|
||||
#: The outbound reply queue (set in `run`), so a "stop" can drop replies
|
||||
#: that were already queued behind the interrupted one.
|
||||
self._outbound: "queue.Queue[OutboundMessage | None] | None" = None
|
||||
|
||||
# ---- requirements ----
|
||||
|
||||
@@ -71,6 +127,7 @@ class VoiceAdapter(GatewayAdapter):
|
||||
outbound: "queue.Queue[OutboundMessage | None]",
|
||||
) -> None:
|
||||
self._on_inbound = on_inbound
|
||||
self._outbound = outbound
|
||||
|
||||
def _pump() -> None:
|
||||
# Drain the service's replies onto the daemon's speaker. Speaking
|
||||
@@ -112,6 +169,27 @@ class VoiceAdapter(GatewayAdapter):
|
||||
self._handle(message)
|
||||
|
||||
def _handle(self, message: InboundMessage) -> None:
|
||||
if message.raw.get("barge_in"):
|
||||
if _is_stop_command(message.text):
|
||||
# A "stop"/"cancel" after a barge-in aborts the spoken reply and
|
||||
# is not a request: the daemon already cut the playback, and any
|
||||
# reply still queued behind it is stale output the user stopped.
|
||||
self.daemon._event("gateway_barge_in_stop", chars=len(message.text))
|
||||
self._drop_pending_speech()
|
||||
self.daemon.set_muted(False)
|
||||
return
|
||||
# A new command after a barge-in: the playback was already aborted,
|
||||
# so just run it (and unmute once it has been handled).
|
||||
self.daemon._event("gateway_barge_in_command", chars=len(message.text))
|
||||
try:
|
||||
if self._on_inbound is not None:
|
||||
self._on_inbound(message)
|
||||
except Exception as exc:
|
||||
self.daemon._event("gateway_handler_failed", message=str(exc)[:300])
|
||||
self.daemon.speak("Sorry, something went wrong handling that.")
|
||||
finally:
|
||||
self.daemon.set_muted(False)
|
||||
return
|
||||
if self._on_inbound is None:
|
||||
return
|
||||
try:
|
||||
@@ -120,10 +198,39 @@ class VoiceAdapter(GatewayAdapter):
|
||||
self.daemon._event("gateway_handler_failed", message=str(exc)[:300])
|
||||
self.daemon.speak("Sorry, something went wrong handling that.")
|
||||
|
||||
def _drop_pending_speech(self) -> None:
|
||||
"""Discard replies already queued behind an interrupted one.
|
||||
|
||||
The daemon aborted the in-flight playback; anything waiting on the
|
||||
outbound queue is output the user asked to stop, so it is dropped rather
|
||||
than spoken next.
|
||||
"""
|
||||
outbound = self._outbound
|
||||
if outbound is None:
|
||||
return
|
||||
dropped = 0
|
||||
while True:
|
||||
try:
|
||||
message = outbound.get_nowait()
|
||||
except queue.Empty:
|
||||
break
|
||||
if message is None:
|
||||
outbound.put(None) # preserve the pump's shutdown sentinel
|
||||
break
|
||||
dropped += 1
|
||||
if dropped:
|
||||
self.daemon._event("gateway_speech_dropped", count=dropped)
|
||||
|
||||
def _on_barge_in(self, active: bool) -> None:
|
||||
"""Daemon callback: mark a barge-in session (True) and its end (False)."""
|
||||
self._barged_in = bool(active)
|
||||
|
||||
def _on_utterance(self, text: str) -> None:
|
||||
text = (text or "").strip()
|
||||
if not text:
|
||||
return
|
||||
barge_in = self._barged_in
|
||||
self._barged_in = False
|
||||
self.daemon._event(
|
||||
"gateway_message",
|
||||
contact_id=self.chat_id,
|
||||
@@ -137,6 +244,6 @@ class VoiceAdapter(GatewayAdapter):
|
||||
chat_type="dm",
|
||||
contact_id=self.chat_id,
|
||||
display_name=self.display_name,
|
||||
raw={},
|
||||
raw={"barge_in": True} if barge_in else {},
|
||||
)
|
||||
)
|
||||
Reference in new issue
Block a user