Last active
March 12, 2026 15:19
-
-
Save otsune/58a3732fafc2ef1d708d9f768a57a27e to your computer and use it in GitHub Desktop.
VOICEVOX coreの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
| #!/usr/bin/env python3 | |
| import argparse | |
| import glob | |
| import os | |
| import subprocess | |
| from pathlib import Path | |
| from voicevox_core.blocking import Onnxruntime, OpenJtalk, Synthesizer, VoiceModelFile | |
| VOICEVOX_DIR = Path(os.environ.get("VOICEVOX_DIR", Path.home() / "voicevox_core")) | |
| DICT_DIR = f"{VOICEVOX_DIR}/dict/open_jtalk_dic_utf_8-1.11" | |
| MODEL_DIR = f"{VOICEVOX_DIR}/models/vvms" | |
| ORT_LIB = f"{VOICEVOX_DIR}/onnxruntime/lib/{Onnxruntime.LIB_VERSIONED_FILENAME}" | |
| SPEAKER_ID = 3 | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("text") | |
| parser.add_argument("--speaker", type=int, default=SPEAKER_ID, | |
| help="スタイル ID (デフォルト: %(default)s = ずんだもん/ノーマル)") | |
| args = parser.parse_args() | |
| # 1. Synthesizer の初期化 | |
| synthesizer = Synthesizer( | |
| Onnxruntime.load_once(filename=ORT_LIB), | |
| OpenJtalk(DICT_DIR), | |
| ) | |
| # 2. model.metas でスタイル ID を確認し、該当する vvm だけをロード | |
| # (対応表: https://github.com/VOICEVOX/voicevox_vvm/blob/main/README.md) | |
| for vvm_path in sorted(glob.glob(f"{MODEL_DIR}/[0-9]*.vvm")): | |
| with VoiceModelFile.open(vvm_path) as model: | |
| style_ids = {style.id for meta in model.metas for style in meta.styles} | |
| if args.speaker in style_ids: | |
| synthesizer.load_voice_model(model) | |
| break | |
| else: | |
| raise SystemExit(f"スタイル ID {args.speaker} が見つかりません") | |
| # 3. テキスト音声合成 | |
| wav = synthesizer.tts(args.text, args.speaker) | |
| subprocess.run( | |
| ["ffplay", "-autoexit", "-nodisp", "-i", "pipe:0"], | |
| input=bytes(wav), stderr=subprocess.DEVNULL | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment