-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddq2voicevox.py
41 lines (35 loc) · 1.13 KB
/
ddq2voicevox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import requests
import wave
class VOICEVOXHTTPRequestHandler(BaseHTTPRequestHandler):
def __init__(self, *args):
BaseHTTPRequestHandler.__init__(self, *args)
def do_POST(self):
content_length = int(self.headers["content-length"])
body = self.rfile.read(content_length).decode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "audio/wave")
self.end_headers()
self.wfile.write(generate_wav(body))
def generate_wav(text, speaker=1):
host = 'localhost'
port = 50021
params = (
('text', text),
('speaker', speaker),
)
response1 = requests.post(
f'http://{host}:{port}/audio_query',
params=params
)
headers = {'Content-Type': 'application/json',}
response2 = requests.post(
f'http://{host}:{port}/synthesis',
headers=headers,
params=params,
data=json.dumps(response1.json())
)
return response2.content
with HTTPServer(('0.0.0.0', 8275), VOICEVOXHTTPRequestHandler) as server:
server.serve_forever()