Files
xiaozhi-esp32-server/main/xiaozhi-server/core/providers/tts/base.py
T

63 lines
2.1 KiB
Python
Raw Normal View History

import asyncio
2025-02-18 00:07:19 +08:00
from config.logger import setup_logging
import os
from abc import ABC, abstractmethod
from core.utils.tts import MarkdownCleaner
2025-05-08 11:18:12 +08:00
from core.utils.util import audio_to_data
2025-02-18 00:07:19 +08:00
TAG = __name__
logger = setup_logging()
2025-02-14 00:54:59 +08:00
class TTSProviderBase(ABC):
def __init__(self, config, delete_audio_file):
self.delete_audio_file = delete_audio_file
2025-03-18 13:25:34 +08:00
self.output_file = config.get("output_dir")
@abstractmethod
def generate_filename(self):
pass
def to_tts(self, text):
tmp_file = self.generate_filename()
try:
max_repeat_time = 5
text = MarkdownCleaner.clean_markdown(text)
while not os.path.exists(tmp_file) and max_repeat_time > 0:
2025-04-22 16:44:45 +08:00
try:
asyncio.run(self.text_to_speak(text, tmp_file))
except Exception as e:
2025-05-06 13:10:26 +08:00
logger.bind(tag=TAG).warning(
f"语音生成失败{5 - max_repeat_time + 1}次: {text},错误: {e}"
)
2025-04-26 07:04:07 +08:00
# 未执行成功,删除文件
if os.path.exists(tmp_file):
os.remove(tmp_file)
2025-04-22 16:44:45 +08:00
max_repeat_time -= 1
2025-02-14 00:54:59 +08:00
if max_repeat_time > 0:
2025-04-24 10:58:42 +08:00
logger.bind(tag=TAG).info(
f"语音生成成功: {text}:{tmp_file},重试{5 - max_repeat_time}次"
)
2025-04-26 07:04:07 +08:00
else:
logger.bind(tag=TAG).error(
f"语音生成失败: {text},请检查网络或服务是否正常"
)
return tmp_file
except Exception as e:
2025-03-23 22:02:13 +08:00
logger.bind(tag=TAG).error(f"Failed to generate TTS file: {e}")
return None
@abstractmethod
async def text_to_speak(self, text, output_file):
pass
2025-05-06 14:57:29 +08:00
def audio_to_pcm_data(self, audio_file_path):
"""音频文件转换为PCM编码"""
2025-05-08 11:18:12 +08:00
return audio_to_data(audio_file_path, is_opus=False)
2025-05-08 11:31:12 +08:00
2025-03-15 00:19:45 +08:00
def audio_to_opus_data(self, audio_file_path):
"""音频文件转换为Opus编码"""
2025-05-08 11:18:12 +08:00
return audio_to_data(audio_file_path, is_opus=True)