Skip to content

Commit

Permalink
example/python/run.pyをブロッキング版に
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Dec 7, 2023
1 parent 9da8b12 commit 16b8428
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TODO:

## 実行

run.py を実行します。 Open JTalk 辞書ディレクトリ、読み上げさせたい文章、出力 wav ファイルのパスをオプションで指定することができます。
run.py もしくは run-asyncio.py を実行します。 Open JTalk 辞書ディレクトリ、読み上げさせたい文章、出力 wav ファイルのパスをオプションで指定することができます。

```console
python ./run.py -h
Expand Down
100 changes: 100 additions & 0 deletions example/python/run-asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import asyncio
import dataclasses
import json
import logging
from argparse import ArgumentParser
from pathlib import Path
from typing import Tuple

import voicevox_core
from voicevox_core import AccelerationMode, AudioQuery
from voicevox_core.asyncio import OpenJtalk, Synthesizer, VoiceModel


# asyncやawaitは必須です。
async def main() -> None:
logging.basicConfig(format="[%(levelname)s] %(name)s: %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel("DEBUG")
logging.getLogger("voicevox_core_python_api").setLevel("DEBUG")
logging.getLogger("voicevox_core").setLevel("DEBUG")

(
acceleration_mode,
vvm_path,
open_jtalk_dict_dir,
text,
out,
style_id,
) = parse_args()

logger.debug("%s", f"{voicevox_core.supported_devices()=}")

logger.info("%s", f"Initializing ({acceleration_mode=}, {open_jtalk_dict_dir=})")
synthesizer = Synthesizer(
await OpenJtalk.new(open_jtalk_dict_dir), acceleration_mode=acceleration_mode
)

logger.debug("%s", f"{synthesizer.metas=}")
logger.debug("%s", f"{synthesizer.is_gpu_mode=}")

logger.info("%s", f"Loading `{vvm_path}`")
model = await VoiceModel.from_path(vvm_path)
await synthesizer.load_voice_model(model)

logger.info("%s", f"Creating an AudioQuery from {text!r}")
audio_query = await synthesizer.audio_query(text, style_id)

logger.info("%s", f"Synthesizing with {display_as_json(audio_query)}")
wav = await synthesizer.synthesis(audio_query, style_id)

out.write_bytes(wav)
logger.info("%s", f"Wrote `{out}`")


def parse_args() -> Tuple[AccelerationMode, Path, Path, str, Path, int]:
argparser = ArgumentParser()
argparser.add_argument(
"--mode",
default="AUTO",
type=AccelerationMode,
help='モード ("AUTO", "CPU", "GPU")',
)
argparser.add_argument(
"vvm",
type=Path,
help="vvmファイルへのパス",
)
argparser.add_argument(
"--dict-dir",
default="./open_jtalk_dic_utf_8-1.11",
type=Path,
help="Open JTalkの辞書ディレクトリ",
)
argparser.add_argument(
"--text",
default="この音声は、ボイスボックスを使用して、出力されています。",
help="読み上げさせたい文章",
)
argparser.add_argument(
"--out",
default="./output.wav",
type=Path,
help="出力wavファイルのパス",
)
argparser.add_argument(
"--style-id",
default=0,
type=int,
help="話者IDを指定",
)
args = argparser.parse_args()
return (args.mode, args.vvm, args.dict_dir, args.text, args.out, args.style_id)


def display_as_json(audio_query: AudioQuery) -> str:
return json.dumps(dataclasses.asdict(audio_query), ensure_ascii=False)


if __name__ == "__main__":
asyncio.run(main())
18 changes: 8 additions & 10 deletions example/python/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import dataclasses
import json
import logging
Expand All @@ -8,11 +7,10 @@

import voicevox_core
from voicevox_core import AccelerationMode, AudioQuery
from voicevox_core.asyncio import OpenJtalk, Synthesizer, VoiceModel
from voicevox_core.blocking import OpenJtalk, Synthesizer, VoiceModel


# asyncやawaitは必須です。
async def main() -> None:
def main() -> None:
logging.basicConfig(format="[%(levelname)s] %(name)s: %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel("DEBUG")
Expand All @@ -32,21 +30,21 @@ async def main() -> None:

logger.info("%s", f"Initializing ({acceleration_mode=}, {open_jtalk_dict_dir=})")
synthesizer = Synthesizer(
await OpenJtalk.new(open_jtalk_dict_dir), acceleration_mode=acceleration_mode
OpenJtalk(open_jtalk_dict_dir), acceleration_mode=acceleration_mode
)

logger.debug("%s", f"{synthesizer.metas=}")
logger.debug("%s", f"{synthesizer.is_gpu_mode=}")

logger.info("%s", f"Loading `{vvm_path}`")
model = await VoiceModel.from_path(vvm_path)
await synthesizer.load_voice_model(model)
model = VoiceModel.from_path(vvm_path)
synthesizer.load_voice_model(model)

logger.info("%s", f"Creating an AudioQuery from {text!r}")
audio_query = await synthesizer.audio_query(text, style_id)
audio_query = synthesizer.audio_query(text, style_id)

logger.info("%s", f"Synthesizing with {display_as_json(audio_query)}")
wav = await synthesizer.synthesis(audio_query, style_id)
wav = synthesizer.synthesis(audio_query, style_id)

out.write_bytes(wav)
logger.info("%s", f"Wrote `{out}`")
Expand Down Expand Up @@ -97,4 +95,4 @@ def display_as_json(audio_query: AudioQuery) -> str:


if __name__ == "__main__":
asyncio.run(main())
main()

0 comments on commit 16b8428

Please sign in to comment.