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:
@@ -10,12 +10,10 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import io
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import wave
|
||||
from collections.abc import Coroutine, Iterable
|
||||
from concurrent.futures import Future
|
||||
from typing import TYPE_CHECKING, Any
|
||||
@@ -68,6 +66,23 @@ def sentences(text: str) -> list[str]:
|
||||
return [s for s in (p.strip() for p in re.split(r"(?<=[.!?])\s+", text)) if s]
|
||||
|
||||
|
||||
def mp3_seconds(data: bytes) -> float:
|
||||
"""An MP3's length, from the frame count LAME writes into its first frame."""
|
||||
header = int.from_bytes(data[:4], "big")
|
||||
version = (header >> 19) & 3 # 3 is MPEG-1, 2 MPEG-2, 0 MPEG-2.5
|
||||
rates = {
|
||||
3: (44100, 48000, 32000),
|
||||
2: (22050, 24000, 16000),
|
||||
0: (11025, 12000, 8000),
|
||||
}
|
||||
rate = rates[version][(header >> 10) & 3]
|
||||
at = max(data.find(b"Xing", 0, 64), data.find(b"Info", 0, 64))
|
||||
if at < 0:
|
||||
raise ValueError("the MP3 has no Xing or Info header to read its length from")
|
||||
frames = int.from_bytes(data[at + 8 : at + 12], "big")
|
||||
return round(frames * (1152 if version == 3 else 576) / rate, 3)
|
||||
|
||||
|
||||
class _Speech(Node):
|
||||
"""What both nodes share: the url, the artifact store and their health."""
|
||||
|
||||
@@ -334,19 +349,17 @@ class TtsNode(_Speech):
|
||||
|
||||
def _spoken(self, url: str, sentence: str) -> dict[str, Any]:
|
||||
"""The reference to ``sentence`` spoken, from the cache or the server."""
|
||||
key = hashlib.sha256(f"{self.cfg.voice}\n{sentence}".encode()).hexdigest()
|
||||
key = hashlib.sha256(f"mp3\n{self.cfg.voice}\n{sentence}".encode()).hexdigest()
|
||||
# Kept in state, so the sweep keeps the bytes it refers to.
|
||||
spoken = dict(self.recall("spoken") or {})
|
||||
reference = spoken.get(key)
|
||||
if reference is not None and self._artifacts.has(reference["digest"]):
|
||||
return dict(reference)
|
||||
data = asyncio.run(client.speak(url + "/v1", sentence, self.cfg.voice))
|
||||
with wave.open(io.BytesIO(data)) as clip:
|
||||
seconds = round(clip.getnframes() / clip.getframerate(), 3)
|
||||
stored: dict[str, Any] = self._artifacts.put(
|
||||
[data], name="speech.wav", media_type="audio/wav"
|
||||
[data], name="speech.mp3", media_type="audio/mpeg"
|
||||
)
|
||||
reference = stored | {"meta": {"seconds": seconds}}
|
||||
reference = stored | {"meta": {"seconds": mp3_seconds(data)}}
|
||||
spoken.pop(key, None)
|
||||
spoken[key] = reference
|
||||
self.remember("spoken", dict(list(spoken.items())[-SPOKEN:]))
|
||||
|
||||
Reference in New Issue
Block a user