sample_voicevox.py ダウンロード/コピー

sample_voicevox.py をダウンロード

sample_voicevox.py
sample_voicevox.py
 1# sample_voicevox.py
 2"""
 3VOICEVOX APIを使用してテキストを音声に変換するシンプルなスクリプト。
 4
 5このスクリプトは、VOICEVOXエンジンを用いて指定されたテキストを音声ファイルに合成します。
 6コマンドライン引数によって、利用可能な話者の一覧表示や、特定の話者による音声合成を実行できます。
 7
 8**使用例:**
 9
10*   デフォルトの話者(四国めたん)でテキストを読み上げ、`sample_voicevox.wav` を生成します::
11
12    python sample_voicevox.py
13
14*   利用可能なすべてのVOICEVOX音声話者の一覧を表示します::
15
16    python sample_voicevox.py list
17
18*   指定した話者(例: ずんだもん)でテキストを読み上げ、`sample_voicevox.wav` を生成します::
19
20    python sample_voicevox.py ずんだもん
21
22:doc:`sample_voicevox_usage`
23"""
24
25import sys
26import tktts_voicevox
27
28
29# ------------------------------------------------------------
30# 利用可能な音声を表示
31# ------------------------------------------------------------
32if len(sys.argv) >= 2 and sys.argv[1].lower() == "list":
33    tktts_voicevox.list_available_voices()
34    sys.exit()
35
36
37# ------------------------------------------------------------
38# 話者を指定
39# ------------------------------------------------------------
40if len(sys.argv) >= 2:
41    voice_name = sys.argv[1]
42else:
43    voice_name = "四国めたん"
44
45
46# ------------------------------------------------------------
47# 話者名からVOICEVOXのspeaker IDを取得
48# ------------------------------------------------------------
49voices, voice_id = tktts_voicevox.resolve_speaker_id(voice_name)
50
51print(f"Voice: {voice_name}")
52print(f"Speaker ID: {voice_id}")
53
54
55# ------------------------------------------------------------
56# 読み上げる文章
57# ------------------------------------------------------------
58text = """
59こんにちは。
60これはVOICEVOXを使った音声合成のサンプルです。
61Pythonから簡単に自然な日本語音声を生成できます。
62"""
63
64
65# ------------------------------------------------------------
66# 音声を生成
67# ------------------------------------------------------------
68tktts_voicevox.speak(
69    outfile="sample_voicevox.wav",
70    text=text,
71    voice=voice_id,
72    speak_rate=1.0,
73    speak_pitch=0.0,
74)
75
76print("sample_voicevox.wav を作成しました。")