Update lib/voicevox.py
Browse files- lib/voicevox.py +30 -0
lib/voicevox.py
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class StatusError(Exception):
|
| 5 |
+
pass
|
| 6 |
+
|
| 7 |
+
def synthesis(text, speaker = 1):
|
| 8 |
+
url = f"https://api.tts.quest/v3/voicevox/synthesis?text={text}&speaker={speaker}"
|
| 9 |
+
resp = requests.get(url)
|
| 10 |
+
resp.raise_for_status()
|
| 11 |
+
data = resp.json()
|
| 12 |
+
is_ready = check_audio_status(data["audioStatusUrl"])
|
| 13 |
+
if not is_ready:
|
| 14 |
+
raise StatusError("Unexpected status")
|
| 15 |
+
|
| 16 |
+
resp = requests.get(data["mp3DownloadUrl"])
|
| 17 |
+
return resp.content
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def check_audio_status(url):
|
| 22 |
+
is_ready = False
|
| 23 |
+
while not is_ready:
|
| 24 |
+
resp = requests.get(url)
|
| 25 |
+
resp.raise_for_status()
|
| 26 |
+
data = resp.json()
|
| 27 |
+
is_ready = data["isAudioReady"]
|
| 28 |
+
return is_ready
|
| 29 |
+
|
| 30 |
+
|