tts speaks MP3: a seventh of WAV's bytes over a slow link to the server

A sentence's length for `append` comes from the Xing header LAME writes into
the first frame. The cache key carries the format, so no WAV is reused.
This commit is contained in:
Melvin Strobl
2026-10-19 21:55:17 +02:00
parent 16383ea6e2
commit f50d8b9570
4 changed files with 35 additions and 22 deletions
+8 -12
View File
@@ -2,8 +2,6 @@
import asyncio
import hashlib
import io
import wave
from typing import Any
from fluksio.ai import client
@@ -105,14 +103,11 @@ def test_an_utterance_is_one_session_committed_when_listening_ends(monkeypatch):
asyncio.run(scenario())
def _wav(seconds: float) -> bytes:
out = io.BytesIO()
with wave.open(out, "wb") as clip:
clip.setnchannels(1)
clip.setsampwidth(2)
clip.setframerate(24000)
clip.writeframes(b"\0\0" * int(24000 * seconds))
return out.getvalue()
def _mp3(frames: int) -> bytes:
"""The first frame LAME writes: MPEG-2 layer III at 24 kHz, mono, and the
Xing header counting the frames, 576 samples each."""
xing = b"Xing" + (15).to_bytes(4, "big") + frames.to_bytes(4, "big")
return bytes.fromhex("fff384c4") + bytes(9) + xing + bytes(100)
def test_a_sentence_is_spoken_once_and_the_rest_of_a_reply_queues(monkeypatch):
@@ -121,7 +116,7 @@ def test_a_sentence_is_spoken_once_and_the_rest_of_a_reply_queues(monkeypatch):
async def speak(api_base, text, voice):
spoken.append(text)
return _wav(len(spoken))
return _mp3(25 * len(spoken))
monkeypatch.setattr(client, "speak", speak)
monkeypatch.setattr(TtsNode, "_say", lambda self, speech: published.append(speech))
@@ -144,4 +139,5 @@ def test_a_sentence_is_spoken_once_and_the_rest_of_a_reply_queues(monkeypatch):
assert spoken == ["Kitchen light on.", "Heating off."]
assert ["append" in speech for speech in published] == [False, True, False]
assert published[2] == published[0]
assert published[1]["meta"] == {"seconds": 2.0}
assert published[1]["meta"] == {"seconds": 1.2}
assert published[1]["media_type"] == "audio/mpeg"