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.

perf: start streamed voice playback sooner

Ruben Lütkec4ab05e534dc
diff --git a/jarvis/audio/player.py b/jarvis/audio/player.py
index 3da1d1e082..d369e4d196 100644
--- a/jarvis/audio/player.py
+++ b/jarvis/audio/player.py
@@ -43,6 +43,11 @@ log = logging.getLogger("jarvis.audio.player")
_PortAudioError: type[BaseException] = sd.PortAudioError if sd is not None else OSError
TTS_SAMPLE_RATE = 24_000 # Gemini 3.1 Flash TTS output rate
+# Start playout as soon as a small, click-safe prefix is available. Later writes
+# stay larger to preserve low CPU overhead and underrun resistance. With a
+# 20 ms provider stream this removes 80 ms from time-to-first-audio while the
+# persistent PortAudio stream keeps the waveform continuous across batches.
+TTS_FIRST_WRITE_BUFFER_MS = 40
TTS_WRITE_BUFFER_MS = 120
_MAX_REPORTED_OUTPUT_LATENCY_S = 5.0
@@ -1121,14 +1126,20 @@ class AudioPlayer:
pending = bytearray()
pending_rate: int | None = None
first_audio_published = False
+ wrote_audio = False
last_flushed_sample = 0
async def _flush_pending(*, final: bool = False) -> bool:
nonlocal pending, pending_rate, first_audio_published
- nonlocal last_flushed_sample
+ nonlocal last_flushed_sample, wrote_audio
if not pending or pending_rate is None:
return True
- min_bytes = int(pending_rate * TTS_WRITE_BUFFER_MS / 1000) * 2
+ target_ms = (
+ TTS_WRITE_BUFFER_MS
+ if wrote_audio
+ else TTS_FIRST_WRITE_BUFFER_MS
+ )
+ min_bytes = int(pending_rate * target_ms / 1000) * 2
if not final and len(pending) < min_bytes:
return True
if should_play is not None and not should_play():
@@ -1169,6 +1180,8 @@ class AudioPlayer:
dev_rate,
playback_generation=playback_generation,
)
+ if arr.size:
+ wrote_audio = True
with stream_state_lock:
playback_superseded = (
getattr(self, "_playback_generation", 0)
diff --git a/tests/unit/audio/test_player_persistent_stream.py b/tests/unit/audio/test_player_persistent_stream.py
index bf19b1036a..d556b99e1f 100644
--- a/tests/unit/audio/test_player_persistent_stream.py
+++ b/tests/unit/audio/test_player_persistent_stream.py
@@ -36,6 +36,12 @@ async def _one_chunk(pcm: bytes, sample_rate: int = 24_000) -> AsyncIterator[Aud
yield AudioChunk(pcm=pcm, sample_rate=sample_rate, timestamp_ns=0, channels=1)
+async def _twenty_ms_chunks(count: int) -> AsyncIterator[AudioChunk]:
+ pcm = b"\x01\x00" * 480
+ for _ in range(count):
+ yield AudioChunk(pcm=pcm, sample_rate=24_000, timestamp_ns=0, channels=1)
+
+
def _make_player(monkeypatch) -> tuple[AudioPlayer, list[str]]:
"""Build an AudioPlayer with stream IO monkeypatched to record events.
@@ -104,6 +110,26 @@ async def test_two_sequential_play_chunks_share_one_stream(monkeypatch) -> None:
assert player._active_source_rate == 24_000
+async def test_first_write_uses_low_latency_prefix_then_larger_batches(
+ monkeypatch,
+) -> None:
+ """A 20 ms provider stream reaches PortAudio after 40 ms, not 120 ms."""
+ player, _events = _make_player(monkeypatch)
+ writes: list[int] = []
+
+ def record_write(stream, arr, src_rate, dev_rate, **_kwargs):
+ writes.append(len(arr))
+
+ monkeypatch.setattr(player, "_write_samples", record_write)
+
+ await player.play_chunks(_twenty_ms_chunks(12))
+
+ assert writes[0] == 960 # 40 ms at 24 kHz
+ assert writes[1] == 2_880 # steady-state 120 ms batch
+ assert sum(writes) == 12 * 480
+
+
@pytest.mark.asyncio
async def test_sample_rate_change_triggers_reopen(monkeypatch) -> None:
"""When the source sample rate changes between two calls (e.g. Gemini