voice: play a beep cue on wake
This commit is contained in:
1 parent
eeed8407f9
commit
d2f9a74bd9
5 files changed
+96
-2
No files matched your search
@@ -959,7 +959,9 @@ CLI, unit tests (24) + integration tests (2).
|
|||||||
reply is speaking, so the agent never transcribes itself (no barge-in yet);
|
reply is speaking, so the agent never transcribes itself (no barge-in yet);
|
||||||
after a reply a `follow_up_window_s` accepts the next utterance without the
|
after a reply a `follow_up_window_s` accepts the next utterance without the
|
||||||
wake word, so answering a question is conversational. `max_speak_chars`
|
wake word, so answering a question is conversational. `max_speak_chars`
|
||||||
truncates long replies before speaking. Every heavy dep
|
truncates long replies before speaking, and a short sine `cue` beep plays on
|
||||||
|
wake (optionally again on transcription) so the user knows they were heard.
|
||||||
|
Every heavy dep
|
||||||
(`sounddevice`/`openwakeword`/`faster_whisper`/`piper`/`onnxruntime`) is
|
(`sounddevice`/`openwakeword`/`faster_whisper`/`piper`/`onnxruntime`) is
|
||||||
**lazy-imported**; `check_requirements()` returns an install hint and the
|
**lazy-imported**; `check_requirements()` returns an install hint and the
|
||||||
gateway refuses to start without the stack. The engine interfaces are plain
|
gateway refuses to start without the stack. The engine interfaces are plain
|
||||||
|
|||||||
@@ -661,7 +661,10 @@ python -m semif_agent.cli gateway --platform voice
|
|||||||
and the Piper voice into `.runtime/voice/tts`; the faster-whisper model lands
|
and the Piper voice into `.runtime/voice/tts`; the faster-whisper model lands
|
||||||
in the `.runtime/hf` cache.
|
in the `.runtime/hf` cache.
|
||||||
- **Behavior.** Half-duplex — capture is discarded while a reply is speaking, so
|
- **Behavior.** Half-duplex — capture is discarded while a reply is speaking, so
|
||||||
the agent never transcribes itself (no barge-in yet). After a reply, the next
|
the agent never transcribes itself (no barge-in yet). A short beep plays when
|
||||||
|
the wake word fires (`gateway.voice.cue`), so you know you're being heard; set
|
||||||
|
`cue.submit_frequency > 0` for a second beep once the utterance is
|
||||||
|
transcribed. After a reply, the next
|
||||||
utterance is accepted **without** the wake word for `follow_up_window_s`, so
|
utterance is accepted **without** the wake word for `follow_up_window_s`, so
|
||||||
answering a question is conversational. `max_speak_chars` truncates long
|
answering a question is conversational. `max_speak_chars` truncates long
|
||||||
replies before speaking. There is no allowlist: the mic is local, so physical
|
replies before speaking. There is no allowlist: the mic is local, so physical
|
||||||
|
|||||||
@@ -168,6 +168,15 @@
|
|||||||
"reply_prefix": "",
|
"reply_prefix": "",
|
||||||
"max_speak_chars": 600,
|
"max_speak_chars": 600,
|
||||||
"follow_up_window_s": 5.0,
|
"follow_up_window_s": 5.0,
|
||||||
|
"cue": {
|
||||||
|
"_comment": "Audible feedback. A short sine beep plays when the wake word fires so you know you're being heard; set submit_frequency > 0 for a second beep once the utterance is transcribed. volume is 0..1.",
|
||||||
|
"enabled": true,
|
||||||
|
"frequency": 880,
|
||||||
|
"duration_ms": 120,
|
||||||
|
"volume": 0.25,
|
||||||
|
"submit_frequency": 0,
|
||||||
|
"submit_duration_ms": 100
|
||||||
|
},
|
||||||
"wake": {
|
"wake": {
|
||||||
"model": "hey_mycroft",
|
"model": "hey_mycroft",
|
||||||
"threshold": 0.5,
|
"threshold": 0.5,
|
||||||
|
|||||||
@@ -303,6 +303,28 @@ def _remove_dc(pcm16: bytes) -> bytes:
|
|||||||
return np.clip(samples, -32768, 32767).astype(np.int16).tobytes()
|
return np.clip(samples, -32768, 32767).astype(np.int16).tobytes()
|
||||||
|
|
||||||
|
|
||||||
|
def _tone_pcm(frequency: float, duration_ms: int, sample_rate: int, volume: float) -> bytes:
|
||||||
|
"""Generate a short sine beep as 16-bit mono PCM (stdlib only).
|
||||||
|
|
||||||
|
A 10 ms fade in/out avoids clicks. Used for the wake-word cue; it needs no
|
||||||
|
audio asset and no numpy, so it works on the stdlib-only path too.
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
import struct
|
||||||
|
|
||||||
|
count = int(sample_rate * max(0, duration_ms) / 1000)
|
||||||
|
if count <= 0 or frequency <= 0:
|
||||||
|
return b""
|
||||||
|
amplitude = int(max(0.0, min(1.0, volume)) * 32767)
|
||||||
|
fade = max(1, int(sample_rate * 0.01)) # 10 ms
|
||||||
|
frames = bytearray()
|
||||||
|
for index in range(count):
|
||||||
|
envelope = min(1.0, index / fade, (count - index) / fade)
|
||||||
|
value = int(amplitude * envelope * math.sin(2 * math.pi * frequency * index / sample_rate))
|
||||||
|
frames += struct.pack("<h", value)
|
||||||
|
return bytes(frames)
|
||||||
|
|
||||||
|
|
||||||
def _device_arg(value):
|
def _device_arg(value):
|
||||||
"""Coerce a config device value to a PortAudio argument.
|
"""Coerce a config device value to a PortAudio argument.
|
||||||
|
|
||||||
@@ -571,9 +593,17 @@ class VoiceDaemon:
|
|||||||
self.max_utterance_s = float(vad_cfg.get("max_utterance_s", 30) or 30)
|
self.max_utterance_s = float(vad_cfg.get("max_utterance_s", 30) or 30)
|
||||||
self.follow_up_window = float(cfg.get("follow_up_window_s", 5.0) or 0.0)
|
self.follow_up_window = float(cfg.get("follow_up_window_s", 5.0) or 0.0)
|
||||||
self.max_speak_chars = int(cfg.get("max_speak_chars", 600) or 0)
|
self.max_speak_chars = int(cfg.get("max_speak_chars", 600) or 0)
|
||||||
|
cue = cfg.get("cue", {}) or {}
|
||||||
|
self.cue_enabled = bool(cue.get("enabled", True))
|
||||||
|
self.cue_frequency = float(cue.get("frequency", 880) or 0)
|
||||||
|
self.cue_duration_ms = int(cue.get("duration_ms", 120) or 0)
|
||||||
|
self.cue_volume = float(cue.get("volume", 0.25) or 0)
|
||||||
|
self.cue_submit_frequency = float(cue.get("submit_frequency", 0) or 0)
|
||||||
|
self.cue_submit_duration_ms = int(cue.get("submit_duration_ms", 100) or 0)
|
||||||
|
|
||||||
self._engines = engines
|
self._engines = engines
|
||||||
self._engines_lock = threading.Lock()
|
self._engines_lock = threading.Lock()
|
||||||
|
self._audio_lock = threading.Lock() # serialize cue/speak playback
|
||||||
self._on_utterance: UtteranceHandler | None = None
|
self._on_utterance: UtteranceHandler | None = None
|
||||||
self._stop = threading.Event()
|
self._stop = threading.Event()
|
||||||
self._speaking = threading.Event()
|
self._speaking = threading.Event()
|
||||||
@@ -660,6 +690,7 @@ class VoiceDaemon:
|
|||||||
engines.wake.reset()
|
engines.wake.reset()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
self._cue(self.cue_frequency, self.cue_duration_ms)
|
||||||
self._follow_until = 0.0
|
self._follow_until = 0.0
|
||||||
utterance = self._collect(engines, pcm)
|
utterance = self._collect(engines, pcm)
|
||||||
if not utterance:
|
if not utterance:
|
||||||
@@ -674,6 +705,7 @@ class VoiceDaemon:
|
|||||||
self._event("voice_stt_empty", samples=len(utterance) // BYTES_PER_SAMPLE)
|
self._event("voice_stt_empty", samples=len(utterance) // BYTES_PER_SAMPLE)
|
||||||
continue
|
continue
|
||||||
self._event("voice_transcribed", chars=len(text))
|
self._event("voice_transcribed", chars=len(text))
|
||||||
|
self._cue(self.cue_submit_frequency, self.cue_submit_duration_ms)
|
||||||
if self._on_utterance is not None:
|
if self._on_utterance is not None:
|
||||||
self._on_utterance(text)
|
self._on_utterance(text)
|
||||||
|
|
||||||
@@ -712,6 +744,24 @@ class VoiceDaemon:
|
|||||||
|
|
||||||
# ---- speaking ----
|
# ---- speaking ----
|
||||||
|
|
||||||
|
def _cue(self, frequency: float, duration_ms: int) -> None:
|
||||||
|
"""Play a short beep (wake / submit cue). Silent on failure."""
|
||||||
|
if not self.cue_enabled or frequency <= 0 or duration_ms <= 0:
|
||||||
|
return
|
||||||
|
engines = self._engines
|
||||||
|
if engines is None:
|
||||||
|
return
|
||||||
|
pcm = _tone_pcm(frequency, duration_ms, self.sample_rate, self.cue_volume)
|
||||||
|
if not pcm:
|
||||||
|
return
|
||||||
|
with self._audio_lock:
|
||||||
|
try:
|
||||||
|
engines.audio.open_output(self.sample_rate)
|
||||||
|
engines.audio.write(pcm, self.sample_rate)
|
||||||
|
self._event("voice_cue", frequency=frequency, duration_ms=duration_ms)
|
||||||
|
except Exception as exc:
|
||||||
|
self._event("voice_cue_failed", message=str(exc)[:200])
|
||||||
|
|
||||||
def speak(self, text: str) -> None:
|
def speak(self, text: str) -> None:
|
||||||
"""Synthesize and play `text`; called from the outbound pump thread."""
|
"""Synthesize and play `text`; called from the outbound pump thread."""
|
||||||
text = (text or "").strip()
|
text = (text or "").strip()
|
||||||
@@ -725,6 +775,7 @@ class VoiceDaemon:
|
|||||||
try:
|
try:
|
||||||
pcm, sample_rate = engines.tts.synthesize(text)
|
pcm, sample_rate = engines.tts.synthesize(text)
|
||||||
if pcm:
|
if pcm:
|
||||||
|
with self._audio_lock:
|
||||||
engines.audio.open_output(sample_rate)
|
engines.audio.open_output(sample_rate)
|
||||||
engines.audio.write(pcm, sample_rate)
|
engines.audio.write(pcm, sample_rate)
|
||||||
self._event("voice_spoke", chars=len(text))
|
self._event("voice_spoke", chars=len(text))
|
||||||
|
|||||||
@@ -265,6 +265,35 @@ def test_resolve_platforms_includes_enabled_voice():
|
|||||||
assert platforms == ["voice"]
|
assert platforms == ["voice"]
|
||||||
|
|
||||||
|
|
||||||
|
# ---- wake cue (beep) -------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_tone_pcm_length():
|
||||||
|
from semif_agent.voice_transport import _tone_pcm
|
||||||
|
|
||||||
|
pcm = _tone_pcm(880, 100, 16000, 0.25)
|
||||||
|
assert len(pcm) == 1600 * 2 # 100 ms @ 16 kHz, int16
|
||||||
|
assert _tone_pcm(0, 100, 16000, 0.25) == b""
|
||||||
|
|
||||||
|
|
||||||
|
def test_wake_plays_cue():
|
||||||
|
daemon, engines = make_daemon([b"WAKE", b"speech", b"SIL", b"SIL"])
|
||||||
|
daemon.run(lambda text: None)
|
||||||
|
# exactly one cue played on wake, at the processing rate
|
||||||
|
assert len(engines.audio.writes) == 1
|
||||||
|
pcm, rate = engines.audio.writes[0]
|
||||||
|
assert rate == 16000
|
||||||
|
assert len(pcm) == int(16000 * 120 / 1000) * 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_cue_disabled_suppresses_beep():
|
||||||
|
daemon, engines = make_daemon(
|
||||||
|
[b"WAKE", b"speech", b"SIL", b"SIL"], config={"cue": {"enabled": False}}
|
||||||
|
)
|
||||||
|
daemon.run(lambda text: None)
|
||||||
|
assert engines.audio.writes == []
|
||||||
|
|
||||||
|
|
||||||
# ---- resampling (numpy is a voice-stack dep; skip without it) --------------
|
# ---- resampling (numpy is a voice-stack dep; skip without it) --------------
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user