This repository has been archived on 2023-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
jarvis-server/jarvis/utils/client_utils.py

48 lines
1.3 KiB
Python
Raw Normal View History

2021-08-01 11:48:33 +02:00
import requests
from requests.structures import CaseInsensitiveDict
2021-08-01 09:58:09 +02:00
from jarvis.utils import config_utils
2021-08-01 11:48:33 +02:00
def speak(sentence, client_ip, client_port):
# TODO: remove when fixed opentts server
print(sentence)
2021-08-01 11:48:33 +02:00
raw_audio_bytes = get_audio_from_sentence(sentence)
2021-08-01 23:15:03 +02:00
if raw_audio_bytes is None:
return "Error, audio not valid!"
2021-08-01 11:48:33 +02:00
play_audio_on_client(raw_audio_bytes, client_ip, client_port)
2021-08-01 09:58:09 +02:00
2021-08-01 11:48:33 +02:00
def get_audio_from_sentence(sentence):
voice = config_utils.get_in_config("TTS_VOICE")
headers = {'accept': '*/*'}
params = (
('voice', voice),
('text', sentence),
)
# TODO : add support for external opentts server
2021-08-01 23:15:03 +02:00
try:
2021-09-14 10:49:20 +02:00
response = requests.get('http://localhost:5500/api/tts', headers=headers, params=params, timeout=0.2)
2021-08-01 23:15:03 +02:00
return response.content
except requests.exceptions.ConnectionError:
print("Error connecting to Open TTS server")
return None
2021-08-01 11:48:33 +02:00
def play_audio_on_client(raw_audio_bytes, client_ip, client_port):
url_service = "http://" + client_ip + ":" + client_port + "/play_raw_audio"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "text/xml; charset=utf8"
response = requests.post(url_service,
headers=headers,
data=raw_audio_bytes)
print(response.content)