diff --git a/AGENTS.md b/AGENTS.md index 08e3ace..8501896 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -959,7 +959,9 @@ CLI, unit tests (24) + integration tests (2). 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 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 **lazy-imported**; `check_requirements()` returns an install hint and the gateway refuses to start without the stack. The engine interfaces are plain diff --git a/README.md b/README.md index 466cf30..6d27a3b 100644 --- a/README.md +++ b/README.md @@ -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 in the `.runtime/hf` cache. - **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 answering a question is conversational. `max_speak_chars` truncates long replies before speaking. There is no allowlist: the mic is local, so physical diff --git a/config.example.json b/config.example.json index 8c54ff0..819a3bd 100644 --- a/config.example.json +++ b/config.example.json @@ -168,6 +168,15 @@ "reply_prefix": "", "max_speak_chars": 600, "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": { "model": "hey_mycroft", "threshold": 0.5, diff --git a/semif_agent/voice_transport.py b/semif_agent/voice_transport.py index 5df765e..b418674 100644 --- a/semif_agent/voice_transport.py +++ b/semif_agent/voice_transport.py @@ -303,6 +303,28 @@ def _remove_dc(pcm16: bytes) -> bytes: 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(" 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: """Synthesize and play `text`; called from the outbound pump thread.""" text = (text or "").strip() @@ -725,8 +775,9 @@ class VoiceDaemon: try: pcm, sample_rate = engines.tts.synthesize(text) if pcm: - engines.audio.open_output(sample_rate) - engines.audio.write(pcm, sample_rate) + with self._audio_lock: + engines.audio.open_output(sample_rate) + engines.audio.write(pcm, sample_rate) self._event("voice_spoke", chars=len(text)) except Exception as exc: self._event("voice_speak_failed", message=str(exc)[:300]) diff --git a/tests/test_voice_gateway.py b/tests/test_voice_gateway.py index d2ec289..22db156 100644 --- a/tests/test_voice_gateway.py +++ b/tests/test_voice_gateway.py @@ -265,6 +265,35 @@ def test_resolve_platforms_includes_enabled_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) --------------