forked from sr2mg/aituber_python_programing_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voicevox_adapter.py
36 lines (35 loc) · 1.34 KB
/
voicevox_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import requests
import io
import soundfile
class VoicevoxAdapter:
URL = 'http://127.0.0.1:50021/'
# 二回postする。一回目で変換、二回目で音声合成
def __init__(self) -> None:
pass
def __create_audio_query(self,text: str,speaker_id: int) ->json:
item_data={
'text':text,
'speaker':speaker_id,
}
response = requests.post(self.URL+'audio_query',params=item_data)
return response.json()
def __create_request_audio(self,query_data,speaker_id: int) -> bytes:
a_params = {
'speaker' :speaker_id,
}
headers = {"accept": "audio/wav", "Content-Type": "application/json"}
res = requests.post(self.URL+'synthesis',params = a_params,data= json.dumps(query_data),headers=headers)
print(res.status_code)
return res.content
def get_voice(self,text: str):
speaker_id = 3
query_data:json = self.__create_audio_query(text,speaker_id=speaker_id)
audio_bytes = self.__create_request_audio(query_data,speaker_id=speaker_id)
audio_stream = io.BytesIO(audio_bytes)
data, sample_rate = soundfile.read(audio_stream)
return data,sample_rate
if __name__ == "__main__":
voicevox = VoicevoxAdapter()
data,sample_rate = voicevox.get_voice("こんにちは")
print(sample_rate)