Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(audio): let one-shot cues play out instead of being discarded

rubenluetke10-beep21edd3c45059
diff --git a/jarvis/audio/chime.py b/jarvis/audio/chime.py
index d68503198d..e41ac958dd 100644
--- a/jarvis/audio/chime.py
+++ b/jarvis/audio/chime.py
@@ -93,7 +93,7 @@ SCREEN_CAPTURE_PREROLL_S: float = 0.045
def generate_screen_capture_pcm(
sample_rate: int = 24_000,
- amplitude: float = 0.45,
+ amplitude: float = 0.2,
) -> bytes:
"""Generate the appshot cue: a soft, rounded two-note "bloop".
@@ -103,6 +103,10 @@ def generate_screen_capture_pcm(
plays the whole cue (the former noise-burst shutter packed half its
energy into the first 5 ms and was inaudible). Synthesized in memory:
portable and free of third-party recordings.
+
+ The peak stays low on purpose: the player's master volume adds up to 4x
+ makeup gain, and a louder source would ride the peak limiter instead of
+ staying soft.
"""
preroll = int(SCREEN_CAPTURE_PREROLL_S * sample_rate)
body_s = 0.34
diff --git a/jarvis/audio/player.py b/jarvis/audio/player.py
index d8bd0ec2ce..57215bddb2 100644
--- a/jarvis/audio/player.py
+++ b/jarvis/audio/player.py
@@ -212,6 +212,21 @@ _HOSTAPI_PREFERENCE = {
_FORBIDDEN_OUTPUT_HOSTAPIS = frozenset({"Windows WDM-KS"})
+def _drain_tail_samples(stream: Any, source_rate: int) -> int:
+ """Silence (in source samples) that pushes a one-shot through the buffer.
+
+ Covers the stream's reported output latency plus a 50 ms margin; a
+ malformed or missing latency falls back to ``DEFAULT_OUTPUT_BUFFER_S``.
+ """
+ try:
+ latency = float(getattr(stream, "latency", DEFAULT_OUTPUT_BUFFER_S))
+ except (TypeError, ValueError):
+ latency = DEFAULT_OUTPUT_BUFFER_S
+ if not 0.0 <= latency <= 2.0:
+ latency = DEFAULT_OUTPUT_BUFFER_S
+ return int(source_rate * (latency + 0.05))
+
+
def _apply_edge_fades(pcm: bytes, sample_rate: int, fade_ms: int = 5) -> bytes:
"""Apply a linear fade-in + fade-out (each ``fade_ms``) to int16 mono PCM.
@@ -927,6 +942,13 @@ class AudioPlayer:
with state_lock:
self._unclean_stream = stream
raise _PlaybackSuperseded
+ # WASAPI's blocking-mode stop() does NOT drain: it discards whatever
+ # still sits in the device buffer. A blob shorter than that buffer
+ # (every earcon vs. the 0.4 s default) was accepted instantly and then
+ # thrown away unheard. Trailing silence one buffer deep makes the last
+ # blocking write return only once the real audio has played out.
+ tail = np.zeros(_drain_tail_samples(stream, source_rate), dtype=np.int16)
+ arr = np.concatenate([arr, tail])
try:
self._write_samples(
stream,
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index 500305f1dd..0df80f38bb 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -8882,7 +8882,6 @@ class SpeechPipeline:
chime is immediate feedback that recording is live; speech is not.
"""
try:
- await self._play_earcon(CHIME_PCM)
if ptt:
# Chime only, and NO dead-zone: the mic opens the instant this
# returns and the user is already holding the key + talking. The
@@ -8890,8 +8889,20 @@ class SpeechPipeline:
# into the mic — PTT has no spoken ACK, so running it would just
# swallow the opening words of every capture (and turn a short
# hold into a silent no-op, since the mic is not open yet when
- # the key is released).
+ # the key is released). ``play_pcm`` returns only once the chime
+ # has played out, so it is not awaited here.
+ # Strong reference: the loop holds only a weak one.
+ pending = getattr(self, "_earcon_tasks", None)
+ if pending is None:
+ pending = set()
+ self._earcon_tasks = pending
+ chime = asyncio.create_task(
+ self._play_earcon(CHIME_PCM), name="ptt-earcon"
+ )
+ pending.add(chime)
+ chime.add_done_callback(pending.discard)
return
+ await self._play_earcon(CHIME_PCM)
if self._ack_pcm:
await self._player.play_pcm(self._ack_pcm, sample_rate=24_000)
# Brief echo suppression keeps a pre-rendered acknowledgement from
diff --git a/tests/unit/audio/test_player_persistent_stream.py b/tests/unit/audio/test_player_persistent_stream.py
index d556b99e1f..d4b6ff3d2e 100644
--- a/tests/unit/audio/test_player_persistent_stream.py
+++ b/tests/unit/audio/test_player_persistent_stream.py
@@ -365,3 +365,29 @@ async def test_multi_sentence_brain_response_keeps_one_stream(monkeypatch) -> No
f"no closes until stop() or rate-change — got {len(closes)} closes"
)
assert len(writes) == 5, f"all 5 sentences must reach the writer, got {writes}"
+
+
+class _LatencyStream:
+ latency = 0.4
+
+
+async def test_one_shot_blob_is_followed_by_a_buffer_deep_silent_tail(monkeypatch) -> None:
+ """WASAPI's blocking stop() discards the device buffer instead of draining it.
+
+ A cue shorter than the buffer was accepted instantly and thrown away
+ unheard, so ``play_pcm`` must write trailing silence one buffer deep.
+ """
+ player, _events = _make_player(monkeypatch)
+ written: list[int] = []
+ monkeypatch.setattr(
+ player, "_open_output_stream", lambda rate: (_LatencyStream(), rate)
+ )
+ monkeypatch.setattr(
+ player,
+ "_write_samples",
+ lambda stream, arr, src, dev, **_k: written.append(int(arr.size)),
+ )
+ blob_samples = 2_400 # 100 ms at 24 kHz, well under the 0.4 s buffer
+ await player.play_pcm(b"\x10\x00" * blob_samples, sample_rate=24_000)
+ assert written == [blob_samples + int(24_000 * 0.45)]
diff --git a/tests/unit/speech/test_pipeline_push_to_talk.py b/tests/unit/speech/test_pipeline_push_to_talk.py
index 825f7a9017..1bc0504b87 100644
--- a/tests/unit/speech/test_pipeline_push_to_talk.py
+++ b/tests/unit/speech/test_pipeline_push_to_talk.py
@@ -390,6 +390,9 @@ async def test_ptt_ack_plays_chime_only_without_dead_zone(monkeypatch):
monkeypatch.setattr("jarvis.speech.pipeline.asyncio.sleep", _fake_sleep)
await pipe._play_ack(ptt=True)
+ # The chime plays in the background so the mic opens without waiting for
+ # it to finish playing out.
+ await asyncio.gather(*pipe._earcon_tasks)
assert len(pipe._player.plays) == 1, "PTT must play the chime only"
assert 0.4 not in slept, "PTT must NOT run the echo dead-zone"