mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
* feat: add 302.ai TTS integration (#166) * update:将tts_302ai tts合并到doubao tts --------- Co-authored-by: Junsen <66542771+Huang-junsen@users.noreply.github.com> Co-authored-by: hrz <1710360675@qq.com>
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
import os
|
|
import uuid
|
|
import json
|
|
import base64
|
|
import requests
|
|
from datetime import datetime
|
|
from core.providers.tts.base import TTSProviderBase
|
|
|
|
|
|
class TTSProvider(TTSProviderBase):
|
|
def __init__(self, config, delete_audio_file):
|
|
super().__init__(config, delete_audio_file)
|
|
self.appid = config.get("appid")
|
|
self.access_token = config.get("access_token")
|
|
self.cluster = config.get("cluster")
|
|
self.voice = config.get("voice")
|
|
self.api_url = config.get("api_url")
|
|
self.authorization = config.get("authorization")
|
|
self.header = {"Authorization": f"{self.authorization}{self.access_token}"}
|
|
|
|
def generate_filename(self, extension=".wav"):
|
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
|
|
|
async def text_to_speak(self, text, output_file):
|
|
request_json = {
|
|
"app": {
|
|
"appid": f"{self.appid}",
|
|
"token": "access_token",
|
|
"cluster": self.cluster
|
|
},
|
|
"user": {
|
|
"uid": "1"
|
|
},
|
|
"audio": {
|
|
"voice_type": self.voice,
|
|
"encoding": "wav",
|
|
"speed_ratio": 1.0,
|
|
"volume_ratio": 1.0,
|
|
"pitch_ratio": 1.0,
|
|
},
|
|
"request": {
|
|
"reqid": str(uuid.uuid4()),
|
|
"text": text,
|
|
"text_type": "plain",
|
|
"operation": "query",
|
|
"with_frontend": 1,
|
|
"frontend_type": "unitTson"
|
|
}
|
|
}
|
|
|
|
try:
|
|
resp = requests.post(self.api_url, json.dumps(request_json), headers=self.header)
|
|
if "data" in resp.json():
|
|
data = resp.json()["data"]
|
|
file_to_save = open(output_file, "wb")
|
|
file_to_save.write(base64.b64decode(data))
|
|
else:
|
|
raise Exception(f"{__name__} status_code: {resp.status_code} response: {resp.content}")
|
|
except Exception as e:
|
|
raise Exception(f"{__name__} error: {e}")
|