Last active
December 8, 2022 19:17
-
-
Save hhsprings/d05981a035c18f1b1fd1cd19eda6bd03 to your computer and use it in GitHub Desktop.
VOICEVOX エンジンを python から使いたいの。のはじめのいっぽ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
using VOICEVOX Engine on docker | |
https://github.com/VOICEVOX/voicevox_engine#docker-%E3%82%A4%E3%83%A1%E3%83%BC%E3%82%B8 | |
""" | |
import io | |
import os | |
import json | |
import base64 | |
import urllib | |
import wave | |
import requests | |
_BASEURL = "http://localhost:50021" | |
def _concat(infiles, outfile): | |
data = [] | |
for infile in infiles: | |
w = wave.open(infile, 'rb') | |
data.append( [w.getparams(), w.readframes(w.getnframes())] ) | |
w.close() | |
output = wave.open(outfile, 'wb') | |
output.setparams(data[0][0]) | |
for i in range(len(data)): | |
output.writeframes(data[i][1]) | |
output.close() | |
def _ini(ifn, ofn="synth.json"): | |
result = [] | |
for styleId, text, kana, paras in json.load(io.open(ifn, encoding="utf-8")): | |
etext = urllib.parse.quote(text.encode("utf-8")) | |
res = requests.post( | |
f"{_BASEURL}/audio_query?speaker={styleId}&text={etext}") | |
d = res.json() | |
if kana: | |
etext = urllib.parse.quote_plus(kana.encode("utf-8")) | |
res = requests.post( | |
f"{_BASEURL}/accent_phrases?speaker={styleId}&is_kana=true&text={etext}") | |
d["accent_phrases"] = res.json() | |
if paras: | |
for k, v in paras.items(): | |
d[k] = v | |
result.append((int(styleId), text, kana, d)) | |
with io.open(ofn, "w", encoding="utf-8") as fo: | |
json.dump(result, fo, indent=4, ensure_ascii=False) | |
def _synth(ifn="synth.json"): | |
ofiles = [] | |
ifnbase, _ = os.path.splitext(os.path.basename(ifn)) | |
queries = json.load(io.open(ifn, encoding="utf-8")) | |
for i, (styleId, text, kana, query) in enumerate(queries): | |
res = requests.post( | |
f"{_BASEURL}/synthesis?speaker={styleId}", | |
data=json.dumps(query), | |
headers={"Content-Type": "application/json"}) | |
ofn = f"{ifnbase}_{i:03d}.wav" | |
io.open(ofn, "wb").write(res.content) | |
ofiles.append(ofn) | |
_concat(ofiles, f"{ifnbase}.wav") | |
try: | |
[os.unlink(fn) for fn in ofiles] | |
except Exception: | |
pass | |
if __name__ == '__main__': | |
import argparse | |
ap = argparse.ArgumentParser() | |
ap.add_argument("file") | |
ap.add_argument("ofnbase", nargs="?") | |
args = ap.parse_args() | |
if not args.ofnbase: | |
args.ofnbase = os.path.splitext(os.path.basename(args.file))[0] | |
_ini(args.file, f"{args.ofnbase}.vv") | |
_synth(f"{args.ofnbase}.vv") | |
os.remove(f"{args.ofnbase}.vv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment