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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ljdk7nDaguh9Uhi3zhcSjn
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
200509c1bd
commit
a12bc5a2ee
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user