xiaozhi-esp32-server/main/xiaozhi-server/core/providers/tts/edge.py
hrz 11d7ada979 fix(tts): edge-tts 失声改走 msedgeservices
微软 2026-10 起对 legacy 消费者接口返回 403,
edge-tts 7.2.8 仍走 speech.platform.bing.com 走不通。
上游 PR rany2/edge-tts#491 切到 api.msedgeservices.com 修好,
但 7.2.8 仍是 PyPI 最新版本。

仓库在 edge.py 顶部临时把 WSS_URL 顶住,等 7.2.9 出了删掉。
2026-10-13 09:10:32 +08:00

83 lines
No EOL
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import uuid
import edge_tts
from datetime import datetime
from core.providers.tts.base import TTSProviderBase
# 微软 2026-10 起对 legacy 消费者接口返回 403,
# edge-tts 7.2.8 仍走 speech.platform.bing.com 走不通。
# 上游 PR rany2/edge-tts#491 切到 api.msedgeservices.com 修好,
# 但 7.2.8 仍是 PyPI 最新版本,临时把 WSS_URL 顶住。
edge_tts.constants.WSS_URL = (
"wss://api.msedgeservices.com/tts/cognitiveservices/websocket/v1"
f"?Ocp-Apim-Subscription-Key={edge_tts.constants.TRUSTED_CLIENT_TOKEN}"
)
class TTSProvider(TTSProviderBase):
TTS_PARAM_CONFIG = [
("ttsVolume", "volume", 0, 100, 50, int),
("ttsRate", "speech_rate", -100, 100, 0, int),
("ttsPitch", "pitch_rate", -100, 100, 0, int),
]
def __init__(self, config, delete_audio_file):
super().__init__(config, delete_audio_file)
if config.get("private_voice"):
self.voice = config.get("private_voice")
else:
self.voice = config.get("voice")
self.audio_file_type = config.get("format", "mp3")
volume = config.get("volume", "50")
self.volume = int(volume) if volume else 50
speech_rate = config.get("rate", "0")
self.speech_rate = int(speech_rate) if speech_rate else 0
pitch_rate = config.get("pitch", "0")
self.pitch_rate = int(pitch_rate) if pitch_rate else 0
# 应用百分比调整
self._apply_percentage_params(config)
self.edge_rate = f"{self.speech_rate:+}%"
self.edge_volume = f"{self.volume:+}%"
self.edge_pitch = f"{self.pitch_rate:+}Hz"
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):
try:
communicate = edge_tts.Communicate(
text,
voice=self.voice,
rate=self.edge_rate,
volume=self.edge_volume,
pitch=self.edge_pitch,
)
if output_file:
# 确保目录存在并创建空文件
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, "wb") as f:
pass
# 流式写入音频数据
with open(output_file, "ab") as f: # 改为追加模式避免覆盖
async for chunk in communicate.stream():
if chunk["type"] == "audio": # 只处理音频数据块
f.write(chunk["data"])
else:
# 返回音频二进制数据
audio_bytes = b""
async for chunk in communicate.stream():
if chunk["type"] == "audio":
audio_bytes += chunk["data"]
return audio_bytes
except Exception as e:
error_msg = f"Edge TTS请求失败: {e}"
raise Exception(error_msg) # 抛出异常,让调用方捕获