Files
bililive-touhou-tts/tts.py
T
chun_qiu 9b277fe2e0 feat: Bilibili Live Danmaku -> Yukkuri TTS Reader
- Python main entry with CLI (--room-id, --voice, --speed)
- blivedm integration for real-time danmaku receiving
- Chinese-to-Japanese katakana conversion (pypinyin + mapping table)
- Node.js persistent bridge for aquestalk.js TTS synthesis
- Audio playback via sounddevice
- Message queue for sequential TTS playback
2026-08-08 11:26:11 +08:00

129 lines
4.4 KiB
Python

"""Node.js TTS Bridge — Persistent Process Manager.
Starts a long-lived Node.js subprocess running tts_bridge.js. Communication uses
stdin/stdout with temporary files for text and WAV data.
Protocol:
Python → writes kana text to temp file
Python → sends "INPUT_FILE_PATH|OUTPUT_FILE_PATH\n" to bridge's stdin
Bridge → reads text, synthesizes, writes WAV to output path
Bridge → prints "OK:OUTPUT_FILE_PATH\n" to stdout (or "ERR:message\n" on error)
"""
import asyncio
import os
import pathlib
import tempfile
import logging
logger = logging.getLogger(__name__)
class TTSBridge:
"""Manages a persistent Node.js subprocess for aquestalk.js TTS synthesis."""
def __init__(self, voice: str = "f1", speed: int = 100, bridge_script: str | None = None):
self._voice = voice
self._speed = speed
self._process: asyncio.subprocess.Process | None = None
self._bridge_script = bridge_script
@property
def bridge_script_path(self) -> str:
if self._bridge_script:
return self._bridge_script
return str(pathlib.Path(__file__).parent / "tts_bridge.js")
async def start(self) -> None:
"""Launch the Node.js bridge process and wait for it to be ready."""
script = self.bridge_script_path
if not pathlib.Path(script).exists():
raise FileNotFoundError(f"Bridge script not found: {script}")
logger.info("Starting Node.js TTS bridge (voice=%s, speed=%d)...", self._voice, self._speed)
self._process = await asyncio.create_subprocess_exec(
"node",
script,
"--voice", self._voice,
"--speed", str(self._speed),
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
line = await asyncio.wait_for(self._process.stdout.readline(), timeout=60)
line_str = line.decode("utf-8").strip()
if "READY" not in line_str:
stderr_data = await self._process.stderr.read()
raise RuntimeError(
f"Bridge failed to start. Got: {line_str}. Stderr: {stderr_data.decode('utf-8', errors='replace')}"
)
logger.info("Node.js TTS bridge ready.")
async def synthesize(self, kana_text: str) -> bytes:
"""Synthesize kana text to WAV audio.
Args:
kana_text: Japanese kana text to synthesize.
Returns:
Raw WAV audio bytes.
"""
if self._process is None or self._process.stdin is None:
raise RuntimeError("Bridge not started. Call start() first.")
tmpdir = pathlib.Path(tempfile.mkdtemp(prefix="tts_"))
try:
input_path = tmpdir / "input.txt"
output_path = tmpdir / "output.wav"
input_path.write_text(kana_text, encoding="utf-8")
cmd_line = f"{input_path}|{output_path}\n"
self._process.stdin.write(cmd_line.encode("utf-8"))
await self._process.stdin.drain()
line = await asyncio.wait_for(self._process.stdout.readline(), timeout=120)
line_str = line.decode("utf-8").strip()
if line_str.startswith("ERR:"):
raise RuntimeError(f"TTS synthesis error: {line_str[4:]}")
if not line_str.startswith("OK:") or line_str[3:] != str(output_path):
raise RuntimeError(f"Unexpected bridge response: {line_str}")
wav_data = output_path.read_bytes()
return wav_data
finally:
for f in tmpdir.iterdir():
try:
f.unlink()
except OSError:
pass
try:
tmpdir.rmdir()
except OSError:
pass
async def stop(self) -> None:
"""Stop the bridge process."""
if self._process is not None:
logger.info("Stopping TTS bridge...")
try:
if self._process.stdin is not None:
self._process.stdin.close()
except OSError:
pass
try:
self._process.terminate()
await asyncio.wait_for(self._process.wait(), timeout=5)
except asyncio.TimeoutError:
self._process.kill()
await self._process.wait()
logger.info("TTS bridge stopped.")
self._process = None