mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-23 15:43:54 +08:00
* fix: 修复manual模式无法识别 (#404) * feat(docs): 新增Issues模板 * fix: 补全core依赖 * fix: 修复manual模式无法识别 * 去除重复依赖.txt 已经有torch和torchaudio --------- Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com> * 修复iot功能中表达式问题 (#400) * Custom paths asr tts (#388) * #164 自定义asr、tts缓存目录,项目启动自动创建目录 * #164 自定义asr、tts缓存目录,项目启动自动创建目录 * fix:修复语音无法找到新配置项output_file的bug * fix:电脑不支持iot音量控制bug --------- Co-authored-by: Junsen <66542771+Huang-junsen@users.noreply.github.com> Co-authored-by: tang <tangyiyong@gmail.com> Co-authored-by: shudongW <178200623@qq.com> Co-authored-by: hrz <1710360675@qq.com>
65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
import os
|
|
import uuid
|
|
import json
|
|
import requests
|
|
import shutil
|
|
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.url = config.get("url", "https://u95167-bd74-2aef8085.westx.seetacloud.com:8443/flashsummary/tts?token=")
|
|
self.voice_id = config.get("voice_id", 1695)
|
|
self.token = config.get("token")
|
|
self.to_lang = config.get("to_lang")
|
|
self.volume_change_dB = config.get("volume_change_dB", 0)
|
|
self.speed_factor = config.get("speed_factor", 1)
|
|
self.stream = config.get("stream", False)
|
|
self.output_file = config.get("output_dir")
|
|
self.pitch_factor = config.get("pitch_factor", 0)
|
|
self.format = config.get("format", "mp3")
|
|
self.emotion = config.get("emotion", 1)
|
|
self.header = {
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
def generate_filename(self, extension=".mp3"):
|
|
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):
|
|
url = f'{self.url}{self.token}'
|
|
result = "firefly"
|
|
payload = json.dumps({
|
|
"to_lang": self.to_lang,
|
|
"text": text,
|
|
"emotion": self.emotion,
|
|
"format": self.format,
|
|
"volume_change_dB": self.volume_change_dB,
|
|
"voice_id": self.voice_id,
|
|
"pitch_factor": self.pitch_factor,
|
|
"speed_factor": self.speed_factor,
|
|
"token": self.token
|
|
})
|
|
|
|
resp = requests.request("POST", url, data=payload)
|
|
if resp.status_code != 200:
|
|
return None
|
|
resp_json = resp.json()
|
|
try:
|
|
result = resp_json['url'] + ':' + str(
|
|
resp_json[
|
|
'port']) + '/flashsummary/retrieveFileData?stream=True&token=' + self.token + '&voice_audio_path=' + \
|
|
resp_json['voice_path']
|
|
except Exception as e:
|
|
print("error:", e)
|
|
|
|
audio_content = requests.get(result)
|
|
with open(output_file, "wb") as f:
|
|
f.write(audio_content.content)
|
|
return True
|
|
voice_path = resp_json.get("voice_path")
|
|
des_path = output_file
|
|
shutil.move(voice_path, des_path)
|