diff --git a/README.md b/README.md index 7b8dd93..3a09a50 100644 --- a/README.md +++ b/README.md @@ -683,9 +683,11 @@ python -m semif_agent.cli gateway --platform voice official models score ~0.99 and use `0.5`). - **Behavior.** Half-duplex — capture is discarded while a reply is speaking, so 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 + the wake word fires (`gateway.voice.cue`), so you know you're being heard; a + short multi-note "bling" (`cue.submit_notes`, each entry + `[frequency_hz, duration_ms]`, spaced by `cue.note_gap_ms`) plays once the + utterance is transcribed. (Set `cue.submit_notes` to `[]` and + `cue.submit_frequency > 0` for the older single submit beep.) After a reply, the next utterance is accepted **without** the wake word for `follow_up_window_s`, so answering a question is conversational. After the wake word, capture waits `listen_timeout_s` for the command to begin, so a pause before speaking diff --git a/config.example.json b/config.example.json index 3e83302..c591769 100644 --- a/config.example.json +++ b/config.example.json @@ -228,13 +228,15 @@ "follow_up_window_s": 5.0, "listen_timeout_s": 3.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.", + "_comment": "Audible feedback. A short sine beep plays when the wake word fires so you know you're being heard; submit_notes is a short multi-note 'bling' played once the utterance is transcribed (each entry [frequency_hz, duration_ms], separated by note_gap_ms of silence). Set submit_notes to [] and submit_frequency > 0 for the older single submit beep, or leave both empty/0 for no submit sound. volume is 0..1.", "enabled": true, "frequency": 880, "duration_ms": 120, "volume": 0.9, "submit_frequency": 0, - "submit_duration_ms": 100 + "submit_duration_ms": 100, + "submit_notes": [[1046.5, 70], [1568.0, 90]], + "note_gap_ms": 20 }, "wake": { "_comment": "model is an openWakeWord name/path. The default 'hey_computer' is a pinned community model whose scores run lower than the official models (measured ~0.26-0.82 on the phrase, <0.02 on negatives), so threshold 0.2 is tuned for it; the official models (e.g. hey_mycroft) score ~0.99 and are fine at 0.5.", diff --git a/semif_agent/voice_transport.py b/semif_agent/voice_transport.py index 839c27b..579bf81 100644 --- a/semif_agent/voice_transport.py +++ b/semif_agent/voice_transport.py @@ -334,6 +334,43 @@ def _tone_pcm(frequency: float, duration_ms: int, sample_rate: int, volume: floa return bytes(frames) +def _tone_sequence(notes, sample_rate: int, volume: float, gap_ms: int = 0) -> bytes: + """Concatenate `_tone_pcm` notes (each `[frequency_hz, duration_ms]`). + + A short inter-note silence (`gap_ms`) makes a multi-note chime read as + distinct notes rather than one warble. Each note already fades in and out, + so there is no click at a boundary whether or not a gap is used. + """ + gap = b"\x00\x00" * int(sample_rate * max(0, gap_ms) / 1000) + parts: list[bytes] = [] + for frequency, duration in notes: + tone = _tone_pcm(frequency, duration, sample_rate, volume) + if not tone: + continue + if parts and gap: + parts.append(gap) + parts.append(tone) + return b"".join(parts) + + +def _parse_notes(value) -> list[tuple[float, int]]: + """Parse a cue note list (`[[frequency_hz, duration_ms], ...]`). + + Bad entries are dropped rather than raising: this is cosmetic audio config, + not a contract. An empty/absent list yields `[]` (the single-beep fallback). + """ + notes: list[tuple[float, int]] = [] + for item in value or []: + try: + frequency, duration = item + frequency, duration = float(frequency), int(duration) + except (TypeError, ValueError): + continue + if frequency > 0 and duration > 0: + notes.append((frequency, duration)) + return notes + + def _device_arg(value): """Coerce a config device value to a PortAudio argument. @@ -644,6 +681,11 @@ class VoiceDaemon: 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) + #: A short multi-note "bling" played once a transcription lands (each + #: entry `[frequency_hz, duration_ms]`). When empty, the single + #: `submit_frequency` beep is used instead (silent when that is 0). + self.cue_submit_notes = _parse_notes(cue.get("submit_notes")) + self.cue_note_gap_ms = int(cue.get("note_gap_ms", 20) or 0) self._engines = engines self._engines_lock = threading.Lock() @@ -756,7 +798,7 @@ class VoiceDaemon: self._event("voice_stt_empty", samples=len(utterance) // BYTES_PER_SAMPLE) continue self._event("voice_transcribed", chars=len(text)) - self._cue(self.cue_submit_frequency, self.cue_submit_duration_ms) + self._bling() if self._on_utterance is not None: self._on_utterance(text) @@ -907,6 +949,36 @@ class VoiceDaemon: except Exception as exc: self._event("voice_cue_failed", message=str(exc)[:200]) + def _bling(self) -> None: + """Play the post-transcription chime. + + A configured `submit_notes` sequence (a short multi-note "bling") wins; + otherwise it falls back to the single `submit_frequency` beep (silent at + the shipped default of 0). + """ + if self.cue_submit_notes: + self._play_tones(self.cue_submit_notes) + else: + self._cue(self.cue_submit_frequency, self.cue_submit_duration_ms) + + def _play_tones(self, notes) -> None: + """Play a note sequence (the multi-note submit cue); silent on failure.""" + if not self.cue_enabled: + return + engines = self._engines + if engines is None: + return + pcm = _tone_sequence(notes, self.sample_rate, self.cue_volume, self.cue_note_gap_ms) + 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", notes=[[float(f), int(d)] for f, d in notes]) + 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() diff --git a/tests/test_voice_gateway.py b/tests/test_voice_gateway.py index bbfbbbd..ad78c73 100644 --- a/tests/test_voice_gateway.py +++ b/tests/test_voice_gateway.py @@ -454,6 +454,52 @@ def test_cue_disabled_suppresses_beep(): assert engines.audio.writes == [] +def test_submit_plays_bling_after_transcription(): + # A configured `submit_notes` chime plays once the utterance is transcribed, + # as a single write (the notes are concatenated) at the processing rate. + daemon, engines = make_daemon( + [b"WAKE", b"speech", b"SIL", b"SIL"], + config={"cue": {"submit_notes": [[1046.5, 70], [1568.0, 90]], "note_gap_ms": 20}}, + ) + daemon.run(lambda text: None) + # The wake beep, then the bling. + assert len(engines.audio.writes) == 2 + pcm, rate = engines.audio.writes[1] + assert rate == 16000 + expected = int(16000 * (70 + 90) / 1000) * 2 + int(16000 * 20 / 1000) * 2 + assert len(pcm) == expected + + +def test_submit_bling_falls_back_to_single_beep(): + daemon, engines = make_daemon( + [b"WAKE", b"speech", b"SIL", b"SIL"], + config={"cue": {"submit_frequency": 1320, "submit_duration_ms": 80}}, + ) + daemon.run(lambda text: None) + assert len(engines.audio.writes) == 2 + pcm, rate = engines.audio.writes[1] + assert len(pcm) == int(16000 * 80 / 1000) * 2 + + +def test_submit_bling_disabled_suppresses(): + daemon, engines = make_daemon( + [b"WAKE", b"speech", b"SIL", b"SIL"], + config={"cue": {"enabled": False, "submit_notes": [[1046.5, 70]]}}, + ) + daemon.run(lambda text: None) + assert engines.audio.writes == [] + + +def test_parse_notes_drops_bad_entries(): + from semif_agent.voice_transport import _parse_notes + + assert _parse_notes([[880, 100], "nope", [0, 50], [440, 0], [660.0, 30.9]]) == [ + (880.0, 100), + (660.0, 30), + ] + assert _parse_notes(None) == [] + + # ---- resampling (numpy is a voice-stack dep; skip without it) --------------