| 1 |
"""HTTP server for text to speech (TTS).""" |
| 2 |
|
| 3 |
import io |
| 4 |
import logging |
| 5 |
import wave |
| 6 |
from pathlib import Path |
| 7 |
from typing import Optional |
| 8 |
|
| 9 |
from flask import Response, request |
| 10 |
|
| 11 |
from wyoming.audio import AudioChunk, AudioStart, AudioStop |
| 12 |
from wyoming.client import AsyncClient |
| 13 |
from wyoming.error import Error |
| 14 |
from wyoming.tts import Synthesize, SynthesizeVoice |
| 15 |
|
| 16 |
from .shared import get_app, get_argument_parser |
| 17 |
|
| 18 |
_DIR = Path(__file__).parent |
| 19 |
CONF_PATH = _DIR / "conf" / "tts.yaml" |
| 20 |
|
| 21 |
|
| 22 |
def main(): |
| 23 |
parser = get_argument_parser() |
| 24 |
parser.add_argument("--voice", help="Default voice for synthesis") |
| 25 |
parser.add_argument("--speaker", help="Default voice speaker for synthesis") |
| 26 |
args = parser.parse_args() |
| 27 |
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) |
| 28 |
|
| 29 |
app = get_app("tts", CONF_PATH, args) |
| 30 |
|
| 31 |
@app.route("/api/text-to-speech", methods=["POST", "GET"]) |
| 32 |
async def api_stt() -> Response: |
| 33 |
uri = request.args.get("uri", args.uri) |
| 34 |
if not uri: |
| 35 |
raise ValueError("URI is required") |
| 36 |
|
| 37 |
if request.method == "POST": |
| 38 |
text = request.data.decode() |
| 39 |
else: |
| 40 |
text = request.args.get("text", "") |
| 41 |
|
| 42 |
if not text: |
| 43 |
raise ValueError("Text is required") |
| 44 |
|
| 45 |
voice: Optional[SynthesizeVoice] = None |
| 46 |
voice_name = request.args.get("voice", args.voice) |
| 47 |
if voice_name: |
| 48 |
voice = SynthesizeVoice( |
| 49 |
name=voice_name, speaker=request.args.get("speaker", args.speaker) |
| 50 |
) |
| 51 |
|
| 52 |
async with AsyncClient.from_uri(uri) as client: |
| 53 |
wav_io = io.BytesIO() |
| 54 |
wav_file = wave.open(wav_io, "wb") |
| 55 |
await client.write_event(Synthesize(text=text, voice=voice).event()) |
| 56 |
|
| 57 |
while True: |
| 58 |
event = await client.read_event() |
| 59 |
if event is None: |
| 60 |
raise RuntimeError("Client disconnected") |
| 61 |
|
| 62 |
if AudioStart.is_type(event.type): |
| 63 |
audio_start = AudioStart.from_event(event) |
| 64 |
wav_file.setframerate(audio_start.rate) |
| 65 |
wav_file.setsampwidth(audio_start.width) |
| 66 |
wav_file.setnchannels(audio_start.channels) |
| 67 |
elif AudioChunk.is_type(event.type): |
| 68 |
audio_chunk = AudioChunk.from_event(event) |
| 69 |
wav_file.writeframes(audio_chunk.audio) |
| 70 |
elif AudioStop.is_type(event.type): |
| 71 |
wav_file.close() |
| 72 |
wav_io.seek(0) |
| 73 |
return Response( |
| 74 |
wav_io.getvalue(), headers={"Content-Type": "audio/wav"} |
| 75 |
) |
| 76 |
elif Error.is_type(event.type): |
| 77 |
error = Error.from_event(event) |
| 78 |
raise RuntimeError( |
| 79 |
f"Unexpected error from client: code={error.code}, text={error.text}" |
| 80 |
) |
| 81 |
|
| 82 |
app.run(args.host, args.port) |
| 83 |
|
| 84 |
|
| 85 |
if __name__ == "__main__": |
| 86 |
main() |